blob: 53835f5337a3e92e2f35988aa32b2e772fe633d1 [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
aPiecek96dc1e32021-10-08 15:45:59 +020051/* Functions are divided into the following basic classes:
52 *
53 * (re)parse functions:
54 * Parse functions parse the expression into
55 * tokens (syntactic analysis).
56 * Reparse functions perform semantic analysis
57 * (do not save the result, just a check) of
58 * the expression and fill repeat indices.
59 *
60 * warn functions:
61 * Warn functions check specific reasonable conditions for schema XPath
62 * and print a warning if they are not satisfied.
63 *
64 * moveto functions:
65 * They and only they actually change the context (set).
66 *
67 * eval functions:
68 * They execute a parsed XPath expression on some data subtree.
69 */
70
Michal Vasko03ff5a72019-09-11 13:49:33 +020071/**
72 * @brief Print the type of an XPath \p set.
73 *
74 * @param[in] set Set to use.
75 * @return Set type string.
76 */
77static const char *
78print_set_type(struct lyxp_set *set)
79{
80 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020081 case LYXP_SET_NODE_SET:
82 return "node set";
83 case LYXP_SET_SCNODE_SET:
84 return "schema node set";
85 case LYXP_SET_BOOLEAN:
86 return "boolean";
87 case LYXP_SET_NUMBER:
88 return "number";
89 case LYXP_SET_STRING:
90 return "string";
91 }
92
93 return NULL;
94}
95
Michal Vasko24cddf82020-06-01 08:17:01 +020096const char *
97lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020098{
99 switch (tok) {
100 case LYXP_TOKEN_PAR1:
101 return "(";
102 case LYXP_TOKEN_PAR2:
103 return ")";
104 case LYXP_TOKEN_BRACK1:
105 return "[";
106 case LYXP_TOKEN_BRACK2:
107 return "]";
108 case LYXP_TOKEN_DOT:
109 return ".";
110 case LYXP_TOKEN_DDOT:
111 return "..";
112 case LYXP_TOKEN_AT:
113 return "@";
114 case LYXP_TOKEN_COMMA:
115 return ",";
116 case LYXP_TOKEN_NAMETEST:
117 return "NameTest";
118 case LYXP_TOKEN_NODETYPE:
119 return "NodeType";
120 case LYXP_TOKEN_FUNCNAME:
121 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200122 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200123 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200124 case LYXP_TOKEN_OPER_EQUAL:
125 return "Operator(Equal)";
126 case LYXP_TOKEN_OPER_NEQUAL:
127 return "Operator(Non-equal)";
128 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200129 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200130 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200131 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200132 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200133 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200134 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200136 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200137 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200138 case LYXP_TOKEN_LITERAL:
139 return "Literal";
140 case LYXP_TOKEN_NUMBER:
141 return "Number";
142 default:
143 LOGINT(NULL);
144 return "";
145 }
146}
147
148/**
149 * @brief Print the whole expression \p exp to debug output.
150 *
151 * @param[in] exp Expression to use.
152 */
153static void
Michal Vasko40308e72020-10-20 16:38:40 +0200154print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200155{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100156#define MSG_BUFFER_SIZE 128
157 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100158 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200159
Radek Krejci52b6d512020-10-12 12:33:17 +0200160 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200161 return;
162 }
163
164 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
165 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200166 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200167 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100168 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200169 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
170 for (j = 1; exp->repeat[i][j]; ++j) {
171 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
172 }
173 strcat(tmp, ")");
174 }
175 LOGDBG(LY_LDGXPATH, tmp);
176 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100177#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200178}
179
180#ifndef NDEBUG
181
182/**
183 * @brief Print XPath set content to debug output.
184 *
185 * @param[in] set Set to print.
186 */
187static void
188print_set_debug(struct lyxp_set *set)
189{
190 uint32_t i;
191 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200192 struct lyxp_set_node *item;
193 struct lyxp_set_scnode *sitem;
194
Radek Krejci52b6d512020-10-12 12:33:17 +0200195 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200196 return;
197 }
198
199 switch (set->type) {
200 case LYXP_SET_NODE_SET:
201 LOGDBG(LY_LDGXPATH, "set NODE SET:");
202 for (i = 0; i < set->used; ++i) {
203 item = &set->val.nodes[i];
204
205 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100206 case LYXP_NODE_NONE:
207 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
208 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200209 case LYXP_NODE_ROOT:
210 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
211 break;
212 case LYXP_NODE_ROOT_CONFIG:
213 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
214 break;
215 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100216 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200218 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100219 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200220 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200221 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200222 } else {
223 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
224 }
225 break;
226 case LYXP_NODE_TEXT:
227 if (item->node->schema->nodetype & LYS_ANYDATA) {
228 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200229 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200230 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200231 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 +0200232 }
233 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100234 case LYXP_NODE_META:
235 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 +0200236 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200237 break;
238 }
239 }
240 break;
241
242 case LYXP_SET_SCNODE_SET:
243 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
244 for (i = 0; i < set->used; ++i) {
245 sitem = &set->val.scnodes[i];
246
247 switch (sitem->type) {
248 case LYXP_NODE_ROOT:
249 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
250 break;
251 case LYXP_NODE_ROOT_CONFIG:
252 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
253 break;
254 case LYXP_NODE_ELEM:
255 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
256 break;
257 default:
258 LOGINT(NULL);
259 break;
260 }
261 }
262 break;
263
Michal Vasko03ff5a72019-09-11 13:49:33 +0200264 case LYXP_SET_BOOLEAN:
265 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200266 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200267 break;
268
269 case LYXP_SET_STRING:
270 LOGDBG(LY_LDGXPATH, "set STRING");
271 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
272 break;
273
274 case LYXP_SET_NUMBER:
275 LOGDBG(LY_LDGXPATH, "set NUMBER");
276
277 if (isnan(set->val.num)) {
278 str = strdup("NaN");
279 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
280 str = strdup("0");
281 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
282 str = strdup("Infinity");
283 } else if (isinf(set->val.num) && signbit(set->val.num)) {
284 str = strdup("-Infinity");
285 } else if ((long long)set->val.num == set->val.num) {
286 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
287 str = NULL;
288 }
289 } else {
290 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
291 str = NULL;
292 }
293 }
294 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
295
296 LOGDBG(LY_LDGXPATH, "\t%s", str);
297 free(str);
298 }
299}
300
301#endif
302
303/**
304 * @brief Realloc the string \p str.
305 *
306 * @param[in] ctx libyang context for logging.
307 * @param[in] needed How much free space is required.
308 * @param[in,out] str Pointer to the string to use.
309 * @param[in,out] used Used bytes in \p str.
310 * @param[in,out] size Allocated bytes in \p str.
311 * @return LY_ERR
312 */
313static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100314cast_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 +0200315{
316 if (*size - *used < needed) {
317 do {
318 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
319 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
320 return LY_EINVAL;
321 }
322 *size += LYXP_STRING_CAST_SIZE_STEP;
323 } while (*size - *used < needed);
324 *str = ly_realloc(*str, *size * sizeof(char));
325 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
326 }
327
328 return LY_SUCCESS;
329}
330
331/**
332 * @brief Cast nodes recursively to one string @p str.
333 *
334 * @param[in] node Node to cast.
335 * @param[in] fake_cont Whether to put the data into a "fake" container.
336 * @param[in] root_type Type of the XPath root.
337 * @param[in] indent Current indent.
338 * @param[in,out] str Resulting string.
339 * @param[in,out] used Used bytes in @p str.
340 * @param[in,out] size Allocated bytes in @p str.
341 * @return LY_ERR
342 */
343static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200344cast_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 +0200345 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200346{
Radek Krejci7f769d72020-07-11 23:13:56 +0200347 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200348 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200349 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200350 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200351 struct lyd_node_any *any;
352 LY_ERR rc;
353
354 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
355 return LY_SUCCESS;
356 }
357
358 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200359 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200360 LY_CHECK_RET(rc);
361 strcpy(*str + (*used - 1), "\n");
362 ++(*used);
363
364 ++indent;
365 }
366
367 switch (node->schema->nodetype) {
368 case LYS_CONTAINER:
369 case LYS_LIST:
370 case LYS_RPC:
371 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200372 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200373 LY_CHECK_RET(rc);
374 strcpy(*str + (*used - 1), "\n");
375 ++(*used);
376
Radek Krejcia1c1e542020-09-29 16:06:52 +0200377 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200378 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
379 LY_CHECK_RET(rc);
380 }
381
382 break;
383
384 case LYS_LEAF:
385 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200386 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200387
388 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200389 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 +0200390 memset(*str + (*used - 1), ' ', indent * 2);
391 *used += indent * 2;
392
393 /* print value */
394 if (*used == 1) {
395 sprintf(*str + (*used - 1), "%s", value_str);
396 *used += strlen(value_str);
397 } else {
398 sprintf(*str + (*used - 1), "%s\n", value_str);
399 *used += strlen(value_str) + 1;
400 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200401
402 break;
403
404 case LYS_ANYXML:
405 case LYS_ANYDATA:
406 any = (struct lyd_node_any *)node;
407 if (!(void *)any->value.tree) {
408 /* no content */
409 buf = strdup("");
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 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200412 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100413
Michal Vasko60ea6352020-06-29 13:39:39 +0200414 if (any->value_type == LYD_ANYDATA_LYB) {
415 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200416 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 +0200417 /* successfully parsed */
418 free(any->value.mem);
419 any->value.tree = tree;
420 any->value_type = LYD_ANYDATA_DATATREE;
421 }
Radek Krejci7931b192020-06-25 17:05:03 +0200422 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200423 }
424
Michal Vasko03ff5a72019-09-11 13:49:33 +0200425 switch (any->value_type) {
426 case LYD_ANYDATA_STRING:
427 case LYD_ANYDATA_XML:
428 case LYD_ANYDATA_JSON:
429 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200430 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200431 break;
432 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200433 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200434 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200435 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100436 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200437 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200438 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200439 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200440 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200441 }
442 }
443
444 line = strtok_r(buf, "\n", &ptr);
445 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 if (rc != LY_SUCCESS) {
448 free(buf);
449 return rc;
450 }
451 memset(*str + (*used - 1), ' ', indent * 2);
452 *used += indent * 2;
453
454 strcpy(*str + (*used - 1), line);
455 *used += strlen(line);
456
457 strcpy(*str + (*used - 1), "\n");
458 *used += 1;
459 } while ((line = strtok_r(NULL, "\n", &ptr)));
460
461 free(buf);
462 break;
463
464 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200465 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200466 }
467
468 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200469 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200470 LY_CHECK_RET(rc);
471 strcpy(*str + (*used - 1), "\n");
472 ++(*used);
473
474 --indent;
475 }
476
477 return LY_SUCCESS;
478}
479
480/**
481 * @brief Cast an element into a string.
482 *
483 * @param[in] node Node to cast.
484 * @param[in] fake_cont Whether to put the data into a "fake" container.
485 * @param[in] root_type Type of the XPath root.
486 * @param[out] str Element cast to dynamically-allocated string.
487 * @return LY_ERR
488 */
489static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200490cast_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 +0200491{
492 uint16_t used, size;
493 LY_ERR rc;
494
495 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200496 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200497 (*str)[0] = '\0';
498 used = 1;
499 size = LYXP_STRING_CAST_SIZE_START;
500
501 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
502 if (rc != LY_SUCCESS) {
503 free(*str);
504 return rc;
505 }
506
507 if (size > used) {
508 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200509 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200510 }
511 return LY_SUCCESS;
512}
513
514/**
515 * @brief Cast a LYXP_SET_NODE_SET set into a string.
516 * Context position aware.
517 *
518 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200519 * @param[out] str Cast dynamically-allocated string.
520 * @return LY_ERR
521 */
522static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100523cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200524{
Michal Vaskoaa677062021-03-09 13:52:53 +0100525 if (!set->used) {
526 *str = strdup("");
527 if (!*str) {
528 LOGMEM_RET(set->ctx);
529 }
530 return LY_SUCCESS;
531 }
532
Michal Vasko03ff5a72019-09-11 13:49:33 +0200533 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100534 case LYXP_NODE_NONE:
535 /* invalid */
536 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200537 case LYXP_NODE_ROOT:
538 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100539 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200540 case LYXP_NODE_ELEM:
541 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100542 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100543 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200544 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200545 if (!*str) {
546 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200547 }
548 return LY_SUCCESS;
549 }
550
551 LOGINT_RET(set->ctx);
552}
553
554/**
555 * @brief Cast a string into an XPath number.
556 *
557 * @param[in] str String to use.
558 * @return Cast number.
559 */
560static long double
561cast_string_to_number(const char *str)
562{
563 long double num;
564 char *ptr;
565
566 errno = 0;
567 num = strtold(str, &ptr);
568 if (errno || *ptr) {
569 num = NAN;
570 }
571 return num;
572}
573
574/**
575 * @brief Callback for checking value equality.
576 *
Michal Vasko62524a92021-02-26 10:08:50 +0100577 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200578 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200579 * @param[in] val1_p First value.
580 * @param[in] val2_p Second value.
581 * @param[in] mod Whether hash table is being modified.
582 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200583 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200584 */
Radek Krejci857189e2020-09-01 13:26:36 +0200585static ly_bool
586set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200587{
588 struct lyxp_set_hash_node *val1, *val2;
589
590 val1 = (struct lyxp_set_hash_node *)val1_p;
591 val2 = (struct lyxp_set_hash_node *)val2_p;
592
593 if ((val1->node == val2->node) && (val1->type == val2->type)) {
594 return 1;
595 }
596
597 return 0;
598}
599
600/**
601 * @brief Insert node and its hash into set.
602 *
603 * @param[in] set et to insert to.
604 * @param[in] node Node with hash.
605 * @param[in] type Node type.
606 */
607static void
608set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
609{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200610 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200611 uint32_t i, hash;
612 struct lyxp_set_hash_node hnode;
613
614 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
615 /* create hash table and add all the nodes */
616 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
617 for (i = 0; i < set->used; ++i) {
618 hnode.node = set->val.nodes[i].node;
619 hnode.type = set->val.nodes[i].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;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200628
Michal Vasko9d6befd2019-12-11 14:56:56 +0100629 if (hnode.node == node) {
630 /* it was just added, do not add it twice */
631 node = NULL;
632 }
633 }
634 }
635
636 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200637 /* add the new node into hash table */
638 hnode.node = node;
639 hnode.type = type;
640
641 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
642 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
643 hash = dict_hash_multi(hash, NULL, 0);
644
645 r = lyht_insert(set->ht, &hnode, hash, NULL);
646 assert(!r);
647 (void)r;
648 }
649}
650
651/**
652 * @brief Remove node and its hash from set.
653 *
654 * @param[in] set Set to remove from.
655 * @param[in] node Node to remove.
656 * @param[in] type Node type.
657 */
658static void
659set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
660{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200661 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200662 struct lyxp_set_hash_node hnode;
663 uint32_t hash;
664
665 if (set->ht) {
666 hnode.node = node;
667 hnode.type = type;
668
669 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
670 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
671 hash = dict_hash_multi(hash, NULL, 0);
672
673 r = lyht_remove(set->ht, &hnode, hash);
674 assert(!r);
675 (void)r;
676
677 if (!set->ht->used) {
678 lyht_free(set->ht);
679 set->ht = NULL;
680 }
681 }
682}
683
684/**
685 * @brief Check whether node is in set based on its hash.
686 *
687 * @param[in] set Set to search in.
688 * @param[in] node Node to search for.
689 * @param[in] type Node type.
690 * @param[in] skip_idx Index in @p set to skip.
691 * @return LY_ERR
692 */
693static LY_ERR
694set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
695{
696 struct lyxp_set_hash_node hnode, *match_p;
697 uint32_t hash;
698
699 hnode.node = node;
700 hnode.type = type;
701
702 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
703 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
704 hash = dict_hash_multi(hash, NULL, 0);
705
706 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
707 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
708 /* we found it on the index that should be skipped, find another */
709 hnode = *match_p;
710 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
711 /* none other found */
712 return LY_SUCCESS;
713 }
714 }
715
716 return LY_EEXIST;
717 }
718
719 /* not found */
720 return LY_SUCCESS;
721}
722
Michal Vaskod3678892020-05-21 10:06:58 +0200723void
724lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200725{
726 if (!set) {
727 return;
728 }
729
730 if (set->type == LYXP_SET_NODE_SET) {
731 free(set->val.nodes);
732 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200733 } else if (set->type == LYXP_SET_SCNODE_SET) {
734 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200735 lyht_free(set->ht);
736 } else {
737 if (set->type == LYXP_SET_STRING) {
738 free(set->val.str);
739 }
740 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200741 }
Michal Vaskod3678892020-05-21 10:06:58 +0200742
743 set->val.nodes = NULL;
744 set->used = 0;
745 set->size = 0;
746 set->ht = NULL;
747 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200748 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200749}
750
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100751/**
752 * @brief Free dynamically-allocated set.
753 *
754 * @param[in] set Set to free.
755 */
756static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200757lyxp_set_free(struct lyxp_set *set)
758{
759 if (!set) {
760 return;
761 }
762
Michal Vaskod3678892020-05-21 10:06:58 +0200763 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200764 free(set);
765}
766
767/**
768 * @brief Initialize set context.
769 *
770 * @param[in] new Set to initialize.
771 * @param[in] set Arbitrary initialized set.
772 */
773static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200774set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200775{
776 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200777 if (set) {
778 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200779 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100780 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200781 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100782 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200783 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200784 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200785 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200786 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200787}
788
789/**
790 * @brief Create a deep copy of a set.
791 *
792 * @param[in] set Set to copy.
793 * @return Copy of @p set.
794 */
795static struct lyxp_set *
796set_copy(struct lyxp_set *set)
797{
798 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100799 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800
801 if (!set) {
802 return NULL;
803 }
804
805 ret = malloc(sizeof *ret);
806 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
807 set_init(ret, set);
808
809 if (set->type == LYXP_SET_SCNODE_SET) {
810 ret->type = set->type;
811
812 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100813 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
814 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200815 uint32_t idx;
816 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
817 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100818 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200819 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200820 lyxp_set_free(ret);
821 return NULL;
822 }
Michal Vaskoba716542019-12-16 10:01:58 +0100823 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200824 }
825 }
826 } else if (set->type == LYXP_SET_NODE_SET) {
827 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200828 if (set->used) {
829 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
830 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
831 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
832 } else {
833 ret->val.nodes = NULL;
834 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200835
836 ret->used = ret->size = set->used;
837 ret->ctx_pos = set->ctx_pos;
838 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100839 if (set->ht) {
840 ret->ht = lyht_dup(set->ht);
841 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200842 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200843 memcpy(ret, set, sizeof *ret);
844 if (set->type == LYXP_SET_STRING) {
845 ret->val.str = strdup(set->val.str);
846 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
847 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200848 }
849
850 return ret;
851}
852
853/**
854 * @brief Fill XPath set with a string. Any current data are disposed of.
855 *
856 * @param[in] set Set to fill.
857 * @param[in] string String to fill into \p set.
858 * @param[in] str_len Length of \p string. 0 is a valid value!
859 */
860static void
861set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
862{
Michal Vaskod3678892020-05-21 10:06:58 +0200863 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200864
865 set->type = LYXP_SET_STRING;
866 if ((str_len == 0) && (string[0] != '\0')) {
867 string = "";
868 }
869 set->val.str = strndup(string, str_len);
870}
871
872/**
873 * @brief Fill XPath set with a number. Any current data are disposed of.
874 *
875 * @param[in] set Set to fill.
876 * @param[in] number Number to fill into \p set.
877 */
878static void
879set_fill_number(struct lyxp_set *set, long double number)
880{
Michal Vaskod3678892020-05-21 10:06:58 +0200881 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200882
883 set->type = LYXP_SET_NUMBER;
884 set->val.num = number;
885}
886
887/**
888 * @brief Fill XPath set with a boolean. Any current data are disposed of.
889 *
890 * @param[in] set Set to fill.
891 * @param[in] boolean Boolean to fill into \p set.
892 */
893static void
Radek Krejci857189e2020-09-01 13:26:36 +0200894set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200895{
Michal Vaskod3678892020-05-21 10:06:58 +0200896 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200897
898 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200899 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200900}
901
902/**
903 * @brief Fill XPath set with the value from another set (deep assign).
904 * Any current data are disposed of.
905 *
906 * @param[in] trg Set to fill.
907 * @param[in] src Source set to copy into \p trg.
908 */
909static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200910set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200911{
912 if (!trg || !src) {
913 return;
914 }
915
916 if (trg->type == LYXP_SET_NODE_SET) {
917 free(trg->val.nodes);
918 } else if (trg->type == LYXP_SET_STRING) {
919 free(trg->val.str);
920 }
921 set_init(trg, src);
922
923 if (src->type == LYXP_SET_SCNODE_SET) {
924 trg->type = LYXP_SET_SCNODE_SET;
925 trg->used = src->used;
926 trg->size = src->used;
927
Michal Vasko08e9b112021-06-11 15:41:17 +0200928 if (trg->size) {
929 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
930 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
931 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
932 } else {
933 trg->val.scnodes = NULL;
934 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200935 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200936 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200937 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200938 set_fill_number(trg, src->val.num);
939 } else if (src->type == LYXP_SET_STRING) {
940 set_fill_string(trg, src->val.str, strlen(src->val.str));
941 } else {
942 if (trg->type == LYXP_SET_NODE_SET) {
943 free(trg->val.nodes);
944 } else if (trg->type == LYXP_SET_STRING) {
945 free(trg->val.str);
946 }
947
Michal Vaskod3678892020-05-21 10:06:58 +0200948 assert(src->type == LYXP_SET_NODE_SET);
949
950 trg->type = LYXP_SET_NODE_SET;
951 trg->used = src->used;
952 trg->size = src->used;
953 trg->ctx_pos = src->ctx_pos;
954 trg->ctx_size = src->ctx_size;
955
Michal Vasko08e9b112021-06-11 15:41:17 +0200956 if (trg->size) {
957 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
958 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
959 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
960 } else {
961 trg->val.nodes = NULL;
962 }
Michal Vaskod3678892020-05-21 10:06:58 +0200963 if (src->ht) {
964 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200965 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200966 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968 }
969}
970
971/**
972 * @brief Clear context of all schema nodes.
973 *
974 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200975 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200976 */
977static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200978set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200979{
980 uint32_t i;
981
982 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100983 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200984 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100985 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
986 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200987 }
988 }
989}
990
991/**
992 * @brief Remove a node from a set. Removing last node changes
993 * set into LYXP_SET_EMPTY. Context position aware.
994 *
995 * @param[in] set Set to use.
996 * @param[in] idx Index from @p set of the node to be removed.
997 */
998static void
999set_remove_node(struct lyxp_set *set, uint32_t idx)
1000{
1001 assert(set && (set->type == LYXP_SET_NODE_SET));
1002 assert(idx < set->used);
1003
1004 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1005
1006 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001007 if (idx < set->used) {
1008 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1009 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001010 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001011 }
1012}
1013
1014/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001015 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001016 *
1017 * @param[in] set Set to use.
1018 * @param[in] idx Index from @p set of the node to be removed.
1019 */
1020static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001021set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001022{
1023 assert(set && (set->type == LYXP_SET_NODE_SET));
1024 assert(idx < set->used);
1025
Michal Vasko2caefc12019-11-14 16:07:56 +01001026 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1027 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1028 }
1029 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001030}
1031
1032/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001033 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001034 * set into LYXP_SET_EMPTY. Context position aware.
1035 *
1036 * @param[in] set Set to consolidate.
1037 */
1038static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001039set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001040{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001041 uint32_t i, orig_used, end = 0;
1042 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001043
Michal Vaskod3678892020-05-21 10:06:58 +02001044 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001045
1046 orig_used = set->used;
1047 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001048 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001049 start = -1;
1050 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001051 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001052 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001053 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001054 end = i;
1055 ++i;
1056 break;
1057 }
1058
1059 ++i;
1060 if (i == orig_used) {
1061 end = i;
1062 }
1063 } while (i < orig_used);
1064
1065 if (start > -1) {
1066 /* move the whole chunk of valid nodes together */
1067 if (set->used != (unsigned)start) {
1068 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1069 }
1070 set->used += end - start;
1071 }
1072 }
Michal Vasko57eab132019-09-24 11:46:26 +02001073}
1074
1075/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001076 * @brief Check for duplicates in a node set.
1077 *
1078 * @param[in] set Set to check.
1079 * @param[in] node Node to look for in @p set.
1080 * @param[in] node_type Type of @p node.
1081 * @param[in] skip_idx Index from @p set to skip.
1082 * @return LY_ERR
1083 */
1084static LY_ERR
1085set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1086{
1087 uint32_t i;
1088
Michal Vasko2caefc12019-11-14 16:07:56 +01001089 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001090 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1091 }
1092
1093 for (i = 0; i < set->used; ++i) {
1094 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1095 continue;
1096 }
1097
1098 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1099 return LY_EEXIST;
1100 }
1101 }
1102
1103 return LY_SUCCESS;
1104}
1105
Radek Krejci857189e2020-09-01 13:26:36 +02001106ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001107lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1108 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001109{
1110 uint32_t i;
1111
1112 for (i = 0; i < set->used; ++i) {
1113 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1114 continue;
1115 }
1116
1117 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001118 if (index_p) {
1119 *index_p = i;
1120 }
1121 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001122 }
1123 }
1124
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001125 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001126}
1127
Michal Vaskoecd62de2019-11-13 12:35:11 +01001128void
1129lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001130{
1131 uint32_t orig_used, i, j;
1132
Michal Vaskod3678892020-05-21 10:06:58 +02001133 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001134
Michal Vaskod3678892020-05-21 10:06:58 +02001135 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001136 return;
1137 }
1138
Michal Vaskod3678892020-05-21 10:06:58 +02001139 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001140 /* release hidden allocated data (lyxp_set.size) */
1141 lyxp_set_free_content(set1);
1142 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001143 memcpy(set1, set2, sizeof *set1);
1144 return;
1145 }
1146
1147 if (set1->used + set2->used > set1->size) {
1148 set1->size = set1->used + set2->used;
1149 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1150 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1151 }
1152
1153 orig_used = set1->used;
1154
1155 for (i = 0; i < set2->used; ++i) {
1156 for (j = 0; j < orig_used; ++j) {
1157 /* detect duplicities */
1158 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1159 break;
1160 }
1161 }
1162
Michal Vasko0587bce2020-12-10 12:19:21 +01001163 if (j < orig_used) {
1164 /* node is there, but update its status if needed */
1165 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1166 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001167 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1168 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1169 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001170 }
1171 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001172 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1173 ++set1->used;
1174 }
1175 }
1176
Michal Vaskod3678892020-05-21 10:06:58 +02001177 lyxp_set_free_content(set2);
1178 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001179}
1180
1181/**
1182 * @brief Insert a node into a set. Context position aware.
1183 *
1184 * @param[in] set Set to use.
1185 * @param[in] node Node to insert to @p set.
1186 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1187 * @param[in] node_type Node type of @p node.
1188 * @param[in] idx Index in @p set to insert into.
1189 */
1190static void
1191set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1192{
Michal Vaskod3678892020-05-21 10:06:58 +02001193 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001194
Michal Vaskod3678892020-05-21 10:06:58 +02001195 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001196 /* first item */
1197 if (idx) {
1198 /* no real harm done, but it is a bug */
1199 LOGINT(set->ctx);
1200 idx = 0;
1201 }
1202 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1203 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1204 set->type = LYXP_SET_NODE_SET;
1205 set->used = 0;
1206 set->size = LYXP_SET_SIZE_START;
1207 set->ctx_pos = 1;
1208 set->ctx_size = 1;
1209 set->ht = NULL;
1210 } else {
1211 /* not an empty set */
1212 if (set->used == set->size) {
1213
1214 /* set is full */
1215 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1216 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1217 set->size += LYXP_SET_SIZE_STEP;
1218 }
1219
1220 if (idx > set->used) {
1221 LOGINT(set->ctx);
1222 idx = set->used;
1223 }
1224
1225 /* make space for the new node */
1226 if (idx < set->used) {
1227 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1228 }
1229 }
1230
1231 /* finally assign the value */
1232 set->val.nodes[idx].node = (struct lyd_node *)node;
1233 set->val.nodes[idx].type = node_type;
1234 set->val.nodes[idx].pos = pos;
1235 ++set->used;
1236
Michal Vasko2416fdd2021-10-01 11:07:10 +02001237 /* add into hash table */
1238 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239}
1240
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001241LY_ERR
1242lyxp_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 +02001243{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001244 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001245
1246 assert(set->type == LYXP_SET_SCNODE_SET);
1247
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001248 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001249 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001250 } else {
1251 if (set->used == set->size) {
1252 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 +02001253 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001254 set->size += LYXP_SET_SIZE_STEP;
1255 }
1256
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001257 index = set->used;
1258 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1259 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001260 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001261 ++set->used;
1262 }
1263
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001264 if (index_p) {
1265 *index_p = index;
1266 }
1267
1268 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001269}
1270
1271/**
1272 * @brief Replace a node in a set with another. Context position aware.
1273 *
1274 * @param[in] set Set to use.
1275 * @param[in] node Node to insert to @p set.
1276 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1277 * @param[in] node_type Node type of @p node.
1278 * @param[in] idx Index in @p set of the node to replace.
1279 */
1280static void
1281set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1282{
1283 assert(set && (idx < set->used));
1284
Michal Vasko2caefc12019-11-14 16:07:56 +01001285 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1286 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1287 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288 set->val.nodes[idx].node = (struct lyd_node *)node;
1289 set->val.nodes[idx].type = node_type;
1290 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001291 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1292 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1293 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001294}
1295
1296/**
1297 * @brief Set all nodes with ctx 1 to a new unique context value.
1298 *
1299 * @param[in] set Set to modify.
1300 * @return New context value.
1301 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001302static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001303set_scnode_new_in_ctx(struct lyxp_set *set)
1304{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001305 uint32_t i;
1306 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001307
1308 assert(set->type == LYXP_SET_SCNODE_SET);
1309
Radek Krejcif13b87b2020-12-01 22:02:17 +01001310 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001311retry:
1312 for (i = 0; i < set->used; ++i) {
1313 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1314 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1315 goto retry;
1316 }
1317 }
1318 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001319 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320 set->val.scnodes[i].in_ctx = ret_ctx;
1321 }
1322 }
1323
1324 return ret_ctx;
1325}
1326
1327/**
1328 * @brief Get unique @p node position in the data.
1329 *
1330 * @param[in] node Node to find.
1331 * @param[in] node_type Node type of @p node.
1332 * @param[in] root Root node.
1333 * @param[in] root_type Type of the XPath @p root node.
1334 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1335 * be used to increase efficiency and start the DFS from this node.
1336 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1337 * @return Node position.
1338 */
1339static uint32_t
1340get_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 +02001341 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001342{
Michal Vasko56daf732020-08-10 10:57:18 +02001343 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001344 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001345 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001346
1347 assert(prev && prev_pos && !root->prev->next);
1348
1349 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1350 return 0;
1351 }
1352
1353 if (*prev) {
1354 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001356 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001357 goto dfs_search;
1358 }
1359
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001360 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001361 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001362dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001363 LYD_TREE_DFS_continue = 0;
1364
Michal Vasko56daf732020-08-10 10:57:18 +02001365 if (*prev && !elem) {
1366 /* resume previous DFS */
1367 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1368 LYD_TREE_DFS_continue = 0;
1369 }
1370
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001372 /* skip */
1373 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001374 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001375 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001376 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377 break;
1378 }
Michal Vasko56daf732020-08-10 10:57:18 +02001379 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001380 }
Michal Vasko56daf732020-08-10 10:57:18 +02001381
1382 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001383 }
1384
1385 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001386 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001387 break;
1388 }
1389 }
1390
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001391 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001392 if (!(*prev)) {
1393 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001394 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001395 return 0;
1396 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001397 /* start the search again from the beginning */
1398 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399
Michal Vasko56daf732020-08-10 10:57:18 +02001400 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001401 pos = 1;
1402 goto dfs_search;
1403 }
1404 }
1405
1406 /* remember the last found node for next time */
1407 *prev = node;
1408 *prev_pos = pos;
1409
1410 return pos;
1411}
1412
1413/**
1414 * @brief Assign (fill) missing node positions.
1415 *
1416 * @param[in] set Set to fill positions in.
1417 * @param[in] root Context root node.
1418 * @param[in] root_type Context root type.
1419 * @return LY_ERR
1420 */
1421static LY_ERR
1422set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1423{
1424 const struct lyd_node *prev = NULL, *tmp_node;
1425 uint32_t i, tmp_pos = 0;
1426
1427 for (i = 0; i < set->used; ++i) {
1428 if (!set->val.nodes[i].pos) {
1429 tmp_node = NULL;
1430 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 case LYXP_NODE_META:
1432 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 if (!tmp_node) {
1434 LOGINT_RET(root->schema->module->ctx);
1435 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001436 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 case LYXP_NODE_ELEM:
1438 case LYXP_NODE_TEXT:
1439 if (!tmp_node) {
1440 tmp_node = set->val.nodes[i].node;
1441 }
1442 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1443 break;
1444 default:
1445 /* all roots have position 0 */
1446 break;
1447 }
1448 }
1449 }
1450
1451 return LY_SUCCESS;
1452}
1453
1454/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001455 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001456 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001457 * @param[in] meta Metadata to use.
1458 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459 */
1460static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001461get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001462{
1463 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001464 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001465
Michal Vasko9f96a052020-03-10 09:41:45 +01001466 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001467 ++pos;
1468 }
1469
Michal Vasko9f96a052020-03-10 09:41:45 +01001470 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001471 return pos;
1472}
1473
1474/**
1475 * @brief Compare 2 nodes in respect to XPath document order.
1476 *
1477 * @param[in] item1 1st node.
1478 * @param[in] item2 2nd node.
1479 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1480 */
1481static int
1482set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1483{
Michal Vasko9f96a052020-03-10 09:41:45 +01001484 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001485
1486 if (item1->pos < item2->pos) {
1487 return -1;
1488 }
1489
1490 if (item1->pos > item2->pos) {
1491 return 1;
1492 }
1493
1494 /* node positions are equal, the fun case */
1495
1496 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1497 /* special case since text nodes are actually saved as their parents */
1498 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1499 if (item1->type == LYXP_NODE_ELEM) {
1500 assert(item2->type == LYXP_NODE_TEXT);
1501 return -1;
1502 } else {
1503 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1504 return 1;
1505 }
1506 }
1507
Michal Vasko9f96a052020-03-10 09:41:45 +01001508 /* we need meta positions now */
1509 if (item1->type == LYXP_NODE_META) {
1510 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001511 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001512 if (item2->type == LYXP_NODE_META) {
1513 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 }
1515
Michal Vasko9f96a052020-03-10 09:41:45 +01001516 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 /* check for duplicates */
1518 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001519 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001520 return 0;
1521 }
1522
Michal Vasko9f96a052020-03-10 09:41:45 +01001523 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001524 /* elem is always first, 2nd node is after it */
1525 if (item1->type == LYXP_NODE_ELEM) {
1526 assert(item2->type != LYXP_NODE_ELEM);
1527 return -1;
1528 }
1529
Michal Vasko9f96a052020-03-10 09:41:45 +01001530 /* 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 +02001531 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001532 if (((item1->type == LYXP_NODE_TEXT) &&
1533 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1534 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1535 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1536 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001537 return 1;
1538 }
1539
Michal Vasko9f96a052020-03-10 09:41:45 +01001540 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001541 /* 2nd is after 1st */
1542 return -1;
1543}
1544
1545/**
1546 * @brief Set cast for comparisons.
1547 *
1548 * @param[in] trg Target set to cast source into.
1549 * @param[in] src Source set.
1550 * @param[in] type Target set type.
1551 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001552 * @return LY_ERR
1553 */
1554static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001555set_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 +02001556{
1557 assert(src->type == LYXP_SET_NODE_SET);
1558
1559 set_init(trg, src);
1560
1561 /* insert node into target set */
1562 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1563
1564 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001565 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001566}
1567
Michal Vasko4c7763f2020-07-27 17:40:37 +02001568/**
1569 * @brief Set content canonization for comparisons.
1570 *
1571 * @param[in] trg Target set to put the canononized source into.
1572 * @param[in] src Source set.
1573 * @param[in] xp_node Source XPath node/meta to use for canonization.
1574 * @return LY_ERR
1575 */
1576static LY_ERR
1577set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1578{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001579 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001580 struct lyd_value val;
1581 struct ly_err_item *err = NULL;
1582 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001583 LY_ERR rc;
1584
1585 /* is there anything to canonize even? */
1586 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1587 /* do we have a type to use for canonization? */
1588 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1589 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1590 } else if (xp_node->type == LYXP_NODE_META) {
1591 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1592 }
1593 }
1594 if (!type) {
1595 goto fill;
1596 }
1597
1598 if (src->type == LYXP_SET_NUMBER) {
1599 /* canonize number */
1600 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1601 LOGMEM(src->ctx);
1602 return LY_EMEM;
1603 }
1604 } else {
1605 /* canonize string */
1606 str = strdup(src->val.str);
1607 }
1608
1609 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001610 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 +01001611 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001612 ly_err_free(err);
1613 if (rc) {
1614 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001615 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001616 goto fill;
1617 }
1618
Michal Vasko4c7763f2020-07-27 17:40:37 +02001619 /* use the canonized value */
1620 set_init(trg, src);
1621 trg->type = src->type;
1622 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001623 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001624 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1625 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001626 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001627 }
1628 type->plugin->free(src->ctx, &val);
1629 return LY_SUCCESS;
1630
1631fill:
1632 /* no canonization needed/possible */
1633 set_fill_set(trg, src);
1634 return LY_SUCCESS;
1635}
1636
Michal Vasko03ff5a72019-09-11 13:49:33 +02001637#ifndef NDEBUG
1638
1639/**
1640 * @brief Bubble sort @p set into XPath document order.
1641 * Context position aware. Unused in the 'Release' build target.
1642 *
1643 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001644 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1645 */
1646static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001647set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001648{
1649 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001650 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001651 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001652 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001653 struct lyxp_set_node item;
1654 struct lyxp_set_hash_node hnode;
1655 uint64_t hash;
1656
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001657 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001658 return 0;
1659 }
1660
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001661 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001662 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001663 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001664
1665 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001666 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001667 return -1;
1668 }
1669
1670 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1671 print_set_debug(set);
1672
1673 for (i = 0; i < set->used; ++i) {
1674 inverted = 0;
1675 change = 0;
1676
1677 for (j = 1; j < set->used - i; ++j) {
1678 /* compare node positions */
1679 if (inverted) {
1680 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1681 } else {
1682 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1683 }
1684
1685 /* swap if needed */
1686 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1687 change = 1;
1688
1689 item = set->val.nodes[j - 1];
1690 set->val.nodes[j - 1] = set->val.nodes[j];
1691 set->val.nodes[j] = item;
1692 } else {
1693 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1694 inverted = !inverted;
1695 }
1696 }
1697
1698 ++ret;
1699
1700 if (!change) {
1701 break;
1702 }
1703 }
1704
1705 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1706 print_set_debug(set);
1707
1708 /* check node hashes */
1709 if (set->used >= LYD_HT_MIN_ITEMS) {
1710 assert(set->ht);
1711 for (i = 0; i < set->used; ++i) {
1712 hnode.node = set->val.nodes[i].node;
1713 hnode.type = set->val.nodes[i].type;
1714
1715 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1716 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1717 hash = dict_hash_multi(hash, NULL, 0);
1718
1719 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1720 }
1721 }
1722
1723 return ret - 1;
1724}
1725
1726/**
1727 * @brief Remove duplicate entries in a sorted node set.
1728 *
1729 * @param[in] set Sorted set to check.
1730 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1731 */
1732static LY_ERR
1733set_sorted_dup_node_clean(struct lyxp_set *set)
1734{
1735 uint32_t i = 0;
1736 LY_ERR ret = LY_SUCCESS;
1737
1738 if (set->used > 1) {
1739 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001740 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1741 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001742 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001743 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001744 }
Michal Vasko57eab132019-09-24 11:46:26 +02001745 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001746 }
1747 }
1748
Michal Vasko2caefc12019-11-14 16:07:56 +01001749 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750 return ret;
1751}
1752
1753#endif
1754
1755/**
1756 * @brief Merge 2 sorted sets into one.
1757 *
1758 * @param[in,out] trg Set to merge into. Duplicates are removed.
1759 * @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 +02001760 * @return LY_ERR
1761 */
1762static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001763set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001764{
1765 uint32_t i, j, k, count, dup_count;
1766 int cmp;
1767 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001768
Michal Vaskod3678892020-05-21 10:06:58 +02001769 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001770 return LY_EINVAL;
1771 }
1772
Michal Vaskod3678892020-05-21 10:06:58 +02001773 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001774 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001775 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001776 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001777 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001778 return LY_SUCCESS;
1779 }
1780
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001781 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001782 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001783 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001784
1785 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001786 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001787 return LY_EINT;
1788 }
1789
1790#ifndef NDEBUG
1791 LOGDBG(LY_LDGXPATH, "MERGE target");
1792 print_set_debug(trg);
1793 LOGDBG(LY_LDGXPATH, "MERGE source");
1794 print_set_debug(src);
1795#endif
1796
1797 /* make memory for the merge (duplicates are not detected yet, so space
1798 * will likely be wasted on them, too bad) */
1799 if (trg->size - trg->used < src->used) {
1800 trg->size = trg->used + src->used;
1801
1802 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1803 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1804 }
1805
1806 i = 0;
1807 j = 0;
1808 count = 0;
1809 dup_count = 0;
1810 do {
1811 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1812 if (!cmp) {
1813 if (!count) {
1814 /* duplicate, just skip it */
1815 ++i;
1816 ++j;
1817 } else {
1818 /* we are copying something already, so let's copy the duplicate too,
1819 * we are hoping that afterwards there are some more nodes to
1820 * copy and this way we can copy them all together */
1821 ++count;
1822 ++dup_count;
1823 ++i;
1824 ++j;
1825 }
1826 } else if (cmp < 0) {
1827 /* inserting src node into trg, just remember it for now */
1828 ++count;
1829 ++i;
1830
1831 /* insert the hash now */
1832 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1833 } else if (count) {
1834copy_nodes:
1835 /* time to actually copy the nodes, we have found the largest block of nodes */
1836 memmove(&trg->val.nodes[j + (count - dup_count)],
1837 &trg->val.nodes[j],
1838 (trg->used - j) * sizeof *trg->val.nodes);
1839 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1840
1841 trg->used += count - dup_count;
1842 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1843 j += count - dup_count;
1844
1845 count = 0;
1846 dup_count = 0;
1847 } else {
1848 ++j;
1849 }
1850 } while ((i < src->used) && (j < trg->used));
1851
1852 if ((i < src->used) || count) {
1853 /* insert all the hashes first */
1854 for (k = i; k < src->used; ++k) {
1855 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1856 }
1857
1858 /* loop ended, but we need to copy something at trg end */
1859 count += src->used - i;
1860 i = src->used;
1861 goto copy_nodes;
1862 }
1863
1864 /* we are inserting hashes before the actual node insert, which causes
1865 * situations when there were initially not enough items for a hash table,
1866 * but even after some were inserted, hash table was not created (during
1867 * insertion the number of items is not updated yet) */
1868 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1869 set_insert_node_hash(trg, NULL, 0);
1870 }
1871
1872#ifndef NDEBUG
1873 LOGDBG(LY_LDGXPATH, "MERGE result");
1874 print_set_debug(trg);
1875#endif
1876
Michal Vaskod3678892020-05-21 10:06:58 +02001877 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001878 return LY_SUCCESS;
1879}
1880
Michal Vasko14676352020-05-29 11:35:55 +02001881LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001882lyxp_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 +02001883{
Michal Vasko004d3152020-06-11 19:59:22 +02001884 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001885 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001886 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001887 }
Michal Vasko14676352020-05-29 11:35:55 +02001888 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001889 }
1890
Michal Vasko004d3152020-06-11 19:59:22 +02001891 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001892 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001893 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001894 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001895 }
Michal Vasko14676352020-05-29 11:35:55 +02001896 return LY_ENOT;
1897 }
1898
1899 return LY_SUCCESS;
1900}
1901
Michal Vasko004d3152020-06-11 19:59:22 +02001902LY_ERR
1903lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1904{
1905 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1906
1907 /* skip the token */
1908 ++(*tok_idx);
1909
1910 return LY_SUCCESS;
1911}
1912
Michal Vasko14676352020-05-29 11:35:55 +02001913/* just like lyxp_check_token() but tests for 2 tokens */
1914static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001915exp_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 +02001916 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001917{
Michal Vasko004d3152020-06-11 19:59:22 +02001918 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001919 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001920 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001921 }
1922 return LY_EINCOMPLETE;
1923 }
1924
Michal Vasko004d3152020-06-11 19:59:22 +02001925 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001926 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001927 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001928 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001929 }
1930 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 }
1932
1933 return LY_SUCCESS;
1934}
1935
Michal Vasko4911eeb2021-06-28 11:23:05 +02001936LY_ERR
1937lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1,
1938 enum lyxp_token want_tok2)
1939{
1940 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1941
1942 /* skip the token */
1943 ++(*tok_idx);
1944
1945 return LY_SUCCESS;
1946}
1947
Michal Vasko03ff5a72019-09-11 13:49:33 +02001948/**
1949 * @brief Stack operation push on the repeat array.
1950 *
1951 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001952 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001953 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1954 */
1955static void
Michal Vasko004d3152020-06-11 19:59:22 +02001956exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957{
1958 uint16_t i;
1959
Michal Vasko004d3152020-06-11 19:59:22 +02001960 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001961 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001962 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1963 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1964 exp->repeat[tok_idx][i] = repeat_op_idx;
1965 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001966 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001967 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1968 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1969 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001970 }
1971}
1972
1973/**
1974 * @brief Reparse Predicate. Logs directly on error.
1975 *
1976 * [7] Predicate ::= '[' Expr ']'
1977 *
1978 * @param[in] ctx Context for logging.
1979 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001980 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001981 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 * @return LY_ERR
1983 */
1984static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001985reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986{
1987 LY_ERR rc;
1988
Michal Vasko004d3152020-06-11 19:59:22 +02001989 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001991 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992
aPiecekbf968d92021-05-27 14:35:05 +02001993 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 LY_CHECK_RET(rc);
1995
Michal Vasko004d3152020-06-11 19:59:22 +02001996 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001998 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999
2000 return LY_SUCCESS;
2001}
2002
2003/**
2004 * @brief Reparse RelativeLocationPath. Logs directly on error.
2005 *
2006 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2007 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2008 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2009 *
2010 * @param[in] ctx Context for logging.
2011 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002012 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002013 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2015 */
2016static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002017reparse_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 +02002018{
2019 LY_ERR rc;
2020
Michal Vasko004d3152020-06-11 19:59:22 +02002021 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 LY_CHECK_RET(rc);
2023
2024 goto step;
2025 do {
2026 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002027 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028
Michal Vasko004d3152020-06-11 19:59:22 +02002029 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030 LY_CHECK_RET(rc);
2031step:
2032 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002033 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002035 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002036 break;
2037
2038 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002039 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 break;
2041
2042 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002043 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002044
Michal Vasko004d3152020-06-11 19:59:22 +02002045 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002047 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002048 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 +02002049 return LY_EVALID;
2050 }
Radek Krejci0f969882020-08-21 16:56:47 +02002051 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002053 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055
2056 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002057 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058
2059 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002060 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002062 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063
2064 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002065 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002067 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068
2069reparse_predicate:
2070 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002071 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002072 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073 LY_CHECK_RET(rc);
2074 }
2075 break;
2076 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002077 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 +02002078 return LY_EVALID;
2079 }
Michal Vasko004d3152020-06-11 19:59:22 +02002080 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081
2082 return LY_SUCCESS;
2083}
2084
2085/**
2086 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2087 *
2088 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2089 *
2090 * @param[in] ctx Context for logging.
2091 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002092 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002093 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 * @return LY_ERR
2095 */
2096static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002097reparse_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 +02002098{
2099 LY_ERR rc;
2100
Michal Vasko004d3152020-06-11 19:59:22 +02002101 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 +02002102
2103 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002104 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002106 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107
Michal Vasko004d3152020-06-11 19:59:22 +02002108 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 return LY_SUCCESS;
2110 }
Michal Vasko004d3152020-06-11 19:59:22 +02002111 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 case LYXP_TOKEN_DOT:
2113 case LYXP_TOKEN_DDOT:
2114 case LYXP_TOKEN_AT:
2115 case LYXP_TOKEN_NAMETEST:
2116 case LYXP_TOKEN_NODETYPE:
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);
Radek Krejci0f969882020-08-21 16:56:47 +02002119 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 default:
2121 break;
2122 }
2123
Michal Vasko03ff5a72019-09-11 13:49:33 +02002124 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002125 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002126 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002127
aPiecekbf968d92021-05-27 14:35:05 +02002128 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 LY_CHECK_RET(rc);
2130 }
2131
2132 return LY_SUCCESS;
2133}
2134
2135/**
2136 * @brief Reparse FunctionCall. Logs directly on error.
2137 *
2138 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2139 *
2140 * @param[in] ctx Context for logging.
2141 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002142 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002143 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 * @return LY_ERR
2145 */
2146static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002147reparse_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 +02002148{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002149 int8_t min_arg_count = -1;
2150 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 LY_ERR rc;
2153
Michal Vasko004d3152020-06-11 19:59:22 +02002154 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002156 func_tok_idx = *tok_idx;
2157 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002159 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160 min_arg_count = 1;
2161 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002162 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 min_arg_count = 1;
2164 max_arg_count = 1;
2165 }
2166 break;
2167 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002168 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002169 min_arg_count = 1;
2170 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002171 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 min_arg_count = 0;
2173 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002174 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002175 min_arg_count = 0;
2176 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002177 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 min_arg_count = 0;
2179 max_arg_count = 0;
2180 }
2181 break;
2182 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002183 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 min_arg_count = 1;
2185 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002186 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002187 min_arg_count = 0;
2188 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002189 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002190 min_arg_count = 1;
2191 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002192 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002193 min_arg_count = 1;
2194 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002195 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 min_arg_count = 1;
2197 max_arg_count = 1;
2198 }
2199 break;
2200 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002201 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002203 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002204 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002205 min_arg_count = 0;
2206 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002207 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 min_arg_count = 0;
2209 max_arg_count = 1;
2210 }
2211 break;
2212 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002213 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 1;
2215 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002216 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 1;
2218 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002219 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002220 min_arg_count = 0;
2221 max_arg_count = 0;
2222 }
2223 break;
2224 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002225 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 2;
2227 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 0;
2230 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002231 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002232 min_arg_count = 2;
2233 max_arg_count = 2;
2234 }
2235 break;
2236 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002237 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238 min_arg_count = 2;
2239 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002240 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 3;
2242 max_arg_count = 3;
2243 }
2244 break;
2245 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002246 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 0;
2248 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002249 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 min_arg_count = 1;
2251 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002252 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253 min_arg_count = 2;
2254 max_arg_count = 2;
2255 }
2256 break;
2257 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002258 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 min_arg_count = 2;
2260 max_arg_count = 2;
2261 }
2262 break;
2263 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002264 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 min_arg_count = 2;
2266 max_arg_count = 2;
2267 }
2268 break;
2269 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002270 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 min_arg_count = 0;
2272 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002273 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 min_arg_count = 0;
2275 max_arg_count = 1;
2276 }
2277 break;
2278 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002279 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 min_arg_count = 0;
2281 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002282 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 min_arg_count = 2;
2284 max_arg_count = 2;
2285 }
2286 break;
2287 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002288 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002289 min_arg_count = 2;
2290 max_arg_count = 2;
2291 }
2292 break;
2293 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002294 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 min_arg_count = 2;
2296 max_arg_count = 2;
2297 }
2298 break;
2299 }
2300 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002301 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 +02002302 return LY_EINVAL;
2303 }
Michal Vasko004d3152020-06-11 19:59:22 +02002304 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305
2306 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002307 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002309 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310
2311 /* ( Expr ( ',' Expr )* )? */
2312 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002313 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002315 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002317 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 LY_CHECK_RET(rc);
2319 }
Michal Vasko004d3152020-06-11 19:59:22 +02002320 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2321 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002322
2323 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002324 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325 LY_CHECK_RET(rc);
2326 }
2327
2328 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002329 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002331 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332
Radek Krejci857189e2020-09-01 13:26:36 +02002333 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002334 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 +02002335 return LY_EVALID;
2336 }
2337
2338 return LY_SUCCESS;
2339}
2340
2341/**
2342 * @brief Reparse PathExpr. Logs directly on error.
2343 *
2344 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2345 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2346 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2347 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2348 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2349 *
2350 * @param[in] ctx Context for logging.
2351 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002352 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002353 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 * @return LY_ERR
2355 */
2356static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002357reparse_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 +02002358{
2359 LY_ERR rc;
2360
Michal Vasko004d3152020-06-11 19:59:22 +02002361 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002362 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 }
2364
Michal Vasko004d3152020-06-11 19:59:22 +02002365 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 case LYXP_TOKEN_PAR1:
2367 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002368 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369
aPiecekbf968d92021-05-27 14:35:05 +02002370 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371 LY_CHECK_RET(rc);
2372
Michal Vasko004d3152020-06-11 19:59:22 +02002373 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002375 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 case LYXP_TOKEN_DOT:
2378 case LYXP_TOKEN_DDOT:
2379 case LYXP_TOKEN_AT:
2380 case LYXP_TOKEN_NAMETEST:
2381 case LYXP_TOKEN_NODETYPE:
2382 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002383 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384 LY_CHECK_RET(rc);
2385 break;
2386 case LYXP_TOKEN_FUNCNAME:
2387 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002388 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002389 LY_CHECK_RET(rc);
2390 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002391 case LYXP_TOKEN_OPER_PATH:
2392 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002394 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002395 LY_CHECK_RET(rc);
2396 break;
2397 case LYXP_TOKEN_LITERAL:
2398 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002399 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002400 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 case LYXP_TOKEN_NUMBER:
2402 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002403 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002406 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 +02002407 return LY_EVALID;
2408 }
2409
2410 return LY_SUCCESS;
2411
2412predicate:
2413 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002414 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002415 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 LY_CHECK_RET(rc);
2417 }
2418
2419 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002420 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002421
2422 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002423 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424
aPiecekbf968d92021-05-27 14:35:05 +02002425 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426 LY_CHECK_RET(rc);
2427 }
2428
2429 return LY_SUCCESS;
2430}
2431
2432/**
2433 * @brief Reparse UnaryExpr. Logs directly on error.
2434 *
2435 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2436 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2437 *
2438 * @param[in] ctx Context for logging.
2439 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002440 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002441 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002442 * @return LY_ERR
2443 */
2444static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002445reparse_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 +02002446{
2447 uint16_t prev_exp;
2448 LY_ERR rc;
2449
2450 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002451 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002452 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2453 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002455 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002456 }
2457
2458 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002459 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002460 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 LY_CHECK_RET(rc);
2462
2463 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002464 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002466 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002467
aPiecekbf968d92021-05-27 14:35:05 +02002468 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 LY_CHECK_RET(rc);
2470 }
2471
2472 return LY_SUCCESS;
2473}
2474
2475/**
2476 * @brief Reparse AdditiveExpr. Logs directly on error.
2477 *
2478 * [15] AdditiveExpr ::= MultiplicativeExpr
2479 * | AdditiveExpr '+' MultiplicativeExpr
2480 * | AdditiveExpr '-' MultiplicativeExpr
2481 * [16] MultiplicativeExpr ::= UnaryExpr
2482 * | MultiplicativeExpr '*' UnaryExpr
2483 * | MultiplicativeExpr 'div' UnaryExpr
2484 * | MultiplicativeExpr 'mod' UnaryExpr
2485 *
2486 * @param[in] ctx Context for logging.
2487 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002488 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002489 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 * @return LY_ERR
2491 */
2492static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002493reparse_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 +02002494{
2495 uint16_t prev_add_exp, prev_mul_exp;
2496 LY_ERR rc;
2497
Michal Vasko004d3152020-06-11 19:59:22 +02002498 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499 goto reparse_multiplicative_expr;
2500
2501 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002502 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2503 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002505 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506
2507reparse_multiplicative_expr:
2508 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002509 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002510 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 LY_CHECK_RET(rc);
2512
2513 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002514 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2515 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002517 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002518
aPiecekbf968d92021-05-27 14:35:05 +02002519 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520 LY_CHECK_RET(rc);
2521 }
2522 }
2523
2524 return LY_SUCCESS;
2525}
2526
2527/**
2528 * @brief Reparse EqualityExpr. Logs directly on error.
2529 *
2530 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2531 * | EqualityExpr '!=' RelationalExpr
2532 * [14] RelationalExpr ::= AdditiveExpr
2533 * | RelationalExpr '<' AdditiveExpr
2534 * | RelationalExpr '>' AdditiveExpr
2535 * | RelationalExpr '<=' AdditiveExpr
2536 * | RelationalExpr '>=' AdditiveExpr
2537 *
2538 * @param[in] ctx Context for logging.
2539 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002540 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002541 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542 * @return LY_ERR
2543 */
2544static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002545reparse_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 +02002546{
2547 uint16_t prev_eq_exp, prev_rel_exp;
2548 LY_ERR rc;
2549
Michal Vasko004d3152020-06-11 19:59:22 +02002550 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551 goto reparse_additive_expr;
2552
2553 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002554 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002556 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002557
2558reparse_additive_expr:
2559 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002560 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002561 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562 LY_CHECK_RET(rc);
2563
2564 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002565 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002567 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002568
aPiecekbf968d92021-05-27 14:35:05 +02002569 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002570 LY_CHECK_RET(rc);
2571 }
2572 }
2573
2574 return LY_SUCCESS;
2575}
2576
2577/**
2578 * @brief Reparse OrExpr. Logs directly on error.
2579 *
2580 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2581 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2582 *
2583 * @param[in] ctx Context for logging.
2584 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002585 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002586 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002587 * @return LY_ERR
2588 */
2589static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002590reparse_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 +02002591{
2592 uint16_t prev_or_exp, prev_and_exp;
2593 LY_ERR rc;
2594
aPiecekbf968d92021-05-27 14:35:05 +02002595 ++depth;
2596 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2597
Michal Vasko004d3152020-06-11 19:59:22 +02002598 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 goto reparse_equality_expr;
2600
2601 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002602 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 +02002603 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002604 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605
2606reparse_equality_expr:
2607 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002608 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002609 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 LY_CHECK_RET(rc);
2611
2612 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002613 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 +02002614 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002615 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002616
aPiecekbf968d92021-05-27 14:35:05 +02002617 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002618 LY_CHECK_RET(rc);
2619 }
2620 }
2621
2622 return LY_SUCCESS;
2623}
Radek Krejcib1646a92018-11-02 16:08:26 +01002624
2625/**
2626 * @brief Parse NCName.
2627 *
2628 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002629 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002630 */
Radek Krejcid4270262019-01-07 15:07:25 +01002631static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002632parse_ncname(const char *ncname)
2633{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002634 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002635 size_t size;
2636 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002637
2638 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2639 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2640 return len;
2641 }
2642
2643 do {
2644 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002645 if (!*ncname) {
2646 break;
2647 }
Radek Krejcid4270262019-01-07 15:07:25 +01002648 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002649 } while (is_xmlqnamechar(uc) && (uc != ':'));
2650
2651 return len;
2652}
2653
2654/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002655 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002656 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002657 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002658 * @param[in] exp Expression to use.
2659 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002660 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002661 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002662 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002663 */
2664static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002665exp_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 +01002666{
2667 uint32_t prev;
2668
2669 if (exp->used == exp->size) {
2670 prev = exp->size;
2671 exp->size += LYXP_EXPR_SIZE_STEP;
2672 if (prev > exp->size) {
2673 LOGINT(ctx);
2674 return LY_EINT;
2675 }
2676
2677 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2678 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2679 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2680 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2681 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2682 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2683 }
2684
2685 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002686 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002687 exp->tok_len[exp->used] = tok_len;
2688 ++exp->used;
2689 return LY_SUCCESS;
2690}
2691
2692void
Michal Vasko14676352020-05-29 11:35:55 +02002693lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002694{
2695 uint16_t i;
2696
2697 if (!expr) {
2698 return;
2699 }
2700
2701 lydict_remove(ctx, expr->expr);
2702 free(expr->tokens);
2703 free(expr->tok_pos);
2704 free(expr->tok_len);
2705 if (expr->repeat) {
2706 for (i = 0; i < expr->used; ++i) {
2707 free(expr->repeat[i]);
2708 }
2709 }
2710 free(expr->repeat);
2711 free(expr);
2712}
2713
Radek Krejcif03a9e22020-09-18 20:09:31 +02002714LY_ERR
2715lyxp_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 +01002716{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002717 LY_ERR ret = LY_SUCCESS;
2718 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002719 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002720 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002721 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002722 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002723
Radek Krejcif03a9e22020-09-18 20:09:31 +02002724 assert(expr_p);
2725
2726 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002727 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002728 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002729 }
2730
2731 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002732 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002733 }
2734 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002735 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002736 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002737 }
2738
2739 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002740 expr = calloc(1, sizeof *expr);
2741 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2742 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2743 expr->used = 0;
2744 expr->size = LYXP_EXPR_SIZE_START;
2745 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2746 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002747
Radek Krejcif03a9e22020-09-18 20:09:31 +02002748 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2749 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002750
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2752 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002753
Michal Vasko004d3152020-06-11 19:59:22 +02002754 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002756
Radek Krejcif03a9e22020-09-18 20:09:31 +02002757 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002758 ++parsed;
2759 }
2760
2761 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002763
2764 /* '(' */
2765 tok_len = 1;
2766 tok_type = LYXP_TOKEN_PAR1;
2767
Radek Krejcif03a9e22020-09-18 20:09:31 +02002768 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002769 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002770 if (((expr->tok_len[expr->used - 1] == 4) &&
2771 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2772 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2773 ((expr->tok_len[expr->used - 1] == 7) &&
2774 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002775 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002776 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002777 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002778 }
2779 prev_function_check = 0;
2780 }
2781
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002783
2784 /* ')' */
2785 tok_len = 1;
2786 tok_type = LYXP_TOKEN_PAR2;
2787
Radek Krejcif03a9e22020-09-18 20:09:31 +02002788 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 /* '[' */
2791 tok_len = 1;
2792 tok_type = LYXP_TOKEN_BRACK1;
2793
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002795
2796 /* ']' */
2797 tok_len = 1;
2798 tok_type = LYXP_TOKEN_BRACK2;
2799
Radek Krejcif03a9e22020-09-18 20:09:31 +02002800 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002801
2802 /* '..' */
2803 tok_len = 2;
2804 tok_type = LYXP_TOKEN_DDOT;
2805
Radek Krejcif03a9e22020-09-18 20:09:31 +02002806 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002807
2808 /* '.' */
2809 tok_len = 1;
2810 tok_type = LYXP_TOKEN_DOT;
2811
Radek Krejcif03a9e22020-09-18 20:09:31 +02002812 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002813
2814 /* '@' */
2815 tok_len = 1;
2816 tok_type = LYXP_TOKEN_AT;
2817
Radek Krejcif03a9e22020-09-18 20:09:31 +02002818 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
2820 /* ',' */
2821 tok_len = 1;
2822 tok_type = LYXP_TOKEN_COMMA;
2823
Radek Krejcif03a9e22020-09-18 20:09:31 +02002824 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002825
2826 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2828 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002829 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002830 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002831 ++tok_len;
2832 tok_type = LYXP_TOKEN_LITERAL;
2833
Radek Krejcif03a9e22020-09-18 20:09:31 +02002834 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002835
2836 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002837 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2838 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002839 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002840 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002841 ++tok_len;
2842 tok_type = LYXP_TOKEN_LITERAL;
2843
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002845
2846 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2848 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002849 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002850 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002851 }
2852 tok_type = LYXP_TOKEN_NUMBER;
2853
Radek Krejcif03a9e22020-09-18 20:09:31 +02002854 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002855
2856 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002857 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002858 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002859 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002860 } else {
2861 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002862 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002863 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002864
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
Michal Vasko3e48bf32020-06-01 08:39:07 +02002867 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002868 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002869 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2870
Radek Krejcif03a9e22020-09-18 20:09:31 +02002871 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002872
2873 /* Operator '<=', '>=' */
2874 tok_len = 2;
2875 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002876
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002878
2879 /* Operator '|' */
2880 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002881 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
2885 /* Operator '+', '-' */
2886 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002887 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002888
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002890
Michal Vasko3e48bf32020-06-01 08:39:07 +02002891 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002892 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002893 tok_type = LYXP_TOKEN_OPER_EQUAL;
2894
Radek Krejcif03a9e22020-09-18 20:09:31 +02002895 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002896
2897 /* Operator '<', '>' */
2898 tok_len = 1;
2899 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002900
Michal Vasko69730152020-10-09 16:30:07 +02002901 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2902 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2903 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2904 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2905 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2906 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2907 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2908 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2909 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2910 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2911 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2912 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002913
2914 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002915 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002916 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002917 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002918
Radek Krejcif03a9e22020-09-18 20:09:31 +02002919 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002920 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002921 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002924 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002925 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002926
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002928 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002929 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002930
2931 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002932 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 +02002933 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 +02002934 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002935 goto error;
2936 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002937 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002938 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002939 goto error;
2940 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002941 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002942
2943 /* NameTest '*' */
2944 tok_len = 1;
2945 tok_type = LYXP_TOKEN_NAMETEST;
2946
2947 } else {
2948
2949 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002950 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002951 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 +02002952 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002953 tok_len = ncname_len;
2954
Radek Krejcif03a9e22020-09-18 20:09:31 +02002955 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002956 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002957 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002958 ++tok_len;
2959 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002960 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02002961 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 +02002962 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002963 tok_len += ncname_len;
2964 }
2965 /* remove old flag to prevent ambiguities */
2966 prev_function_check = 0;
2967 tok_type = LYXP_TOKEN_NAMETEST;
2968 } else {
2969 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2970 prev_function_check = 1;
2971 tok_type = LYXP_TOKEN_NAMETEST;
2972 }
2973 }
2974
2975 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002976 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002977 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002978 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002979 ++parsed;
2980 }
2981
Radek Krejcif03a9e22020-09-18 20:09:31 +02002982 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002983
Michal Vasko004d3152020-06-11 19:59:22 +02002984 if (reparse) {
2985 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002986 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2987 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002988
Michal Vasko004d3152020-06-11 19:59:22 +02002989 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002990 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002991 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002992 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002993 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002994 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002995 goto error;
2996 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002997 }
2998
Radek Krejcif03a9e22020-09-18 20:09:31 +02002999 print_expr_struct_debug(expr);
3000 *expr_p = expr;
3001 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003002
3003error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003004 lyxp_expr_free(ctx, expr);
3005 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003006}
3007
Michal Vasko1734be92020-09-22 08:55:10 +02003008LY_ERR
3009lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003010{
Michal Vasko1734be92020-09-22 08:55:10 +02003011 LY_ERR ret = LY_SUCCESS;
3012 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003013 uint32_t i, j;
3014
Michal Vasko7f45cf22020-10-01 12:49:44 +02003015 if (!exp) {
3016 goto cleanup;
3017 }
3018
Michal Vasko004d3152020-06-11 19:59:22 +02003019 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003020 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003021
Michal Vasko08e9b112021-06-11 15:41:17 +02003022 if (exp->used) {
3023 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3024 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3025 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003026
Michal Vasko08e9b112021-06-11 15:41:17 +02003027 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3028 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3029 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003030
Michal Vasko08e9b112021-06-11 15:41:17 +02003031 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3032 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3033 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003034
Michal Vasko08e9b112021-06-11 15:41:17 +02003035 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3036 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3037 for (i = 0; i < exp->used; ++i) {
3038 if (!exp->repeat[i]) {
3039 dup->repeat[i] = NULL;
3040 } else {
3041 for (j = 0; exp->repeat[i][j]; ++j) {}
3042 /* the ending 0 as well */
3043 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003044
Michal Vasko08e9b112021-06-11 15:41:17 +02003045 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3046 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3047 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3048 dup->repeat[i][j - 1] = 0;
3049 }
Michal Vasko004d3152020-06-11 19:59:22 +02003050 }
3051 }
3052
3053 dup->used = exp->used;
3054 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003055 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003056
Michal Vasko1734be92020-09-22 08:55:10 +02003057cleanup:
3058 if (ret) {
3059 lyxp_expr_free(ctx, dup);
3060 } else {
3061 *dup_p = dup;
3062 }
3063 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003064}
3065
Michal Vasko03ff5a72019-09-11 13:49:33 +02003066/**
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
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005322 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005324 * XPath @p set is expected to be a (sc)node set!
5325 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005326 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5327 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005328 * @param[in] set Set with general XPath context.
5329 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005330 * @param[out] moveto_mod Expected module of a matching node.
5331 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005332 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005333static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005334moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5335 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005337 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005338 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005339 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340
Michal Vasko2104e9f2020-03-06 08:23:25 +01005341 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5342
Michal Vasko6346ece2019-09-24 13:12:53 +02005343 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5344 /* specific module */
5345 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005346 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005347
Michal Vasko004d3152020-06-11 19:59:22 +02005348 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005349 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005350 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005351 return LY_EVALID;
5352 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005353
Michal Vasko6346ece2019-09-24 13:12:53 +02005354 *qname += pref_len + 1;
5355 *qname_len -= pref_len + 1;
5356 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5357 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005358 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005360 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005361 case LY_VALUE_SCHEMA:
5362 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005363 /* current module */
5364 mod = set->cur_mod;
5365 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005366 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005367 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005368 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005369 /* inherit parent (context node) module */
5370 if (ctx_scnode) {
5371 mod = ctx_scnode->module;
5372 } else {
5373 mod = NULL;
5374 }
5375 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005376 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005377 /* all nodes need to be prefixed */
5378 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5379 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 }
5382
Michal Vasko6346ece2019-09-24 13:12:53 +02005383 *moveto_mod = mod;
5384 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385}
5386
5387/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005388 * @brief Move context @p set to the root. Handles absolute path.
5389 * Result is LYXP_SET_NODE_SET.
5390 *
5391 * @param[in,out] set Set to use.
5392 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005393 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005395static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005396moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397{
aPiecek8b0cc152021-05-31 16:40:31 +02005398 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005399
5400 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005401 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005402 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005403 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005404 set->type = LYXP_SET_NODE_SET;
5405 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005406 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005408
5409 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005410}
5411
5412/**
5413 * @brief Check @p node as a part of NameTest processing.
5414 *
5415 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005416 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005417 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005418 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005419 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005420 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5421 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005422 */
5423static LY_ERR
Michal Vaskodb08ce52021-10-06 08:57:49 +02005424moveto_node_check(const struct lyd_node *node, const struct lyxp_set *set, const char *node_name,
5425 const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005426{
Michal Vaskodca9f122021-07-16 13:56:22 +02005427 if (!node->schema) {
5428 /* opaque node never matches */
5429 return LY_ENOT;
5430 }
5431
Michal Vasko03ff5a72019-09-11 13:49:33 +02005432 /* module check */
5433 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005434 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005435 }
5436
Michal Vasko5c4e5892019-11-14 12:31:38 +01005437 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005438 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005440 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5441 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005442 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443 }
5444
5445 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005446 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005447 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005448 }
5449
Michal Vaskoa1424542019-11-14 16:08:52 +01005450 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005451 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005452 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005453 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454
5455 /* match */
5456 return LY_SUCCESS;
5457}
5458
5459/**
5460 * @brief Check @p node as a part of schema NameTest processing.
5461 *
5462 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005463 * @param[in] ctx_scnode Context node.
5464 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005465 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005466 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005467 * @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 +02005468 */
5469static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005470moveto_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 +02005471 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005473 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5474 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005475 case LY_VALUE_SCHEMA:
5476 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005477 /* use current module */
5478 moveto_mod = set->cur_mod;
5479 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005480 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005481 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005482 /* inherit module of the context node, if any */
5483 if (ctx_scnode) {
5484 moveto_mod = ctx_scnode->module;
5485 }
5486 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005487 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005488 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005489 /* not defined */
5490 LOGINT(set->ctx);
5491 return LY_EINVAL;
5492 }
5493 }
5494
Michal Vasko03ff5a72019-09-11 13:49:33 +02005495 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005496 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005497 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005498 }
5499
5500 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005501 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005502 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005503 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005504 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005505 }
5506
5507 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005508 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005509 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005510 }
5511
5512 /* match */
5513 return LY_SUCCESS;
5514}
5515
5516/**
Michal Vaskod3678892020-05-21 10:06:58 +02005517 * @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 +02005518 *
5519 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005520 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005521 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005522 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005523 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5524 */
5525static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005526moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005527{
Michal Vaskof03ed032020-03-04 13:31:44 +01005528 uint32_t i;
Michal Vaskodb08ce52021-10-06 08:57:49 +02005529 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005530 LY_ERR rc;
5531
aPiecek8b0cc152021-05-31 16:40:31 +02005532 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005533 return LY_SUCCESS;
5534 }
5535
5536 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005537 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005538 return LY_EVALID;
5539 }
5540
Michal Vasko03ff5a72019-09-11 13:49:33 +02005541 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005542 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005543
5544 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 +01005545 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005546
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005547 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005548 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005549 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005550 /* search in children */
Michal Vaskodb08ce52021-10-06 08:57:49 +02005551 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005552 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005553
Michal Vaskod3678892020-05-21 10:06:58 +02005554 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005555 rc = moveto_node_check(sub, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005556 if (rc == LY_SUCCESS) {
5557 if (!replaced) {
5558 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5559 replaced = 1;
5560 } else {
5561 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005562 }
Michal Vaskod3678892020-05-21 10:06:58 +02005563 ++i;
5564 } else if (rc == LY_EINCOMPLETE) {
5565 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005566 }
5567 }
5568
5569 if (!replaced) {
5570 /* no match */
5571 set_remove_node(set, i);
5572 }
5573 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005574
5575 return LY_SUCCESS;
5576}
5577
5578/**
Michal Vaskod3678892020-05-21 10:06:58 +02005579 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5580 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581 *
5582 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005583 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005584 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskocdad7122020-11-09 21:04:44 +01005585 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005586 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5587 */
5588static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005589moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5590 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005591{
Michal Vasko004d3152020-06-11 19:59:22 +02005592 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005593 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005594 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005595 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005596
Michal Vasko004d3152020-06-11 19:59:22 +02005597 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005598
aPiecek8b0cc152021-05-31 16:40:31 +02005599 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005600 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005601 }
5602
5603 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005604 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005605 ret = LY_EVALID;
5606 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005607 }
5608
5609 /* context check for all the nodes since we have the schema node */
5610 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5611 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005612 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005613 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5614 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005615 lyxp_set_free_content(set);
5616 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005617 }
5618
5619 /* create specific data instance if needed */
5620 if (scnode->nodetype == LYS_LIST) {
5621 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5622 } else if (scnode->nodetype == LYS_LEAFLIST) {
5623 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005624 }
5625
5626 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005627 siblings = NULL;
5628
5629 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5630 assert(!set->val.nodes[i].node);
5631
5632 /* search in all the trees */
5633 siblings = set->tree;
5634 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5635 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005636 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005637 }
5638
5639 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005640 if (inst) {
5641 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005642 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005643 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005644 }
5645
5646 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005647 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005648 ret = LY_EINCOMPLETE;
5649 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005650 }
5651
5652 if (sub) {
5653 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005654 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005655 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005656 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005657 /* no match */
5658 set_remove_node(set, i);
5659 }
5660 }
5661
Michal Vasko004d3152020-06-11 19:59:22 +02005662cleanup:
5663 lyd_free_tree(inst);
5664 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005665}
5666
5667/**
5668 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5669 *
5670 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005671 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005672 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 * @param[in] options XPath options.
5674 * @return LY_ERR
5675 */
5676static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005677moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678{
Radek Krejci857189e2020-09-01 13:26:36 +02005679 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005680 uint32_t getnext_opts;
5681 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005683 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005684 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685
aPiecek8b0cc152021-05-31 16:40:31 +02005686 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005687 return LY_SUCCESS;
5688 }
5689
5690 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005691 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692 return LY_EVALID;
5693 }
5694
Michal Vaskocafad9d2019-11-07 15:20:03 +01005695 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005696 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005697 if (options & LYXP_SCNODE_OUTPUT) {
5698 getnext_opts |= LYS_GETNEXT_OUTPUT;
5699 }
5700
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 orig_used = set->used;
5702 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005703 uint32_t idx;
5704
Radek Krejcif13b87b2020-12-01 22:02:17 +01005705 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5706 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005707 continue;
5708 }
5709
5710 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005711 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005712 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005713 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715
5716 start_parent = set->val.scnodes[i].scnode;
5717
5718 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005719 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005720 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02005722 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005723 iter = NULL;
Michal Vasko919954a2021-07-26 09:21:42 +02005724 /* module may not be implemented or not compiled yet */
5725 while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005726 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005727 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5728
Michal Vasko03ff5a72019-09-11 13:49:33 +02005729 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005730 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005731 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005732 temp_ctx = 1;
5733 }
5734 }
5735 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736 }
5737
Michal Vasko519fd602020-05-26 12:17:39 +02005738 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5739 iter = NULL;
5740 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005741 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005742 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5743
5744 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005745 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 temp_ctx = 1;
5747 }
5748 }
5749 }
5750 }
5751 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752
5753 /* correct temporary in_ctx values */
5754 if (temp_ctx) {
5755 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005756 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5757 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 }
5759 }
5760 }
5761
5762 return LY_SUCCESS;
5763}
5764
5765/**
Michal Vaskod3678892020-05-21 10:06:58 +02005766 * @brief Move context @p set to a node and all its descendants. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
Michal Vasko03ff5a72019-09-11 13:49:33 +02005767 * Context position aware.
5768 *
5769 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005770 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005771 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005772 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005773 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5774 */
5775static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005776moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005777{
5778 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780 struct lyxp_set ret_set;
5781 LY_ERR rc;
5782
aPiecek8b0cc152021-05-31 16:40:31 +02005783 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 return LY_SUCCESS;
5785 }
5786
5787 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005788 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789 return LY_EVALID;
5790 }
5791
Michal Vasko9f96a052020-03-10 09:41:45 +01005792 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskocdad7122020-11-09 21:04:44 +01005793 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005794 LY_CHECK_RET(rc);
5795
Michal Vasko6346ece2019-09-24 13:12:53 +02005796 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005797 set_init(&ret_set, set);
5798 for (i = 0; i < set->used; ++i) {
5799
5800 /* TREE DFS */
5801 start = set->val.nodes[i].node;
5802 for (elem = next = start; elem; elem = next) {
Michal Vaskodb08ce52021-10-06 08:57:49 +02005803 rc = moveto_node_check(elem, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005804 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005805 /* add matching node into result set */
5806 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5807 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5808 /* the node is a duplicate, we'll process it later in the set */
5809 goto skip_children;
5810 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005811 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005812 return rc;
5813 } else if (rc == LY_EINVAL) {
5814 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 }
5816
5817 /* TREE DFS NEXT ELEM */
5818 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005819 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005820 if (!next) {
5821skip_children:
5822 /* no children, so try siblings, but only if it's not the start,
5823 * that is considered to be the root and it's siblings are not traversed */
5824 if (elem != start) {
5825 next = elem->next;
5826 } else {
5827 break;
5828 }
5829 }
5830 while (!next) {
5831 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005832 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833 /* we are done, no next element to process */
5834 break;
5835 }
5836 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005837 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 next = elem->next;
5839 }
5840 }
5841 }
5842
5843 /* make the temporary set the current one */
5844 ret_set.ctx_pos = set->ctx_pos;
5845 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005846 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 memcpy(set, &ret_set, sizeof *set);
5848
5849 return LY_SUCCESS;
5850}
5851
5852/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005853 * @brief Move context @p set to a schema node and all its descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005854 *
5855 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005856 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005857 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858 * @param[in] options XPath options.
5859 * @return LY_ERR
5860 */
5861static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005862moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005863{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005864 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005866 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005867
aPiecek8b0cc152021-05-31 16:40:31 +02005868 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005869 return LY_SUCCESS;
5870 }
5871
5872 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005873 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874 return LY_EVALID;
5875 }
5876
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877 orig_used = set->used;
5878 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005879 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5880 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005881 continue;
5882 }
5883
5884 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005885 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005886 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005887 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005888 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005889
5890 /* TREE DFS */
5891 start = set->val.scnodes[i].scnode;
5892 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005893 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5894 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896 }
5897
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005898 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005899 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005900 uint32_t idx;
5901
5902 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005903 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005904 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005905 /* we will process it later in the set */
5906 goto skip_children;
5907 }
5908 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005909 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005910 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005911 } else if (rc == LY_EINVAL) {
5912 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 }
5914
5915next_iter:
5916 /* TREE DFS NEXT ELEM */
5917 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005918 next = lysc_node_child(elem);
5919 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5920 next = next->next;
5921 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5922 next = next->next;
5923 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 if (!next) {
5925skip_children:
5926 /* no children, so try siblings, but only if it's not the start,
5927 * that is considered to be the root and it's siblings are not traversed */
5928 if (elem != start) {
5929 next = elem->next;
5930 } else {
5931 break;
5932 }
5933 }
5934 while (!next) {
5935 /* no siblings, go back through the parents */
5936 if (elem->parent == start) {
5937 /* we are done, no next element to process */
5938 break;
5939 }
5940 /* parent is already processed, go to its sibling */
5941 elem = elem->parent;
5942 next = elem->next;
5943 }
5944 }
5945 }
5946
5947 return LY_SUCCESS;
5948}
5949
5950/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005951 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005952 * Indirectly context position aware.
5953 *
5954 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005955 * @param[in] mod Matching metadata module, NULL for any.
5956 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005957 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005958 * @return LY_ERR
5959 */
5960static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005961moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005962{
Michal Vasko9f96a052020-03-10 09:41:45 +01005963 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005964
aPiecek8b0cc152021-05-31 16:40:31 +02005965 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005966 return LY_SUCCESS;
5967 }
5968
5969 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005970 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005971 return LY_EVALID;
5972 }
5973
Radek Krejci1deb5be2020-08-26 16:43:36 +02005974 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005975 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005976
5977 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5978 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005979 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005980 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981
5982 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005983 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 continue;
5985 }
5986
Michal Vaskod3678892020-05-21 10:06:58 +02005987 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005988 /* match */
5989 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005990 set->val.meta[i].meta = sub;
5991 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992 /* pos does not change */
5993 replaced = 1;
5994 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005995 set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_META, i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005996 }
5997 ++i;
5998 }
5999 }
6000 }
6001
6002 if (!replaced) {
6003 /* no match */
6004 set_remove_node(set, i);
6005 }
6006 }
6007
6008 return LY_SUCCESS;
6009}
6010
6011/**
6012 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006013 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 *
6015 * @param[in,out] set1 Set to use for the result.
6016 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017 * @return LY_ERR
6018 */
6019static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006020moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021{
6022 LY_ERR rc;
6023
Michal Vaskod3678892020-05-21 10:06:58 +02006024 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006025 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006026 return LY_EVALID;
6027 }
6028
6029 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006030 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006031 return LY_SUCCESS;
6032 }
6033
Michal Vaskod3678892020-05-21 10:06:58 +02006034 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02006035 /* release hidden allocated data (lyxp_set.size) */
6036 lyxp_set_free_content(set1);
6037 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006038 memcpy(set1, set2, sizeof *set1);
6039 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006040 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041 return LY_SUCCESS;
6042 }
6043
6044 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006045 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046
6047 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006048 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049 LY_CHECK_RET(rc);
6050
6051 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006052 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053
6054 return LY_SUCCESS;
6055}
6056
6057/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006058 * @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 +02006059 * Context position aware.
6060 *
6061 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006062 * @param[in] mod Matching metadata module, NULL for any.
6063 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006064 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006065 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6066 */
6067static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006068moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069{
Michal Vasko9f96a052020-03-10 09:41:45 +01006070 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006071 struct lyxp_set *set_all_desc = NULL;
6072 LY_ERR rc;
6073
aPiecek8b0cc152021-05-31 16:40:31 +02006074 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006075 return LY_SUCCESS;
6076 }
6077
6078 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006079 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006080 return LY_EVALID;
6081 }
6082
Michal Vasko03ff5a72019-09-11 13:49:33 +02006083 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6084 * but it likely won't be used much, so it's a waste of time */
6085 /* copy the context */
6086 set_all_desc = set_copy(set);
6087 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006088 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006089 if (rc != LY_SUCCESS) {
6090 lyxp_set_free(set_all_desc);
6091 return rc;
6092 }
6093 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006094 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006095 if (rc != LY_SUCCESS) {
6096 lyxp_set_free(set_all_desc);
6097 return rc;
6098 }
6099 lyxp_set_free(set_all_desc);
6100
Radek Krejci1deb5be2020-08-26 16:43:36 +02006101 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006102 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006103
6104 /* only attributes of an elem can be in the result, skip all the rest,
6105 * we have all attributes qualified in lyd tree */
6106 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006107 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006109 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110 continue;
6111 }
6112
Michal Vaskod3678892020-05-21 10:06:58 +02006113 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006114 /* match */
6115 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006116 set->val.meta[i].meta = sub;
6117 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006118 /* pos does not change */
6119 replaced = 1;
6120 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006121 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 +02006122 }
6123 ++i;
6124 }
6125 }
6126 }
6127
6128 if (!replaced) {
6129 /* no match */
6130 set_remove_node(set, i);
6131 }
6132 }
6133
6134 return LY_SUCCESS;
6135}
6136
6137/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006138 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6139 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006140 *
6141 * @param[in] parent Current parent.
6142 * @param[in] parent_pos Position of @p parent.
6143 * @param[in] parent_type Node type of @p parent.
6144 * @param[in,out] to_set Set to use.
6145 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006146 * @param[in] options XPath options.
6147 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6148 */
6149static LY_ERR
6150moveto_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 +02006151 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006152{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006153 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006154 LY_ERR rc;
6155
6156 switch (parent_type) {
6157 case LYXP_NODE_ROOT:
6158 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006159 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006160
Michal Vasko61ac2f62020-05-25 12:39:51 +02006161 /* add all top-level nodes as elements */
6162 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006163 break;
6164 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006165 /* add just the text node of this term element node */
6166 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006167 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6168 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6169 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006170 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006172
6173 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006174 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006175 break;
6176 default:
6177 LOGINT_RET(parent->schema->module->ctx);
6178 }
6179
Michal Vasko61ac2f62020-05-25 12:39:51 +02006180 /* add all top-level nodes as elements */
6181 LY_LIST_FOR(first, iter) {
6182 /* context check */
6183 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6184 continue;
6185 }
6186
6187 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006188 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006189 return LY_EINCOMPLETE;
6190 }
6191
6192 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6193 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6194
6195 /* also add all the children of this node, recursively */
6196 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6197 LY_CHECK_RET(rc);
6198 }
6199 }
6200
Michal Vasko03ff5a72019-09-11 13:49:33 +02006201 return LY_SUCCESS;
6202}
6203
6204/**
6205 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6206 * (or LYXP_SET_EMPTY). Context position aware.
6207 *
6208 * @param[in,out] set Set to use.
6209 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6210 * @param[in] options XPath options.
6211 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6212 */
6213static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006214moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006215{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006216 struct lyxp_set ret_set;
6217 LY_ERR rc;
6218
aPiecek8b0cc152021-05-31 16:40:31 +02006219 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006220 return LY_SUCCESS;
6221 }
6222
6223 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006224 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006225 return LY_EVALID;
6226 }
6227
6228 /* nothing to do */
6229 if (!all_desc) {
6230 return LY_SUCCESS;
6231 }
6232
Michal Vasko03ff5a72019-09-11 13:49:33 +02006233 /* add all the children, they get added recursively */
6234 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006235 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236 /* copy the current node to tmp */
6237 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6238
6239 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006240 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006241 continue;
6242 }
6243
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244 /* add all the children */
6245 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 +02006246 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006247 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006248 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006249 return rc;
6250 }
6251 }
6252
6253 /* use the temporary set as the current one */
6254 ret_set.ctx_pos = set->ctx_pos;
6255 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006256 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006257 memcpy(set, &ret_set, sizeof *set);
6258
6259 return LY_SUCCESS;
6260}
6261
6262/**
6263 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6264 * (or LYXP_SET_EMPTY).
6265 *
6266 * @param[in,out] set Set to use.
6267 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6268 * @param[in] options XPath options.
6269 * @return LY_ERR
6270 */
6271static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006272moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006274 uint32_t getnext_opts;
6275 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006276 const struct lysc_node *iter, *start_parent;
6277 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006278
aPiecek8b0cc152021-05-31 16:40:31 +02006279 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006280 return LY_SUCCESS;
6281 }
6282
6283 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006284 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006285 return LY_EVALID;
6286 }
6287
Michal Vasko519fd602020-05-26 12:17:39 +02006288 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006289 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006290 if (options & LYXP_SCNODE_OUTPUT) {
6291 getnext_opts |= LYS_GETNEXT_OUTPUT;
6292 }
6293
6294 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006295 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006296 if (!all_desc) {
6297 /* traverse the start node */
6298 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6299 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6300 }
6301 continue;
6302 }
6303
Radek Krejcif13b87b2020-12-01 22:02:17 +01006304 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6305 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006306 continue;
6307 }
6308
Michal Vasko519fd602020-05-26 12:17:39 +02006309 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006310 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006311 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006312 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313 }
6314
Michal Vasko519fd602020-05-26 12:17:39 +02006315 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006316
Michal Vasko519fd602020-05-26 12:17:39 +02006317 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6318 /* it can actually be in any module, it's all <running> */
6319 mod_idx = 0;
Michal Vaskoa51ef072021-07-02 10:40:30 +02006320 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02006321 iter = NULL;
6322 /* module may not be implemented */
6323 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6324 /* context check */
6325 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6326 continue;
6327 }
6328
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006329 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006330 /* throw away the insert index, we want to consider that node again, recursively */
6331 }
6332 }
6333
6334 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6335 iter = NULL;
6336 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006337 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006338 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006339 continue;
6340 }
6341
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006342 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343 }
6344 }
6345 }
6346
6347 return LY_SUCCESS;
6348}
6349
6350/**
6351 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6352 * (or LYXP_SET_EMPTY). Context position aware.
6353 *
6354 * @param[in] set Set to use.
6355 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6356 * @param[in] options XPath options.
6357 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6358 */
6359static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006360moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006361{
6362 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006364 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365
aPiecek8b0cc152021-05-31 16:40:31 +02006366 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006367 return LY_SUCCESS;
6368 }
6369
6370 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006371 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006372 return LY_EVALID;
6373 }
6374
6375 if (all_desc) {
6376 /* <path>//.. == <path>//./.. */
6377 rc = moveto_self(set, 1, options);
6378 LY_CHECK_RET(rc);
6379 }
6380
Radek Krejci1deb5be2020-08-26 16:43:36 +02006381 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006382 node = set->val.nodes[i].node;
6383
6384 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006385 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006386 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6387 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006388 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6389 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006390 if (!new_node) {
6391 LOGINT_RET(set->ctx);
6392 }
6393 } else {
6394 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006395 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006396 continue;
6397 }
6398
Michal Vaskoa1424542019-11-14 16:08:52 +01006399 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006400 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 +02006401 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006402 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006403
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006404 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006405 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006406 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006407
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006409 /* 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 +02006410 new_type = LYXP_NODE_ELEM;
6411 }
6412
Michal Vasko03ff5a72019-09-11 13:49:33 +02006413 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006414 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006415 } else {
6416 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006417 }
6418 }
6419
Michal Vasko2caefc12019-11-14 16:07:56 +01006420 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006421 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422
6423 return LY_SUCCESS;
6424}
6425
6426/**
6427 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6428 * (or LYXP_SET_EMPTY).
6429 *
6430 * @param[in] set Set to use.
6431 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6432 * @param[in] options XPath options.
6433 * @return LY_ERR
6434 */
6435static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006436moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006437{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006438 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006439 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006440 const struct lysc_node *node, *new_node;
6441 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006442
aPiecek8b0cc152021-05-31 16:40:31 +02006443 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006444 return LY_SUCCESS;
6445 }
6446
6447 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006448 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006449 return LY_EVALID;
6450 }
6451
6452 if (all_desc) {
6453 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006454 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006455 }
6456
Michal Vasko03ff5a72019-09-11 13:49:33 +02006457 orig_used = set->used;
6458 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006459 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6460 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006461 continue;
6462 }
6463
6464 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006465 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006466 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006467 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006468 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006469
6470 node = set->val.scnodes[i].scnode;
6471
6472 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006473 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006474 } else {
6475 /* root does not have a parent */
6476 continue;
6477 }
6478
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006479 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006480 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006481 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006482
Michal Vasko03ff5a72019-09-11 13:49:33 +02006483 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006484 /* 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 +02006485 new_type = LYXP_NODE_ELEM;
6486 }
6487
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006488 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6489 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006490 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491 temp_ctx = 1;
6492 }
6493 }
6494
6495 if (temp_ctx) {
6496 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006497 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6498 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499 }
6500 }
6501 }
6502
6503 return LY_SUCCESS;
6504}
6505
6506/**
6507 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6508 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6509 *
6510 * @param[in,out] set1 Set to use for the result.
6511 * @param[in] set2 Set acting as the second operand for @p op.
6512 * @param[in] op Comparison operator to process.
6513 * @param[in] options XPath options.
6514 * @return LY_ERR
6515 */
6516static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006517moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006518{
6519 /*
6520 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6521 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6522 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6523 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6524 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6525 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6526 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6527 *
6528 * '=' or '!='
6529 * BOOLEAN + BOOLEAN
6530 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6531 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6532 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6533 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6534 * NUMBER + NUMBER
6535 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6536 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6537 * STRING + STRING
6538 *
6539 * '<=', '<', '>=', '>'
6540 * NUMBER + NUMBER
6541 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6542 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6543 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6544 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6545 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6546 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6547 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6548 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6549 */
6550 struct lyxp_set iter1, iter2;
6551 int result;
6552 int64_t i;
6553 LY_ERR rc;
6554
Michal Vasko004d3152020-06-11 19:59:22 +02006555 memset(&iter1, 0, sizeof iter1);
6556 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006557
6558 /* iterative evaluation with node-sets */
6559 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6560 if (set1->type == LYXP_SET_NODE_SET) {
6561 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006562 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006563 switch (set2->type) {
6564 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006565 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566 break;
6567 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006568 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006569 break;
6570 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006571 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006572 break;
6573 }
6574 LY_CHECK_RET(rc);
6575
Michal Vasko4c7763f2020-07-27 17:40:37 +02006576 /* canonize set2 */
6577 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6578
6579 /* compare recursively */
6580 rc = moveto_op_comp(&iter1, &iter2, op, options);
6581 lyxp_set_free_content(&iter2);
6582 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006583
6584 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006585 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586 set_fill_boolean(set1, 1);
6587 return LY_SUCCESS;
6588 }
6589 }
6590 } else {
6591 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006592 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006593 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006594 case LYXP_SET_NUMBER:
6595 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6596 break;
6597 case LYXP_SET_BOOLEAN:
6598 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6599 break;
6600 default:
6601 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6602 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006603 }
6604 LY_CHECK_RET(rc);
6605
Michal Vasko4c7763f2020-07-27 17:40:37 +02006606 /* canonize set1 */
6607 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 +02006608
Michal Vasko4c7763f2020-07-27 17:40:37 +02006609 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006610 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006611 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006612 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613
6614 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006615 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006616 set_fill_boolean(set1, 1);
6617 return LY_SUCCESS;
6618 }
6619 }
6620 }
6621
6622 /* false for all nodes */
6623 set_fill_boolean(set1, 0);
6624 return LY_SUCCESS;
6625 }
6626
6627 /* first convert properly */
6628 if ((op[0] == '=') || (op[0] == '!')) {
6629 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006630 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6631 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006632 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006633 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006634 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006635 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006636 LY_CHECK_RET(rc);
6637 } /* else we have 2 strings */
6638 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006639 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006640 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006641 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006642 LY_CHECK_RET(rc);
6643 }
6644
6645 assert(set1->type == set2->type);
6646
6647 /* compute result */
6648 if (op[0] == '=') {
6649 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006650 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006651 } else if (set1->type == LYXP_SET_NUMBER) {
6652 result = (set1->val.num == set2->val.num);
6653 } else {
6654 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006655 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006656 }
6657 } else if (op[0] == '!') {
6658 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006659 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 } else if (set1->type == LYXP_SET_NUMBER) {
6661 result = (set1->val.num != set2->val.num);
6662 } else {
6663 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006664 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006665 }
6666 } else {
6667 assert(set1->type == LYXP_SET_NUMBER);
6668 if (op[0] == '<') {
6669 if (op[1] == '=') {
6670 result = (set1->val.num <= set2->val.num);
6671 } else {
6672 result = (set1->val.num < set2->val.num);
6673 }
6674 } else {
6675 if (op[1] == '=') {
6676 result = (set1->val.num >= set2->val.num);
6677 } else {
6678 result = (set1->val.num > set2->val.num);
6679 }
6680 }
6681 }
6682
6683 /* assign result */
6684 if (result) {
6685 set_fill_boolean(set1, 1);
6686 } else {
6687 set_fill_boolean(set1, 0);
6688 }
6689
6690 return LY_SUCCESS;
6691}
6692
6693/**
6694 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6695 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6696 *
6697 * @param[in,out] set1 Set to use for the result.
6698 * @param[in] set2 Set acting as the second operand for @p op.
6699 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700 * @return LY_ERR
6701 */
6702static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006703moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006704{
6705 LY_ERR rc;
6706
6707 /* unary '-' */
6708 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006709 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006710 LY_CHECK_RET(rc);
6711 set1->val.num *= -1;
6712 lyxp_set_free(set2);
6713 return LY_SUCCESS;
6714 }
6715
6716 assert(set1 && set2);
6717
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006718 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006720 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721 LY_CHECK_RET(rc);
6722
6723 switch (op[0]) {
6724 /* '+' */
6725 case '+':
6726 set1->val.num += set2->val.num;
6727 break;
6728
6729 /* '-' */
6730 case '-':
6731 set1->val.num -= set2->val.num;
6732 break;
6733
6734 /* '*' */
6735 case '*':
6736 set1->val.num *= set2->val.num;
6737 break;
6738
6739 /* 'div' */
6740 case 'd':
6741 set1->val.num /= set2->val.num;
6742 break;
6743
6744 /* 'mod' */
6745 case 'm':
6746 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6747 break;
6748
6749 default:
6750 LOGINT_RET(set1->ctx);
6751 }
6752
6753 return LY_SUCCESS;
6754}
6755
Michal Vasko03ff5a72019-09-11 13:49:33 +02006756/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006757 * @brief Evaluate Predicate. Logs directly on error.
6758 *
Michal Vaskod3678892020-05-21 10:06:58 +02006759 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006760 *
6761 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006762 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006763 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 * @param[in] options XPath options.
6765 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6766 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6767 */
6768static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006769eval_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 +02006770{
6771 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006772 uint16_t orig_exp;
6773 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006774 int32_t pred_in_ctx;
aPiecekfff4dca2021-10-07 10:59:53 +02006775 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776 struct lyd_node *orig_parent;
6777
6778 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006779 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006780 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006781 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006782
aPiecek8b0cc152021-05-31 16:40:31 +02006783 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006784only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006785 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006786 LY_CHECK_RET(rc);
6787 } else if (set->type == LYXP_SET_NODE_SET) {
6788 /* 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 +01006789 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790
6791 /* empty set, nothing to evaluate */
6792 if (!set->used) {
6793 goto only_parse;
6794 }
6795
Michal Vasko004d3152020-06-11 19:59:22 +02006796 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797 orig_pos = 0;
6798 orig_size = set->used;
6799 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006800 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801 set_init(&set2, set);
6802 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6803 /* remember the node context position for position() and context size for last(),
6804 * predicates should always be evaluated with respect to the child axis (since we do
6805 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006806 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6807 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006808 orig_pos = 1;
6809 } else {
6810 ++orig_pos;
6811 }
6812
6813 set2.ctx_pos = orig_pos;
6814 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006815 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006816
Michal Vasko004d3152020-06-11 19:59:22 +02006817 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006819 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006820 return rc;
6821 }
6822
6823 /* number is a position */
6824 if (set2.type == LYXP_SET_NUMBER) {
6825 if ((long long)set2.val.num == orig_pos) {
6826 set2.val.num = 1;
6827 } else {
6828 set2.val.num = 0;
6829 }
6830 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006831 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832
6833 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006834 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006835 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 }
6837 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006838 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006839
6840 } else if (set->type == LYXP_SET_SCNODE_SET) {
6841 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006842 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006843 /* there is a currently-valid node */
6844 break;
6845 }
6846 }
6847 /* empty set, nothing to evaluate */
6848 if (i == set->used) {
6849 goto only_parse;
6850 }
6851
Michal Vasko004d3152020-06-11 19:59:22 +02006852 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006853
Michal Vasko03ff5a72019-09-11 13:49:33 +02006854 /* set special in_ctx to all the valid snodes */
6855 pred_in_ctx = set_scnode_new_in_ctx(set);
6856
6857 /* use the valid snodes one-by-one */
6858 for (i = 0; i < set->used; ++i) {
6859 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6860 continue;
6861 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006862 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006863
Michal Vasko004d3152020-06-11 19:59:22 +02006864 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006865
Michal Vasko004d3152020-06-11 19:59:22 +02006866 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006867 LY_CHECK_RET(rc);
6868
6869 set->val.scnodes[i].in_ctx = pred_in_ctx;
6870 }
6871
6872 /* restore the state as it was before the predicate */
6873 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006874 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006875 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006876 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006877 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006878 }
6879 }
6880
6881 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006882 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006883 set_fill_set(&set2, set);
6884
Michal Vasko004d3152020-06-11 19:59:22 +02006885 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006886 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006887 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006888 return rc;
6889 }
6890
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006891 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006892 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006893 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006894 }
Michal Vaskod3678892020-05-21 10:06:58 +02006895 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006896 }
6897
6898 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006899 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006900 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006901 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006902 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903
6904 return LY_SUCCESS;
6905}
6906
6907/**
Michal Vaskod3678892020-05-21 10:06:58 +02006908 * @brief Evaluate Literal. Logs directly on error.
6909 *
6910 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006911 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006912 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6913 */
6914static void
Michal Vasko40308e72020-10-20 16:38:40 +02006915eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006916{
6917 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006918 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006919 set_fill_string(set, "", 0);
6920 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006921 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 +02006922 }
6923 }
6924 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006925 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006926 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006927}
6928
6929/**
Michal Vasko004d3152020-06-11 19:59:22 +02006930 * @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 +02006931 *
Michal Vasko004d3152020-06-11 19:59:22 +02006932 * @param[in] exp Full parsed XPath expression.
6933 * @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 +02006934 * @param[in] ctx_node Found schema node as the context for the predicate.
6935 * @param[in] cur_mod Current module for the expression.
6936 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006937 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006938 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006939 * @param[out] predicates Parsed predicates.
6940 * @param[out] pred_type Type of @p predicates.
6941 * @return LY_SUCCESS on success,
6942 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006943 */
6944static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006945eval_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 +02006946 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 +02006947 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006948{
Michal Vasko004d3152020-06-11 19:59:22 +02006949 LY_ERR ret = LY_SUCCESS;
6950 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006951 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006952 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006953 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006954 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006955
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006956 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006957
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006958 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006959 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006960 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006961 return LY_EINVAL;
6962 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006963 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 +02006964 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006965
Michal Vasko004d3152020-06-11 19:59:22 +02006966 /* learn where the predicates end */
6967 e_idx = *tok_idx;
6968 while (key_count) {
6969 /* '[' */
6970 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6971 return LY_EINVAL;
6972 }
6973 ++e_idx;
6974
Michal Vasko3354d272021-04-06 09:40:06 +02006975 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6976 /* definitely not a key */
6977 return LY_EINVAL;
6978 }
6979
Michal Vasko004d3152020-06-11 19:59:22 +02006980 /* ']' */
6981 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6982 ++e_idx;
6983 }
6984 ++e_idx;
6985
6986 /* another presumably key predicate parsed */
6987 --key_count;
6988 }
Michal Vasko004d3152020-06-11 19:59:22 +02006989 } else {
6990 /* learn just where this single predicate ends */
6991 e_idx = *tok_idx;
6992
Michal Vaskod3678892020-05-21 10:06:58 +02006993 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006994 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6995 return LY_EINVAL;
6996 }
6997 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006998
Michal Vasko3354d272021-04-06 09:40:06 +02006999 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7000 /* definitely not the value */
7001 return LY_EINVAL;
7002 }
7003
Michal Vaskod3678892020-05-21 10:06:58 +02007004 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007005 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7006 ++e_idx;
7007 }
7008 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007009 }
7010
Michal Vasko004d3152020-06-11 19:59:22 +02007011 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7012 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7013
7014 /* turn logging off */
7015 prev_lo = ly_log_options(0);
7016
7017 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007018 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7019 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007020
7021 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007022 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7023 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007024 LY_CHECK_GOTO(ret, cleanup);
7025
7026 /* success, the predicate must include all the needed information for hash-based search */
7027 *tok_idx = e_idx;
7028
7029cleanup:
7030 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007031 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007032 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007033}
7034
7035/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007036 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7037 * data node(s) could be found using a single hash-based search.
7038 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007039 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007040 * @param[in] node Next context node to check.
7041 * @param[in] name Expected node name.
7042 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007043 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007044 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007045 * @param[in] format Prefix format.
7046 * @param[in,out] found Previously found node, is updated.
7047 * @return LY_SUCCESS on success,
7048 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7049 */
7050static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007051eval_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 +02007052 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 +02007053 const struct lysc_node **found)
7054{
7055 const struct lysc_node *scnode;
7056 const struct lys_module *mod;
7057 uint32_t idx = 0;
7058
Radek Krejci8df109d2021-04-23 12:19:08 +02007059 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007060
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007061continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007062 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007063 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007064 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007065 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007066 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007067 if (!mod->implemented) {
7068 continue;
7069 }
7070
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007071 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7072 if (scnode) {
7073 /* we have found a match */
7074 break;
7075 }
7076 }
7077
Michal Vasko7d1d0912020-10-16 08:38:30 +02007078 if (!scnode) {
7079 /* all modules searched */
7080 idx = 0;
7081 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007082 } else {
7083 /* search in top-level */
7084 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7085 }
7086 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007087 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007088 /* we must adjust the module to inherit the one from the context node */
7089 moveto_mod = node->schema->module;
7090 }
7091
7092 /* search in children, do not repeat the same search */
7093 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007094 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007095
7096 /* additional context check */
7097 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7098 scnode = NULL;
7099 }
7100
7101 if (scnode) {
7102 if (*found) {
7103 /* we found a schema node with the same name but at different level, give up, too complicated
7104 * (more hash-based searches would be required, not supported) */
7105 return LY_ENOT;
7106 } else {
7107 /* remember the found schema node and continue to make sure it can be used */
7108 *found = scnode;
7109 }
7110 }
7111
7112 if (idx) {
7113 /* continue searching all the following models */
7114 goto continue_search;
7115 }
7116
7117 return LY_SUCCESS;
7118}
7119
7120/**
Michal Vaskod3678892020-05-21 10:06:58 +02007121 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7122 *
7123 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7124 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7125 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7126 *
7127 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007128 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007129 * @param[in] attr_axis Whether to search attributes or standard nodes.
7130 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007131 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007132 * @param[in] options XPath options.
7133 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7134 */
7135static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007136eval_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 +02007137 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007138{
Michal Vaskod3678892020-05-21 10:06:58 +02007139 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007140 const char *ncname, *ncname_dict = NULL;
7141 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007142 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007143 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007144 struct ly_path_predicate *predicates = NULL;
7145 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007146 LY_ERR rc = LY_SUCCESS;
7147
aPiecek8b0cc152021-05-31 16:40:31 +02007148 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007149 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007150 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007151
aPiecek8b0cc152021-05-31 16:40:31 +02007152 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007153 goto moveto;
7154 }
7155
Michal Vasko004d3152020-06-11 19:59:22 +02007156 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7157 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007158
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007159 if ((ncname[0] == '*') && (ncname_len == 1)) {
7160 /* all nodes will match */
7161 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007162 }
7163
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007164 /* parse (and skip) module name */
7165 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007166 LY_CHECK_GOTO(rc, cleanup);
7167
Radek Krejci8df109d2021-04-23 12:19:08 +02007168 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 +02007169 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007170 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007171 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7172 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007173 /* check failed */
7174 scnode = NULL;
7175 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007176 }
7177 }
7178
Michal Vasko004d3152020-06-11 19:59:22 +02007179 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7180 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007181 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7182 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007183 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007184 scnode = NULL;
7185 }
7186 }
7187 }
7188
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007189 if (!scnode) {
7190 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007191 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007192 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007193 }
7194
7195moveto:
7196 /* move to the attribute(s), data node(s), or schema node(s) */
7197 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007198 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007199 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007200 } else {
7201 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007202 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007203 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007204 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007205 }
7206 LY_CHECK_GOTO(rc, cleanup);
7207 }
7208 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007209 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007210 int64_t i;
7211
Michal Vaskod3678892020-05-21 10:06:58 +02007212 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007213 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007214 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007215 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007216 }
7217 LY_CHECK_GOTO(rc, cleanup);
7218
7219 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007220 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007221 break;
7222 }
7223 }
7224 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007225 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007226 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7227 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007228 free(path);
7229 }
7230 } else {
7231 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007232 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007233 } else {
7234 if (scnode) {
7235 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007236 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007237 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007238 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007239 }
7240 }
7241 LY_CHECK_GOTO(rc, cleanup);
7242 }
7243 }
7244
7245 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007246 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7247 rc = eval_predicate(exp, tok_idx, set, options, 1);
aPiecek12677592021-10-07 11:28:01 +02007248 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007249 }
7250
7251cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007252 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007253 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007254 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007255 }
Michal Vaskod3678892020-05-21 10:06:58 +02007256 return rc;
7257}
7258
7259/**
7260 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7261 *
7262 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7263 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7264 * [8] NodeType ::= 'text' | 'node'
7265 *
7266 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007267 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007268 * @param[in] attr_axis Whether to search attributes or standard nodes.
7269 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007270 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007271 * @param[in] options XPath options.
7272 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7273 */
7274static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007275eval_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 +02007276 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007277{
7278 LY_ERR rc;
7279
7280 /* TODO */
7281 (void)attr_axis;
7282 (void)all_desc;
7283
aPiecek8b0cc152021-05-31 16:40:31 +02007284 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007285 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007286 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007287 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007288 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007289 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007290 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007291 rc = xpath_node(NULL, 0, set, options);
7292 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007293 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007294 rc = xpath_text(NULL, 0, set, options);
7295 }
7296 LY_CHECK_RET(rc);
7297 }
7298 }
aPiecek8b0cc152021-05-31 16:40:31 +02007299 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007300 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007301 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007302
7303 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007304 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007305 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007306 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007307 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007308
7309 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007310 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007311 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007312 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007313 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007314
7315 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007316 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7317 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007318 LY_CHECK_RET(rc);
7319 }
7320
7321 return LY_SUCCESS;
7322}
7323
7324/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007325 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7326 *
7327 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7328 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007329 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 *
7331 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007332 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007334 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335 * @param[in] options XPath options.
7336 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7337 */
7338static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007339eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7340 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341{
Radek Krejci857189e2020-09-01 13:26:36 +02007342 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 LY_ERR rc;
7344
7345 goto step;
7346 do {
7347 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007348 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007349 all_desc = 0;
7350 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007351 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 all_desc = 1;
7353 }
aPiecek8b0cc152021-05-31 16:40:31 +02007354 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007355 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007356 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357
7358step:
Michal Vaskod3678892020-05-21 10:06:58 +02007359 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007360 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007361 attr_axis = 1;
7362
aPiecek8b0cc152021-05-31 16:40:31 +02007363 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007364 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007365 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007366 } else {
7367 attr_axis = 0;
7368 }
7369
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007371 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 case LYXP_TOKEN_DOT:
7373 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007374 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375 rc = moveto_scnode_self(set, all_desc, options);
7376 } else {
7377 rc = moveto_self(set, all_desc, options);
7378 }
7379 LY_CHECK_RET(rc);
7380 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007381 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007382 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 break;
7384
7385 case LYXP_TOKEN_DDOT:
7386 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007387 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 rc = moveto_scnode_parent(set, all_desc, options);
7389 } else {
7390 rc = moveto_parent(set, all_desc, options);
7391 }
7392 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007393 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007394 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007395 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 break;
7397
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007399 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007400 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007402 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403
Michal Vaskod3678892020-05-21 10:06:58 +02007404 case LYXP_TOKEN_NODETYPE:
7405 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007406 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007407 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408 break;
7409
7410 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007411 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 }
Michal Vasko004d3152020-06-11 19:59:22 +02007413 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414
7415 return LY_SUCCESS;
7416}
7417
7418/**
7419 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7420 *
7421 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7422 *
7423 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007424 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007425 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 * @param[in] options XPath options.
7427 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7428 */
7429static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007430eval_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 +02007431{
Radek Krejci857189e2020-09-01 13:26:36 +02007432 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433
aPiecek8b0cc152021-05-31 16:40:31 +02007434 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007436 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 }
7438
7439 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007440 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 /* evaluate '/' - deferred */
7442 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007443 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007444 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007445 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446
Michal Vasko004d3152020-06-11 19:59:22 +02007447 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 return LY_SUCCESS;
7449 }
Michal Vasko004d3152020-06-11 19:59:22 +02007450 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451 case LYXP_TOKEN_DOT:
7452 case LYXP_TOKEN_DDOT:
7453 case LYXP_TOKEN_AT:
7454 case LYXP_TOKEN_NAMETEST:
7455 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007456 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 break;
7458 default:
7459 break;
7460 }
7461
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007463 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7465 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007466 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007467 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007468 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469
Michal Vaskob0099a92020-08-31 14:55:23 +02007470 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 }
7472
7473 return LY_SUCCESS;
7474}
7475
7476/**
7477 * @brief Evaluate FunctionCall. Logs directly on error.
7478 *
Michal Vaskod3678892020-05-21 10:06:58 +02007479 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 *
7481 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007482 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007483 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 * @param[in] options XPath options.
7485 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7486 */
7487static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007488eval_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 +02007489{
7490 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007491
Radek Krejci1deb5be2020-08-26 16:43:36 +02007492 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007493 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 struct lyxp_set **args = NULL, **args_aux;
7495
aPiecek8b0cc152021-05-31 16:40:31 +02007496 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007498 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007500 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007502 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 xpath_func = &xpath_sum;
7504 }
7505 break;
7506 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007507 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007509 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007511 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007513 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 xpath_func = &xpath_true;
7515 }
7516 break;
7517 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007518 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007520 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007522 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007524 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007526 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 xpath_func = &xpath_deref;
7528 }
7529 break;
7530 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007531 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007533 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007535 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_string;
7537 }
7538 break;
7539 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007540 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007542 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007544 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_current;
7546 }
7547 break;
7548 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007549 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007551 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007553 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 xpath_func = &xpath_re_match;
7555 }
7556 break;
7557 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007558 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007560 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_translate;
7562 }
7563 break;
7564 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007565 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007567 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 xpath_func = &xpath_bit_is_set;
7571 }
7572 break;
7573 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007574 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 xpath_func = &xpath_starts_with;
7576 }
7577 break;
7578 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007579 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580 xpath_func = &xpath_derived_from;
7581 }
7582 break;
7583 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007584 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007586 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 xpath_func = &xpath_string_length;
7588 }
7589 break;
7590 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007591 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007592 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007593 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 xpath_func = &xpath_substring_after;
7595 }
7596 break;
7597 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007598 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007599 xpath_func = &xpath_substring_before;
7600 }
7601 break;
7602 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007603 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 xpath_func = &xpath_derived_from_or_self;
7605 }
7606 break;
7607 }
7608
7609 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007610 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 +02007611 return LY_EVALID;
7612 }
7613 }
7614
aPiecek8b0cc152021-05-31 16:40:31 +02007615 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007616 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007617 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007618
7619 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007620 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007621 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007622 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007623 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007624
7625 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007626 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007627 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007628 args = malloc(sizeof *args);
7629 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7630 arg_count = 1;
7631 args[0] = set_copy(set);
7632 if (!args[0]) {
7633 rc = LY_EMEM;
7634 goto cleanup;
7635 }
7636
Michal Vasko004d3152020-06-11 19:59:22 +02007637 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007638 LY_CHECK_GOTO(rc, cleanup);
7639 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007640 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 LY_CHECK_GOTO(rc, cleanup);
7642 }
7643 }
Michal Vasko004d3152020-06-11 19:59:22 +02007644 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007645 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007646 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007647 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007648
aPiecek8b0cc152021-05-31 16:40:31 +02007649 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 ++arg_count;
7651 args_aux = realloc(args, arg_count * sizeof *args);
7652 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7653 args = args_aux;
7654 args[arg_count - 1] = set_copy(set);
7655 if (!args[arg_count - 1]) {
7656 rc = LY_EMEM;
7657 goto cleanup;
7658 }
7659
Michal Vasko004d3152020-06-11 19:59:22 +02007660 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 LY_CHECK_GOTO(rc, cleanup);
7662 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007663 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007664 LY_CHECK_GOTO(rc, cleanup);
7665 }
7666 }
7667
7668 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007669 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007670 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007671 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007672 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007673
aPiecek8b0cc152021-05-31 16:40:31 +02007674 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 /* evaluate function */
7676 rc = xpath_func(args, arg_count, set, options);
7677
7678 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 /* merge all nodes from arg evaluations */
7680 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007681 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007682 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007683 }
7684 }
7685 } else {
7686 rc = LY_SUCCESS;
7687 }
7688
7689cleanup:
7690 for (i = 0; i < arg_count; ++i) {
7691 lyxp_set_free(args[i]);
7692 }
7693 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007694 return rc;
7695}
7696
7697/**
7698 * @brief Evaluate Number. Logs directly on error.
7699 *
7700 * @param[in] ctx Context for errors.
7701 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007702 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7704 * @return LY_ERR
7705 */
7706static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007707eval_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 +02007708{
7709 long double num;
7710 char *endptr;
7711
7712 if (set) {
7713 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007714 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007716 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7717 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007718 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007719 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007720 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007721 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7722 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007723 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007724 return LY_EVALID;
7725 }
7726
7727 set_fill_number(set, num);
7728 }
7729
7730 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007731 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007732 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007733 return LY_SUCCESS;
7734}
7735
7736/**
7737 * @brief Evaluate PathExpr. Logs directly on error.
7738 *
Michal Vaskod3678892020-05-21 10:06:58 +02007739 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007740 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7741 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7742 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007743 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007744 *
7745 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007746 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007747 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 * @param[in] options XPath options.
7749 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7750 */
7751static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007752eval_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 +02007753{
Radek Krejci857189e2020-09-01 13:26:36 +02007754 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007755 LY_ERR rc;
7756
Michal Vasko004d3152020-06-11 19:59:22 +02007757 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 case LYXP_TOKEN_PAR1:
7759 /* '(' Expr ')' */
7760
7761 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007762 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007763 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007764 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007765
7766 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007767 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 LY_CHECK_RET(rc);
7769
7770 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007771 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007772 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007773 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007774 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007775
7776 parent_pos_pred = 0;
7777 goto predicate;
7778
7779 case LYXP_TOKEN_DOT:
7780 case LYXP_TOKEN_DDOT:
7781 case LYXP_TOKEN_AT:
7782 case LYXP_TOKEN_NAMETEST:
7783 case LYXP_TOKEN_NODETYPE:
7784 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007785 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786 LY_CHECK_RET(rc);
7787 break;
7788
7789 case LYXP_TOKEN_FUNCNAME:
7790 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007791 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 LY_CHECK_RET(rc);
7793
7794 parent_pos_pred = 1;
7795 goto predicate;
7796
Michal Vasko3e48bf32020-06-01 08:39:07 +02007797 case LYXP_TOKEN_OPER_PATH:
7798 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007800 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801 LY_CHECK_RET(rc);
7802 break;
7803
7804 case LYXP_TOKEN_LITERAL:
7805 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007806 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7807 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007808 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809 }
Michal Vasko004d3152020-06-11 19:59:22 +02007810 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007812 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813 }
7814
7815 parent_pos_pred = 1;
7816 goto predicate;
7817
7818 case LYXP_TOKEN_NUMBER:
7819 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007820 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7821 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007822 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007823 }
Michal Vasko004d3152020-06-11 19:59:22 +02007824 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007825 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007826 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827 }
7828 LY_CHECK_RET(rc);
7829
7830 parent_pos_pred = 1;
7831 goto predicate;
7832
7833 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007834 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 +02007835 return LY_EVALID;
7836 }
7837
7838 return LY_SUCCESS;
7839
7840predicate:
7841 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007842 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7843 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007844 LY_CHECK_RET(rc);
7845 }
7846
7847 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007848 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007849
7850 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007851 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007852 all_desc = 0;
7853 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007854 all_desc = 1;
7855 }
7856
aPiecek8b0cc152021-05-31 16:40:31 +02007857 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007858 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007859 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860
Michal Vasko004d3152020-06-11 19:59:22 +02007861 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 LY_CHECK_RET(rc);
7863 }
7864
7865 return LY_SUCCESS;
7866}
7867
7868/**
7869 * @brief Evaluate UnionExpr. Logs directly on error.
7870 *
Michal Vaskod3678892020-05-21 10:06:58 +02007871 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007872 *
7873 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007874 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007875 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007876 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007877 * @param[in] options XPath options.
7878 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7879 */
7880static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007881eval_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 +02007882{
7883 LY_ERR rc = LY_SUCCESS;
7884 struct lyxp_set orig_set, set2;
7885 uint16_t i;
7886
7887 assert(repeat);
7888
7889 set_init(&orig_set, set);
7890 set_init(&set2, set);
7891
7892 set_fill_set(&orig_set, set);
7893
Michal Vasko004d3152020-06-11 19:59:22 +02007894 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007895 LY_CHECK_GOTO(rc, cleanup);
7896
7897 /* ('|' PathExpr)* */
7898 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007899 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007900 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007901 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007902 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007903
aPiecek8b0cc152021-05-31 16:40:31 +02007904 if (options & LYXP_SKIP_EXPR) {
7905 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007906 LY_CHECK_GOTO(rc, cleanup);
7907 continue;
7908 }
7909
7910 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007911 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007912 LY_CHECK_GOTO(rc, cleanup);
7913
7914 /* eval */
7915 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007916 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007917 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007918 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007919 LY_CHECK_GOTO(rc, cleanup);
7920 }
7921 }
7922
7923cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007924 lyxp_set_free_content(&orig_set);
7925 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007926 return rc;
7927}
7928
7929/**
7930 * @brief Evaluate UnaryExpr. Logs directly on error.
7931 *
Michal Vaskod3678892020-05-21 10:06:58 +02007932 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007933 *
7934 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007935 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007936 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007937 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007938 * @param[in] options XPath options.
7939 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7940 */
7941static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007942eval_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 +02007943{
7944 LY_ERR rc;
7945 uint16_t this_op, i;
7946
7947 assert(repeat);
7948
7949 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007950 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007952 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 +02007953
aPiecek8b0cc152021-05-31 16:40:31 +02007954 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007955 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007956 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007957 }
7958
Michal Vasko004d3152020-06-11 19:59:22 +02007959 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 LY_CHECK_RET(rc);
7961
aPiecek8b0cc152021-05-31 16:40:31 +02007962 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963 if (options & LYXP_SCNODE_ALL) {
7964 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7965 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007966 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007967 LY_CHECK_RET(rc);
7968 }
7969 }
7970
7971 return LY_SUCCESS;
7972}
7973
7974/**
7975 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7976 *
Michal Vaskod3678892020-05-21 10:06:58 +02007977 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 * | MultiplicativeExpr '*' UnaryExpr
7979 * | MultiplicativeExpr 'div' UnaryExpr
7980 * | MultiplicativeExpr 'mod' UnaryExpr
7981 *
7982 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007983 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007984 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007985 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007986 * @param[in] options XPath options.
7987 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7988 */
7989static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007990eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7991 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007992{
7993 LY_ERR rc;
7994 uint16_t this_op;
7995 struct lyxp_set orig_set, set2;
7996 uint16_t i;
7997
7998 assert(repeat);
7999
8000 set_init(&orig_set, set);
8001 set_init(&set2, set);
8002
8003 set_fill_set(&orig_set, set);
8004
Michal Vasko004d3152020-06-11 19:59:22 +02008005 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008006 LY_CHECK_GOTO(rc, cleanup);
8007
8008 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8009 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008010 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008011
Michal Vasko004d3152020-06-11 19:59:22 +02008012 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008013 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008014 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008015 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016
aPiecek8b0cc152021-05-31 16:40:31 +02008017 if (options & LYXP_SKIP_EXPR) {
8018 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019 LY_CHECK_GOTO(rc, cleanup);
8020 continue;
8021 }
8022
8023 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008024 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008025 LY_CHECK_GOTO(rc, cleanup);
8026
8027 /* eval */
8028 if (options & LYXP_SCNODE_ALL) {
8029 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008030 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008031 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008033 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 LY_CHECK_GOTO(rc, cleanup);
8035 }
8036 }
8037
8038cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008039 lyxp_set_free_content(&orig_set);
8040 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 return rc;
8042}
8043
8044/**
8045 * @brief Evaluate AdditiveExpr. Logs directly on error.
8046 *
Michal Vaskod3678892020-05-21 10:06:58 +02008047 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008048 * | AdditiveExpr '+' MultiplicativeExpr
8049 * | AdditiveExpr '-' MultiplicativeExpr
8050 *
8051 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008052 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008054 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008055 * @param[in] options XPath options.
8056 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8057 */
8058static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008059eval_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 +02008060{
8061 LY_ERR rc;
8062 uint16_t this_op;
8063 struct lyxp_set orig_set, set2;
8064 uint16_t i;
8065
8066 assert(repeat);
8067
8068 set_init(&orig_set, set);
8069 set_init(&set2, set);
8070
8071 set_fill_set(&orig_set, set);
8072
Michal Vasko004d3152020-06-11 19:59:22 +02008073 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008074 LY_CHECK_GOTO(rc, cleanup);
8075
8076 /* ('+' / '-' MultiplicativeExpr)* */
8077 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008078 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008079
Michal Vasko004d3152020-06-11 19:59:22 +02008080 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008081 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008082 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008083 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084
aPiecek8b0cc152021-05-31 16:40:31 +02008085 if (options & LYXP_SKIP_EXPR) {
8086 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 LY_CHECK_GOTO(rc, cleanup);
8088 continue;
8089 }
8090
8091 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008092 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008093 LY_CHECK_GOTO(rc, cleanup);
8094
8095 /* eval */
8096 if (options & LYXP_SCNODE_ALL) {
8097 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008098 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008099 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008100 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008101 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008102 LY_CHECK_GOTO(rc, cleanup);
8103 }
8104 }
8105
8106cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008107 lyxp_set_free_content(&orig_set);
8108 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109 return rc;
8110}
8111
8112/**
8113 * @brief Evaluate RelationalExpr. Logs directly on error.
8114 *
Michal Vaskod3678892020-05-21 10:06:58 +02008115 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008116 * | RelationalExpr '<' AdditiveExpr
8117 * | RelationalExpr '>' AdditiveExpr
8118 * | RelationalExpr '<=' AdditiveExpr
8119 * | RelationalExpr '>=' AdditiveExpr
8120 *
8121 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008122 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008124 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 * @param[in] options XPath options.
8126 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8127 */
8128static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008129eval_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 +02008130{
8131 LY_ERR rc;
8132 uint16_t this_op;
8133 struct lyxp_set orig_set, set2;
8134 uint16_t i;
8135
8136 assert(repeat);
8137
8138 set_init(&orig_set, set);
8139 set_init(&set2, set);
8140
8141 set_fill_set(&orig_set, set);
8142
Michal Vasko004d3152020-06-11 19:59:22 +02008143 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008144 LY_CHECK_GOTO(rc, cleanup);
8145
8146 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8147 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008148 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008149
Michal Vasko004d3152020-06-11 19:59:22 +02008150 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008151 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008152 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008153 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008154
aPiecek8b0cc152021-05-31 16:40:31 +02008155 if (options & LYXP_SKIP_EXPR) {
8156 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 LY_CHECK_GOTO(rc, cleanup);
8158 continue;
8159 }
8160
8161 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008162 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008163 LY_CHECK_GOTO(rc, cleanup);
8164
8165 /* eval */
8166 if (options & LYXP_SCNODE_ALL) {
8167 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008168 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008169 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008170 } else {
8171 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8172 LY_CHECK_GOTO(rc, cleanup);
8173 }
8174 }
8175
8176cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008177 lyxp_set_free_content(&orig_set);
8178 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179 return rc;
8180}
8181
8182/**
8183 * @brief Evaluate EqualityExpr. Logs directly on error.
8184 *
Michal Vaskod3678892020-05-21 10:06:58 +02008185 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008186 * | EqualityExpr '!=' RelationalExpr
8187 *
8188 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008189 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008191 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 * @param[in] options XPath options.
8193 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8194 */
8195static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008196eval_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 +02008197{
8198 LY_ERR rc;
8199 uint16_t this_op;
8200 struct lyxp_set orig_set, set2;
8201 uint16_t i;
8202
8203 assert(repeat);
8204
8205 set_init(&orig_set, set);
8206 set_init(&set2, set);
8207
8208 set_fill_set(&orig_set, set);
8209
Michal Vasko004d3152020-06-11 19:59:22 +02008210 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 LY_CHECK_GOTO(rc, cleanup);
8212
8213 /* ('=' / '!=' RelationalExpr)* */
8214 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008215 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008216
Michal Vasko004d3152020-06-11 19:59:22 +02008217 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008218 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008219 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008220 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221
aPiecek8b0cc152021-05-31 16:40:31 +02008222 if (options & LYXP_SKIP_EXPR) {
8223 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 LY_CHECK_GOTO(rc, cleanup);
8225 continue;
8226 }
8227
8228 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008229 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 LY_CHECK_GOTO(rc, cleanup);
8231
8232 /* eval */
8233 if (options & LYXP_SCNODE_ALL) {
8234 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008235 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8236 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008237 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008238 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008240 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8241 LY_CHECK_GOTO(rc, cleanup);
8242 }
8243 }
8244
8245cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008246 lyxp_set_free_content(&orig_set);
8247 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 return rc;
8249}
8250
8251/**
8252 * @brief Evaluate AndExpr. Logs directly on error.
8253 *
Michal Vaskod3678892020-05-21 10:06:58 +02008254 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 *
8256 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008257 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008259 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 * @param[in] options XPath options.
8261 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8262 */
8263static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008264eval_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 +02008265{
8266 LY_ERR rc;
8267 struct lyxp_set orig_set, set2;
8268 uint16_t i;
8269
8270 assert(repeat);
8271
8272 set_init(&orig_set, set);
8273 set_init(&set2, set);
8274
8275 set_fill_set(&orig_set, set);
8276
Michal Vasko004d3152020-06-11 19:59:22 +02008277 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278 LY_CHECK_GOTO(rc, cleanup);
8279
8280 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008281 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008282 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008284 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 }
8286
8287 /* ('and' EqualityExpr)* */
8288 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008289 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008290 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008291 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008292 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293
8294 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008295 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8296 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008297 LY_CHECK_GOTO(rc, cleanup);
8298 continue;
8299 }
8300
8301 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008302 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303 LY_CHECK_GOTO(rc, cleanup);
8304
8305 /* eval - just get boolean value actually */
8306 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008307 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008308 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008310 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008311 set_fill_set(set, &set2);
8312 }
8313 }
8314
8315cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008316 lyxp_set_free_content(&orig_set);
8317 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008318 return rc;
8319}
8320
8321/**
8322 * @brief Evaluate OrExpr. Logs directly on error.
8323 *
Michal Vaskod3678892020-05-21 10:06:58 +02008324 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 *
8326 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008327 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008329 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 * @param[in] options XPath options.
8331 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8332 */
8333static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008334eval_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 +02008335{
8336 LY_ERR rc;
8337 struct lyxp_set orig_set, set2;
8338 uint16_t i;
8339
8340 assert(repeat);
8341
8342 set_init(&orig_set, set);
8343 set_init(&set2, set);
8344
8345 set_fill_set(&orig_set, set);
8346
Michal Vasko004d3152020-06-11 19:59:22 +02008347 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348 LY_CHECK_GOTO(rc, cleanup);
8349
8350 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008351 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008352 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008354 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008355 }
8356
8357 /* ('or' AndExpr)* */
8358 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008359 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008360 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008361 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008362 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363
8364 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008365 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8366 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 LY_CHECK_GOTO(rc, cleanup);
8368 continue;
8369 }
8370
8371 set_fill_set(&set2, &orig_set);
8372 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8373 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008374 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 LY_CHECK_GOTO(rc, cleanup);
8376
8377 /* eval - just get boolean value actually */
8378 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008379 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008380 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008382 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 set_fill_set(set, &set2);
8384 }
8385 }
8386
8387cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008388 lyxp_set_free_content(&orig_set);
8389 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 return rc;
8391}
8392
8393/**
Michal Vasko004d3152020-06-11 19:59:22 +02008394 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008395 *
8396 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008397 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008399 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400 * @param[in] options XPath options.
8401 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8402 */
8403static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008404eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8405 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406{
8407 uint16_t i, count;
8408 enum lyxp_expr_type next_etype;
8409 LY_ERR rc;
8410
8411 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008412 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 next_etype = LYXP_EXPR_NONE;
8414 } else {
8415 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008416 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417
8418 /* select one-priority lower because etype expression called us */
8419 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008420 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008422 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423 } else {
8424 next_etype = LYXP_EXPR_NONE;
8425 }
8426 }
8427
8428 /* decide what expression are we parsing based on the repeat */
8429 switch (next_etype) {
8430 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008431 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 break;
8433 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008434 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 break;
8436 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008437 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 break;
8439 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008440 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441 break;
8442 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008443 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 break;
8445 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008446 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 break;
8448 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008449 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 break;
8451 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008452 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 break;
8454 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008455 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 break;
8457 default:
8458 LOGINT_RET(set->ctx);
8459 }
8460
8461 return rc;
8462}
8463
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008464/**
8465 * @brief Get root type.
8466 *
8467 * @param[in] ctx_node Context node.
8468 * @param[in] ctx_scnode Schema context node.
8469 * @param[in] options XPath options.
8470 * @return Root type.
8471 */
8472static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008473lyxp_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 +01008474{
Michal Vasko6b26e742020-07-17 15:02:10 +02008475 const struct lysc_node *op;
8476
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008477 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008478 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008479 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008480
8481 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008482 /* general root that can access everything */
8483 return LYXP_NODE_ROOT;
8484 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8485 /* root context node can access only config data (because we said so, it is unspecified) */
8486 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008487 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008488 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008489 }
8490
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008491 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008492 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008493 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008494
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008495 if (op || !(options & LYXP_SCHEMA)) {
8496 /* general root that can access everything */
8497 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008498 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008499 /* root context node can access only config data (because we said so, it is unspecified) */
8500 return LYXP_NODE_ROOT_CONFIG;
8501 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008502 return LYXP_NODE_ROOT;
8503}
8504
Michal Vasko03ff5a72019-09-11 13:49:33 +02008505LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008506lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008507 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 +01008508 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008509{
Michal Vasko004d3152020-06-11 19:59:22 +02008510 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008511 LY_ERR rc;
8512
Michal Vasko400e9672021-01-11 13:39:17 +01008513 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008514 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008515 LOGARG(NULL, "Current module must be set if schema format is used.");
8516 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008517 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008518
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008519 if (tree) {
8520 /* adjust the pointer to be the first top-level sibling */
8521 while (tree->parent) {
8522 tree = lyd_parent(tree);
8523 }
8524 tree = lyd_first_sibling(tree);
8525 }
8526
Michal Vasko03ff5a72019-09-11 13:49:33 +02008527 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008529 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008530 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8531 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8532
Michal Vasko400e9672021-01-11 13:39:17 +01008533 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008534 set->cur_node = ctx_node;
8535 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008536 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8537 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008538 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008539 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008541 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542
Radek Krejciddace2c2021-01-08 11:30:56 +01008543 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008544
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008546 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008547 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008548 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008549 }
8550
Radek Krejciddace2c2021-01-08 11:30:56 +01008551 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 return rc;
8553}
8554
8555#if 0
8556
8557/* full xml printing of set elements, not used currently */
8558
8559void
8560lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8561{
8562 uint32_t i;
8563 char *str_num;
8564 struct lyout out;
8565
8566 memset(&out, 0, sizeof out);
8567
8568 out.type = LYOUT_STREAM;
8569 out.method.f = f;
8570
8571 switch (set->type) {
8572 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008573 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 break;
8575 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008576 ly_print_(&out, "Boolean XPath set:\n");
8577 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578 break;
8579 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008580 ly_print_(&out, "String XPath set:\n");
8581 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 break;
8583 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008584 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585
8586 if (isnan(set->value.num)) {
8587 str_num = strdup("NaN");
8588 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8589 str_num = strdup("0");
8590 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8591 str_num = strdup("Infinity");
8592 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8593 str_num = strdup("-Infinity");
8594 } else if ((long long)set->value.num == set->value.num) {
8595 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8596 str_num = NULL;
8597 }
8598 } else {
8599 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8600 str_num = NULL;
8601 }
8602 }
8603 if (!str_num) {
8604 LOGMEM;
8605 return;
8606 }
Michal Vasko5233e962020-08-14 14:26:20 +02008607 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008608 free(str_num);
8609 break;
8610 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008611 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612
8613 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008614 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 switch (set->node_type[i]) {
8616 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008617 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 break;
8619 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008620 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 break;
8622 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008623 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 break;
8625 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008626 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 break;
8628 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008629 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 break;
8631 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008632 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 break;
8634 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008635 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008637 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008638 break;
8639 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008640 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 +02008641 break;
8642 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008643 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 +02008644 break;
8645 }
8646 }
8647 break;
8648 }
8649}
8650
8651#endif
8652
8653LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008654lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008655{
8656 long double num;
8657 char *str;
8658 LY_ERR rc;
8659
8660 if (!set || (set->type == target)) {
8661 return LY_SUCCESS;
8662 }
8663
8664 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008665 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008666
8667 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008668 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008669 return LY_EINVAL;
8670 }
8671
8672 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008673 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008674 switch (set->type) {
8675 case LYXP_SET_NUMBER:
8676 if (isnan(set->val.num)) {
8677 set->val.str = strdup("NaN");
8678 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8679 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8680 set->val.str = strdup("0");
8681 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8682 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8683 set->val.str = strdup("Infinity");
8684 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8685 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8686 set->val.str = strdup("-Infinity");
8687 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8688 } else if ((long long)set->val.num == set->val.num) {
8689 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8690 LOGMEM_RET(set->ctx);
8691 }
8692 set->val.str = str;
8693 } else {
8694 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8695 LOGMEM_RET(set->ctx);
8696 }
8697 set->val.str = str;
8698 }
8699 break;
8700 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008701 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008702 set->val.str = strdup("true");
8703 } else {
8704 set->val.str = strdup("false");
8705 }
8706 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8707 break;
8708 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008709 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008710 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008711
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008712 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008713 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008714 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008715 set->val.str = str;
8716 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008717 default:
8718 LOGINT_RET(set->ctx);
8719 }
8720 set->type = LYXP_SET_STRING;
8721 }
8722
8723 /* to NUMBER */
8724 if (target == LYXP_SET_NUMBER) {
8725 switch (set->type) {
8726 case LYXP_SET_STRING:
8727 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008728 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 set->val.num = num;
8730 break;
8731 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008732 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008733 set->val.num = 1;
8734 } else {
8735 set->val.num = 0;
8736 }
8737 break;
8738 default:
8739 LOGINT_RET(set->ctx);
8740 }
8741 set->type = LYXP_SET_NUMBER;
8742 }
8743
8744 /* to BOOLEAN */
8745 if (target == LYXP_SET_BOOLEAN) {
8746 switch (set->type) {
8747 case LYXP_SET_NUMBER:
8748 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008749 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008750 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008751 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 }
8753 break;
8754 case LYXP_SET_STRING:
8755 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008756 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008757 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008759 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008760 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008761 }
8762 break;
8763 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008764 if (set->used) {
8765 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008766 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008767 } else {
8768 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008769 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008770 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008771 break;
8772 default:
8773 LOGINT_RET(set->ctx);
8774 }
8775 set->type = LYXP_SET_BOOLEAN;
8776 }
8777
Michal Vasko03ff5a72019-09-11 13:49:33 +02008778 return LY_SUCCESS;
8779}
8780
8781LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008782lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008783 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008784 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008785{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008786 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008787 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008788
Michal Vasko400e9672021-01-11 13:39:17 +01008789 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008790 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008791 LOGARG(NULL, "Current module must be set if schema format is used.");
8792 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008793 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008794
8795 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008796 memset(set, 0, sizeof *set);
8797 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008798 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8799 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 +01008800 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008801
Michal Vasko400e9672021-01-11 13:39:17 +01008802 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008803 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008804 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008805 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8806 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008807 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008808 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008809 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008810
Radek Krejciddace2c2021-01-08 11:30:56 +01008811 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008812
Michal Vasko03ff5a72019-09-11 13:49:33 +02008813 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008814 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8815
Radek Krejciddace2c2021-01-08 11:30:56 +01008816 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008817 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008818}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008819
8820API const char *
8821lyxp_get_expr(const struct lyxp_expr *path)
8822{
8823 if (!path) {
8824 return NULL;
8825 }
8826
8827 return path->expr;
8828}