blob: 9491ddb9b1a74d6e7353d9cc7cc468240defa25b [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
aPiecekbf968d92021-05-27 14:35:05 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100136#define MSG_BUFFER_SIZE 128
137 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100138 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139
Radek Krejci52b6d512020-10-12 12:33:17 +0200140 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return;
142 }
143
144 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
145 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200146 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200147 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100148 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
150 for (j = 1; exp->repeat[i][j]; ++j) {
151 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
152 }
153 strcat(tmp, ")");
154 }
155 LOGDBG(LY_LDGXPATH, tmp);
156 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200158}
159
160#ifndef NDEBUG
161
162/**
163 * @brief Print XPath set content to debug output.
164 *
165 * @param[in] set Set to print.
166 */
167static void
168print_set_debug(struct lyxp_set *set)
169{
170 uint32_t i;
171 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 struct lyxp_set_node *item;
173 struct lyxp_set_scnode *sitem;
174
Radek Krejci52b6d512020-10-12 12:33:17 +0200175 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200176 return;
177 }
178
179 switch (set->type) {
180 case LYXP_SET_NODE_SET:
181 LOGDBG(LY_LDGXPATH, "set NODE SET:");
182 for (i = 0; i < set->used; ++i) {
183 item = &set->val.nodes[i];
184
185 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100186 case LYXP_NODE_NONE:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
188 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200189 case LYXP_NODE_ROOT:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ROOT_CONFIG:
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
194 break;
195 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100196 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200198 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100199 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200201 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200211 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200212 }
213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100214 case LYXP_NODE_META:
215 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
Michal Vasko69730152020-10-09 16:30:07 +0200216 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 break;
218 }
219 }
220 break;
221
222 case LYXP_SET_SCNODE_SET:
223 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
224 for (i = 0; i < set->used; ++i) {
225 sitem = &set->val.scnodes[i];
226
227 switch (sitem->type) {
228 case LYXP_NODE_ROOT:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ROOT_CONFIG:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ELEM:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
236 break;
237 default:
238 LOGINT(NULL);
239 break;
240 }
241 }
242 break;
243
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 case LYXP_SET_BOOLEAN:
245 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200246 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200247 break;
248
249 case LYXP_SET_STRING:
250 LOGDBG(LY_LDGXPATH, "set STRING");
251 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
252 break;
253
254 case LYXP_SET_NUMBER:
255 LOGDBG(LY_LDGXPATH, "set NUMBER");
256
257 if (isnan(set->val.num)) {
258 str = strdup("NaN");
259 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
260 str = strdup("0");
261 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
262 str = strdup("Infinity");
263 } else if (isinf(set->val.num) && signbit(set->val.num)) {
264 str = strdup("-Infinity");
265 } else if ((long long)set->val.num == set->val.num) {
266 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
267 str = NULL;
268 }
269 } else {
270 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
271 str = NULL;
272 }
273 }
274 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
275
276 LOGDBG(LY_LDGXPATH, "\t%s", str);
277 free(str);
278 }
279}
280
281#endif
282
283/**
284 * @brief Realloc the string \p str.
285 *
286 * @param[in] ctx libyang context for logging.
287 * @param[in] needed How much free space is required.
288 * @param[in,out] str Pointer to the string to use.
289 * @param[in,out] used Used bytes in \p str.
290 * @param[in,out] size Allocated bytes in \p str.
291 * @return LY_ERR
292 */
293static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100294cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295{
296 if (*size - *used < needed) {
297 do {
298 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
299 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
300 return LY_EINVAL;
301 }
302 *size += LYXP_STRING_CAST_SIZE_STEP;
303 } while (*size - *used < needed);
304 *str = ly_realloc(*str, *size * sizeof(char));
305 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
306 }
307
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Cast nodes recursively to one string @p str.
313 *
314 * @param[in] node Node to cast.
315 * @param[in] fake_cont Whether to put the data into a "fake" container.
316 * @param[in] root_type Type of the XPath root.
317 * @param[in] indent Current indent.
318 * @param[in,out] str Resulting string.
319 * @param[in,out] used Used bytes in @p str.
320 * @param[in,out] size Allocated bytes in @p str.
321 * @return LY_ERR
322 */
323static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200324cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326{
Radek Krejci7f769d72020-07-11 23:13:56 +0200327 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 struct lyd_node_any *any;
332 LY_ERR rc;
333
334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
335 return LY_SUCCESS;
336 }
337
338 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200339 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 LY_CHECK_RET(rc);
341 strcpy(*str + (*used - 1), "\n");
342 ++(*used);
343
344 ++indent;
345 }
346
347 switch (node->schema->nodetype) {
348 case LYS_CONTAINER:
349 case LYS_LIST:
350 case LYS_RPC:
351 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200352 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 LY_CHECK_RET(rc);
354 strcpy(*str + (*used - 1), "\n");
355 ++(*used);
356
Radek Krejcia1c1e542020-09-29 16:06:52 +0200357 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
359 LY_CHECK_RET(rc);
360 }
361
362 break;
363
364 case LYS_LEAF:
365 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200366 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367
368 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200369 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200370 memset(*str + (*used - 1), ' ', indent * 2);
371 *used += indent * 2;
372
373 /* print value */
374 if (*used == 1) {
375 sprintf(*str + (*used - 1), "%s", value_str);
376 *used += strlen(value_str);
377 } else {
378 sprintf(*str + (*used - 1), "%s\n", value_str);
379 *used += strlen(value_str) + 1;
380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381
382 break;
383
384 case LYS_ANYXML:
385 case LYS_ANYDATA:
386 any = (struct lyd_node_any *)node;
387 if (!(void *)any->value.tree) {
388 /* no content */
389 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200390 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (any->value_type == LYD_ANYDATA_LYB) {
395 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200396 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200397 /* successfully parsed */
398 free(any->value.mem);
399 any->value.tree = tree;
400 any->value_type = LYD_ANYDATA_DATATREE;
401 }
Radek Krejci7931b192020-06-25 17:05:03 +0200402 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 }
404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200413 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200414 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200419 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200426 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200445 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
448 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200470cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vaskoaa677062021-03-09 13:52:53 +0100505 if (!set->used) {
506 *str = strdup("");
507 if (!*str) {
508 LOGMEM_RET(set->ctx);
509 }
510 return LY_SUCCESS;
511 }
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200524 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200525 if (!*str) {
526 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 }
528 return LY_SUCCESS;
529 }
530
531 LOGINT_RET(set->ctx);
532}
533
534/**
535 * @brief Cast a string into an XPath number.
536 *
537 * @param[in] str String to use.
538 * @return Cast number.
539 */
540static long double
541cast_string_to_number(const char *str)
542{
543 long double num;
544 char *ptr;
545
546 errno = 0;
547 num = strtold(str, &ptr);
548 if (errno || *ptr) {
549 num = NAN;
550 }
551 return num;
552}
553
554/**
555 * @brief Callback for checking value equality.
556 *
Michal Vasko62524a92021-02-26 10:08:50 +0100557 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200558 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200563 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 */
Radek Krejci857189e2020-09-01 13:26:36 +0200565static ly_bool
566set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200590 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200728 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200754set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200759 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200761 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100793 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
794 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200795 uint32_t idx;
796 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
797 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100798 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200799 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 lyxp_set_free(ret);
801 return NULL;
802 }
Michal Vaskoba716542019-12-16 10:01:58 +0100803 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 }
805 }
806 } else if (set->type == LYXP_SET_NODE_SET) {
807 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200808 if (set->used) {
809 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
810 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
811 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
812 } else {
813 ret->val.nodes = NULL;
814 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815
816 ret->used = ret->size = set->used;
817 ret->ctx_pos = set->ctx_pos;
818 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100819 if (set->ht) {
820 ret->ht = lyht_dup(set->ht);
821 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200823 memcpy(ret, set, sizeof *ret);
824 if (set->type == LYXP_SET_STRING) {
825 ret->val.str = strdup(set->val.str);
826 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
827 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828 }
829
830 return ret;
831}
832
833/**
834 * @brief Fill XPath set with a string. Any current data are disposed of.
835 *
836 * @param[in] set Set to fill.
837 * @param[in] string String to fill into \p set.
838 * @param[in] str_len Length of \p string. 0 is a valid value!
839 */
840static void
841set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
842{
Michal Vaskod3678892020-05-21 10:06:58 +0200843 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844
845 set->type = LYXP_SET_STRING;
846 if ((str_len == 0) && (string[0] != '\0')) {
847 string = "";
848 }
849 set->val.str = strndup(string, str_len);
850}
851
852/**
853 * @brief Fill XPath set with a number. Any current data are disposed of.
854 *
855 * @param[in] set Set to fill.
856 * @param[in] number Number to fill into \p set.
857 */
858static void
859set_fill_number(struct lyxp_set *set, long double number)
860{
Michal Vaskod3678892020-05-21 10:06:58 +0200861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862
863 set->type = LYXP_SET_NUMBER;
864 set->val.num = number;
865}
866
867/**
868 * @brief Fill XPath set with a boolean. Any current data are disposed of.
869 *
870 * @param[in] set Set to fill.
871 * @param[in] boolean Boolean to fill into \p set.
872 */
873static void
Radek Krejci857189e2020-09-01 13:26:36 +0200874set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875{
Michal Vaskod3678892020-05-21 10:06:58 +0200876 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877
878 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200879 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200880}
881
882/**
883 * @brief Fill XPath set with the value from another set (deep assign).
884 * Any current data are disposed of.
885 *
886 * @param[in] trg Set to fill.
887 * @param[in] src Source set to copy into \p trg.
888 */
889static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200890set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891{
892 if (!trg || !src) {
893 return;
894 }
895
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901 set_init(trg, src);
902
903 if (src->type == LYXP_SET_SCNODE_SET) {
904 trg->type = LYXP_SET_SCNODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907
Michal Vasko08e9b112021-06-11 15:41:17 +0200908 if (trg->size) {
909 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
910 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
911 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
912 } else {
913 trg->val.scnodes = NULL;
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200916 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200917 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 set_fill_number(trg, src->val.num);
919 } else if (src->type == LYXP_SET_STRING) {
920 set_fill_string(trg, src->val.str, strlen(src->val.str));
921 } else {
922 if (trg->type == LYXP_SET_NODE_SET) {
923 free(trg->val.nodes);
924 } else if (trg->type == LYXP_SET_STRING) {
925 free(trg->val.str);
926 }
927
Michal Vaskod3678892020-05-21 10:06:58 +0200928 assert(src->type == LYXP_SET_NODE_SET);
929
930 trg->type = LYXP_SET_NODE_SET;
931 trg->used = src->used;
932 trg->size = src->used;
933 trg->ctx_pos = src->ctx_pos;
934 trg->ctx_size = src->ctx_size;
935
Michal Vasko08e9b112021-06-11 15:41:17 +0200936 if (trg->size) {
937 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
938 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
939 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
940 } else {
941 trg->val.nodes = NULL;
942 }
Michal Vaskod3678892020-05-21 10:06:58 +0200943 if (src->ht) {
944 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200946 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200947 }
948 }
949}
950
951/**
952 * @brief Clear context of all schema nodes.
953 *
954 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200955 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 */
957static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200958set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200959{
960 uint32_t i;
961
962 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100963 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200964 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100965 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
966 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968 }
969}
970
971/**
972 * @brief Remove a node from a set. Removing last node changes
973 * set into LYXP_SET_EMPTY. Context position aware.
974 *
975 * @param[in] set Set to use.
976 * @param[in] idx Index from @p set of the node to be removed.
977 */
978static void
979set_remove_node(struct lyxp_set *set, uint32_t idx)
980{
981 assert(set && (set->type == LYXP_SET_NODE_SET));
982 assert(idx < set->used);
983
984 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
985
986 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +0200987 if (idx < set->used) {
988 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
989 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +0200990 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991 }
992}
993
994/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200996 *
997 * @param[in] set Set to use.
998 * @param[in] idx Index from @p set of the node to be removed.
999 */
1000static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001001set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001002{
1003 assert(set && (set->type == LYXP_SET_NODE_SET));
1004 assert(idx < set->used);
1005
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1007 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1008 }
1009 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001010}
1011
1012/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001014 * set into LYXP_SET_EMPTY. Context position aware.
1015 *
1016 * @param[in] set Set to consolidate.
1017 */
1018static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001019set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001020{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001021 uint32_t i, orig_used, end = 0;
1022 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001023
Michal Vaskod3678892020-05-21 10:06:58 +02001024 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001025
1026 orig_used = set->used;
1027 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001028 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001029 start = -1;
1030 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001031 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001032 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001033 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001034 end = i;
1035 ++i;
1036 break;
1037 }
1038
1039 ++i;
1040 if (i == orig_used) {
1041 end = i;
1042 }
1043 } while (i < orig_used);
1044
1045 if (start > -1) {
1046 /* move the whole chunk of valid nodes together */
1047 if (set->used != (unsigned)start) {
1048 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1049 }
1050 set->used += end - start;
1051 }
1052 }
Michal Vasko57eab132019-09-24 11:46:26 +02001053}
1054
1055/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001056 * @brief Check for duplicates in a node set.
1057 *
1058 * @param[in] set Set to check.
1059 * @param[in] node Node to look for in @p set.
1060 * @param[in] node_type Type of @p node.
1061 * @param[in] skip_idx Index from @p set to skip.
1062 * @return LY_ERR
1063 */
1064static LY_ERR
1065set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1066{
1067 uint32_t i;
1068
Michal Vasko2caefc12019-11-14 16:07:56 +01001069 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001070 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1071 }
1072
1073 for (i = 0; i < set->used; ++i) {
1074 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1075 continue;
1076 }
1077
1078 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1079 return LY_EEXIST;
1080 }
1081 }
1082
1083 return LY_SUCCESS;
1084}
1085
Radek Krejci857189e2020-09-01 13:26:36 +02001086ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1088 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t i;
1091
1092 for (i = 0; i < set->used; ++i) {
1093 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1094 continue;
1095 }
1096
1097 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001098 if (index_p) {
1099 *index_p = i;
1100 }
1101 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102 }
1103 }
1104
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001105 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106}
1107
Michal Vaskoecd62de2019-11-13 12:35:11 +01001108void
1109lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110{
1111 uint32_t orig_used, i, j;
1112
Michal Vaskod3678892020-05-21 10:06:58 +02001113 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114
Michal Vaskod3678892020-05-21 10:06:58 +02001115 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001116 return;
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001120 memcpy(set1, set2, sizeof *set1);
1121 return;
1122 }
1123
1124 if (set1->used + set2->used > set1->size) {
1125 set1->size = set1->used + set2->used;
1126 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1127 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1128 }
1129
1130 orig_used = set1->used;
1131
1132 for (i = 0; i < set2->used; ++i) {
1133 for (j = 0; j < orig_used; ++j) {
1134 /* detect duplicities */
1135 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1136 break;
1137 }
1138 }
1139
Michal Vasko0587bce2020-12-10 12:19:21 +01001140 if (j < orig_used) {
1141 /* node is there, but update its status if needed */
1142 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1143 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001144 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1145 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1146 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001147 }
1148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001149 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1150 ++set1->used;
1151 }
1152 }
1153
Michal Vaskod3678892020-05-21 10:06:58 +02001154 lyxp_set_free_content(set2);
1155 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156}
1157
1158/**
1159 * @brief Insert a node into a set. Context position aware.
1160 *
1161 * @param[in] set Set to use.
1162 * @param[in] node Node to insert to @p set.
1163 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1164 * @param[in] node_type Node type of @p node.
1165 * @param[in] idx Index in @p set to insert into.
1166 */
1167static void
1168set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1169{
Michal Vaskod3678892020-05-21 10:06:58 +02001170 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001171
Michal Vaskod3678892020-05-21 10:06:58 +02001172 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001173 /* first item */
1174 if (idx) {
1175 /* no real harm done, but it is a bug */
1176 LOGINT(set->ctx);
1177 idx = 0;
1178 }
1179 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1180 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1181 set->type = LYXP_SET_NODE_SET;
1182 set->used = 0;
1183 set->size = LYXP_SET_SIZE_START;
1184 set->ctx_pos = 1;
1185 set->ctx_size = 1;
1186 set->ht = NULL;
1187 } else {
1188 /* not an empty set */
1189 if (set->used == set->size) {
1190
1191 /* set is full */
1192 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1193 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1194 set->size += LYXP_SET_SIZE_STEP;
1195 }
1196
1197 if (idx > set->used) {
1198 LOGINT(set->ctx);
1199 idx = set->used;
1200 }
1201
1202 /* make space for the new node */
1203 if (idx < set->used) {
1204 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1205 }
1206 }
1207
1208 /* finally assign the value */
1209 set->val.nodes[idx].node = (struct lyd_node *)node;
1210 set->val.nodes[idx].type = node_type;
1211 set->val.nodes[idx].pos = pos;
1212 ++set->used;
1213
Michal Vasko2416fdd2021-10-01 11:07:10 +02001214 /* add into hash table */
1215 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001216}
1217
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001218LY_ERR
1219lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001220{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001221 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001222
1223 assert(set->type == LYXP_SET_SCNODE_SET);
1224
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001225 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001226 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001227 } else {
1228 if (set->used == set->size) {
1229 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001230 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001231 set->size += LYXP_SET_SIZE_STEP;
1232 }
1233
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001234 index = set->used;
1235 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1236 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001237 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001238 ++set->used;
1239 }
1240
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001241 if (index_p) {
1242 *index_p = index;
1243 }
1244
1245 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001246}
1247
1248/**
1249 * @brief Replace a node in a set with another. Context position aware.
1250 *
1251 * @param[in] set Set to use.
1252 * @param[in] node Node to insert to @p set.
1253 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1254 * @param[in] node_type Node type of @p node.
1255 * @param[in] idx Index in @p set of the node to replace.
1256 */
1257static void
1258set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1259{
1260 assert(set && (idx < set->used));
1261
Michal Vasko2caefc12019-11-14 16:07:56 +01001262 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1263 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1264 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001265 set->val.nodes[idx].node = (struct lyd_node *)node;
1266 set->val.nodes[idx].type = node_type;
1267 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001268 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1269 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1270 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001271}
1272
1273/**
1274 * @brief Set all nodes with ctx 1 to a new unique context value.
1275 *
1276 * @param[in] set Set to modify.
1277 * @return New context value.
1278 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001279static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001280set_scnode_new_in_ctx(struct lyxp_set *set)
1281{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001282 uint32_t i;
1283 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001284
1285 assert(set->type == LYXP_SET_SCNODE_SET);
1286
Radek Krejcif13b87b2020-12-01 22:02:17 +01001287 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288retry:
1289 for (i = 0; i < set->used; ++i) {
1290 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1291 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1292 goto retry;
1293 }
1294 }
1295 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001296 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001297 set->val.scnodes[i].in_ctx = ret_ctx;
1298 }
1299 }
1300
1301 return ret_ctx;
1302}
1303
1304/**
1305 * @brief Get unique @p node position in the data.
1306 *
1307 * @param[in] node Node to find.
1308 * @param[in] node_type Node type of @p node.
1309 * @param[in] root Root node.
1310 * @param[in] root_type Type of the XPath @p root node.
1311 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1312 * be used to increase efficiency and start the DFS from this node.
1313 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1314 * @return Node position.
1315 */
1316static uint32_t
1317get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
Radek Krejci0f969882020-08-21 16:56:47 +02001318 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001319{
Michal Vasko56daf732020-08-10 10:57:18 +02001320 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001321 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001322 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323
1324 assert(prev && prev_pos && !root->prev->next);
1325
1326 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1327 return 0;
1328 }
1329
1330 if (*prev) {
1331 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001332 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001333 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001334 goto dfs_search;
1335 }
1336
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001337 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001338 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001339dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001340 LYD_TREE_DFS_continue = 0;
1341
Michal Vasko56daf732020-08-10 10:57:18 +02001342 if (*prev && !elem) {
1343 /* resume previous DFS */
1344 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1345 LYD_TREE_DFS_continue = 0;
1346 }
1347
Michal Vasko03ff5a72019-09-11 13:49:33 +02001348 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001349 /* skip */
1350 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001351 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001352 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001353 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001354 break;
1355 }
Michal Vasko56daf732020-08-10 10:57:18 +02001356 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001357 }
Michal Vasko56daf732020-08-10 10:57:18 +02001358
1359 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001360 }
1361
1362 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001363 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001364 break;
1365 }
1366 }
1367
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001368 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001369 if (!(*prev)) {
1370 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001371 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001372 return 0;
1373 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001374 /* start the search again from the beginning */
1375 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001376
Michal Vasko56daf732020-08-10 10:57:18 +02001377 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001378 pos = 1;
1379 goto dfs_search;
1380 }
1381 }
1382
1383 /* remember the last found node for next time */
1384 *prev = node;
1385 *prev_pos = pos;
1386
1387 return pos;
1388}
1389
1390/**
1391 * @brief Assign (fill) missing node positions.
1392 *
1393 * @param[in] set Set to fill positions in.
1394 * @param[in] root Context root node.
1395 * @param[in] root_type Context root type.
1396 * @return LY_ERR
1397 */
1398static LY_ERR
1399set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1400{
1401 const struct lyd_node *prev = NULL, *tmp_node;
1402 uint32_t i, tmp_pos = 0;
1403
1404 for (i = 0; i < set->used; ++i) {
1405 if (!set->val.nodes[i].pos) {
1406 tmp_node = NULL;
1407 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001408 case LYXP_NODE_META:
1409 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001410 if (!tmp_node) {
1411 LOGINT_RET(root->schema->module->ctx);
1412 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001413 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001414 case LYXP_NODE_ELEM:
1415 case LYXP_NODE_TEXT:
1416 if (!tmp_node) {
1417 tmp_node = set->val.nodes[i].node;
1418 }
1419 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1420 break;
1421 default:
1422 /* all roots have position 0 */
1423 break;
1424 }
1425 }
1426 }
1427
1428 return LY_SUCCESS;
1429}
1430
1431/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001432 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001434 * @param[in] meta Metadata to use.
1435 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436 */
1437static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001438get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001439{
1440 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001441 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001442
Michal Vasko9f96a052020-03-10 09:41:45 +01001443 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001444 ++pos;
1445 }
1446
Michal Vasko9f96a052020-03-10 09:41:45 +01001447 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001448 return pos;
1449}
1450
1451/**
1452 * @brief Compare 2 nodes in respect to XPath document order.
1453 *
1454 * @param[in] item1 1st node.
1455 * @param[in] item2 2nd node.
1456 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1457 */
1458static int
1459set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1460{
Michal Vasko9f96a052020-03-10 09:41:45 +01001461 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001462
1463 if (item1->pos < item2->pos) {
1464 return -1;
1465 }
1466
1467 if (item1->pos > item2->pos) {
1468 return 1;
1469 }
1470
1471 /* node positions are equal, the fun case */
1472
1473 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1474 /* special case since text nodes are actually saved as their parents */
1475 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1476 if (item1->type == LYXP_NODE_ELEM) {
1477 assert(item2->type == LYXP_NODE_TEXT);
1478 return -1;
1479 } else {
1480 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1481 return 1;
1482 }
1483 }
1484
Michal Vasko9f96a052020-03-10 09:41:45 +01001485 /* we need meta positions now */
1486 if (item1->type == LYXP_NODE_META) {
1487 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001488 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001489 if (item2->type == LYXP_NODE_META) {
1490 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 }
1492
Michal Vasko9f96a052020-03-10 09:41:45 +01001493 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001494 /* check for duplicates */
1495 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001496 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 return 0;
1498 }
1499
Michal Vasko9f96a052020-03-10 09:41:45 +01001500 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001501 /* elem is always first, 2nd node is after it */
1502 if (item1->type == LYXP_NODE_ELEM) {
1503 assert(item2->type != LYXP_NODE_ELEM);
1504 return -1;
1505 }
1506
Michal Vasko9f96a052020-03-10 09:41:45 +01001507 /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001508 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001509 if (((item1->type == LYXP_NODE_TEXT) &&
1510 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1511 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1512 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1513 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 return 1;
1515 }
1516
Michal Vasko9f96a052020-03-10 09:41:45 +01001517 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518 /* 2nd is after 1st */
1519 return -1;
1520}
1521
1522/**
1523 * @brief Set cast for comparisons.
1524 *
1525 * @param[in] trg Target set to cast source into.
1526 * @param[in] src Source set.
1527 * @param[in] type Target set type.
1528 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001529 * @return LY_ERR
1530 */
1531static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001532set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001533{
1534 assert(src->type == LYXP_SET_NODE_SET);
1535
1536 set_init(trg, src);
1537
1538 /* insert node into target set */
1539 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1540
1541 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001542 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001543}
1544
Michal Vasko4c7763f2020-07-27 17:40:37 +02001545/**
1546 * @brief Set content canonization for comparisons.
1547 *
1548 * @param[in] trg Target set to put the canononized source into.
1549 * @param[in] src Source set.
1550 * @param[in] xp_node Source XPath node/meta to use for canonization.
1551 * @return LY_ERR
1552 */
1553static LY_ERR
1554set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1555{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001556 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001557 struct lyd_value val;
1558 struct ly_err_item *err = NULL;
1559 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001560 LY_ERR rc;
1561
1562 /* is there anything to canonize even? */
1563 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1564 /* do we have a type to use for canonization? */
1565 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1566 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1567 } else if (xp_node->type == LYXP_NODE_META) {
1568 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1569 }
1570 }
1571 if (!type) {
1572 goto fill;
1573 }
1574
1575 if (src->type == LYXP_SET_NUMBER) {
1576 /* canonize number */
1577 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1578 LOGMEM(src->ctx);
1579 return LY_EMEM;
1580 }
1581 } else {
1582 /* canonize string */
1583 str = strdup(src->val.str);
1584 }
1585
1586 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001587 rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001588 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001589 ly_err_free(err);
1590 if (rc) {
1591 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001592 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001593 goto fill;
1594 }
1595
Michal Vasko4c7763f2020-07-27 17:40:37 +02001596 /* use the canonized value */
1597 set_init(trg, src);
1598 trg->type = src->type;
1599 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001600 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001601 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1602 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001603 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001604 }
1605 type->plugin->free(src->ctx, &val);
1606 return LY_SUCCESS;
1607
1608fill:
1609 /* no canonization needed/possible */
1610 set_fill_set(trg, src);
1611 return LY_SUCCESS;
1612}
1613
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614#ifndef NDEBUG
1615
1616/**
1617 * @brief Bubble sort @p set into XPath document order.
1618 * Context position aware. Unused in the 'Release' build target.
1619 *
1620 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001621 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1622 */
1623static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001624set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001625{
1626 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001627 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001628 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 struct lyxp_set_node item;
1631 struct lyxp_set_hash_node hnode;
1632 uint64_t hash;
1633
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001634 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001635 return 0;
1636 }
1637
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001638 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001639 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001640 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001641
1642 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001643 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001644 return -1;
1645 }
1646
1647 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1648 print_set_debug(set);
1649
1650 for (i = 0; i < set->used; ++i) {
1651 inverted = 0;
1652 change = 0;
1653
1654 for (j = 1; j < set->used - i; ++j) {
1655 /* compare node positions */
1656 if (inverted) {
1657 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1658 } else {
1659 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1660 }
1661
1662 /* swap if needed */
1663 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1664 change = 1;
1665
1666 item = set->val.nodes[j - 1];
1667 set->val.nodes[j - 1] = set->val.nodes[j];
1668 set->val.nodes[j] = item;
1669 } else {
1670 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1671 inverted = !inverted;
1672 }
1673 }
1674
1675 ++ret;
1676
1677 if (!change) {
1678 break;
1679 }
1680 }
1681
1682 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1683 print_set_debug(set);
1684
1685 /* check node hashes */
1686 if (set->used >= LYD_HT_MIN_ITEMS) {
1687 assert(set->ht);
1688 for (i = 0; i < set->used; ++i) {
1689 hnode.node = set->val.nodes[i].node;
1690 hnode.type = set->val.nodes[i].type;
1691
1692 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1693 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1694 hash = dict_hash_multi(hash, NULL, 0);
1695
1696 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1697 }
1698 }
1699
1700 return ret - 1;
1701}
1702
1703/**
1704 * @brief Remove duplicate entries in a sorted node set.
1705 *
1706 * @param[in] set Sorted set to check.
1707 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1708 */
1709static LY_ERR
1710set_sorted_dup_node_clean(struct lyxp_set *set)
1711{
1712 uint32_t i = 0;
1713 LY_ERR ret = LY_SUCCESS;
1714
1715 if (set->used > 1) {
1716 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001717 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1718 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001719 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001720 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721 }
Michal Vasko57eab132019-09-24 11:46:26 +02001722 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001723 }
1724 }
1725
Michal Vasko2caefc12019-11-14 16:07:56 +01001726 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727 return ret;
1728}
1729
1730#endif
1731
1732/**
1733 * @brief Merge 2 sorted sets into one.
1734 *
1735 * @param[in,out] trg Set to merge into. Duplicates are removed.
1736 * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001737 * @return LY_ERR
1738 */
1739static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001740set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001741{
1742 uint32_t i, j, k, count, dup_count;
1743 int cmp;
1744 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001745
Michal Vaskod3678892020-05-21 10:06:58 +02001746 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001747 return LY_EINVAL;
1748 }
1749
Michal Vaskod3678892020-05-21 10:06:58 +02001750 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001751 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001752 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001753 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001754 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001755 return LY_SUCCESS;
1756 }
1757
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001758 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001759 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001760 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001761
1762 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001763 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001764 return LY_EINT;
1765 }
1766
1767#ifndef NDEBUG
1768 LOGDBG(LY_LDGXPATH, "MERGE target");
1769 print_set_debug(trg);
1770 LOGDBG(LY_LDGXPATH, "MERGE source");
1771 print_set_debug(src);
1772#endif
1773
1774 /* make memory for the merge (duplicates are not detected yet, so space
1775 * will likely be wasted on them, too bad) */
1776 if (trg->size - trg->used < src->used) {
1777 trg->size = trg->used + src->used;
1778
1779 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1780 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1781 }
1782
1783 i = 0;
1784 j = 0;
1785 count = 0;
1786 dup_count = 0;
1787 do {
1788 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1789 if (!cmp) {
1790 if (!count) {
1791 /* duplicate, just skip it */
1792 ++i;
1793 ++j;
1794 } else {
1795 /* we are copying something already, so let's copy the duplicate too,
1796 * we are hoping that afterwards there are some more nodes to
1797 * copy and this way we can copy them all together */
1798 ++count;
1799 ++dup_count;
1800 ++i;
1801 ++j;
1802 }
1803 } else if (cmp < 0) {
1804 /* inserting src node into trg, just remember it for now */
1805 ++count;
1806 ++i;
1807
1808 /* insert the hash now */
1809 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1810 } else if (count) {
1811copy_nodes:
1812 /* time to actually copy the nodes, we have found the largest block of nodes */
1813 memmove(&trg->val.nodes[j + (count - dup_count)],
1814 &trg->val.nodes[j],
1815 (trg->used - j) * sizeof *trg->val.nodes);
1816 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1817
1818 trg->used += count - dup_count;
1819 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1820 j += count - dup_count;
1821
1822 count = 0;
1823 dup_count = 0;
1824 } else {
1825 ++j;
1826 }
1827 } while ((i < src->used) && (j < trg->used));
1828
1829 if ((i < src->used) || count) {
1830 /* insert all the hashes first */
1831 for (k = i; k < src->used; ++k) {
1832 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1833 }
1834
1835 /* loop ended, but we need to copy something at trg end */
1836 count += src->used - i;
1837 i = src->used;
1838 goto copy_nodes;
1839 }
1840
1841 /* we are inserting hashes before the actual node insert, which causes
1842 * situations when there were initially not enough items for a hash table,
1843 * but even after some were inserted, hash table was not created (during
1844 * insertion the number of items is not updated yet) */
1845 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1846 set_insert_node_hash(trg, NULL, 0);
1847 }
1848
1849#ifndef NDEBUG
1850 LOGDBG(LY_LDGXPATH, "MERGE result");
1851 print_set_debug(trg);
1852#endif
1853
Michal Vaskod3678892020-05-21 10:06:58 +02001854 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001855 return LY_SUCCESS;
1856}
1857
1858/*
1859 * (re)parse functions
1860 *
1861 * Parse functions parse the expression into
1862 * tokens (syntactic analysis).
1863 *
1864 * Reparse functions perform semantic analysis
1865 * (do not save the result, just a check) of
1866 * the expression and fill repeat indices.
1867 */
1868
Michal Vasko14676352020-05-29 11:35:55 +02001869LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001870lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001871{
Michal Vasko004d3152020-06-11 19:59:22 +02001872 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001873 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001874 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001875 }
Michal Vasko14676352020-05-29 11:35:55 +02001876 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001877 }
1878
Michal Vasko004d3152020-06-11 19:59:22 +02001879 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001880 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001881 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001882 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001883 }
Michal Vasko14676352020-05-29 11:35:55 +02001884 return LY_ENOT;
1885 }
1886
1887 return LY_SUCCESS;
1888}
1889
Michal Vasko004d3152020-06-11 19:59:22 +02001890LY_ERR
1891lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1892{
1893 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1894
1895 /* skip the token */
1896 ++(*tok_idx);
1897
1898 return LY_SUCCESS;
1899}
1900
Michal Vasko14676352020-05-29 11:35:55 +02001901/* just like lyxp_check_token() but tests for 2 tokens */
1902static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001903exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001904 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001905{
Michal Vasko004d3152020-06-11 19:59:22 +02001906 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001907 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001908 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001909 }
1910 return LY_EINCOMPLETE;
1911 }
1912
Michal Vasko004d3152020-06-11 19:59:22 +02001913 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001914 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001915 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001916 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001917 }
1918 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001919 }
1920
1921 return LY_SUCCESS;
1922}
1923
Michal Vasko4911eeb2021-06-28 11:23:05 +02001924LY_ERR
1925lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1926 enum lyxp_token want_tok2)
1927{
1928 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1929
1930 /* skip the token */
1931 ++(*tok_idx);
1932
1933 return LY_SUCCESS;
1934}
1935
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936/**
1937 * @brief Stack operation push on the repeat array.
1938 *
1939 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001940 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1942 */
1943static void
Michal Vasko004d3152020-06-11 19:59:22 +02001944exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945{
1946 uint16_t i;
1947
Michal Vasko004d3152020-06-11 19:59:22 +02001948 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001949 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001950 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1951 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1952 exp->repeat[tok_idx][i] = repeat_op_idx;
1953 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001955 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1956 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1957 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958 }
1959}
1960
1961/**
1962 * @brief Reparse Predicate. Logs directly on error.
1963 *
1964 * [7] Predicate ::= '[' Expr ']'
1965 *
1966 * @param[in] ctx Context for logging.
1967 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001968 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001969 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001970 * @return LY_ERR
1971 */
1972static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001973reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974{
1975 LY_ERR rc;
1976
Michal Vasko004d3152020-06-11 19:59:22 +02001977 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001979 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980
aPiecekbf968d92021-05-27 14:35:05 +02001981 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 LY_CHECK_RET(rc);
1983
Michal Vasko004d3152020-06-11 19:59:22 +02001984 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001986 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987
1988 return LY_SUCCESS;
1989}
1990
1991/**
1992 * @brief Reparse RelativeLocationPath. Logs directly on error.
1993 *
1994 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1995 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1996 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1997 *
1998 * @param[in] ctx Context for logging.
1999 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002000 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002001 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2003 */
2004static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002005reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002006{
2007 LY_ERR rc;
2008
Michal Vasko004d3152020-06-11 19:59:22 +02002009 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002010 LY_CHECK_RET(rc);
2011
2012 goto step;
2013 do {
2014 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002015 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016
Michal Vasko004d3152020-06-11 19:59:22 +02002017 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018 LY_CHECK_RET(rc);
2019step:
2020 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002021 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002023 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 break;
2025
2026 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002027 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 break;
2029
2030 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002031 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032
Michal Vasko004d3152020-06-11 19:59:22 +02002033 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002035 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002036 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 return LY_EVALID;
2038 }
Radek Krejci0f969882020-08-21 16:56:47 +02002039 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002041 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042 goto reparse_predicate;
2043 break;
2044
2045 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002046 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047
2048 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002049 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002051 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052
2053 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002054 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002056 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057
2058reparse_predicate:
2059 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002060 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002061 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002062 LY_CHECK_RET(rc);
2063 }
2064 break;
2065 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002066 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 return LY_EVALID;
2068 }
Michal Vasko004d3152020-06-11 19:59:22 +02002069 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070
2071 return LY_SUCCESS;
2072}
2073
2074/**
2075 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2076 *
2077 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2078 *
2079 * @param[in] ctx Context for logging.
2080 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002081 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002082 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 * @return LY_ERR
2084 */
2085static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002086reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087{
2088 LY_ERR rc;
2089
Michal Vasko004d3152020-06-11 19:59:22 +02002090 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091
2092 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002093 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002095 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096
Michal Vasko004d3152020-06-11 19:59:22 +02002097 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002098 return LY_SUCCESS;
2099 }
Michal Vasko004d3152020-06-11 19:59:22 +02002100 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 case LYXP_TOKEN_DOT:
2102 case LYXP_TOKEN_DDOT:
2103 case LYXP_TOKEN_AT:
2104 case LYXP_TOKEN_NAMETEST:
2105 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002106 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002108 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 default:
2110 break;
2111 }
2112
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002114 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002115 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116
aPiecekbf968d92021-05-27 14:35:05 +02002117 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118 LY_CHECK_RET(rc);
2119 }
2120
2121 return LY_SUCCESS;
2122}
2123
2124/**
2125 * @brief Reparse FunctionCall. Logs directly on error.
2126 *
2127 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2128 *
2129 * @param[in] ctx Context for logging.
2130 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002131 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002132 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 * @return LY_ERR
2134 */
2135static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002136reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002138 int8_t min_arg_count = -1;
2139 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 LY_ERR rc;
2142
Michal Vasko004d3152020-06-11 19:59:22 +02002143 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002145 func_tok_idx = *tok_idx;
2146 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002148 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 1;
2150 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 1;
2153 max_arg_count = 1;
2154 }
2155 break;
2156 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002157 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 min_arg_count = 1;
2159 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002160 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 0;
2162 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002163 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 0;
2165 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002166 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 0;
2168 max_arg_count = 0;
2169 }
2170 break;
2171 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002172 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 1;
2174 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002175 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 min_arg_count = 0;
2177 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002178 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 1;
2180 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002181 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 1;
2183 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002184 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 1;
2186 max_arg_count = 1;
2187 }
2188 break;
2189 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002190 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002192 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002193 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 0;
2195 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002196 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 0;
2198 max_arg_count = 1;
2199 }
2200 break;
2201 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002202 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 1;
2204 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002205 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 1;
2207 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002208 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 min_arg_count = 0;
2210 max_arg_count = 0;
2211 }
2212 break;
2213 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002214 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 2;
2216 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002217 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 0;
2219 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002220 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 2;
2222 max_arg_count = 2;
2223 }
2224 break;
2225 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002226 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 2;
2228 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002229 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 3;
2231 max_arg_count = 3;
2232 }
2233 break;
2234 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002235 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002236 min_arg_count = 0;
2237 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002238 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 1;
2240 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002241 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 2;
2243 max_arg_count = 2;
2244 }
2245 break;
2246 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002247 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 2;
2249 max_arg_count = 2;
2250 }
2251 break;
2252 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002253 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254 min_arg_count = 2;
2255 max_arg_count = 2;
2256 }
2257 break;
2258 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002259 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 min_arg_count = 0;
2261 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002262 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 min_arg_count = 0;
2264 max_arg_count = 1;
2265 }
2266 break;
2267 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002268 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 min_arg_count = 0;
2270 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002271 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 min_arg_count = 2;
2273 max_arg_count = 2;
2274 }
2275 break;
2276 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002277 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 min_arg_count = 2;
2279 max_arg_count = 2;
2280 }
2281 break;
2282 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002283 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 min_arg_count = 2;
2285 max_arg_count = 2;
2286 }
2287 break;
2288 }
2289 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002290 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002291 return LY_EINVAL;
2292 }
Michal Vasko004d3152020-06-11 19:59:22 +02002293 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002294
2295 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002296 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002298 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002299
2300 /* ( Expr ( ',' Expr )* )? */
2301 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002302 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002303 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002304 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002306 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 LY_CHECK_RET(rc);
2308 }
Michal Vasko004d3152020-06-11 19:59:22 +02002309 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2310 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311
2312 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002313 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 LY_CHECK_RET(rc);
2315 }
2316
2317 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002318 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002320 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321
Radek Krejci857189e2020-09-01 13:26:36 +02002322 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002323 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324 return LY_EVALID;
2325 }
2326
2327 return LY_SUCCESS;
2328}
2329
2330/**
2331 * @brief Reparse PathExpr. Logs directly on error.
2332 *
2333 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2334 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2335 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2336 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2337 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2338 *
2339 * @param[in] ctx Context for logging.
2340 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002341 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002342 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002343 * @return LY_ERR
2344 */
2345static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002346reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347{
2348 LY_ERR rc;
2349
Michal Vasko004d3152020-06-11 19:59:22 +02002350 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002351 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 }
2353
Michal Vasko004d3152020-06-11 19:59:22 +02002354 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 case LYXP_TOKEN_PAR1:
2356 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002357 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358
aPiecekbf968d92021-05-27 14:35:05 +02002359 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 LY_CHECK_RET(rc);
2361
Michal Vasko004d3152020-06-11 19:59:22 +02002362 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002364 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365 goto predicate;
2366 break;
2367 case LYXP_TOKEN_DOT:
2368 case LYXP_TOKEN_DDOT:
2369 case LYXP_TOKEN_AT:
2370 case LYXP_TOKEN_NAMETEST:
2371 case LYXP_TOKEN_NODETYPE:
2372 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002373 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374 LY_CHECK_RET(rc);
2375 break;
2376 case LYXP_TOKEN_FUNCNAME:
2377 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002378 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379 LY_CHECK_RET(rc);
2380 goto predicate;
2381 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002382 case LYXP_TOKEN_OPER_PATH:
2383 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002385 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386 LY_CHECK_RET(rc);
2387 break;
2388 case LYXP_TOKEN_LITERAL:
2389 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002390 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391 goto predicate;
2392 break;
2393 case LYXP_TOKEN_NUMBER:
2394 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002395 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 goto predicate;
2397 break;
2398 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002399 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002400 return LY_EVALID;
2401 }
2402
2403 return LY_SUCCESS;
2404
2405predicate:
2406 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002407 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002408 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409 LY_CHECK_RET(rc);
2410 }
2411
2412 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002413 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414
2415 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002416 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417
aPiecekbf968d92021-05-27 14:35:05 +02002418 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002419 LY_CHECK_RET(rc);
2420 }
2421
2422 return LY_SUCCESS;
2423}
2424
2425/**
2426 * @brief Reparse UnaryExpr. Logs directly on error.
2427 *
2428 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2429 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2430 *
2431 * @param[in] ctx Context for logging.
2432 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002433 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002434 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 * @return LY_ERR
2436 */
2437static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002438reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439{
2440 uint16_t prev_exp;
2441 LY_ERR rc;
2442
2443 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002444 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002445 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2446 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002448 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 }
2450
2451 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002452 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002453 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454 LY_CHECK_RET(rc);
2455
2456 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002457 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002458 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002459 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460
aPiecekbf968d92021-05-27 14:35:05 +02002461 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462 LY_CHECK_RET(rc);
2463 }
2464
2465 return LY_SUCCESS;
2466}
2467
2468/**
2469 * @brief Reparse AdditiveExpr. Logs directly on error.
2470 *
2471 * [15] AdditiveExpr ::= MultiplicativeExpr
2472 * | AdditiveExpr '+' MultiplicativeExpr
2473 * | AdditiveExpr '-' MultiplicativeExpr
2474 * [16] MultiplicativeExpr ::= UnaryExpr
2475 * | MultiplicativeExpr '*' UnaryExpr
2476 * | MultiplicativeExpr 'div' UnaryExpr
2477 * | MultiplicativeExpr 'mod' UnaryExpr
2478 *
2479 * @param[in] ctx Context for logging.
2480 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002481 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002482 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483 * @return LY_ERR
2484 */
2485static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002486reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002487{
2488 uint16_t prev_add_exp, prev_mul_exp;
2489 LY_ERR rc;
2490
Michal Vasko004d3152020-06-11 19:59:22 +02002491 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002492 goto reparse_multiplicative_expr;
2493
2494 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002495 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2496 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002497 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002498 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499
2500reparse_multiplicative_expr:
2501 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002502 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002503 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 LY_CHECK_RET(rc);
2505
2506 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002507 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2508 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002510 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511
aPiecekbf968d92021-05-27 14:35:05 +02002512 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 LY_CHECK_RET(rc);
2514 }
2515 }
2516
2517 return LY_SUCCESS;
2518}
2519
2520/**
2521 * @brief Reparse EqualityExpr. Logs directly on error.
2522 *
2523 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2524 * | EqualityExpr '!=' RelationalExpr
2525 * [14] RelationalExpr ::= AdditiveExpr
2526 * | RelationalExpr '<' AdditiveExpr
2527 * | RelationalExpr '>' AdditiveExpr
2528 * | RelationalExpr '<=' AdditiveExpr
2529 * | RelationalExpr '>=' AdditiveExpr
2530 *
2531 * @param[in] ctx Context for logging.
2532 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002533 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002534 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 * @return LY_ERR
2536 */
2537static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002538reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002539{
2540 uint16_t prev_eq_exp, prev_rel_exp;
2541 LY_ERR rc;
2542
Michal Vasko004d3152020-06-11 19:59:22 +02002543 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002544 goto reparse_additive_expr;
2545
2546 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002547 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002549 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550
2551reparse_additive_expr:
2552 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002553 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002554 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 LY_CHECK_RET(rc);
2556
2557 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002558 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002559 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002560 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561
aPiecekbf968d92021-05-27 14:35:05 +02002562 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 LY_CHECK_RET(rc);
2564 }
2565 }
2566
2567 return LY_SUCCESS;
2568}
2569
2570/**
2571 * @brief Reparse OrExpr. Logs directly on error.
2572 *
2573 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2574 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2575 *
2576 * @param[in] ctx Context for logging.
2577 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002578 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002579 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002580 * @return LY_ERR
2581 */
2582static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002583reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002584{
2585 uint16_t prev_or_exp, prev_and_exp;
2586 LY_ERR rc;
2587
aPiecekbf968d92021-05-27 14:35:05 +02002588 ++depth;
2589 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2590
Michal Vasko004d3152020-06-11 19:59:22 +02002591 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002592 goto reparse_equality_expr;
2593
2594 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002595 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002596 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002597 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002598
2599reparse_equality_expr:
2600 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002601 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002602 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002603 LY_CHECK_RET(rc);
2604
2605 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002606 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002607 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002608 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002609
aPiecekbf968d92021-05-27 14:35:05 +02002610 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002611 LY_CHECK_RET(rc);
2612 }
2613 }
2614
2615 return LY_SUCCESS;
2616}
Radek Krejcib1646a92018-11-02 16:08:26 +01002617
2618/**
2619 * @brief Parse NCName.
2620 *
2621 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002622 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002623 */
Radek Krejcid4270262019-01-07 15:07:25 +01002624static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002625parse_ncname(const char *ncname)
2626{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002627 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002628 size_t size;
2629 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002630
2631 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2632 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2633 return len;
2634 }
2635
2636 do {
2637 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002638 if (!*ncname) {
2639 break;
2640 }
Radek Krejcid4270262019-01-07 15:07:25 +01002641 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002642 } while (is_xmlqnamechar(uc) && (uc != ':'));
2643
2644 return len;
2645}
2646
2647/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002648 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002649 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002650 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002651 * @param[in] exp Expression to use.
2652 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002653 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002654 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002655 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002656 */
2657static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002658exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002659{
2660 uint32_t prev;
2661
2662 if (exp->used == exp->size) {
2663 prev = exp->size;
2664 exp->size += LYXP_EXPR_SIZE_STEP;
2665 if (prev > exp->size) {
2666 LOGINT(ctx);
2667 return LY_EINT;
2668 }
2669
2670 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2671 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2672 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2673 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2674 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2675 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2676 }
2677
2678 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002679 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002680 exp->tok_len[exp->used] = tok_len;
2681 ++exp->used;
2682 return LY_SUCCESS;
2683}
2684
2685void
Michal Vasko14676352020-05-29 11:35:55 +02002686lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002687{
2688 uint16_t i;
2689
2690 if (!expr) {
2691 return;
2692 }
2693
2694 lydict_remove(ctx, expr->expr);
2695 free(expr->tokens);
2696 free(expr->tok_pos);
2697 free(expr->tok_len);
2698 if (expr->repeat) {
2699 for (i = 0; i < expr->used; ++i) {
2700 free(expr->repeat[i]);
2701 }
2702 }
2703 free(expr->repeat);
2704 free(expr);
2705}
2706
Radek Krejcif03a9e22020-09-18 20:09:31 +02002707LY_ERR
2708lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p)
Radek Krejcib1646a92018-11-02 16:08:26 +01002709{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710 LY_ERR ret = LY_SUCCESS;
2711 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002712 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002713 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002714 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002715 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002716
Radek Krejcif03a9e22020-09-18 20:09:31 +02002717 assert(expr_p);
2718
2719 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002720 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002721 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002722 }
2723
2724 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002725 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002726 }
2727 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002728 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002729 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002730 }
2731
2732 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002733 expr = calloc(1, sizeof *expr);
2734 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2735 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2736 expr->used = 0;
2737 expr->size = LYXP_EXPR_SIZE_START;
2738 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2739 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002740
Radek Krejcif03a9e22020-09-18 20:09:31 +02002741 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2742 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
Radek Krejcif03a9e22020-09-18 20:09:31 +02002744 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2745 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002746
Michal Vasko004d3152020-06-11 19:59:22 +02002747 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002748 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002749
Radek Krejcif03a9e22020-09-18 20:09:31 +02002750 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002751 ++parsed;
2752 }
2753
2754 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002756
2757 /* '(' */
2758 tok_len = 1;
2759 tok_type = LYXP_TOKEN_PAR1;
2760
Radek Krejcif03a9e22020-09-18 20:09:31 +02002761 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002762 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002763 if (((expr->tok_len[expr->used - 1] == 4) &&
2764 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2765 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2766 ((expr->tok_len[expr->used - 1] == 7) &&
2767 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002768 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002769 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002770 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002771 }
2772 prev_function_check = 0;
2773 }
2774
Radek Krejcif03a9e22020-09-18 20:09:31 +02002775 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002776
2777 /* ')' */
2778 tok_len = 1;
2779 tok_type = LYXP_TOKEN_PAR2;
2780
Radek Krejcif03a9e22020-09-18 20:09:31 +02002781 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002782
2783 /* '[' */
2784 tok_len = 1;
2785 tok_type = LYXP_TOKEN_BRACK1;
2786
Radek Krejcif03a9e22020-09-18 20:09:31 +02002787 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002788
2789 /* ']' */
2790 tok_len = 1;
2791 tok_type = LYXP_TOKEN_BRACK2;
2792
Radek Krejcif03a9e22020-09-18 20:09:31 +02002793 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002794
2795 /* '..' */
2796 tok_len = 2;
2797 tok_type = LYXP_TOKEN_DDOT;
2798
Radek Krejcif03a9e22020-09-18 20:09:31 +02002799 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002800
2801 /* '.' */
2802 tok_len = 1;
2803 tok_type = LYXP_TOKEN_DOT;
2804
Radek Krejcif03a9e22020-09-18 20:09:31 +02002805 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002806
2807 /* '@' */
2808 tok_len = 1;
2809 tok_type = LYXP_TOKEN_AT;
2810
Radek Krejcif03a9e22020-09-18 20:09:31 +02002811 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002812
2813 /* ',' */
2814 tok_len = 1;
2815 tok_type = LYXP_TOKEN_COMMA;
2816
Radek Krejcif03a9e22020-09-18 20:09:31 +02002817 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002818
2819 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002820 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2821 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002822 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002823 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002824 ++tok_len;
2825 tok_type = LYXP_TOKEN_LITERAL;
2826
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002828
2829 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002830 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2831 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002832 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002833 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002834 ++tok_len;
2835 tok_type = LYXP_TOKEN_LITERAL;
2836
Radek Krejcif03a9e22020-09-18 20:09:31 +02002837 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002838
2839 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002840 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2841 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002842 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002843 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002844 }
2845 tok_type = LYXP_TOKEN_NUMBER;
2846
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002848
2849 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002850 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002851 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002852 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002853 } else {
2854 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002855 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002856 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002857
Radek Krejcif03a9e22020-09-18 20:09:31 +02002858 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002859
Michal Vasko3e48bf32020-06-01 08:39:07 +02002860 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002861 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002862 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2863
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002865
2866 /* Operator '<=', '>=' */
2867 tok_len = 2;
2868 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002869
Radek Krejcif03a9e22020-09-18 20:09:31 +02002870 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002871
2872 /* Operator '|' */
2873 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002874 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002875
Radek Krejcif03a9e22020-09-18 20:09:31 +02002876 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002877
2878 /* Operator '+', '-' */
2879 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002880 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002881
Radek Krejcif03a9e22020-09-18 20:09:31 +02002882 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002883
Michal Vasko3e48bf32020-06-01 08:39:07 +02002884 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002885 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002886 tok_type = LYXP_TOKEN_OPER_EQUAL;
2887
Radek Krejcif03a9e22020-09-18 20:09:31 +02002888 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002889
2890 /* Operator '<', '>' */
2891 tok_len = 1;
2892 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002893
Michal Vasko69730152020-10-09 16:30:07 +02002894 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2895 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2896 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2897 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2898 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2899 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2900 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2901 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2902 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2903 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2904 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2905 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002906
2907 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002908 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002909 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002910 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002911
Radek Krejcif03a9e22020-09-18 20:09:31 +02002912 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002913 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002914 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002915
Radek Krejcif03a9e22020-09-18 20:09:31 +02002916 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002917 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002918 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002919
Radek Krejcif03a9e22020-09-18 20:09:31 +02002920 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002921 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002922 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002923
2924 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002925 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002926 expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002928 goto error;
2929 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002930 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002932 goto error;
2933 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002935
2936 /* NameTest '*' */
2937 tok_len = 1;
2938 tok_type = LYXP_TOKEN_NAMETEST;
2939
2940 } else {
2941
2942 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002943 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002944 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02002945 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002946 tok_len = ncname_len;
2947
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002949 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002950 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002951 ++tok_len;
2952 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002953 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002954 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02002955 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002956 tok_len += ncname_len;
2957 }
2958 /* remove old flag to prevent ambiguities */
2959 prev_function_check = 0;
2960 tok_type = LYXP_TOKEN_NAMETEST;
2961 } else {
2962 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2963 prev_function_check = 1;
2964 tok_type = LYXP_TOKEN_NAMETEST;
2965 }
2966 }
2967
2968 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002969 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002970 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002972 ++parsed;
2973 }
2974
Radek Krejcif03a9e22020-09-18 20:09:31 +02002975 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002976
Michal Vasko004d3152020-06-11 19:59:22 +02002977 if (reparse) {
2978 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002979 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2980 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002981
Michal Vasko004d3152020-06-11 19:59:22 +02002982 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002983 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002984 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002985 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002986 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002987 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002988 goto error;
2989 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002990 }
2991
Radek Krejcif03a9e22020-09-18 20:09:31 +02002992 print_expr_struct_debug(expr);
2993 *expr_p = expr;
2994 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002995
2996error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002997 lyxp_expr_free(ctx, expr);
2998 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002999}
3000
Michal Vasko1734be92020-09-22 08:55:10 +02003001LY_ERR
3002lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003003{
Michal Vasko1734be92020-09-22 08:55:10 +02003004 LY_ERR ret = LY_SUCCESS;
3005 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003006 uint32_t i, j;
3007
Michal Vasko7f45cf22020-10-01 12:49:44 +02003008 if (!exp) {
3009 goto cleanup;
3010 }
3011
Michal Vasko004d3152020-06-11 19:59:22 +02003012 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003013 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003014
Michal Vasko08e9b112021-06-11 15:41:17 +02003015 if (exp->used) {
3016 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3017 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3018 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003019
Michal Vasko08e9b112021-06-11 15:41:17 +02003020 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3021 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3022 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003023
Michal Vasko08e9b112021-06-11 15:41:17 +02003024 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3025 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3026 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003027
Michal Vasko08e9b112021-06-11 15:41:17 +02003028 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3029 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3030 for (i = 0; i < exp->used; ++i) {
3031 if (!exp->repeat[i]) {
3032 dup->repeat[i] = NULL;
3033 } else {
3034 for (j = 0; exp->repeat[i][j]; ++j) {}
3035 /* the ending 0 as well */
3036 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003037
Michal Vasko08e9b112021-06-11 15:41:17 +02003038 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3039 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3040 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3041 dup->repeat[i][j - 1] = 0;
3042 }
Michal Vasko004d3152020-06-11 19:59:22 +02003043 }
3044 }
3045
3046 dup->used = exp->used;
3047 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003048 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003049
Michal Vasko1734be92020-09-22 08:55:10 +02003050cleanup:
3051 if (ret) {
3052 lyxp_expr_free(ctx, dup);
3053 } else {
3054 *dup_p = dup;
3055 }
3056 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003057}
3058
Michal Vasko03ff5a72019-09-11 13:49:33 +02003059/*
3060 * warn functions
3061 *
3062 * Warn functions check specific reasonable conditions for schema XPath
3063 * and print a warning if they are not satisfied.
3064 */
3065
3066/**
3067 * @brief Get the last-added schema node that is currently in the context.
3068 *
3069 * @param[in] set Set to search in.
3070 * @return Last-added schema context node, NULL if no node is in context.
3071 */
3072static struct lysc_node *
3073warn_get_scnode_in_ctx(struct lyxp_set *set)
3074{
3075 uint32_t i;
3076
3077 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3078 return NULL;
3079 }
3080
3081 i = set->used;
3082 do {
3083 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003084 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003085 /* if there are more, simply return the first found (last added) */
3086 return set->val.scnodes[i].scnode;
3087 }
3088 } while (i);
3089
3090 return NULL;
3091}
3092
3093/**
3094 * @brief Test whether a type is numeric - integer type or decimal64.
3095 *
3096 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003097 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003098 */
Radek Krejci857189e2020-09-01 13:26:36 +02003099static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003100warn_is_numeric_type(struct lysc_type *type)
3101{
3102 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003103 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003104 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003105
3106 switch (type->basetype) {
3107 case LY_TYPE_DEC64:
3108 case LY_TYPE_INT8:
3109 case LY_TYPE_UINT8:
3110 case LY_TYPE_INT16:
3111 case LY_TYPE_UINT16:
3112 case LY_TYPE_INT32:
3113 case LY_TYPE_UINT32:
3114 case LY_TYPE_INT64:
3115 case LY_TYPE_UINT64:
3116 return 1;
3117 case LY_TYPE_UNION:
3118 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003119 LY_ARRAY_FOR(uni->types, u) {
3120 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003121 if (ret) {
3122 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003123 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003124 }
3125 }
3126 /* did not find any suitable type */
3127 return 0;
3128 case LY_TYPE_LEAFREF:
3129 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3130 default:
3131 return 0;
3132 }
3133}
3134
3135/**
3136 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3137 *
3138 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003139 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003140 */
Radek Krejci857189e2020-09-01 13:26:36 +02003141static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003142warn_is_string_type(struct lysc_type *type)
3143{
3144 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003145 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003146 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003147
3148 switch (type->basetype) {
3149 case LY_TYPE_BITS:
3150 case LY_TYPE_ENUM:
3151 case LY_TYPE_IDENT:
3152 case LY_TYPE_INST:
3153 case LY_TYPE_STRING:
3154 return 1;
3155 case LY_TYPE_UNION:
3156 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003157 LY_ARRAY_FOR(uni->types, u) {
3158 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003159 if (ret) {
3160 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003161 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003162 }
3163 }
3164 /* did not find any suitable type */
3165 return 0;
3166 case LY_TYPE_LEAFREF:
3167 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3168 default:
3169 return 0;
3170 }
3171}
3172
3173/**
3174 * @brief Test whether a type is one specific type.
3175 *
3176 * @param[in] type Type to test.
3177 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003178 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003179 */
Radek Krejci857189e2020-09-01 13:26:36 +02003180static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003181warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3182{
3183 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003184 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003185 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003186
3187 if (type->basetype == base) {
3188 return 1;
3189 } else if (type->basetype == LY_TYPE_UNION) {
3190 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003191 LY_ARRAY_FOR(uni->types, u) {
3192 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003193 if (ret) {
3194 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003195 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003196 }
3197 }
3198 /* did not find any suitable type */
3199 return 0;
3200 } else if (type->basetype == LY_TYPE_LEAFREF) {
3201 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3202 }
3203
3204 return 0;
3205}
3206
3207/**
3208 * @brief Get next type of a (union) type.
3209 *
3210 * @param[in] type Base type.
3211 * @param[in] prev_type Previously returned type.
3212 * @return Next type or NULL.
3213 */
3214static struct lysc_type *
3215warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3216{
3217 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003218 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003219 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003220
3221 switch (type->basetype) {
3222 case LY_TYPE_UNION:
3223 uni = (struct lysc_type_union *)type;
3224 if (!prev_type) {
3225 return uni->types[0];
3226 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003227 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003228 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003229 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003230 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003231 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003232 found = 1;
3233 }
3234 }
3235 return NULL;
3236 default:
3237 if (prev_type) {
3238 assert(type == prev_type);
3239 return NULL;
3240 } else {
3241 return type;
3242 }
3243 }
3244}
3245
3246/**
3247 * @brief Test whether 2 types have a common type.
3248 *
3249 * @param[in] type1 First type.
3250 * @param[in] type2 Second type.
3251 * @return 1 if they do, 0 otherwise.
3252 */
3253static int
3254warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3255{
3256 struct lysc_type *t1, *rt1, *t2, *rt2;
3257
3258 t1 = NULL;
3259 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3260 if (t1->basetype == LY_TYPE_LEAFREF) {
3261 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3262 } else {
3263 rt1 = t1;
3264 }
3265
3266 t2 = NULL;
3267 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3268 if (t2->basetype == LY_TYPE_LEAFREF) {
3269 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3270 } else {
3271 rt2 = t2;
3272 }
3273
3274 if (rt2->basetype == rt1->basetype) {
3275 /* match found */
3276 return 1;
3277 }
3278 }
3279 }
3280
3281 return 0;
3282}
3283
3284/**
3285 * @brief Check both operands of comparison operators.
3286 *
3287 * @param[in] ctx Context for errors.
3288 * @param[in] set1 First operand set.
3289 * @param[in] set2 Second operand set.
3290 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3291 * @param[in] expr Start of the expression to print with the warning.
3292 * @param[in] tok_pos Token position.
3293 */
3294static void
Radek Krejci857189e2020-09-01 13:26:36 +02003295warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003296{
3297 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003298 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003299
3300 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3301 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3302
3303 if (!node1 && !node2) {
3304 /* no node-sets involved, nothing to do */
3305 return;
3306 }
3307
3308 if (node1) {
3309 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3310 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3311 warning = 1;
3312 leaves = 0;
3313 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3314 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3315 warning = 1;
3316 }
3317 }
3318
3319 if (node2) {
3320 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3321 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3322 warning = 1;
3323 leaves = 0;
3324 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3325 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3326 warning = 1;
3327 }
3328 }
3329
3330 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003331 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3332 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3333 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3334 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003335 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3336 warning = 1;
3337 }
3338 }
3339
3340 if (warning) {
3341 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3342 }
3343}
3344
3345/**
3346 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3347 *
3348 * @param[in] exp Parsed XPath expression.
3349 * @param[in] set Set with the leaf/leaf-list.
3350 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3351 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3352 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3353 */
3354static void
Michal Vasko40308e72020-10-20 16:38:40 +02003355warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3356 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003357{
3358 struct lysc_node *scnode;
3359 struct lysc_type *type;
3360 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003361 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003362 LY_ERR rc;
3363 struct ly_err_item *err = NULL;
3364
Michal Vasko69730152020-10-09 16:30:07 +02003365 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3366 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003367 /* check that the node can have the specified value */
3368 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3369 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3370 } else {
3371 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3372 }
3373 if (!value) {
3374 LOGMEM(set->ctx);
3375 return;
3376 }
3377
3378 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3379 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003380 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003382 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003383 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003384 }
3385
3386 type = ((struct lysc_node_leaf *)scnode)->type;
3387 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003388 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003389 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003390 if (rc == LY_EINCOMPLETE) {
3391 rc = LY_SUCCESS;
3392 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393
3394 if (err) {
3395 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3396 ly_err_free(err);
3397 } else if (rc != LY_SUCCESS) {
3398 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3399 }
3400 if (rc != LY_SUCCESS) {
3401 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003402 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003403 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003404 } else {
3405 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003406 }
3407 }
3408 free(value);
3409 }
3410}
3411
3412/*
3413 * XPath functions
3414 */
3415
3416/**
3417 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3418 * depending on whether the first node bit value from the second argument is set.
3419 *
3420 * @param[in] args Array of arguments.
3421 * @param[in] arg_count Count of elements in @p args.
3422 * @param[in,out] set Context and result set at the same time.
3423 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003424 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003425 */
3426static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003427xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003428{
3429 struct lyd_node_term *leaf;
3430 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003431 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003433 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003434
3435 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003436 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003437 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003438 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3439 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3440 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3441 sleaf->name);
3442 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3443 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3444 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003445 }
3446
3447 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3448 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003449 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3450 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003451 } else if (!warn_is_string_type(sleaf->type)) {
3452 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003453 }
3454 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003455 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003456 return rc;
3457 }
3458
Michal Vaskod3678892020-05-21 10:06:58 +02003459 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003460 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003461 return LY_EVALID;
3462 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003463 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003464 LY_CHECK_RET(rc);
3465
3466 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003467 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003469 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3470 LYD_VALUE_GET(&leaf->value, bits);
3471 LY_ARRAY_FOR(bits->items, u) {
3472 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003473 set_fill_boolean(set, 1);
3474 break;
3475 }
3476 }
3477 }
3478 }
3479
3480 return LY_SUCCESS;
3481}
3482
3483/**
3484 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3485 * with the argument converted to boolean.
3486 *
3487 * @param[in] args Array of arguments.
3488 * @param[in] arg_count Count of elements in @p args.
3489 * @param[in,out] set Context and result set at the same time.
3490 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003491 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003492 */
3493static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003494xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003495{
3496 LY_ERR rc;
3497
3498 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003499 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500 return LY_SUCCESS;
3501 }
3502
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003503 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003504 LY_CHECK_RET(rc);
3505 set_fill_set(set, args[0]);
3506
3507 return LY_SUCCESS;
3508}
3509
3510/**
3511 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3512 * with the first argument rounded up to the nearest integer.
3513 *
3514 * @param[in] args Array of arguments.
3515 * @param[in] arg_count Count of elements in @p args.
3516 * @param[in,out] set Context and result set at the same time.
3517 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003518 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003519 */
3520static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003521xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522{
3523 struct lysc_node_leaf *sleaf;
3524 LY_ERR rc = LY_SUCCESS;
3525
3526 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003527 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003529 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3530 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3531 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3532 sleaf->name);
3533 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3534 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3535 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003536 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003537 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003538 return rc;
3539 }
3540
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003541 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003542 LY_CHECK_RET(rc);
3543 if ((long long)args[0]->val.num != args[0]->val.num) {
3544 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3545 } else {
3546 set_fill_number(set, args[0]->val.num);
3547 }
3548
3549 return LY_SUCCESS;
3550}
3551
3552/**
3553 * @brief Execute the XPath concat(string, string, string*) function.
3554 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3555 *
3556 * @param[in] args Array of arguments.
3557 * @param[in] arg_count Count of elements in @p args.
3558 * @param[in,out] set Context and result set at the same time.
3559 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003560 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003561 */
3562static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003563xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003564{
3565 uint16_t i;
3566 char *str = NULL;
3567 size_t used = 1;
3568 LY_ERR rc = LY_SUCCESS;
3569 struct lysc_node_leaf *sleaf;
3570
3571 if (options & LYXP_SCNODE_ALL) {
3572 for (i = 0; i < arg_count; ++i) {
3573 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3574 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3575 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003576 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003577 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003578 LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003579 }
3580 }
3581 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003582 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 return rc;
3584 }
3585
3586 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003587 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 if (rc != LY_SUCCESS) {
3589 free(str);
3590 return rc;
3591 }
3592
3593 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3594 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3595 strcpy(str + used - 1, args[i]->val.str);
3596 used += strlen(args[i]->val.str);
3597 }
3598
3599 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003600 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003601 set->type = LYXP_SET_STRING;
3602 set->val.str = str;
3603
3604 return LY_SUCCESS;
3605}
3606
3607/**
3608 * @brief Execute the XPath contains(string, string) function.
3609 * Returns LYXP_SET_BOOLEAN whether the second argument can
3610 * be found in the first or not.
3611 *
3612 * @param[in] args Array of arguments.
3613 * @param[in] arg_count Count of elements in @p args.
3614 * @param[in,out] set Context and result set at the same time.
3615 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003616 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003617 */
3618static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003619xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620{
3621 struct lysc_node_leaf *sleaf;
3622 LY_ERR rc = LY_SUCCESS;
3623
3624 if (options & LYXP_SCNODE_ALL) {
3625 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3626 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003627 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3628 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003629 } else if (!warn_is_string_type(sleaf->type)) {
3630 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 }
3632 }
3633
3634 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3635 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003636 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3637 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 } else if (!warn_is_string_type(sleaf->type)) {
3639 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 }
3641 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003642 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003643 return rc;
3644 }
3645
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003646 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003648 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 LY_CHECK_RET(rc);
3650
3651 if (strstr(args[0]->val.str, args[1]->val.str)) {
3652 set_fill_boolean(set, 1);
3653 } else {
3654 set_fill_boolean(set, 0);
3655 }
3656
3657 return LY_SUCCESS;
3658}
3659
3660/**
3661 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3662 * with the size of the node-set from the argument.
3663 *
3664 * @param[in] args Array of arguments.
3665 * @param[in] arg_count Count of elements in @p args.
3666 * @param[in,out] set Context and result set at the same time.
3667 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003668 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 */
3670static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003671xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003672{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 LY_ERR rc = LY_SUCCESS;
3674
3675 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003676 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003679 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 return rc;
3681 }
3682
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003684 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 return LY_EVALID;
3686 }
3687
3688 set_fill_number(set, args[0]->used);
3689 return LY_SUCCESS;
3690}
3691
3692/**
3693 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3694 * with the context with the intial node.
3695 *
3696 * @param[in] args Array of arguments.
3697 * @param[in] arg_count Count of elements in @p args.
3698 * @param[in,out] set Context and result set at the same time.
3699 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003700 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003701 */
3702static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003703xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704{
3705 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003706 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 return LY_EVALID;
3708 }
3709
3710 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003711 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003712
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003713 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003715 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716
3717 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003718 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719 }
3720
3721 return LY_SUCCESS;
3722}
3723
3724/**
3725 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3726 * leafref or instance-identifier target node(s).
3727 *
3728 * @param[in] args Array of arguments.
3729 * @param[in] arg_count Count of elements in @p args.
3730 * @param[in,out] set Context and result set at the same time.
3731 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003732 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 */
3734static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003735xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736{
3737 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003738 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003739 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003740 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003741 struct ly_path *p;
3742 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003744 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003745 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746
3747 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003748 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003750 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003751 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003752 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3753 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003754 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3755 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003756 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3757 __func__, sleaf->name);
3758 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003760 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003761 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003762 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003763 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003764
3765 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003766 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003767 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003768 if (!r) {
3769 /* get the target node */
3770 target = p[LY_ARRAY_COUNT(p) - 1].node;
3771 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003772
Michal Vasko741bb562021-06-24 11:59:50 +02003773 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3774 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003775 }
3776
Michal Vasko741bb562021-06-24 11:59:50 +02003777 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003778 }
3779
Michal Vaskod3678892020-05-21 10:06:58 +02003780 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003781 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003782 return LY_EVALID;
3783 }
3784
Michal Vaskod3678892020-05-21 10:06:58 +02003785 lyxp_set_free_content(set);
3786 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3788 sleaf = (struct lysc_node_leaf *)leaf->schema;
3789 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3790 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3791 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003792 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003793 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003794 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003795 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003796 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 } else {
3799 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003800 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003801 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003802 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 return LY_EVALID;
3804 }
3805 }
Michal Vasko004d3152020-06-11 19:59:22 +02003806
3807 /* insert it */
3808 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003809 }
3810 }
3811
3812 return LY_SUCCESS;
3813}
3814
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003816xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func)
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003817{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003818 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003819 LY_ARRAY_COUNT_TYPE u;
3820 struct lyd_node_term *leaf;
3821 struct lysc_node_leaf *sleaf;
3822 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003823 struct lyd_value *val;
3824 const struct lys_module *mod;
3825 const char *id_name;
3826 uint16_t id_len;
3827 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003828 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003829 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003830
3831 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003832 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003833 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003834 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3835 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3836 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3837 sleaf->name);
3838 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3839 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3840 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003841 }
3842
3843 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3844 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3845 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003846 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003847 } else if (!warn_is_string_type(sleaf->type)) {
3848 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3849 }
3850 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003851 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003852 return rc;
3853 }
3854
3855 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003856 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003857 return LY_EVALID;
3858 }
3859 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3860 LY_CHECK_RET(rc);
3861
Michal Vasko93923692021-05-07 15:28:02 +02003862 /* parse the identity */
3863 id_name = args[1]->val.str;
3864 id_len = strlen(id_name);
3865 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3866 LY_CHECK_RET(rc);
3867 if (!mod) {
3868 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3869 return LY_EVALID;
3870 }
3871
3872 /* find the identity */
3873 found = 0;
3874 LY_ARRAY_FOR(mod->identities, u) {
3875 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3876 /* we have match */
3877 found = 1;
3878 break;
3879 }
3880 }
3881 if (!found) {
3882 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3883 return LY_EVALID;
3884 }
3885 id = &mod->identities[u];
3886
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003887 set_fill_boolean(set, 0);
3888 found = 0;
3889 for (i = 0; i < args[0]->used; ++i) {
3890 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3891 continue;
3892 }
3893
3894 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3895 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3896 sleaf = (struct lysc_node_leaf *)leaf->schema;
3897 val = &leaf->value;
3898 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3899 /* uninteresting */
3900 continue;
3901 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003902 } else {
3903 meta = args[0]->val.meta[i].meta;
3904 val = &meta->value;
3905 if (val->realtype->basetype != LY_TYPE_IDENT) {
3906 /* uninteresting */
3907 continue;
3908 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003909 }
3910
Michal Vasko93923692021-05-07 15:28:02 +02003911 /* check the identity itself */
3912 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003913 set_fill_boolean(set, 1);
3914 found = 1;
3915 }
Michal Vasko93923692021-05-07 15:28:02 +02003916 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3917 set_fill_boolean(set, 1);
3918 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003919 }
3920
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003921 if (found) {
3922 break;
3923 }
3924 }
3925
3926 return LY_SUCCESS;
3927}
3928
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929/**
3930 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3931 * on whether the first argument nodes contain a node of an identity derived from the second
3932 * argument identity.
3933 *
3934 * @param[in] args Array of arguments.
3935 * @param[in] arg_count Count of elements in @p args.
3936 * @param[in,out] set Context and result set at the same time.
3937 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003938 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 */
3940static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003941xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003942{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003943 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944}
3945
3946/**
3947 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3948 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3949 * the second argument identity.
3950 *
3951 * @param[in] args Array of arguments.
3952 * @param[in] arg_count Count of elements in @p args.
3953 * @param[in,out] set Context and result set at the same time.
3954 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003955 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003956 */
3957static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003958xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003959{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003960 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003961}
3962
3963/**
3964 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3965 * with the integer value of the first node's enum value, otherwise NaN.
3966 *
3967 * @param[in] args Array of arguments.
3968 * @param[in] arg_count Count of elements in @p args.
3969 * @param[in,out] set Context and result set at the same time.
3970 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003971 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 */
3973static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003974xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975{
3976 struct lyd_node_term *leaf;
3977 struct lysc_node_leaf *sleaf;
3978 LY_ERR rc = LY_SUCCESS;
3979
3980 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003981 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003982 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003983 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3984 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3985 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3986 sleaf->name);
3987 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3988 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3989 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003991 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 return rc;
3993 }
3994
Michal Vaskod3678892020-05-21 10:06:58 +02003995 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003996 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 return LY_EVALID;
3998 }
3999
4000 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004001 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4003 sleaf = (struct lysc_node_leaf *)leaf->schema;
4004 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4005 set_fill_number(set, leaf->value.enum_item->value);
4006 }
4007 }
4008
4009 return LY_SUCCESS;
4010}
4011
4012/**
4013 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4014 * with false value.
4015 *
4016 * @param[in] args Array of arguments.
4017 * @param[in] arg_count Count of elements in @p args.
4018 * @param[in,out] set Context and result set at the same time.
4019 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004020 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 */
4022static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004023xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024{
4025 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004026 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004027 return LY_SUCCESS;
4028 }
4029
4030 set_fill_boolean(set, 0);
4031 return LY_SUCCESS;
4032}
4033
4034/**
4035 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4036 * with the first argument floored (truncated).
4037 *
4038 * @param[in] args Array of arguments.
4039 * @param[in] arg_count Count of elements in @p args.
4040 * @param[in,out] set Context and result set at the same time.
4041 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004042 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004043 */
4044static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004045xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02004046{
4047 LY_ERR rc;
4048
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004049 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 LY_CHECK_RET(rc);
4051 if (isfinite(args[0]->val.num)) {
4052 set_fill_number(set, (long long)args[0]->val.num);
4053 }
4054
4055 return LY_SUCCESS;
4056}
4057
4058/**
4059 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4060 * whether the language of the text matches the one from the argument.
4061 *
4062 * @param[in] args Array of arguments.
4063 * @param[in] arg_count Count of elements in @p args.
4064 * @param[in,out] set Context and result set at the same time.
4065 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004066 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 */
4068static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004069xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070{
4071 const struct lyd_node *node;
4072 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004073 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 LY_ERR rc = LY_SUCCESS;
4076
4077 if (options & LYXP_SCNODE_ALL) {
4078 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4079 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004080 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4081 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004082 } else if (!warn_is_string_type(sleaf->type)) {
4083 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084 }
4085 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004086 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 return rc;
4088 }
4089
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004090 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091 LY_CHECK_RET(rc);
4092
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004094 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004096 } else if (!set->used) {
4097 set_fill_boolean(set, 0);
4098 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 }
4100
4101 switch (set->val.nodes[0].type) {
4102 case LYXP_NODE_ELEM:
4103 case LYXP_NODE_TEXT:
4104 node = set->val.nodes[0].node;
4105 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004106 case LYXP_NODE_META:
4107 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 break;
4109 default:
4110 /* nothing to do with roots */
4111 set_fill_boolean(set, 0);
4112 return LY_SUCCESS;
4113 }
4114
Michal Vasko9f96a052020-03-10 09:41:45 +01004115 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004116 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004117 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004118 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004119 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 break;
4121 }
4122 }
4123
Michal Vasko9f96a052020-03-10 09:41:45 +01004124 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125 break;
4126 }
4127 }
4128
4129 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004130 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004131 set_fill_boolean(set, 0);
4132 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004133 uint64_t i;
4134
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004135 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 for (i = 0; args[0]->val.str[i]; ++i) {
4137 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4138 set_fill_boolean(set, 0);
4139 break;
4140 }
4141 }
4142 if (!args[0]->val.str[i]) {
4143 if (!val[i] || (val[i] == '-')) {
4144 set_fill_boolean(set, 1);
4145 } else {
4146 set_fill_boolean(set, 0);
4147 }
4148 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 }
4150
4151 return LY_SUCCESS;
4152}
4153
4154/**
4155 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4156 * with the context size.
4157 *
4158 * @param[in] args Array of arguments.
4159 * @param[in] arg_count Count of elements in @p args.
4160 * @param[in,out] set Context and result set at the same time.
4161 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004162 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163 */
4164static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004165xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166{
4167 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004168 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169 return LY_SUCCESS;
4170 }
4171
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004173 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004175 } else if (!set->used) {
4176 set_fill_number(set, 0);
4177 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 }
4179
4180 set_fill_number(set, set->ctx_size);
4181 return LY_SUCCESS;
4182}
4183
4184/**
4185 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4186 * with the node name without namespace from the argument or the context.
4187 *
4188 * @param[in] args Array of arguments.
4189 * @param[in] arg_count Count of elements in @p args.
4190 * @param[in,out] set Context and result set at the same time.
4191 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004192 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 */
4194static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004195xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196{
4197 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004198
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199 /* suppress unused variable warning */
4200 (void)options;
4201
4202 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004203 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204 return LY_SUCCESS;
4205 }
4206
4207 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004209 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004210 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004212 } else if (!args[0]->used) {
4213 set_fill_string(set, "", 0);
4214 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 }
4216
4217 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004218 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219
4220 item = &args[0]->val.nodes[0];
4221 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004223 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004225 } else if (!set->used) {
4226 set_fill_string(set, "", 0);
4227 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 }
4229
4230 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004231 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004232
4233 item = &set->val.nodes[0];
4234 }
4235
4236 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004237 case LYXP_NODE_NONE:
4238 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 case LYXP_NODE_ROOT:
4240 case LYXP_NODE_ROOT_CONFIG:
4241 case LYXP_NODE_TEXT:
4242 set_fill_string(set, "", 0);
4243 break;
4244 case LYXP_NODE_ELEM:
4245 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4246 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004247 case LYXP_NODE_META:
4248 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004249 break;
4250 }
4251
4252 return LY_SUCCESS;
4253}
4254
4255/**
4256 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4257 * with the node name fully qualified (with namespace) from the argument or the context.
4258 *
4259 * @param[in] args Array of arguments.
4260 * @param[in] arg_count Count of elements in @p args.
4261 * @param[in,out] set Context and result set at the same time.
4262 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004263 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264 */
4265static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004266xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267{
4268 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004269 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004271 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272
4273 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004274 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275 return LY_SUCCESS;
4276 }
4277
4278 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004280 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004281 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004282 } else if (!args[0]->used) {
4283 set_fill_string(set, "", 0);
4284 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 }
4286
4287 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004288 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289
4290 item = &args[0]->val.nodes[0];
4291 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004293 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004295 } else if (!set->used) {
4296 set_fill_string(set, "", 0);
4297 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004298 }
4299
4300 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004301 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302
4303 item = &set->val.nodes[0];
4304 }
4305
4306 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004307 case LYXP_NODE_NONE:
4308 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309 case LYXP_NODE_ROOT:
4310 case LYXP_NODE_ROOT_CONFIG:
4311 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004312 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 break;
4314 case LYXP_NODE_ELEM:
4315 mod = item->node->schema->module;
4316 name = item->node->schema->name;
4317 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004318 case LYXP_NODE_META:
4319 mod = ((struct lyd_meta *)item->node)->annotation->module;
4320 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 break;
4322 }
4323
4324 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004325 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4327 set_fill_string(set, str, strlen(str));
4328 free(str);
4329 } else {
4330 set_fill_string(set, "", 0);
4331 }
4332
4333 return LY_SUCCESS;
4334}
4335
4336/**
4337 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4338 * with the namespace of the node from the argument or the context.
4339 *
4340 * @param[in] args Array of arguments.
4341 * @param[in] arg_count Count of elements in @p args.
4342 * @param[in,out] set Context and result set at the same time.
4343 * @param[in] options XPath options.
4344 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4345 */
4346static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004347xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004348{
4349 struct lyxp_set_node *item;
4350 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004351
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352 /* suppress unused variable warning */
4353 (void)options;
4354
4355 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004356 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004357 return LY_SUCCESS;
4358 }
4359
4360 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004361 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004362 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004363 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004364 return LY_EVALID;
4365 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 set_fill_string(set, "", 0);
4367 return LY_SUCCESS;
4368 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369
4370 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004371 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372
4373 item = &args[0]->val.nodes[0];
4374 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004376 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004378 } else if (!set->used) {
4379 set_fill_string(set, "", 0);
4380 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 }
4382
4383 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004384 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004385
4386 item = &set->val.nodes[0];
4387 }
4388
4389 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004390 case LYXP_NODE_NONE:
4391 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 case LYXP_NODE_ROOT:
4393 case LYXP_NODE_ROOT_CONFIG:
4394 case LYXP_NODE_TEXT:
4395 set_fill_string(set, "", 0);
4396 break;
4397 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004398 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004399 if (item->type == LYXP_NODE_ELEM) {
4400 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004401 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004402 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004403 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 }
4405
4406 set_fill_string(set, mod->ns, strlen(mod->ns));
4407 break;
4408 }
4409
4410 return LY_SUCCESS;
4411}
4412
4413/**
4414 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4415 * with only nodes from the context. In practice it either leaves the context
4416 * as it is or returns an empty node set.
4417 *
4418 * @param[in] args Array of arguments.
4419 * @param[in] arg_count Count of elements in @p args.
4420 * @param[in,out] set Context and result set at the same time.
4421 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004422 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004423 */
4424static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004425xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004426{
4427 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004428 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429 return LY_SUCCESS;
4430 }
4431
4432 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004433 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004434 }
4435 return LY_SUCCESS;
4436}
4437
4438/**
4439 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4440 * with normalized value (no leading, trailing, double white spaces) of the node
4441 * from the argument or the context.
4442 *
4443 * @param[in] args Array of arguments.
4444 * @param[in] arg_count Count of elements in @p args.
4445 * @param[in,out] set Context and result set at the same time.
4446 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004447 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448 */
4449static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004450xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451{
4452 uint16_t i, new_used;
4453 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004454 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004455 struct lysc_node_leaf *sleaf;
4456 LY_ERR rc = LY_SUCCESS;
4457
4458 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004459 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4460 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004462 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4463 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 } else if (!warn_is_string_type(sleaf->type)) {
4465 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466 }
4467 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004468 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 return rc;
4470 }
4471
4472 if (arg_count) {
4473 set_fill_set(set, args[0]);
4474 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004475 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 LY_CHECK_RET(rc);
4477
4478 /* is there any normalization necessary? */
4479 for (i = 0; set->val.str[i]; ++i) {
4480 if (is_xmlws(set->val.str[i])) {
4481 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4482 have_spaces = 1;
4483 break;
4484 }
4485 space_before = 1;
4486 } else {
4487 space_before = 0;
4488 }
4489 }
4490
4491 /* yep, there is */
4492 if (have_spaces) {
4493 /* it's enough, at least one character will go, makes space for ending '\0' */
4494 new = malloc(strlen(set->val.str) * sizeof(char));
4495 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4496 new_used = 0;
4497
4498 space_before = 0;
4499 for (i = 0; set->val.str[i]; ++i) {
4500 if (is_xmlws(set->val.str[i])) {
4501 if ((i == 0) || space_before) {
4502 space_before = 1;
4503 continue;
4504 } else {
4505 space_before = 1;
4506 }
4507 } else {
4508 space_before = 0;
4509 }
4510
4511 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4512 ++new_used;
4513 }
4514
4515 /* at worst there is one trailing space now */
4516 if (new_used && is_xmlws(new[new_used - 1])) {
4517 --new_used;
4518 }
4519
4520 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4521 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4522 new[new_used] = '\0';
4523
4524 free(set->val.str);
4525 set->val.str = new;
4526 }
4527
4528 return LY_SUCCESS;
4529}
4530
4531/**
4532 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4533 * with the argument converted to boolean and logically inverted.
4534 *
4535 * @param[in] args Array of arguments.
4536 * @param[in] arg_count Count of elements in @p args.
4537 * @param[in,out] set Context and result set at the same time.
4538 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004539 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 */
4541static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004542xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543{
4544 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004545 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 return LY_SUCCESS;
4547 }
4548
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004549 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004550 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 set_fill_boolean(set, 0);
4552 } else {
4553 set_fill_boolean(set, 1);
4554 }
4555
4556 return LY_SUCCESS;
4557}
4558
4559/**
4560 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4561 * with the number representation of either the argument or the context.
4562 *
4563 * @param[in] args Array of arguments.
4564 * @param[in] arg_count Count of elements in @p args.
4565 * @param[in,out] set Context and result set at the same time.
4566 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004567 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 */
4569static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004570xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004571{
4572 LY_ERR rc;
4573
4574 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004575 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004576 return LY_SUCCESS;
4577 }
4578
4579 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004580 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 LY_CHECK_RET(rc);
4582 set_fill_set(set, args[0]);
4583 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004584 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004585 LY_CHECK_RET(rc);
4586 }
4587
4588 return LY_SUCCESS;
4589}
4590
4591/**
4592 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4593 * with the context position.
4594 *
4595 * @param[in] args Array of arguments.
4596 * @param[in] arg_count Count of elements in @p args.
4597 * @param[in,out] set Context and result set at the same time.
4598 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004599 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 */
4601static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004602xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603{
4604 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004605 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004606 return LY_SUCCESS;
4607 }
4608
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004610 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004612 } else if (!set->used) {
4613 set_fill_number(set, 0);
4614 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 }
4616
4617 set_fill_number(set, set->ctx_pos);
4618
4619 /* UNUSED in 'Release' build type */
4620 (void)options;
4621 return LY_SUCCESS;
4622}
4623
4624/**
4625 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4626 * depending on whether the second argument regex matches the first argument string. For details refer to
4627 * YANG 1.1 RFC section 10.2.1.
4628 *
4629 * @param[in] args Array of arguments.
4630 * @param[in] arg_count Count of elements in @p args.
4631 * @param[in,out] set Context and result set at the same time.
4632 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004633 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 */
4635static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004636xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004637{
4638 struct lysc_pattern **patterns = NULL, **pattern;
4639 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 LY_ERR rc = LY_SUCCESS;
4641 struct ly_err_item *err;
4642
4643 if (options & LYXP_SCNODE_ALL) {
4644 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4645 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4646 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 } else if (!warn_is_string_type(sleaf->type)) {
4648 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004649 }
4650 }
4651
4652 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4653 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4654 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004655 } else if (!warn_is_string_type(sleaf->type)) {
4656 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657 }
4658 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004659 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 return rc;
4661 }
4662
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004663 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004664 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004665 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 LY_CHECK_RET(rc);
4667
4668 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004669 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004670 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004671 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004672 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004673 if (rc != LY_SUCCESS) {
4674 LY_ARRAY_FREE(patterns);
4675 return rc;
4676 }
4677
Radek Krejci0b013302021-03-29 15:22:32 +02004678 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 pcre2_code_free((*pattern)->code);
4680 free(*pattern);
4681 LY_ARRAY_FREE(patterns);
4682 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004683 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 ly_err_free(err);
4685 return rc;
4686 }
4687
4688 if (rc == LY_EVALID) {
4689 ly_err_free(err);
4690 set_fill_boolean(set, 0);
4691 } else {
4692 set_fill_boolean(set, 1);
4693 }
4694
4695 return LY_SUCCESS;
4696}
4697
4698/**
4699 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4700 * with the rounded first argument. For details refer to
4701 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4702 *
4703 * @param[in] args Array of arguments.
4704 * @param[in] arg_count Count of elements in @p args.
4705 * @param[in,out] set Context and result set at the same time.
4706 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004707 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 */
4709static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004710xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004711{
4712 struct lysc_node_leaf *sleaf;
4713 LY_ERR rc = LY_SUCCESS;
4714
4715 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004716 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004718 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4719 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4720 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4721 sleaf->name);
4722 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4723 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4724 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004725 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004726 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 return rc;
4728 }
4729
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004730 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 LY_CHECK_RET(rc);
4732
4733 /* cover only the cases where floor can't be used */
4734 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4735 set_fill_number(set, -0.0f);
4736 } else {
4737 args[0]->val.num += 0.5;
4738 rc = xpath_floor(args, 1, args[0], options);
4739 LY_CHECK_RET(rc);
4740 set_fill_number(set, args[0]->val.num);
4741 }
4742
4743 return LY_SUCCESS;
4744}
4745
4746/**
4747 * @brief Execute the XPath starts-with(string, string) function.
4748 * Returns LYXP_SET_BOOLEAN whether the second argument is
4749 * the prefix of the first or not.
4750 *
4751 * @param[in] args Array of arguments.
4752 * @param[in] arg_count Count of elements in @p args.
4753 * @param[in,out] set Context and result set at the same time.
4754 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004755 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756 */
4757static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004758xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004759{
4760 struct lysc_node_leaf *sleaf;
4761 LY_ERR rc = LY_SUCCESS;
4762
4763 if (options & LYXP_SCNODE_ALL) {
4764 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4765 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4766 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004767 } else if (!warn_is_string_type(sleaf->type)) {
4768 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 }
4770 }
4771
4772 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4773 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4774 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004775 } else if (!warn_is_string_type(sleaf->type)) {
4776 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 }
4778 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004779 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 return rc;
4781 }
4782
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004783 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004784 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004785 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 LY_CHECK_RET(rc);
4787
4788 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4789 set_fill_boolean(set, 0);
4790 } else {
4791 set_fill_boolean(set, 1);
4792 }
4793
4794 return LY_SUCCESS;
4795}
4796
4797/**
4798 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4799 * with the string representation of either the argument or the context.
4800 *
4801 * @param[in] args Array of arguments.
4802 * @param[in] arg_count Count of elements in @p args.
4803 * @param[in,out] set Context and result set at the same time.
4804 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004805 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004806 */
4807static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004808xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809{
4810 LY_ERR rc;
4811
4812 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004813 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 return LY_SUCCESS;
4815 }
4816
4817 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004818 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004819 LY_CHECK_RET(rc);
4820 set_fill_set(set, args[0]);
4821 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004822 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823 LY_CHECK_RET(rc);
4824 }
4825
4826 return LY_SUCCESS;
4827}
4828
4829/**
4830 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4831 * with the length of the string in either the argument or the context.
4832 *
4833 * @param[in] args Array of arguments.
4834 * @param[in] arg_count Count of elements in @p args.
4835 * @param[in,out] set Context and result set at the same time.
4836 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004837 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 */
4839static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004840xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841{
4842 struct lysc_node_leaf *sleaf;
4843 LY_ERR rc = LY_SUCCESS;
4844
4845 if (options & LYXP_SCNODE_ALL) {
4846 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4847 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4848 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004849 } else if (!warn_is_string_type(sleaf->type)) {
4850 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 }
4852 }
4853 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4854 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4855 LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004856 } else if (!warn_is_string_type(sleaf->type)) {
4857 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 }
4859 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004860 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 return rc;
4862 }
4863
4864 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004865 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004866 LY_CHECK_RET(rc);
4867 set_fill_number(set, strlen(args[0]->val.str));
4868 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004869 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 LY_CHECK_RET(rc);
4871 set_fill_number(set, strlen(set->val.str));
4872 }
4873
4874 return LY_SUCCESS;
4875}
4876
4877/**
4878 * @brief Execute the XPath substring(string, number, number?) function.
4879 * Returns LYXP_SET_STRING substring of the first argument starting
4880 * on the second argument index ending on the third argument index,
4881 * indexed from 1. For exact definition refer to
4882 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4883 *
4884 * @param[in] args Array of arguments.
4885 * @param[in] arg_count Count of elements in @p args.
4886 * @param[in,out] set Context and result set at the same time.
4887 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004888 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004889 */
4890static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004891xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004893 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004894 uint16_t str_start, str_len, pos;
4895 struct lysc_node_leaf *sleaf;
4896 LY_ERR rc = LY_SUCCESS;
4897
4898 if (options & LYXP_SCNODE_ALL) {
4899 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4900 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4901 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004902 } else if (!warn_is_string_type(sleaf->type)) {
4903 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 }
4905 }
4906
4907 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4908 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4909 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004910 } else if (!warn_is_numeric_type(sleaf->type)) {
4911 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 }
4913 }
4914
Michal Vasko69730152020-10-09 16:30:07 +02004915 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4916 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004917 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4918 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004919 } else if (!warn_is_numeric_type(sleaf->type)) {
4920 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 }
4922 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004923 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004924 return rc;
4925 }
4926
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004927 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 LY_CHECK_RET(rc);
4929
4930 /* start */
4931 if (xpath_round(&args[1], 1, args[1], options)) {
4932 return -1;
4933 }
4934 if (isfinite(args[1]->val.num)) {
4935 start = args[1]->val.num - 1;
4936 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004937 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004939 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 }
4941
4942 /* len */
4943 if (arg_count == 3) {
4944 rc = xpath_round(&args[2], 1, args[2], options);
4945 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004946 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004948 } else if (isfinite(args[2]->val.num)) {
4949 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004951 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004952 }
4953 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004954 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004955 }
4956
4957 /* find matching character positions */
4958 str_start = 0;
4959 str_len = 0;
4960 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4961 if (pos < start) {
4962 ++str_start;
4963 } else if (pos < start + len) {
4964 ++str_len;
4965 } else {
4966 break;
4967 }
4968 }
4969
4970 set_fill_string(set, args[0]->val.str + str_start, str_len);
4971 return LY_SUCCESS;
4972}
4973
4974/**
4975 * @brief Execute the XPath substring-after(string, string) function.
4976 * Returns LYXP_SET_STRING with the string succeeding the occurance
4977 * of the second argument in the first or an empty string.
4978 *
4979 * @param[in] args Array of arguments.
4980 * @param[in] arg_count Count of elements in @p args.
4981 * @param[in,out] set Context and result set at the same time.
4982 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004983 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 */
4985static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004986xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987{
4988 char *ptr;
4989 struct lysc_node_leaf *sleaf;
4990 LY_ERR rc = LY_SUCCESS;
4991
4992 if (options & LYXP_SCNODE_ALL) {
4993 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4994 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4995 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004996 } else if (!warn_is_string_type(sleaf->type)) {
4997 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004998 }
4999 }
5000
5001 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5002 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5003 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005004 } else if (!warn_is_string_type(sleaf->type)) {
5005 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005006 }
5007 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005008 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009 return rc;
5010 }
5011
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005012 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005013 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005014 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015 LY_CHECK_RET(rc);
5016
5017 ptr = strstr(args[0]->val.str, args[1]->val.str);
5018 if (ptr) {
5019 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5020 } else {
5021 set_fill_string(set, "", 0);
5022 }
5023
5024 return LY_SUCCESS;
5025}
5026
5027/**
5028 * @brief Execute the XPath substring-before(string, string) function.
5029 * Returns LYXP_SET_STRING with the string preceding the occurance
5030 * of the second argument in the first or an empty string.
5031 *
5032 * @param[in] args Array of arguments.
5033 * @param[in] arg_count Count of elements in @p args.
5034 * @param[in,out] set Context and result set at the same time.
5035 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005036 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 */
5038static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005039xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040{
5041 char *ptr;
5042 struct lysc_node_leaf *sleaf;
5043 LY_ERR rc = LY_SUCCESS;
5044
5045 if (options & LYXP_SCNODE_ALL) {
5046 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5047 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5048 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005049 } else if (!warn_is_string_type(sleaf->type)) {
5050 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 }
5052 }
5053
5054 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5055 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5056 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005057 } else if (!warn_is_string_type(sleaf->type)) {
5058 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005059 }
5060 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005061 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 return rc;
5063 }
5064
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005065 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005067 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 LY_CHECK_RET(rc);
5069
5070 ptr = strstr(args[0]->val.str, args[1]->val.str);
5071 if (ptr) {
5072 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5073 } else {
5074 set_fill_string(set, "", 0);
5075 }
5076
5077 return LY_SUCCESS;
5078}
5079
5080/**
5081 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5082 * with the sum of all the nodes in the context.
5083 *
5084 * @param[in] args Array of arguments.
5085 * @param[in] arg_count Count of elements in @p args.
5086 * @param[in,out] set Context and result set at the same time.
5087 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005088 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 */
5090static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005091xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092{
5093 long double num;
5094 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005095 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 struct lyxp_set set_item;
5097 struct lysc_node_leaf *sleaf;
5098 LY_ERR rc = LY_SUCCESS;
5099
5100 if (options & LYXP_SCNODE_ALL) {
5101 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5102 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005103 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5105 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5106 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005107 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 } else if (!warn_is_numeric_type(sleaf->type)) {
5109 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 }
5111 }
5112 }
5113 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005114 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005115 return rc;
5116 }
5117
5118 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005119
5120 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005121 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005122 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005123 } else if (!args[0]->used) {
5124 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 }
5126
Michal Vasko5c4e5892019-11-14 12:31:38 +01005127 set_init(&set_item, set);
5128
Michal Vasko03ff5a72019-09-11 13:49:33 +02005129 set_item.type = LYXP_SET_NODE_SET;
5130 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5131 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5132
5133 set_item.used = 1;
5134 set_item.size = 1;
5135
5136 for (i = 0; i < args[0]->used; ++i) {
5137 set_item.val.nodes[0] = args[0]->val.nodes[i];
5138
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005139 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 LY_CHECK_RET(rc);
5141 num = cast_string_to_number(str);
5142 free(str);
5143 set->val.num += num;
5144 }
5145
5146 free(set_item.val.nodes);
5147
5148 return LY_SUCCESS;
5149}
5150
5151/**
5152 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5153 * with the text content of the nodes in the context.
5154 *
5155 * @param[in] args Array of arguments.
5156 * @param[in] arg_count Count of elements in @p args.
5157 * @param[in,out] set Context and result set at the same time.
5158 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005159 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005160 */
5161static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005162xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163{
5164 uint32_t i;
5165
5166 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005167 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 return LY_SUCCESS;
5169 }
5170
Michal Vasko03ff5a72019-09-11 13:49:33 +02005171 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005172 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 return LY_EVALID;
5174 }
5175
Michal Vaskod989ba02020-08-24 10:59:24 +02005176 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005177 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005178 case LYXP_NODE_NONE:
5179 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005181 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5182 set->val.nodes[i].type = LYXP_NODE_TEXT;
5183 ++i;
5184 break;
5185 }
Radek Krejci0f969882020-08-21 16:56:47 +02005186 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005187 case LYXP_NODE_ROOT:
5188 case LYXP_NODE_ROOT_CONFIG:
5189 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005190 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191 set_remove_node(set, i);
5192 break;
5193 }
5194 }
5195
5196 return LY_SUCCESS;
5197}
5198
5199/**
5200 * @brief Execute the XPath translate(string, string, string) function.
5201 * Returns LYXP_SET_STRING with the first argument with the characters
5202 * from the second argument replaced by those on the corresponding
5203 * positions in the third argument.
5204 *
5205 * @param[in] args Array of arguments.
5206 * @param[in] arg_count Count of elements in @p args.
5207 * @param[in,out] set Context and result set at the same time.
5208 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005209 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210 */
5211static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005212xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005213{
5214 uint16_t i, j, new_used;
5215 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005216 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 struct lysc_node_leaf *sleaf;
5218 LY_ERR rc = LY_SUCCESS;
5219
5220 if (options & LYXP_SCNODE_ALL) {
5221 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5222 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5223 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005224 } else if (!warn_is_string_type(sleaf->type)) {
5225 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 }
5227 }
5228
5229 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5230 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5231 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005232 } else if (!warn_is_string_type(sleaf->type)) {
5233 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 }
5235 }
5236
5237 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5238 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5239 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005240 } else if (!warn_is_string_type(sleaf->type)) {
5241 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242 }
5243 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005244 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005245 return rc;
5246 }
5247
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005248 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005250 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005252 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005253 LY_CHECK_RET(rc);
5254
5255 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5256 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5257 new_used = 0;
5258
5259 have_removed = 0;
5260 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005261 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262
5263 for (j = 0; args[1]->val.str[j]; ++j) {
5264 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5265 /* removing this char */
5266 if (j >= strlen(args[2]->val.str)) {
5267 have_removed = 1;
5268 found = 1;
5269 break;
5270 }
5271 /* replacing this char */
5272 new[new_used] = args[2]->val.str[j];
5273 ++new_used;
5274 found = 1;
5275 break;
5276 }
5277 }
5278
5279 /* copying this char */
5280 if (!found) {
5281 new[new_used] = args[0]->val.str[i];
5282 ++new_used;
5283 }
5284 }
5285
5286 if (have_removed) {
5287 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5288 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5289 }
5290 new[new_used] = '\0';
5291
Michal Vaskod3678892020-05-21 10:06:58 +02005292 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 set->type = LYXP_SET_STRING;
5294 set->val.str = new;
5295
5296 return LY_SUCCESS;
5297}
5298
5299/**
5300 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5301 * with true value.
5302 *
5303 * @param[in] args Array of arguments.
5304 * @param[in] arg_count Count of elements in @p args.
5305 * @param[in,out] set Context and result set at the same time.
5306 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005307 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 */
5309static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005310xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005311{
5312 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005313 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 return LY_SUCCESS;
5315 }
5316
5317 set_fill_boolean(set, 1);
5318 return LY_SUCCESS;
5319}
5320
5321/*
5322 * moveto functions
5323 *
5324 * They and only they actually change the context (set).
5325 */
5326
5327/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005328 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005330 * XPath @p set is expected to be a (sc)node set!
5331 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005332 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5333 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005334 * @param[in] set Set with general XPath context.
5335 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005336 * @param[out] moveto_mod Expected module of a matching node.
5337 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005339static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005340moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5341 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005343 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005344 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005345 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005346
Michal Vasko2104e9f2020-03-06 08:23:25 +01005347 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5348
Michal Vasko6346ece2019-09-24 13:12:53 +02005349 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5350 /* specific module */
5351 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005352 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005353
Michal Vasko004d3152020-06-11 19:59:22 +02005354 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005355 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005356 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005357 return LY_EVALID;
5358 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005359
Michal Vasko6346ece2019-09-24 13:12:53 +02005360 *qname += pref_len + 1;
5361 *qname_len -= pref_len + 1;
5362 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5363 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005365 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005366 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005367 case LY_VALUE_SCHEMA:
5368 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005369 /* current module */
5370 mod = set->cur_mod;
5371 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005372 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005373 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005374 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005375 /* inherit parent (context node) module */
5376 if (ctx_scnode) {
5377 mod = ctx_scnode->module;
5378 } else {
5379 mod = NULL;
5380 }
5381 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005382 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005383 /* all nodes need to be prefixed */
5384 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5385 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005386 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 }
5388
Michal Vasko6346ece2019-09-24 13:12:53 +02005389 *moveto_mod = mod;
5390 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391}
5392
5393/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 * @brief Move context @p set to the root. Handles absolute path.
5395 * Result is LYXP_SET_NODE_SET.
5396 *
5397 * @param[in,out] set Set to use.
5398 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005399 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005401static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005402moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005403{
aPiecek8b0cc152021-05-31 16:40:31 +02005404 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405
5406 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005407 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005408 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005410 set->type = LYXP_SET_NODE_SET;
5411 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005412 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005413 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005414
5415 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416}
5417
5418/**
5419 * @brief Check @p node as a part of NameTest processing.
5420 *
5421 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005422 * @param[in] ctx_node Context node.
5423 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005424 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005425 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005426 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005427 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5428 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005429 */
5430static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005431moveto_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 +01005432 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005433{
Michal Vaskodca9f122021-07-16 13:56:22 +02005434 if (!node->schema) {
5435 /* opaque node never matches */
5436 return LY_ENOT;
5437 }
5438
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005439 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5440 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005441 case LY_VALUE_SCHEMA:
5442 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005443 /* use current module */
5444 moveto_mod = set->cur_mod;
5445 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005446 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005447 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005448 /* inherit module of the context node, if any */
5449 if (ctx_node) {
5450 moveto_mod = ctx_node->schema->module;
5451 }
5452 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005453 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005454 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005455 /* not defined */
5456 LOGINT(set->ctx);
5457 return LY_EINVAL;
5458 }
5459 }
5460
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461 /* module check */
5462 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005463 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005464 }
5465
Michal Vasko5c4e5892019-11-14 12:31:38 +01005466 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005467 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005469 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5470 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005471 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472 }
5473
5474 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005475 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005476 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005477 }
5478
Michal Vaskoa1424542019-11-14 16:08:52 +01005479 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005480 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005482 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005483
5484 /* match */
5485 return LY_SUCCESS;
5486}
5487
5488/**
5489 * @brief Check @p node as a part of schema NameTest processing.
5490 *
5491 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005492 * @param[in] ctx_scnode Context node.
5493 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005494 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005495 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005496 * @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 +02005497 */
5498static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005499moveto_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 +02005500 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005501{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005502 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5503 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005504 case LY_VALUE_SCHEMA:
5505 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005506 /* use current module */
5507 moveto_mod = set->cur_mod;
5508 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005509 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005510 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005511 /* inherit module of the context node, if any */
5512 if (ctx_scnode) {
5513 moveto_mod = ctx_scnode->module;
5514 }
5515 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005516 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005517 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005518 /* not defined */
5519 LOGINT(set->ctx);
5520 return LY_EINVAL;
5521 }
5522 }
5523
Michal Vasko03ff5a72019-09-11 13:49:33 +02005524 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005525 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005526 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005527 }
5528
5529 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005530 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005531 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005532 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005533 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005534 }
5535
5536 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005537 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005538 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005539 }
5540
5541 /* match */
5542 return LY_SUCCESS;
5543}
5544
5545/**
Michal Vaskod3678892020-05-21 10:06:58 +02005546 * @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 +02005547 *
5548 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005549 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005550 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005551 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005552 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5553 */
5554static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005555moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005556{
Michal Vaskof03ed032020-03-04 13:31:44 +01005557 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005558 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005559 LY_ERR rc;
5560
aPiecek8b0cc152021-05-31 16:40:31 +02005561 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005562 return LY_SUCCESS;
5563 }
5564
5565 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005566 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005567 return LY_EVALID;
5568 }
5569
Michal Vasko03ff5a72019-09-11 13:49:33 +02005570 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005571 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572
5573 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 +01005574 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005575
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005576 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005577 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005578 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005579 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005580 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005581 ctx_node = set->val.nodes[i].node;
5582 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005583 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005584
Michal Vaskod3678892020-05-21 10:06:58 +02005585 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005586 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005587 if (rc == LY_SUCCESS) {
5588 if (!replaced) {
5589 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5590 replaced = 1;
5591 } else {
5592 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005593 }
Michal Vaskod3678892020-05-21 10:06:58 +02005594 ++i;
5595 } else if (rc == LY_EINCOMPLETE) {
5596 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005597 }
5598 }
5599
5600 if (!replaced) {
5601 /* no match */
5602 set_remove_node(set, i);
5603 }
5604 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005605
5606 return LY_SUCCESS;
5607}
5608
5609/**
Michal Vaskod3678892020-05-21 10:06:58 +02005610 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5611 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005612 *
5613 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005614 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005615 * @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 +01005616 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005617 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5618 */
5619static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005620moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5621 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005622{
Michal Vasko004d3152020-06-11 19:59:22 +02005623 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005624 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005625 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005626 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005627
Michal Vasko004d3152020-06-11 19:59:22 +02005628 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005629
aPiecek8b0cc152021-05-31 16:40:31 +02005630 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005631 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005632 }
5633
5634 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005635 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005636 ret = LY_EVALID;
5637 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005638 }
5639
5640 /* context check for all the nodes since we have the schema node */
5641 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5642 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005643 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005644 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5645 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005646 lyxp_set_free_content(set);
5647 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005648 }
5649
5650 /* create specific data instance if needed */
5651 if (scnode->nodetype == LYS_LIST) {
5652 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5653 } else if (scnode->nodetype == LYS_LEAFLIST) {
5654 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005655 }
5656
5657 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005658 siblings = NULL;
5659
5660 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5661 assert(!set->val.nodes[i].node);
5662
5663 /* search in all the trees */
5664 siblings = set->tree;
5665 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5666 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005667 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005668 }
5669
5670 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005671 if (inst) {
5672 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005673 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005674 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005675 }
5676
5677 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005678 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005679 ret = LY_EINCOMPLETE;
5680 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005681 }
5682
5683 if (sub) {
5684 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005685 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005686 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005687 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005688 /* no match */
5689 set_remove_node(set, i);
5690 }
5691 }
5692
Michal Vasko004d3152020-06-11 19:59:22 +02005693cleanup:
5694 lyd_free_tree(inst);
5695 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005696}
5697
5698/**
5699 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5700 *
5701 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005702 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005703 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704 * @param[in] options XPath options.
5705 * @return LY_ERR
5706 */
5707static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005708moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005709{
Radek Krejci857189e2020-09-01 13:26:36 +02005710 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005711 uint32_t getnext_opts;
5712 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005714 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005715 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005716
aPiecek8b0cc152021-05-31 16:40:31 +02005717 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718 return LY_SUCCESS;
5719 }
5720
5721 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005722 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723 return LY_EVALID;
5724 }
5725
Michal Vaskocafad9d2019-11-07 15:20:03 +01005726 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005727 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005728 if (options & LYXP_SCNODE_OUTPUT) {
5729 getnext_opts |= LYS_GETNEXT_OUTPUT;
5730 }
5731
Michal Vasko03ff5a72019-09-11 13:49:33 +02005732 orig_used = set->used;
5733 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005734 uint32_t idx;
5735
Radek Krejcif13b87b2020-12-01 22:02:17 +01005736 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5737 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005738 continue;
5739 }
5740
5741 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005742 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005743 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005744 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746
5747 start_parent = set->val.scnodes[i].scnode;
5748
5749 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 +02005750 /* 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 +02005751 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005753 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005754 iter = NULL;
Michal Vasko919954a2021-07-26 09:21:42 +02005755 /* module may not be implemented or not compiled yet */
5756 while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005757 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005758 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5759
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005761 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005762 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005763 temp_ctx = 1;
5764 }
5765 }
5766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005767 }
5768
Michal Vasko519fd602020-05-26 12:17:39 +02005769 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5770 iter = NULL;
5771 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005772 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005773 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5774
5775 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005776 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005777 temp_ctx = 1;
5778 }
5779 }
5780 }
5781 }
5782 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005783
5784 /* correct temporary in_ctx values */
5785 if (temp_ctx) {
5786 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005787 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5788 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789 }
5790 }
5791 }
5792
5793 return LY_SUCCESS;
5794}
5795
5796/**
Michal Vaskod3678892020-05-21 10:06:58 +02005797 * @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 +02005798 * Context position aware.
5799 *
5800 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005801 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005802 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005803 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005804 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5805 */
5806static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005807moveto_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 +02005808{
5809 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005810 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 struct lyxp_set ret_set;
5812 LY_ERR rc;
5813
aPiecek8b0cc152021-05-31 16:40:31 +02005814 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 return LY_SUCCESS;
5816 }
5817
5818 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005819 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005820 return LY_EVALID;
5821 }
5822
Michal Vasko9f96a052020-03-10 09:41:45 +01005823 /* 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 +01005824 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005825 LY_CHECK_RET(rc);
5826
Michal Vasko6346ece2019-09-24 13:12:53 +02005827 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005828 set_init(&ret_set, set);
5829 for (i = 0; i < set->used; ++i) {
5830
5831 /* TREE DFS */
5832 start = set->val.nodes[i].node;
5833 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005834 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005835 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005836 /* add matching node into result set */
5837 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5838 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5839 /* the node is a duplicate, we'll process it later in the set */
5840 goto skip_children;
5841 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005842 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005843 return rc;
5844 } else if (rc == LY_EINVAL) {
5845 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846 }
5847
5848 /* TREE DFS NEXT ELEM */
5849 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005850 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005851 if (!next) {
5852skip_children:
5853 /* no children, so try siblings, but only if it's not the start,
5854 * that is considered to be the root and it's siblings are not traversed */
5855 if (elem != start) {
5856 next = elem->next;
5857 } else {
5858 break;
5859 }
5860 }
5861 while (!next) {
5862 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005863 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 /* we are done, no next element to process */
5865 break;
5866 }
5867 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005868 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005869 next = elem->next;
5870 }
5871 }
5872 }
5873
5874 /* make the temporary set the current one */
5875 ret_set.ctx_pos = set->ctx_pos;
5876 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005877 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005878 memcpy(set, &ret_set, sizeof *set);
5879
5880 return LY_SUCCESS;
5881}
5882
5883/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005884 * @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 +02005885 *
5886 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005887 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005888 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005889 * @param[in] options XPath options.
5890 * @return LY_ERR
5891 */
5892static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005893moveto_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 +02005894{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005895 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005897 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005898
aPiecek8b0cc152021-05-31 16:40:31 +02005899 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005900 return LY_SUCCESS;
5901 }
5902
5903 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005904 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005905 return LY_EVALID;
5906 }
5907
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 orig_used = set->used;
5909 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005910 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5911 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005912 continue;
5913 }
5914
5915 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005916 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005917 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005918 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005920
5921 /* TREE DFS */
5922 start = set->val.scnodes[i].scnode;
5923 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005924 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5925 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005926 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927 }
5928
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005929 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005930 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005931 uint32_t idx;
5932
5933 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005934 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005935 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936 /* we will process it later in the set */
5937 goto skip_children;
5938 }
5939 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005940 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005941 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005942 } else if (rc == LY_EINVAL) {
5943 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 }
5945
5946next_iter:
5947 /* TREE DFS NEXT ELEM */
5948 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005949 next = lysc_node_child(elem);
5950 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5951 next = next->next;
5952 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5953 next = next->next;
5954 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005955 if (!next) {
5956skip_children:
5957 /* no children, so try siblings, but only if it's not the start,
5958 * that is considered to be the root and it's siblings are not traversed */
5959 if (elem != start) {
5960 next = elem->next;
5961 } else {
5962 break;
5963 }
5964 }
5965 while (!next) {
5966 /* no siblings, go back through the parents */
5967 if (elem->parent == start) {
5968 /* we are done, no next element to process */
5969 break;
5970 }
5971 /* parent is already processed, go to its sibling */
5972 elem = elem->parent;
5973 next = elem->next;
5974 }
5975 }
5976 }
5977
5978 return LY_SUCCESS;
5979}
5980
5981/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005982 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983 * Indirectly context position aware.
5984 *
5985 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005986 * @param[in] mod Matching metadata module, NULL for any.
5987 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005988 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989 * @return LY_ERR
5990 */
5991static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005992moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005993{
Michal Vasko9f96a052020-03-10 09:41:45 +01005994 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005995
aPiecek8b0cc152021-05-31 16:40:31 +02005996 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997 return LY_SUCCESS;
5998 }
5999
6000 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006001 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002 return LY_EVALID;
6003 }
6004
Radek Krejci1deb5be2020-08-26 16:43:36 +02006005 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006006 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007
6008 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6009 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006010 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006011 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006012
6013 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006014 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006015 continue;
6016 }
6017
Michal Vaskod3678892020-05-21 10:06:58 +02006018 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 /* match */
6020 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006021 set->val.meta[i].meta = sub;
6022 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006023 /* pos does not change */
6024 replaced = 1;
6025 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006026 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 +02006027 }
6028 ++i;
6029 }
6030 }
6031 }
6032
6033 if (!replaced) {
6034 /* no match */
6035 set_remove_node(set, i);
6036 }
6037 }
6038
6039 return LY_SUCCESS;
6040}
6041
6042/**
6043 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006044 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 *
6046 * @param[in,out] set1 Set to use for the result.
6047 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 * @return LY_ERR
6049 */
6050static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006051moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052{
6053 LY_ERR rc;
6054
Michal Vaskod3678892020-05-21 10:06:58 +02006055 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006056 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057 return LY_EVALID;
6058 }
6059
6060 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006061 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006062 return LY_SUCCESS;
6063 }
6064
Michal Vaskod3678892020-05-21 10:06:58 +02006065 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066 memcpy(set1, set2, sizeof *set1);
6067 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006068 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069 return LY_SUCCESS;
6070 }
6071
6072 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006073 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006074
6075 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006076 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006077 LY_CHECK_RET(rc);
6078
6079 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006080 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006081
6082 return LY_SUCCESS;
6083}
6084
6085/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006086 * @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 +02006087 * Context position aware.
6088 *
6089 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006090 * @param[in] mod Matching metadata module, NULL for any.
6091 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006092 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6094 */
6095static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006096moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097{
Michal Vasko9f96a052020-03-10 09:41:45 +01006098 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099 struct lyxp_set *set_all_desc = NULL;
6100 LY_ERR rc;
6101
aPiecek8b0cc152021-05-31 16:40:31 +02006102 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006103 return LY_SUCCESS;
6104 }
6105
6106 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006107 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108 return LY_EVALID;
6109 }
6110
Michal Vasko03ff5a72019-09-11 13:49:33 +02006111 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6112 * but it likely won't be used much, so it's a waste of time */
6113 /* copy the context */
6114 set_all_desc = set_copy(set);
6115 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006116 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006117 if (rc != LY_SUCCESS) {
6118 lyxp_set_free(set_all_desc);
6119 return rc;
6120 }
6121 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006122 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006123 if (rc != LY_SUCCESS) {
6124 lyxp_set_free(set_all_desc);
6125 return rc;
6126 }
6127 lyxp_set_free(set_all_desc);
6128
Radek Krejci1deb5be2020-08-26 16:43:36 +02006129 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006130 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006131
6132 /* only attributes of an elem can be in the result, skip all the rest,
6133 * we have all attributes qualified in lyd tree */
6134 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006135 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006136 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006137 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 continue;
6139 }
6140
Michal Vaskod3678892020-05-21 10:06:58 +02006141 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006142 /* match */
6143 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006144 set->val.meta[i].meta = sub;
6145 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006146 /* pos does not change */
6147 replaced = 1;
6148 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006149 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 +02006150 }
6151 ++i;
6152 }
6153 }
6154 }
6155
6156 if (!replaced) {
6157 /* no match */
6158 set_remove_node(set, i);
6159 }
6160 }
6161
6162 return LY_SUCCESS;
6163}
6164
6165/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006166 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6167 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006168 *
6169 * @param[in] parent Current parent.
6170 * @param[in] parent_pos Position of @p parent.
6171 * @param[in] parent_type Node type of @p parent.
6172 * @param[in,out] to_set Set to use.
6173 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 * @param[in] options XPath options.
6175 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6176 */
6177static LY_ERR
6178moveto_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 +02006179 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006180{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006181 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006182 LY_ERR rc;
6183
6184 switch (parent_type) {
6185 case LYXP_NODE_ROOT:
6186 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006187 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006188
Michal Vasko61ac2f62020-05-25 12:39:51 +02006189 /* add all top-level nodes as elements */
6190 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006191 break;
6192 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006193 /* add just the text node of this term element node */
6194 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006195 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6196 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6197 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006198 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006199 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006200
6201 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006202 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006203 break;
6204 default:
6205 LOGINT_RET(parent->schema->module->ctx);
6206 }
6207
Michal Vasko61ac2f62020-05-25 12:39:51 +02006208 /* add all top-level nodes as elements */
6209 LY_LIST_FOR(first, iter) {
6210 /* context check */
6211 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6212 continue;
6213 }
6214
6215 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006216 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006217 return LY_EINCOMPLETE;
6218 }
6219
6220 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6221 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6222
6223 /* also add all the children of this node, recursively */
6224 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6225 LY_CHECK_RET(rc);
6226 }
6227 }
6228
Michal Vasko03ff5a72019-09-11 13:49:33 +02006229 return LY_SUCCESS;
6230}
6231
6232/**
6233 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6234 * (or LYXP_SET_EMPTY). Context position aware.
6235 *
6236 * @param[in,out] set Set to use.
6237 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6238 * @param[in] options XPath options.
6239 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6240 */
6241static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006242moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006243{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244 struct lyxp_set ret_set;
6245 LY_ERR rc;
6246
aPiecek8b0cc152021-05-31 16:40:31 +02006247 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006248 return LY_SUCCESS;
6249 }
6250
6251 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006252 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006253 return LY_EVALID;
6254 }
6255
6256 /* nothing to do */
6257 if (!all_desc) {
6258 return LY_SUCCESS;
6259 }
6260
Michal Vasko03ff5a72019-09-11 13:49:33 +02006261 /* add all the children, they get added recursively */
6262 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006263 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006264 /* copy the current node to tmp */
6265 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6266
6267 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006268 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006269 continue;
6270 }
6271
Michal Vasko03ff5a72019-09-11 13:49:33 +02006272 /* add all the children */
6273 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 +02006274 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006275 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006276 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277 return rc;
6278 }
6279 }
6280
6281 /* use the temporary set as the current one */
6282 ret_set.ctx_pos = set->ctx_pos;
6283 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006284 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006285 memcpy(set, &ret_set, sizeof *set);
6286
6287 return LY_SUCCESS;
6288}
6289
6290/**
6291 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6292 * (or LYXP_SET_EMPTY).
6293 *
6294 * @param[in,out] set Set to use.
6295 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6296 * @param[in] options XPath options.
6297 * @return LY_ERR
6298 */
6299static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006300moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006301{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006302 uint32_t getnext_opts;
6303 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006304 const struct lysc_node *iter, *start_parent;
6305 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006306
aPiecek8b0cc152021-05-31 16:40:31 +02006307 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006308 return LY_SUCCESS;
6309 }
6310
6311 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006312 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313 return LY_EVALID;
6314 }
6315
Michal Vasko519fd602020-05-26 12:17:39 +02006316 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006317 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006318 if (options & LYXP_SCNODE_OUTPUT) {
6319 getnext_opts |= LYS_GETNEXT_OUTPUT;
6320 }
6321
6322 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006323 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006324 if (!all_desc) {
6325 /* traverse the start node */
6326 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6327 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6328 }
6329 continue;
6330 }
6331
Radek Krejcif13b87b2020-12-01 22:02:17 +01006332 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6333 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006334 continue;
6335 }
6336
Michal Vasko519fd602020-05-26 12:17:39 +02006337 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006338 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006339 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006340 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341 }
6342
Michal Vasko519fd602020-05-26 12:17:39 +02006343 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006344
Michal Vasko519fd602020-05-26 12:17:39 +02006345 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6346 /* it can actually be in any module, it's all <running> */
6347 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006348 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006349 iter = NULL;
6350 /* module may not be implemented */
6351 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6352 /* context check */
6353 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6354 continue;
6355 }
6356
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006357 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006358 /* throw away the insert index, we want to consider that node again, recursively */
6359 }
6360 }
6361
6362 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6363 iter = NULL;
6364 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006366 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006367 continue;
6368 }
6369
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006370 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006371 }
6372 }
6373 }
6374
6375 return LY_SUCCESS;
6376}
6377
6378/**
6379 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6380 * (or LYXP_SET_EMPTY). Context position aware.
6381 *
6382 * @param[in] set Set to use.
6383 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6384 * @param[in] options XPath options.
6385 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6386 */
6387static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006388moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389{
6390 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006391 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006392 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393
aPiecek8b0cc152021-05-31 16:40:31 +02006394 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395 return LY_SUCCESS;
6396 }
6397
6398 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006399 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006400 return LY_EVALID;
6401 }
6402
6403 if (all_desc) {
6404 /* <path>//.. == <path>//./.. */
6405 rc = moveto_self(set, 1, options);
6406 LY_CHECK_RET(rc);
6407 }
6408
Radek Krejci1deb5be2020-08-26 16:43:36 +02006409 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006410 node = set->val.nodes[i].node;
6411
6412 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006413 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006414 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6415 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006416 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6417 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006418 if (!new_node) {
6419 LOGINT_RET(set->ctx);
6420 }
6421 } else {
6422 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006423 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424 continue;
6425 }
6426
Michal Vaskoa1424542019-11-14 16:08:52 +01006427 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006428 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 +02006429 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006430 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006432 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006433 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006434 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006435
Michal Vasko03ff5a72019-09-11 13:49:33 +02006436 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006437 /* 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 +02006438 new_type = LYXP_NODE_ELEM;
6439 }
6440
Michal Vasko03ff5a72019-09-11 13:49:33 +02006441 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006442 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006443 } else {
6444 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006445 }
6446 }
6447
Michal Vasko2caefc12019-11-14 16:07:56 +01006448 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006449 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006450
6451 return LY_SUCCESS;
6452}
6453
6454/**
6455 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6456 * (or LYXP_SET_EMPTY).
6457 *
6458 * @param[in] set Set to use.
6459 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6460 * @param[in] options XPath options.
6461 * @return LY_ERR
6462 */
6463static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006464moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006465{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006466 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006467 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006468 const struct lysc_node *node, *new_node;
6469 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006470
aPiecek8b0cc152021-05-31 16:40:31 +02006471 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472 return LY_SUCCESS;
6473 }
6474
6475 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006476 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006477 return LY_EVALID;
6478 }
6479
6480 if (all_desc) {
6481 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006482 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006483 }
6484
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485 orig_used = set->used;
6486 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006487 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6488 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006489 continue;
6490 }
6491
6492 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006493 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006494 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006495 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006496 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497
6498 node = set->val.scnodes[i].scnode;
6499
6500 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006501 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006502 } else {
6503 /* root does not have a parent */
6504 continue;
6505 }
6506
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006507 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006508 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006509 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006510
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006512 /* 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 +02006513 new_type = LYXP_NODE_ELEM;
6514 }
6515
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006516 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6517 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006518 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006519 temp_ctx = 1;
6520 }
6521 }
6522
6523 if (temp_ctx) {
6524 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006525 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6526 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006527 }
6528 }
6529 }
6530
6531 return LY_SUCCESS;
6532}
6533
6534/**
6535 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6536 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6537 *
6538 * @param[in,out] set1 Set to use for the result.
6539 * @param[in] set2 Set acting as the second operand for @p op.
6540 * @param[in] op Comparison operator to process.
6541 * @param[in] options XPath options.
6542 * @return LY_ERR
6543 */
6544static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006545moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006546{
6547 /*
6548 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6549 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6550 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6551 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6552 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6553 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6554 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6555 *
6556 * '=' or '!='
6557 * BOOLEAN + BOOLEAN
6558 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6559 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6560 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6561 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6562 * NUMBER + NUMBER
6563 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6564 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6565 * STRING + STRING
6566 *
6567 * '<=', '<', '>=', '>'
6568 * NUMBER + NUMBER
6569 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6570 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6571 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6572 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6573 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6574 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6575 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6576 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6577 */
6578 struct lyxp_set iter1, iter2;
6579 int result;
6580 int64_t i;
6581 LY_ERR rc;
6582
Michal Vasko004d3152020-06-11 19:59:22 +02006583 memset(&iter1, 0, sizeof iter1);
6584 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006585
6586 /* iterative evaluation with node-sets */
6587 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6588 if (set1->type == LYXP_SET_NODE_SET) {
6589 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006590 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006591 switch (set2->type) {
6592 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006593 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594 break;
6595 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006596 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006597 break;
6598 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006599 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006600 break;
6601 }
6602 LY_CHECK_RET(rc);
6603
Michal Vasko4c7763f2020-07-27 17:40:37 +02006604 /* canonize set2 */
6605 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6606
6607 /* compare recursively */
6608 rc = moveto_op_comp(&iter1, &iter2, op, options);
6609 lyxp_set_free_content(&iter2);
6610 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006611
6612 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006613 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006614 set_fill_boolean(set1, 1);
6615 return LY_SUCCESS;
6616 }
6617 }
6618 } else {
6619 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006620 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006621 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006622 case LYXP_SET_NUMBER:
6623 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6624 break;
6625 case LYXP_SET_BOOLEAN:
6626 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6627 break;
6628 default:
6629 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6630 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006631 }
6632 LY_CHECK_RET(rc);
6633
Michal Vasko4c7763f2020-07-27 17:40:37 +02006634 /* canonize set1 */
6635 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 +02006636
Michal Vasko4c7763f2020-07-27 17:40:37 +02006637 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006639 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006640 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006641
6642 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006643 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006644 set_fill_boolean(set1, 1);
6645 return LY_SUCCESS;
6646 }
6647 }
6648 }
6649
6650 /* false for all nodes */
6651 set_fill_boolean(set1, 0);
6652 return LY_SUCCESS;
6653 }
6654
6655 /* first convert properly */
6656 if ((op[0] == '=') || (op[0] == '!')) {
6657 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006658 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6659 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006661 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006662 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006663 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006664 LY_CHECK_RET(rc);
6665 } /* else we have 2 strings */
6666 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006667 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006668 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006669 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006670 LY_CHECK_RET(rc);
6671 }
6672
6673 assert(set1->type == set2->type);
6674
6675 /* compute result */
6676 if (op[0] == '=') {
6677 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006678 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006679 } else if (set1->type == LYXP_SET_NUMBER) {
6680 result = (set1->val.num == set2->val.num);
6681 } else {
6682 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006683 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006684 }
6685 } else if (op[0] == '!') {
6686 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006687 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006688 } else if (set1->type == LYXP_SET_NUMBER) {
6689 result = (set1->val.num != set2->val.num);
6690 } else {
6691 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006692 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006693 }
6694 } else {
6695 assert(set1->type == LYXP_SET_NUMBER);
6696 if (op[0] == '<') {
6697 if (op[1] == '=') {
6698 result = (set1->val.num <= set2->val.num);
6699 } else {
6700 result = (set1->val.num < set2->val.num);
6701 }
6702 } else {
6703 if (op[1] == '=') {
6704 result = (set1->val.num >= set2->val.num);
6705 } else {
6706 result = (set1->val.num > set2->val.num);
6707 }
6708 }
6709 }
6710
6711 /* assign result */
6712 if (result) {
6713 set_fill_boolean(set1, 1);
6714 } else {
6715 set_fill_boolean(set1, 0);
6716 }
6717
6718 return LY_SUCCESS;
6719}
6720
6721/**
6722 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6723 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6724 *
6725 * @param[in,out] set1 Set to use for the result.
6726 * @param[in] set2 Set acting as the second operand for @p op.
6727 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006728 * @return LY_ERR
6729 */
6730static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006731moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006732{
6733 LY_ERR rc;
6734
6735 /* unary '-' */
6736 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006737 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006738 LY_CHECK_RET(rc);
6739 set1->val.num *= -1;
6740 lyxp_set_free(set2);
6741 return LY_SUCCESS;
6742 }
6743
6744 assert(set1 && set2);
6745
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006746 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006748 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006749 LY_CHECK_RET(rc);
6750
6751 switch (op[0]) {
6752 /* '+' */
6753 case '+':
6754 set1->val.num += set2->val.num;
6755 break;
6756
6757 /* '-' */
6758 case '-':
6759 set1->val.num -= set2->val.num;
6760 break;
6761
6762 /* '*' */
6763 case '*':
6764 set1->val.num *= set2->val.num;
6765 break;
6766
6767 /* 'div' */
6768 case 'd':
6769 set1->val.num /= set2->val.num;
6770 break;
6771
6772 /* 'mod' */
6773 case 'm':
6774 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6775 break;
6776
6777 default:
6778 LOGINT_RET(set1->ctx);
6779 }
6780
6781 return LY_SUCCESS;
6782}
6783
6784/*
6785 * eval functions
6786 *
6787 * They execute a parsed XPath expression on some data subtree.
6788 */
6789
6790/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791 * @brief Evaluate Predicate. Logs directly on error.
6792 *
Michal Vaskod3678892020-05-21 10:06:58 +02006793 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006794 *
6795 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006796 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006797 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798 * @param[in] options XPath options.
6799 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6800 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6801 */
6802static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006803eval_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 +02006804{
6805 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006806 uint16_t orig_exp;
6807 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006808 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006809 struct lyxp_set set2;
6810 struct lyd_node *orig_parent;
6811
6812 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006813 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006814 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006815 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006816
aPiecek8b0cc152021-05-31 16:40:31 +02006817 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006819 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006820 LY_CHECK_RET(rc);
6821 } else if (set->type == LYXP_SET_NODE_SET) {
6822 /* 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 +01006823 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006824
6825 /* empty set, nothing to evaluate */
6826 if (!set->used) {
6827 goto only_parse;
6828 }
6829
Michal Vasko004d3152020-06-11 19:59:22 +02006830 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006831 orig_pos = 0;
6832 orig_size = set->used;
6833 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006834 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006835 set_init(&set2, set);
6836 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6837 /* remember the node context position for position() and context size for last(),
6838 * predicates should always be evaluated with respect to the child axis (since we do
6839 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006840 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6841 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006842 orig_pos = 1;
6843 } else {
6844 ++orig_pos;
6845 }
6846
6847 set2.ctx_pos = orig_pos;
6848 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006849 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006850
Michal Vasko004d3152020-06-11 19:59:22 +02006851 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006852 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006853 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006854 return rc;
6855 }
6856
6857 /* number is a position */
6858 if (set2.type == LYXP_SET_NUMBER) {
6859 if ((long long)set2.val.num == orig_pos) {
6860 set2.val.num = 1;
6861 } else {
6862 set2.val.num = 0;
6863 }
6864 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006865 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006866
6867 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006868 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006869 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870 }
6871 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006872 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006873
6874 } else if (set->type == LYXP_SET_SCNODE_SET) {
6875 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006876 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006877 /* there is a currently-valid node */
6878 break;
6879 }
6880 }
6881 /* empty set, nothing to evaluate */
6882 if (i == set->used) {
6883 goto only_parse;
6884 }
6885
Michal Vasko004d3152020-06-11 19:59:22 +02006886 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006887
Michal Vasko03ff5a72019-09-11 13:49:33 +02006888 /* set special in_ctx to all the valid snodes */
6889 pred_in_ctx = set_scnode_new_in_ctx(set);
6890
6891 /* use the valid snodes one-by-one */
6892 for (i = 0; i < set->used; ++i) {
6893 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6894 continue;
6895 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006896 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006897
Michal Vasko004d3152020-06-11 19:59:22 +02006898 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006899
Michal Vasko004d3152020-06-11 19:59:22 +02006900 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006901 LY_CHECK_RET(rc);
6902
6903 set->val.scnodes[i].in_ctx = pred_in_ctx;
6904 }
6905
6906 /* restore the state as it was before the predicate */
6907 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006908 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006909 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006910 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006911 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006912 }
6913 }
6914
6915 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006916 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006917 set_fill_set(&set2, set);
6918
Michal Vasko004d3152020-06-11 19:59:22 +02006919 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006920 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006921 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006922 return rc;
6923 }
6924
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006925 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006926 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006927 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006928 }
Michal Vaskod3678892020-05-21 10:06:58 +02006929 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006930 }
6931
6932 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006933 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006934 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006935 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006936 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006937
6938 return LY_SUCCESS;
6939}
6940
6941/**
Michal Vaskod3678892020-05-21 10:06:58 +02006942 * @brief Evaluate Literal. Logs directly on error.
6943 *
6944 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006945 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006946 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6947 */
6948static void
Michal Vasko40308e72020-10-20 16:38:40 +02006949eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006950{
6951 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006952 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006953 set_fill_string(set, "", 0);
6954 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006955 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 +02006956 }
6957 }
6958 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006959 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006960 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006961}
6962
6963/**
Michal Vasko004d3152020-06-11 19:59:22 +02006964 * @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 +02006965 *
Michal Vasko004d3152020-06-11 19:59:22 +02006966 * @param[in] exp Full parsed XPath expression.
6967 * @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 +02006968 * @param[in] ctx_node Found schema node as the context for the predicate.
6969 * @param[in] cur_mod Current module for the expression.
6970 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006971 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006972 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006973 * @param[out] predicates Parsed predicates.
6974 * @param[out] pred_type Type of @p predicates.
6975 * @return LY_SUCCESS on success,
6976 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006977 */
6978static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006979eval_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 +02006980 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 +02006981 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006982{
Michal Vasko004d3152020-06-11 19:59:22 +02006983 LY_ERR ret = LY_SUCCESS;
6984 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006985 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006986 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006987 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006988 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006989
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006990 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006991
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006992 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006993 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006994 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006995 return LY_EINVAL;
6996 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006997 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 +02006998 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006999
Michal Vasko004d3152020-06-11 19:59:22 +02007000 /* learn where the predicates end */
7001 e_idx = *tok_idx;
7002 while (key_count) {
7003 /* '[' */
7004 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7005 return LY_EINVAL;
7006 }
7007 ++e_idx;
7008
Michal Vasko3354d272021-04-06 09:40:06 +02007009 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
7010 /* definitely not a key */
7011 return LY_EINVAL;
7012 }
7013
Michal Vasko004d3152020-06-11 19:59:22 +02007014 /* ']' */
7015 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7016 ++e_idx;
7017 }
7018 ++e_idx;
7019
7020 /* another presumably key predicate parsed */
7021 --key_count;
7022 }
Michal Vasko004d3152020-06-11 19:59:22 +02007023 } else {
7024 /* learn just where this single predicate ends */
7025 e_idx = *tok_idx;
7026
Michal Vaskod3678892020-05-21 10:06:58 +02007027 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007028 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7029 return LY_EINVAL;
7030 }
7031 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007032
Michal Vasko3354d272021-04-06 09:40:06 +02007033 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7034 /* definitely not the value */
7035 return LY_EINVAL;
7036 }
7037
Michal Vaskod3678892020-05-21 10:06:58 +02007038 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007039 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7040 ++e_idx;
7041 }
7042 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007043 }
7044
Michal Vasko004d3152020-06-11 19:59:22 +02007045 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7046 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7047
7048 /* turn logging off */
7049 prev_lo = ly_log_options(0);
7050
7051 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007052 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7053 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007054
7055 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007056 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7057 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007058 LY_CHECK_GOTO(ret, cleanup);
7059
7060 /* success, the predicate must include all the needed information for hash-based search */
7061 *tok_idx = e_idx;
7062
7063cleanup:
7064 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007065 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007066 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007067}
7068
7069/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007070 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7071 * data node(s) could be found using a single hash-based search.
7072 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007073 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007074 * @param[in] node Next context node to check.
7075 * @param[in] name Expected node name.
7076 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007077 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007078 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007079 * @param[in] format Prefix format.
7080 * @param[in,out] found Previously found node, is updated.
7081 * @return LY_SUCCESS on success,
7082 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7083 */
7084static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007085eval_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 +02007086 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 +02007087 const struct lysc_node **found)
7088{
7089 const struct lysc_node *scnode;
7090 const struct lys_module *mod;
7091 uint32_t idx = 0;
7092
Radek Krejci8df109d2021-04-23 12:19:08 +02007093 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007094
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007095continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007096 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007097 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007098 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007099 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007100 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007101 if (!mod->implemented) {
7102 continue;
7103 }
7104
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007105 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7106 if (scnode) {
7107 /* we have found a match */
7108 break;
7109 }
7110 }
7111
Michal Vasko7d1d0912020-10-16 08:38:30 +02007112 if (!scnode) {
7113 /* all modules searched */
7114 idx = 0;
7115 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007116 } else {
7117 /* search in top-level */
7118 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7119 }
7120 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007121 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007122 /* we must adjust the module to inherit the one from the context node */
7123 moveto_mod = node->schema->module;
7124 }
7125
7126 /* search in children, do not repeat the same search */
7127 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007128 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007129
7130 /* additional context check */
7131 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7132 scnode = NULL;
7133 }
7134
7135 if (scnode) {
7136 if (*found) {
7137 /* we found a schema node with the same name but at different level, give up, too complicated
7138 * (more hash-based searches would be required, not supported) */
7139 return LY_ENOT;
7140 } else {
7141 /* remember the found schema node and continue to make sure it can be used */
7142 *found = scnode;
7143 }
7144 }
7145
7146 if (idx) {
7147 /* continue searching all the following models */
7148 goto continue_search;
7149 }
7150
7151 return LY_SUCCESS;
7152}
7153
7154/**
Michal Vaskod3678892020-05-21 10:06:58 +02007155 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7156 *
7157 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7158 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7159 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7160 *
7161 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007162 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007163 * @param[in] attr_axis Whether to search attributes or standard nodes.
7164 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007165 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007166 * @param[in] options XPath options.
7167 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7168 */
7169static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007170eval_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 +02007171 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007172{
Michal Vaskod3678892020-05-21 10:06:58 +02007173 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007174 const char *ncname, *ncname_dict = NULL;
7175 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007176 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007177 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007178 struct ly_path_predicate *predicates = NULL;
7179 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007180 LY_ERR rc = LY_SUCCESS;
7181
aPiecek8b0cc152021-05-31 16:40:31 +02007182 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007183 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007184 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007185
aPiecek8b0cc152021-05-31 16:40:31 +02007186 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007187 goto moveto;
7188 }
7189
Michal Vasko004d3152020-06-11 19:59:22 +02007190 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7191 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007192
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007193 if ((ncname[0] == '*') && (ncname_len == 1)) {
7194 /* all nodes will match */
7195 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007196 }
7197
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007198 /* parse (and skip) module name */
7199 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007200 LY_CHECK_GOTO(rc, cleanup);
7201
Radek Krejci8df109d2021-04-23 12:19:08 +02007202 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 +02007203 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007204 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007205 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7206 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007207 /* check failed */
7208 scnode = NULL;
7209 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007210 }
7211 }
7212
Michal Vasko004d3152020-06-11 19:59:22 +02007213 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7214 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007215 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7216 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007217 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007218 scnode = NULL;
7219 }
7220 }
7221 }
7222
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007223 if (!scnode) {
7224 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007225 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007226 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007227 }
7228
7229moveto:
7230 /* move to the attribute(s), data node(s), or schema node(s) */
7231 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007232 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007233 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007234 } else {
7235 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007236 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007237 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007238 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007239 }
7240 LY_CHECK_GOTO(rc, cleanup);
7241 }
7242 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007243 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007244 int64_t i;
7245
Michal Vaskod3678892020-05-21 10:06:58 +02007246 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007247 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007248 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007249 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007250 }
7251 LY_CHECK_GOTO(rc, cleanup);
7252
7253 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007254 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007255 break;
7256 }
7257 }
7258 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007259 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007260 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7261 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007262 free(path);
7263 }
7264 } else {
7265 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007266 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007267 } else {
7268 if (scnode) {
7269 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007270 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007271 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007272 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007273 }
7274 }
7275 LY_CHECK_GOTO(rc, cleanup);
7276 }
7277 }
7278
7279 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007280 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7281 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007282 LY_CHECK_RET(rc);
7283 }
7284
7285cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007286 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007287 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007288 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007289 }
Michal Vaskod3678892020-05-21 10:06:58 +02007290 return rc;
7291}
7292
7293/**
7294 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7295 *
7296 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7297 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7298 * [8] NodeType ::= 'text' | 'node'
7299 *
7300 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007301 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007302 * @param[in] attr_axis Whether to search attributes or standard nodes.
7303 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007304 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007305 * @param[in] options XPath options.
7306 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7307 */
7308static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007309eval_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 +02007310 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007311{
7312 LY_ERR rc;
7313
7314 /* TODO */
7315 (void)attr_axis;
7316 (void)all_desc;
7317
aPiecek8b0cc152021-05-31 16:40:31 +02007318 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007319 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007320 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007321 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007322 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007323 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007324 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007325 rc = xpath_node(NULL, 0, set, options);
7326 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007327 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007328 rc = xpath_text(NULL, 0, set, options);
7329 }
7330 LY_CHECK_RET(rc);
7331 }
7332 }
aPiecek8b0cc152021-05-31 16:40:31 +02007333 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007334 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007335 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007336
7337 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007338 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007339 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007340 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007341 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007342
7343 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007344 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007345 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007346 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007347 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007348
7349 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007350 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7351 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007352 LY_CHECK_RET(rc);
7353 }
7354
7355 return LY_SUCCESS;
7356}
7357
7358/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7360 *
7361 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7362 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007363 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364 *
7365 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007366 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007367 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007368 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369 * @param[in] options XPath options.
7370 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7371 */
7372static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007373eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7374 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375{
Radek Krejci857189e2020-09-01 13:26:36 +02007376 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 LY_ERR rc;
7378
7379 goto step;
7380 do {
7381 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007382 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 all_desc = 0;
7384 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007385 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007386 all_desc = 1;
7387 }
aPiecek8b0cc152021-05-31 16:40:31 +02007388 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007389 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007390 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391
7392step:
Michal Vaskod3678892020-05-21 10:06:58 +02007393 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007394 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007395 attr_axis = 1;
7396
aPiecek8b0cc152021-05-31 16:40:31 +02007397 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007398 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007399 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007400 } else {
7401 attr_axis = 0;
7402 }
7403
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007405 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007406 case LYXP_TOKEN_DOT:
7407 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007408 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409 rc = moveto_scnode_self(set, all_desc, options);
7410 } else {
7411 rc = moveto_self(set, all_desc, options);
7412 }
7413 LY_CHECK_RET(rc);
7414 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007415 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007416 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417 break;
7418
7419 case LYXP_TOKEN_DDOT:
7420 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007421 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007422 rc = moveto_scnode_parent(set, all_desc, options);
7423 } else {
7424 rc = moveto_parent(set, all_desc, options);
7425 }
7426 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007427 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007428 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007429 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430 break;
7431
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007433 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007434 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007436 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437
Michal Vaskod3678892020-05-21 10:06:58 +02007438 case LYXP_TOKEN_NODETYPE:
7439 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007440 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007441 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442 break;
7443
7444 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007445 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446 }
Michal Vasko004d3152020-06-11 19:59:22 +02007447 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448
7449 return LY_SUCCESS;
7450}
7451
7452/**
7453 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7454 *
7455 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7456 *
7457 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007458 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007459 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 * @param[in] options XPath options.
7461 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7462 */
7463static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007464eval_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 +02007465{
Radek Krejci857189e2020-09-01 13:26:36 +02007466 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467
aPiecek8b0cc152021-05-31 16:40:31 +02007468 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007470 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 }
7472
7473 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007474 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 /* evaluate '/' - deferred */
7476 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007477 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007478 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007479 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480
Michal Vasko004d3152020-06-11 19:59:22 +02007481 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 return LY_SUCCESS;
7483 }
Michal Vasko004d3152020-06-11 19:59:22 +02007484 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 case LYXP_TOKEN_DOT:
7486 case LYXP_TOKEN_DDOT:
7487 case LYXP_TOKEN_AT:
7488 case LYXP_TOKEN_NAMETEST:
7489 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007490 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 break;
7492 default:
7493 break;
7494 }
7495
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007497 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7499 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007500 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007501 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007502 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503
Michal Vaskob0099a92020-08-31 14:55:23 +02007504 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007505 }
7506
7507 return LY_SUCCESS;
7508}
7509
7510/**
7511 * @brief Evaluate FunctionCall. Logs directly on error.
7512 *
Michal Vaskod3678892020-05-21 10:06:58 +02007513 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 *
7515 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007516 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007517 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518 * @param[in] options XPath options.
7519 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7520 */
7521static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007522eval_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 +02007523{
7524 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007525
Radek Krejci1deb5be2020-08-26 16:43:36 +02007526 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007527 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 struct lyxp_set **args = NULL, **args_aux;
7529
aPiecek8b0cc152021-05-31 16:40:31 +02007530 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007532 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007534 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007536 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 xpath_func = &xpath_sum;
7538 }
7539 break;
7540 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007541 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007542 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007543 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007544 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007545 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 xpath_func = &xpath_true;
7549 }
7550 break;
7551 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007552 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007554 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007555 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007556 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007558 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007560 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_deref;
7562 }
7563 break;
7564 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007565 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007567 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 xpath_func = &xpath_string;
7571 }
7572 break;
7573 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007574 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007576 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007578 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 xpath_func = &xpath_current;
7580 }
7581 break;
7582 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007583 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007585 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007587 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007588 xpath_func = &xpath_re_match;
7589 }
7590 break;
7591 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007592 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007594 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 xpath_func = &xpath_translate;
7596 }
7597 break;
7598 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007599 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007601 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007603 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 xpath_func = &xpath_bit_is_set;
7605 }
7606 break;
7607 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007608 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007609 xpath_func = &xpath_starts_with;
7610 }
7611 break;
7612 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007613 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 xpath_func = &xpath_derived_from;
7615 }
7616 break;
7617 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007618 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007620 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007621 xpath_func = &xpath_string_length;
7622 }
7623 break;
7624 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007625 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007626 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007627 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007628 xpath_func = &xpath_substring_after;
7629 }
7630 break;
7631 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007632 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007633 xpath_func = &xpath_substring_before;
7634 }
7635 break;
7636 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007637 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007638 xpath_func = &xpath_derived_from_or_self;
7639 }
7640 break;
7641 }
7642
7643 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007644 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 +02007645 return LY_EVALID;
7646 }
7647 }
7648
aPiecek8b0cc152021-05-31 16:40:31 +02007649 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007650 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007651 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007652
7653 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007654 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007655 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007656 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007657 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007658
7659 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007660 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007661 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007662 args = malloc(sizeof *args);
7663 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7664 arg_count = 1;
7665 args[0] = set_copy(set);
7666 if (!args[0]) {
7667 rc = LY_EMEM;
7668 goto cleanup;
7669 }
7670
Michal Vasko004d3152020-06-11 19:59:22 +02007671 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007672 LY_CHECK_GOTO(rc, cleanup);
7673 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007674 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 LY_CHECK_GOTO(rc, cleanup);
7676 }
7677 }
Michal Vasko004d3152020-06-11 19:59:22 +02007678 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007679 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007680 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007681 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007682
aPiecek8b0cc152021-05-31 16:40:31 +02007683 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684 ++arg_count;
7685 args_aux = realloc(args, arg_count * sizeof *args);
7686 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7687 args = args_aux;
7688 args[arg_count - 1] = set_copy(set);
7689 if (!args[arg_count - 1]) {
7690 rc = LY_EMEM;
7691 goto cleanup;
7692 }
7693
Michal Vasko004d3152020-06-11 19:59:22 +02007694 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 LY_CHECK_GOTO(rc, cleanup);
7696 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007697 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007698 LY_CHECK_GOTO(rc, cleanup);
7699 }
7700 }
7701
7702 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007703 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007704 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007705 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007706 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707
aPiecek8b0cc152021-05-31 16:40:31 +02007708 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709 /* evaluate function */
7710 rc = xpath_func(args, arg_count, set, options);
7711
7712 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007713 /* merge all nodes from arg evaluations */
7714 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007715 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007716 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007717 }
7718 }
7719 } else {
7720 rc = LY_SUCCESS;
7721 }
7722
7723cleanup:
7724 for (i = 0; i < arg_count; ++i) {
7725 lyxp_set_free(args[i]);
7726 }
7727 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 return rc;
7729}
7730
7731/**
7732 * @brief Evaluate Number. Logs directly on error.
7733 *
7734 * @param[in] ctx Context for errors.
7735 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007736 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007737 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7738 * @return LY_ERR
7739 */
7740static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007741eval_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 +02007742{
7743 long double num;
7744 char *endptr;
7745
7746 if (set) {
7747 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007748 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007750 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7751 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007752 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007753 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007754 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007755 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7756 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007757 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 return LY_EVALID;
7759 }
7760
7761 set_fill_number(set, num);
7762 }
7763
7764 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007765 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007766 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 return LY_SUCCESS;
7768}
7769
7770/**
7771 * @brief Evaluate PathExpr. Logs directly on error.
7772 *
Michal Vaskod3678892020-05-21 10:06:58 +02007773 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7775 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7776 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007777 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007778 *
7779 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007780 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007781 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007782 * @param[in] options XPath options.
7783 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7784 */
7785static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007786eval_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 +02007787{
Radek Krejci857189e2020-09-01 13:26:36 +02007788 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007789 LY_ERR rc;
7790
Michal Vasko004d3152020-06-11 19:59:22 +02007791 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 case LYXP_TOKEN_PAR1:
7793 /* '(' Expr ')' */
7794
7795 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007796 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007797 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007798 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799
7800 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007801 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802 LY_CHECK_RET(rc);
7803
7804 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007805 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007806 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007807 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007808 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809
7810 parent_pos_pred = 0;
7811 goto predicate;
7812
7813 case LYXP_TOKEN_DOT:
7814 case LYXP_TOKEN_DDOT:
7815 case LYXP_TOKEN_AT:
7816 case LYXP_TOKEN_NAMETEST:
7817 case LYXP_TOKEN_NODETYPE:
7818 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007819 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007820 LY_CHECK_RET(rc);
7821 break;
7822
7823 case LYXP_TOKEN_FUNCNAME:
7824 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007825 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007826 LY_CHECK_RET(rc);
7827
7828 parent_pos_pred = 1;
7829 goto predicate;
7830
Michal Vasko3e48bf32020-06-01 08:39:07 +02007831 case LYXP_TOKEN_OPER_PATH:
7832 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007833 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007834 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007835 LY_CHECK_RET(rc);
7836 break;
7837
7838 case LYXP_TOKEN_LITERAL:
7839 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007840 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7841 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007842 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843 }
Michal Vasko004d3152020-06-11 19:59:22 +02007844 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007846 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 }
7848
7849 parent_pos_pred = 1;
7850 goto predicate;
7851
7852 case LYXP_TOKEN_NUMBER:
7853 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007854 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7855 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007856 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007857 }
Michal Vasko004d3152020-06-11 19:59:22 +02007858 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007860 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007861 }
7862 LY_CHECK_RET(rc);
7863
7864 parent_pos_pred = 1;
7865 goto predicate;
7866
7867 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007868 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 +02007869 return LY_EVALID;
7870 }
7871
7872 return LY_SUCCESS;
7873
7874predicate:
7875 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007876 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7877 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007878 LY_CHECK_RET(rc);
7879 }
7880
7881 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007882 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007883
7884 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007885 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007886 all_desc = 0;
7887 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007888 all_desc = 1;
7889 }
7890
aPiecek8b0cc152021-05-31 16:40:31 +02007891 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007892 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007893 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007894
Michal Vasko004d3152020-06-11 19:59:22 +02007895 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007896 LY_CHECK_RET(rc);
7897 }
7898
7899 return LY_SUCCESS;
7900}
7901
7902/**
7903 * @brief Evaluate UnionExpr. Logs directly on error.
7904 *
Michal Vaskod3678892020-05-21 10:06:58 +02007905 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007906 *
7907 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007908 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007909 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007910 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007911 * @param[in] options XPath options.
7912 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7913 */
7914static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007915eval_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 +02007916{
7917 LY_ERR rc = LY_SUCCESS;
7918 struct lyxp_set orig_set, set2;
7919 uint16_t i;
7920
7921 assert(repeat);
7922
7923 set_init(&orig_set, set);
7924 set_init(&set2, set);
7925
7926 set_fill_set(&orig_set, set);
7927
Michal Vasko004d3152020-06-11 19:59:22 +02007928 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007929 LY_CHECK_GOTO(rc, cleanup);
7930
7931 /* ('|' PathExpr)* */
7932 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007933 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007934 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007935 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007936 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007937
aPiecek8b0cc152021-05-31 16:40:31 +02007938 if (options & LYXP_SKIP_EXPR) {
7939 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007940 LY_CHECK_GOTO(rc, cleanup);
7941 continue;
7942 }
7943
7944 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007945 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007946 LY_CHECK_GOTO(rc, cleanup);
7947
7948 /* eval */
7949 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007950 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007952 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953 LY_CHECK_GOTO(rc, cleanup);
7954 }
7955 }
7956
7957cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007958 lyxp_set_free_content(&orig_set);
7959 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 return rc;
7961}
7962
7963/**
7964 * @brief Evaluate UnaryExpr. Logs directly on error.
7965 *
Michal Vaskod3678892020-05-21 10:06:58 +02007966 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007967 *
7968 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007969 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007970 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007971 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007972 * @param[in] options XPath options.
7973 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7974 */
7975static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007976eval_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 +02007977{
7978 LY_ERR rc;
7979 uint16_t this_op, i;
7980
7981 assert(repeat);
7982
7983 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007984 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007986 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 +02007987
aPiecek8b0cc152021-05-31 16:40:31 +02007988 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007989 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007990 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 }
7992
Michal Vasko004d3152020-06-11 19:59:22 +02007993 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007994 LY_CHECK_RET(rc);
7995
aPiecek8b0cc152021-05-31 16:40:31 +02007996 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007997 if (options & LYXP_SCNODE_ALL) {
7998 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7999 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008000 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008001 LY_CHECK_RET(rc);
8002 }
8003 }
8004
8005 return LY_SUCCESS;
8006}
8007
8008/**
8009 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
8010 *
Michal Vaskod3678892020-05-21 10:06:58 +02008011 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008012 * | MultiplicativeExpr '*' UnaryExpr
8013 * | MultiplicativeExpr 'div' UnaryExpr
8014 * | MultiplicativeExpr 'mod' UnaryExpr
8015 *
8016 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008017 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008018 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008019 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020 * @param[in] options XPath options.
8021 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8022 */
8023static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008024eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8025 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026{
8027 LY_ERR rc;
8028 uint16_t this_op;
8029 struct lyxp_set orig_set, set2;
8030 uint16_t i;
8031
8032 assert(repeat);
8033
8034 set_init(&orig_set, set);
8035 set_init(&set2, set);
8036
8037 set_fill_set(&orig_set, set);
8038
Michal Vasko004d3152020-06-11 19:59:22 +02008039 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040 LY_CHECK_GOTO(rc, cleanup);
8041
8042 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8043 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008044 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045
Michal Vasko004d3152020-06-11 19:59:22 +02008046 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008047 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008048 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008049 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008050
aPiecek8b0cc152021-05-31 16:40:31 +02008051 if (options & LYXP_SKIP_EXPR) {
8052 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053 LY_CHECK_GOTO(rc, cleanup);
8054 continue;
8055 }
8056
8057 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008058 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 LY_CHECK_GOTO(rc, cleanup);
8060
8061 /* eval */
8062 if (options & LYXP_SCNODE_ALL) {
8063 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008064 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008065 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008066 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008067 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 LY_CHECK_GOTO(rc, cleanup);
8069 }
8070 }
8071
8072cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008073 lyxp_set_free_content(&orig_set);
8074 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008075 return rc;
8076}
8077
8078/**
8079 * @brief Evaluate AdditiveExpr. Logs directly on error.
8080 *
Michal Vaskod3678892020-05-21 10:06:58 +02008081 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008082 * | AdditiveExpr '+' MultiplicativeExpr
8083 * | AdditiveExpr '-' MultiplicativeExpr
8084 *
8085 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008086 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008088 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089 * @param[in] options XPath options.
8090 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8091 */
8092static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008093eval_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 +02008094{
8095 LY_ERR rc;
8096 uint16_t this_op;
8097 struct lyxp_set orig_set, set2;
8098 uint16_t i;
8099
8100 assert(repeat);
8101
8102 set_init(&orig_set, set);
8103 set_init(&set2, set);
8104
8105 set_fill_set(&orig_set, set);
8106
Michal Vasko004d3152020-06-11 19:59:22 +02008107 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 LY_CHECK_GOTO(rc, cleanup);
8109
8110 /* ('+' / '-' MultiplicativeExpr)* */
8111 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008112 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113
Michal Vasko004d3152020-06-11 19:59:22 +02008114 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008116 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008117 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008118
aPiecek8b0cc152021-05-31 16:40:31 +02008119 if (options & LYXP_SKIP_EXPR) {
8120 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008121 LY_CHECK_GOTO(rc, cleanup);
8122 continue;
8123 }
8124
8125 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008126 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 LY_CHECK_GOTO(rc, cleanup);
8128
8129 /* eval */
8130 if (options & LYXP_SCNODE_ALL) {
8131 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008132 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008133 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008134 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008135 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136 LY_CHECK_GOTO(rc, cleanup);
8137 }
8138 }
8139
8140cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008141 lyxp_set_free_content(&orig_set);
8142 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143 return rc;
8144}
8145
8146/**
8147 * @brief Evaluate RelationalExpr. Logs directly on error.
8148 *
Michal Vaskod3678892020-05-21 10:06:58 +02008149 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008150 * | RelationalExpr '<' AdditiveExpr
8151 * | RelationalExpr '>' AdditiveExpr
8152 * | RelationalExpr '<=' AdditiveExpr
8153 * | RelationalExpr '>=' AdditiveExpr
8154 *
8155 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008156 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008158 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008159 * @param[in] options XPath options.
8160 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8161 */
8162static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008163eval_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 +02008164{
8165 LY_ERR rc;
8166 uint16_t this_op;
8167 struct lyxp_set orig_set, set2;
8168 uint16_t i;
8169
8170 assert(repeat);
8171
8172 set_init(&orig_set, set);
8173 set_init(&set2, set);
8174
8175 set_fill_set(&orig_set, set);
8176
Michal Vasko004d3152020-06-11 19:59:22 +02008177 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178 LY_CHECK_GOTO(rc, cleanup);
8179
8180 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8181 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008182 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008183
Michal Vasko004d3152020-06-11 19:59:22 +02008184 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008185 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008186 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008187 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188
aPiecek8b0cc152021-05-31 16:40:31 +02008189 if (options & LYXP_SKIP_EXPR) {
8190 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 LY_CHECK_GOTO(rc, cleanup);
8192 continue;
8193 }
8194
8195 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008196 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 LY_CHECK_GOTO(rc, cleanup);
8198
8199 /* eval */
8200 if (options & LYXP_SCNODE_ALL) {
8201 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008202 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008203 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008204 } else {
8205 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8206 LY_CHECK_GOTO(rc, cleanup);
8207 }
8208 }
8209
8210cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008211 lyxp_set_free_content(&orig_set);
8212 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008213 return rc;
8214}
8215
8216/**
8217 * @brief Evaluate EqualityExpr. Logs directly on error.
8218 *
Michal Vaskod3678892020-05-21 10:06:58 +02008219 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008220 * | EqualityExpr '!=' RelationalExpr
8221 *
8222 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008223 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008225 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008226 * @param[in] options XPath options.
8227 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8228 */
8229static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008230eval_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 +02008231{
8232 LY_ERR rc;
8233 uint16_t this_op;
8234 struct lyxp_set orig_set, set2;
8235 uint16_t i;
8236
8237 assert(repeat);
8238
8239 set_init(&orig_set, set);
8240 set_init(&set2, set);
8241
8242 set_fill_set(&orig_set, set);
8243
Michal Vasko004d3152020-06-11 19:59:22 +02008244 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 LY_CHECK_GOTO(rc, cleanup);
8246
8247 /* ('=' / '!=' RelationalExpr)* */
8248 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008249 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250
Michal Vasko004d3152020-06-11 19:59:22 +02008251 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008252 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008253 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008254 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255
aPiecek8b0cc152021-05-31 16:40:31 +02008256 if (options & LYXP_SKIP_EXPR) {
8257 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258 LY_CHECK_GOTO(rc, cleanup);
8259 continue;
8260 }
8261
8262 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008263 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 LY_CHECK_GOTO(rc, cleanup);
8265
8266 /* eval */
8267 if (options & LYXP_SCNODE_ALL) {
8268 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008269 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8270 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008271 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008272 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8275 LY_CHECK_GOTO(rc, cleanup);
8276 }
8277 }
8278
8279cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008280 lyxp_set_free_content(&orig_set);
8281 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008282 return rc;
8283}
8284
8285/**
8286 * @brief Evaluate AndExpr. Logs directly on error.
8287 *
Michal Vaskod3678892020-05-21 10:06:58 +02008288 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008289 *
8290 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008291 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008293 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 * @param[in] options XPath options.
8295 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8296 */
8297static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008298eval_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 +02008299{
8300 LY_ERR rc;
8301 struct lyxp_set orig_set, set2;
8302 uint16_t i;
8303
8304 assert(repeat);
8305
8306 set_init(&orig_set, set);
8307 set_init(&set2, set);
8308
8309 set_fill_set(&orig_set, set);
8310
Michal Vasko004d3152020-06-11 19:59:22 +02008311 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008312 LY_CHECK_GOTO(rc, cleanup);
8313
8314 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008315 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008316 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008317 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008318 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008319 }
8320
8321 /* ('and' EqualityExpr)* */
8322 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008323 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008324 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008325 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008326 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327
8328 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008329 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8330 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008331 LY_CHECK_GOTO(rc, cleanup);
8332 continue;
8333 }
8334
8335 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008336 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008337 LY_CHECK_GOTO(rc, cleanup);
8338
8339 /* eval - just get boolean value actually */
8340 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008341 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008342 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008344 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 set_fill_set(set, &set2);
8346 }
8347 }
8348
8349cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008350 lyxp_set_free_content(&orig_set);
8351 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 return rc;
8353}
8354
8355/**
8356 * @brief Evaluate OrExpr. Logs directly on error.
8357 *
Michal Vaskod3678892020-05-21 10:06:58 +02008358 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359 *
8360 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008361 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008363 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008364 * @param[in] options XPath options.
8365 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8366 */
8367static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008368eval_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 +02008369{
8370 LY_ERR rc;
8371 struct lyxp_set orig_set, set2;
8372 uint16_t i;
8373
8374 assert(repeat);
8375
8376 set_init(&orig_set, set);
8377 set_init(&set2, set);
8378
8379 set_fill_set(&orig_set, set);
8380
Michal Vasko004d3152020-06-11 19:59:22 +02008381 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 LY_CHECK_GOTO(rc, cleanup);
8383
8384 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008385 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008386 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008387 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008388 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 }
8390
8391 /* ('or' AndExpr)* */
8392 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008393 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008394 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008395 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008396 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397
8398 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008399 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8400 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 LY_CHECK_GOTO(rc, cleanup);
8402 continue;
8403 }
8404
8405 set_fill_set(&set2, &orig_set);
8406 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8407 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008408 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409 LY_CHECK_GOTO(rc, cleanup);
8410
8411 /* eval - just get boolean value actually */
8412 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008413 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008414 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008416 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417 set_fill_set(set, &set2);
8418 }
8419 }
8420
8421cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008422 lyxp_set_free_content(&orig_set);
8423 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008424 return rc;
8425}
8426
8427/**
Michal Vasko004d3152020-06-11 19:59:22 +02008428 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 *
8430 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008431 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008433 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434 * @param[in] options XPath options.
8435 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8436 */
8437static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008438eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8439 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008440{
8441 uint16_t i, count;
8442 enum lyxp_expr_type next_etype;
8443 LY_ERR rc;
8444
8445 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008446 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 next_etype = LYXP_EXPR_NONE;
8448 } else {
8449 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008450 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008451
8452 /* select one-priority lower because etype expression called us */
8453 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008454 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008456 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457 } else {
8458 next_etype = LYXP_EXPR_NONE;
8459 }
8460 }
8461
8462 /* decide what expression are we parsing based on the repeat */
8463 switch (next_etype) {
8464 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008465 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008466 break;
8467 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008468 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 break;
8470 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008471 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008472 break;
8473 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008474 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475 break;
8476 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008477 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478 break;
8479 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008480 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008481 break;
8482 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008483 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008484 break;
8485 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008486 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487 break;
8488 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008489 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490 break;
8491 default:
8492 LOGINT_RET(set->ctx);
8493 }
8494
8495 return rc;
8496}
8497
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008498/**
8499 * @brief Get root type.
8500 *
8501 * @param[in] ctx_node Context node.
8502 * @param[in] ctx_scnode Schema context node.
8503 * @param[in] options XPath options.
8504 * @return Root type.
8505 */
8506static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008507lyxp_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 +01008508{
Michal Vasko6b26e742020-07-17 15:02:10 +02008509 const struct lysc_node *op;
8510
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008511 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008512 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008513 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008514
8515 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008516 /* general root that can access everything */
8517 return LYXP_NODE_ROOT;
8518 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8519 /* root context node can access only config data (because we said so, it is unspecified) */
8520 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008521 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008522 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008523 }
8524
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008525 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008526 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008527 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008528
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008529 if (op || !(options & LYXP_SCHEMA)) {
8530 /* general root that can access everything */
8531 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008532 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008533 /* root context node can access only config data (because we said so, it is unspecified) */
8534 return LYXP_NODE_ROOT_CONFIG;
8535 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008536 return LYXP_NODE_ROOT;
8537}
8538
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008540lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008541 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 +01008542 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543{
Michal Vasko004d3152020-06-11 19:59:22 +02008544 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 LY_ERR rc;
8546
Michal Vasko400e9672021-01-11 13:39:17 +01008547 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008548 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008549 LOGARG(NULL, "Current module must be set if schema format is used.");
8550 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008551 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008553 if (tree) {
8554 /* adjust the pointer to be the first top-level sibling */
8555 while (tree->parent) {
8556 tree = lyd_parent(tree);
8557 }
8558 tree = lyd_first_sibling(tree);
8559 }
8560
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008563 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008564 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8565 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8566
Michal Vasko400e9672021-01-11 13:39:17 +01008567 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008568 set->cur_node = ctx_node;
8569 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008570 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8571 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008572 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008573 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008575 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576
Radek Krejciddace2c2021-01-08 11:30:56 +01008577 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008578
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008580 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008581 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008582 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 }
8584
Radek Krejciddace2c2021-01-08 11:30:56 +01008585 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 return rc;
8587}
8588
8589#if 0
8590
8591/* full xml printing of set elements, not used currently */
8592
8593void
8594lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8595{
8596 uint32_t i;
8597 char *str_num;
8598 struct lyout out;
8599
8600 memset(&out, 0, sizeof out);
8601
8602 out.type = LYOUT_STREAM;
8603 out.method.f = f;
8604
8605 switch (set->type) {
8606 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008607 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008608 break;
8609 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008610 ly_print_(&out, "Boolean XPath set:\n");
8611 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612 break;
8613 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008614 ly_print_(&out, "String XPath set:\n");
8615 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616 break;
8617 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008618 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008619
8620 if (isnan(set->value.num)) {
8621 str_num = strdup("NaN");
8622 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8623 str_num = strdup("0");
8624 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8625 str_num = strdup("Infinity");
8626 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8627 str_num = strdup("-Infinity");
8628 } else if ((long long)set->value.num == set->value.num) {
8629 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8630 str_num = NULL;
8631 }
8632 } else {
8633 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8634 str_num = NULL;
8635 }
8636 }
8637 if (!str_num) {
8638 LOGMEM;
8639 return;
8640 }
Michal Vasko5233e962020-08-14 14:26:20 +02008641 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 free(str_num);
8643 break;
8644 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008645 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008646
8647 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008648 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 switch (set->node_type[i]) {
8650 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008651 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008652 break;
8653 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008654 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008655 break;
8656 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008657 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658 break;
8659 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008660 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661 break;
8662 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008663 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008664 break;
8665 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008666 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008667 break;
8668 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008669 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008670 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008671 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008672 break;
8673 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008674 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 +02008675 break;
8676 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008677 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 +02008678 break;
8679 }
8680 }
8681 break;
8682 }
8683}
8684
8685#endif
8686
8687LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008688lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008689{
8690 long double num;
8691 char *str;
8692 LY_ERR rc;
8693
8694 if (!set || (set->type == target)) {
8695 return LY_SUCCESS;
8696 }
8697
8698 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008699 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008700
8701 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008702 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008703 return LY_EINVAL;
8704 }
8705
8706 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008707 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 switch (set->type) {
8709 case LYXP_SET_NUMBER:
8710 if (isnan(set->val.num)) {
8711 set->val.str = strdup("NaN");
8712 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8713 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8714 set->val.str = strdup("0");
8715 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8716 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8717 set->val.str = strdup("Infinity");
8718 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8719 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8720 set->val.str = strdup("-Infinity");
8721 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8722 } else if ((long long)set->val.num == set->val.num) {
8723 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8724 LOGMEM_RET(set->ctx);
8725 }
8726 set->val.str = str;
8727 } else {
8728 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8729 LOGMEM_RET(set->ctx);
8730 }
8731 set->val.str = str;
8732 }
8733 break;
8734 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008735 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736 set->val.str = strdup("true");
8737 } else {
8738 set->val.str = strdup("false");
8739 }
8740 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8741 break;
8742 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008743 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008744 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008745
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008746 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008747 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008748 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008749 set->val.str = str;
8750 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008751 default:
8752 LOGINT_RET(set->ctx);
8753 }
8754 set->type = LYXP_SET_STRING;
8755 }
8756
8757 /* to NUMBER */
8758 if (target == LYXP_SET_NUMBER) {
8759 switch (set->type) {
8760 case LYXP_SET_STRING:
8761 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008762 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763 set->val.num = num;
8764 break;
8765 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008766 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008767 set->val.num = 1;
8768 } else {
8769 set->val.num = 0;
8770 }
8771 break;
8772 default:
8773 LOGINT_RET(set->ctx);
8774 }
8775 set->type = LYXP_SET_NUMBER;
8776 }
8777
8778 /* to BOOLEAN */
8779 if (target == LYXP_SET_BOOLEAN) {
8780 switch (set->type) {
8781 case LYXP_SET_NUMBER:
8782 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008783 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008784 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008785 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008786 }
8787 break;
8788 case LYXP_SET_STRING:
8789 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008790 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008791 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008792 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008793 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008794 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008795 }
8796 break;
8797 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008798 if (set->used) {
8799 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008800 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008801 } else {
8802 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008803 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008804 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008805 break;
8806 default:
8807 LOGINT_RET(set->ctx);
8808 }
8809 set->type = LYXP_SET_BOOLEAN;
8810 }
8811
Michal Vasko03ff5a72019-09-11 13:49:33 +02008812 return LY_SUCCESS;
8813}
8814
8815LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008816lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008817 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008818 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008819{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008820 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008821 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008822
Michal Vasko400e9672021-01-11 13:39:17 +01008823 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008824 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008825 LOGARG(NULL, "Current module must be set if schema format is used.");
8826 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008827 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008828
8829 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008830 memset(set, 0, sizeof *set);
8831 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008832 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8833 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 +01008834 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008835
Michal Vasko400e9672021-01-11 13:39:17 +01008836 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008837 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008838 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008839 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8840 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008841 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008842 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008843 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008844
Radek Krejciddace2c2021-01-08 11:30:56 +01008845 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008846
Michal Vasko03ff5a72019-09-11 13:49:33 +02008847 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008848 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8849
Radek Krejciddace2c2021-01-08 11:30:56 +01008850 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008851 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008852}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008853
8854API const char *
8855lyxp_get_expr(const struct lyxp_expr *path)
8856{
8857 if (!path) {
8858 return NULL;
8859 }
8860
8861 return path->expr;
8862}