blob: 4b46c7d39ae0e00d95edf90d981c8c1e9a7cfa5e [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 */
15#include <sys/cdefs.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010016
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010020#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020034#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020035#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010040#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vasko004d3152020-06-11 19:59:22 +020046static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
Michal Vasko40308e72020-10-20 16:38:40 +020047static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
48 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020049static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
50 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020051
52/**
53 * @brief Print the type of an XPath \p set.
54 *
55 * @param[in] set Set to use.
56 * @return Set type string.
57 */
58static const char *
59print_set_type(struct lyxp_set *set)
60{
61 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020062 case LYXP_SET_NODE_SET:
63 return "node set";
64 case LYXP_SET_SCNODE_SET:
65 return "schema node set";
66 case LYXP_SET_BOOLEAN:
67 return "boolean";
68 case LYXP_SET_NUMBER:
69 return "number";
70 case LYXP_SET_STRING:
71 return "string";
72 }
73
74 return NULL;
75}
76
Michal Vasko24cddf82020-06-01 08:17:01 +020077const char *
78lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020079{
80 switch (tok) {
81 case LYXP_TOKEN_PAR1:
82 return "(";
83 case LYXP_TOKEN_PAR2:
84 return ")";
85 case LYXP_TOKEN_BRACK1:
86 return "[";
87 case LYXP_TOKEN_BRACK2:
88 return "]";
89 case LYXP_TOKEN_DOT:
90 return ".";
91 case LYXP_TOKEN_DDOT:
92 return "..";
93 case LYXP_TOKEN_AT:
94 return "@";
95 case LYXP_TOKEN_COMMA:
96 return ",";
97 case LYXP_TOKEN_NAMETEST:
98 return "NameTest";
99 case LYXP_TOKEN_NODETYPE:
100 return "NodeType";
101 case LYXP_TOKEN_FUNCNAME:
102 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200103 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200104 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200105 case LYXP_TOKEN_OPER_EQUAL:
106 return "Operator(Equal)";
107 case LYXP_TOKEN_OPER_NEQUAL:
108 return "Operator(Non-equal)";
109 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200111 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200112 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200113 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200114 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200115 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200117 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200118 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200119 case LYXP_TOKEN_LITERAL:
120 return "Literal";
121 case LYXP_TOKEN_NUMBER:
122 return "Number";
123 default:
124 LOGINT(NULL);
125 return "";
126 }
127}
128
129/**
130 * @brief Print the whole expression \p exp to debug output.
131 *
132 * @param[in] exp Expression to use.
133 */
134static void
Michal Vasko40308e72020-10-20 16:38:40 +0200135print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200136{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100137#define MSG_BUFFER_SIZE 128
138 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100139 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140
Radek Krejci52b6d512020-10-12 12:33:17 +0200141 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200142 return;
143 }
144
145 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
146 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200147 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200148 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100149 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200150 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
151 for (j = 1; exp->repeat[i][j]; ++j) {
152 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
153 }
154 strcat(tmp, ")");
155 }
156 LOGDBG(LY_LDGXPATH, tmp);
157 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100158#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200159}
160
161#ifndef NDEBUG
162
163/**
164 * @brief Print XPath set content to debug output.
165 *
166 * @param[in] set Set to print.
167 */
168static void
169print_set_debug(struct lyxp_set *set)
170{
171 uint32_t i;
172 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200173 struct lyxp_set_node *item;
174 struct lyxp_set_scnode *sitem;
175
Radek Krejci52b6d512020-10-12 12:33:17 +0200176 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200177 return;
178 }
179
180 switch (set->type) {
181 case LYXP_SET_NODE_SET:
182 LOGDBG(LY_LDGXPATH, "set NODE SET:");
183 for (i = 0; i < set->used; ++i) {
184 item = &set->val.nodes[i];
185
186 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100187 case LYXP_NODE_NONE:
188 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
189 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200190 case LYXP_NODE_ROOT:
191 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
192 break;
193 case LYXP_NODE_ROOT_CONFIG:
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
195 break;
196 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100197 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200198 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200199 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100200 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200202 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200203 } else {
204 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
205 }
206 break;
207 case LYXP_NODE_TEXT:
208 if (item->node->schema->nodetype & LYS_ANYDATA) {
209 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200210 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200211 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200212 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 +0200213 }
214 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100215 case LYXP_NODE_META:
216 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 +0200217 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200218 break;
219 }
220 }
221 break;
222
223 case LYXP_SET_SCNODE_SET:
224 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
225 for (i = 0; i < set->used; ++i) {
226 sitem = &set->val.scnodes[i];
227
228 switch (sitem->type) {
229 case LYXP_NODE_ROOT:
230 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
231 break;
232 case LYXP_NODE_ROOT_CONFIG:
233 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
234 break;
235 case LYXP_NODE_ELEM:
236 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
237 break;
238 default:
239 LOGINT(NULL);
240 break;
241 }
242 }
243 break;
244
Michal Vasko03ff5a72019-09-11 13:49:33 +0200245 case LYXP_SET_BOOLEAN:
246 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200247 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200248 break;
249
250 case LYXP_SET_STRING:
251 LOGDBG(LY_LDGXPATH, "set STRING");
252 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
253 break;
254
255 case LYXP_SET_NUMBER:
256 LOGDBG(LY_LDGXPATH, "set NUMBER");
257
258 if (isnan(set->val.num)) {
259 str = strdup("NaN");
260 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
261 str = strdup("0");
262 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
263 str = strdup("Infinity");
264 } else if (isinf(set->val.num) && signbit(set->val.num)) {
265 str = strdup("-Infinity");
266 } else if ((long long)set->val.num == set->val.num) {
267 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
268 str = NULL;
269 }
270 } else {
271 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
272 str = NULL;
273 }
274 }
275 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
276
277 LOGDBG(LY_LDGXPATH, "\t%s", str);
278 free(str);
279 }
280}
281
282#endif
283
284/**
285 * @brief Realloc the string \p str.
286 *
287 * @param[in] ctx libyang context for logging.
288 * @param[in] needed How much free space is required.
289 * @param[in,out] str Pointer to the string to use.
290 * @param[in,out] used Used bytes in \p str.
291 * @param[in,out] size Allocated bytes in \p str.
292 * @return LY_ERR
293 */
294static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100295cast_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 +0200296{
297 if (*size - *used < needed) {
298 do {
299 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
300 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
301 return LY_EINVAL;
302 }
303 *size += LYXP_STRING_CAST_SIZE_STEP;
304 } while (*size - *used < needed);
305 *str = ly_realloc(*str, *size * sizeof(char));
306 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
307 }
308
309 return LY_SUCCESS;
310}
311
312/**
313 * @brief Cast nodes recursively to one string @p str.
314 *
315 * @param[in] node Node to cast.
316 * @param[in] fake_cont Whether to put the data into a "fake" container.
317 * @param[in] root_type Type of the XPath root.
318 * @param[in] indent Current indent.
319 * @param[in,out] str Resulting string.
320 * @param[in,out] used Used bytes in @p str.
321 * @param[in,out] size Allocated bytes in @p str.
322 * @return LY_ERR
323 */
324static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200325cast_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 +0200326 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200327{
Radek Krejci7f769d72020-07-11 23:13:56 +0200328 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200330 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200331 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200332 struct lyd_node_any *any;
333 LY_ERR rc;
334
335 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
336 return LY_SUCCESS;
337 }
338
339 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200340 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200341 LY_CHECK_RET(rc);
342 strcpy(*str + (*used - 1), "\n");
343 ++(*used);
344
345 ++indent;
346 }
347
348 switch (node->schema->nodetype) {
349 case LYS_CONTAINER:
350 case LYS_LIST:
351 case LYS_RPC:
352 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200353 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200354 LY_CHECK_RET(rc);
355 strcpy(*str + (*used - 1), "\n");
356 ++(*used);
357
Radek Krejcia1c1e542020-09-29 16:06:52 +0200358 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200359 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
360 LY_CHECK_RET(rc);
361 }
362
363 break;
364
365 case LYS_LEAF:
366 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200367 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200368
369 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200370 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 +0200371 memset(*str + (*used - 1), ' ', indent * 2);
372 *used += indent * 2;
373
374 /* print value */
375 if (*used == 1) {
376 sprintf(*str + (*used - 1), "%s", value_str);
377 *used += strlen(value_str);
378 } else {
379 sprintf(*str + (*used - 1), "%s\n", value_str);
380 *used += strlen(value_str) + 1;
381 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200382
383 break;
384
385 case LYS_ANYXML:
386 case LYS_ANYDATA:
387 any = (struct lyd_node_any *)node;
388 if (!(void *)any->value.tree) {
389 /* no content */
390 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200391 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200392 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200393 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100394
Michal Vasko60ea6352020-06-29 13:39:39 +0200395 if (any->value_type == LYD_ANYDATA_LYB) {
396 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200397 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 +0200398 /* successfully parsed */
399 free(any->value.mem);
400 any->value.tree = tree;
401 any->value_type = LYD_ANYDATA_DATATREE;
402 }
Radek Krejci7931b192020-06-25 17:05:03 +0200403 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200404 }
405
Michal Vasko03ff5a72019-09-11 13:49:33 +0200406 switch (any->value_type) {
407 case LYD_ANYDATA_STRING:
408 case LYD_ANYDATA_XML:
409 case LYD_ANYDATA_JSON:
410 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200411 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200412 break;
413 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200414 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200415 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200416 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100417 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200419 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200420 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200421 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200422 }
423 }
424
425 line = strtok_r(buf, "\n", &ptr);
426 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200427 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200428 if (rc != LY_SUCCESS) {
429 free(buf);
430 return rc;
431 }
432 memset(*str + (*used - 1), ' ', indent * 2);
433 *used += indent * 2;
434
435 strcpy(*str + (*used - 1), line);
436 *used += strlen(line);
437
438 strcpy(*str + (*used - 1), "\n");
439 *used += 1;
440 } while ((line = strtok_r(NULL, "\n", &ptr)));
441
442 free(buf);
443 break;
444
445 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 }
448
449 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200450 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200451 LY_CHECK_RET(rc);
452 strcpy(*str + (*used - 1), "\n");
453 ++(*used);
454
455 --indent;
456 }
457
458 return LY_SUCCESS;
459}
460
461/**
462 * @brief Cast an element into a string.
463 *
464 * @param[in] node Node to cast.
465 * @param[in] fake_cont Whether to put the data into a "fake" container.
466 * @param[in] root_type Type of the XPath root.
467 * @param[out] str Element cast to dynamically-allocated string.
468 * @return LY_ERR
469 */
470static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200471cast_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 +0200472{
473 uint16_t used, size;
474 LY_ERR rc;
475
476 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200477 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200478 (*str)[0] = '\0';
479 used = 1;
480 size = LYXP_STRING_CAST_SIZE_START;
481
482 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
483 if (rc != LY_SUCCESS) {
484 free(*str);
485 return rc;
486 }
487
488 if (size > used) {
489 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200490 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200491 }
492 return LY_SUCCESS;
493}
494
495/**
496 * @brief Cast a LYXP_SET_NODE_SET set into a string.
497 * Context position aware.
498 *
499 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200500 * @param[out] str Cast dynamically-allocated string.
501 * @return LY_ERR
502 */
503static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100504cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200505{
Michal Vaskoaa677062021-03-09 13:52:53 +0100506 if (!set->used) {
507 *str = strdup("");
508 if (!*str) {
509 LOGMEM_RET(set->ctx);
510 }
511 return LY_SUCCESS;
512 }
513
Michal Vasko03ff5a72019-09-11 13:49:33 +0200514 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100515 case LYXP_NODE_NONE:
516 /* invalid */
517 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200518 case LYXP_NODE_ROOT:
519 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100520 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200521 case LYXP_NODE_ELEM:
522 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100523 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100524 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200525 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200526 if (!*str) {
527 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200528 }
529 return LY_SUCCESS;
530 }
531
532 LOGINT_RET(set->ctx);
533}
534
535/**
536 * @brief Cast a string into an XPath number.
537 *
538 * @param[in] str String to use.
539 * @return Cast number.
540 */
541static long double
542cast_string_to_number(const char *str)
543{
544 long double num;
545 char *ptr;
546
547 errno = 0;
548 num = strtold(str, &ptr);
549 if (errno || *ptr) {
550 num = NAN;
551 }
552 return num;
553}
554
555/**
556 * @brief Callback for checking value equality.
557 *
Michal Vasko62524a92021-02-26 10:08:50 +0100558 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200559 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200560 * @param[in] val1_p First value.
561 * @param[in] val2_p Second value.
562 * @param[in] mod Whether hash table is being modified.
563 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200564 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200565 */
Radek Krejci857189e2020-09-01 13:26:36 +0200566static ly_bool
567set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200568{
569 struct lyxp_set_hash_node *val1, *val2;
570
571 val1 = (struct lyxp_set_hash_node *)val1_p;
572 val2 = (struct lyxp_set_hash_node *)val2_p;
573
574 if ((val1->node == val2->node) && (val1->type == val2->type)) {
575 return 1;
576 }
577
578 return 0;
579}
580
581/**
582 * @brief Insert node and its hash into set.
583 *
584 * @param[in] set et to insert to.
585 * @param[in] node Node with hash.
586 * @param[in] type Node type.
587 */
588static void
589set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
590{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200591 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200592 uint32_t i, hash;
593 struct lyxp_set_hash_node hnode;
594
595 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
596 /* create hash table and add all the nodes */
597 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
598 for (i = 0; i < set->used; ++i) {
599 hnode.node = set->val.nodes[i].node;
600 hnode.type = set->val.nodes[i].type;
601
602 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
603 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
604 hash = dict_hash_multi(hash, NULL, 0);
605
606 r = lyht_insert(set->ht, &hnode, hash, NULL);
607 assert(!r);
608 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200609
Michal Vasko9d6befd2019-12-11 14:56:56 +0100610 if (hnode.node == node) {
611 /* it was just added, do not add it twice */
612 node = NULL;
613 }
614 }
615 }
616
617 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200618 /* add the new node into hash table */
619 hnode.node = node;
620 hnode.type = type;
621
622 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
623 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
624 hash = dict_hash_multi(hash, NULL, 0);
625
626 r = lyht_insert(set->ht, &hnode, hash, NULL);
627 assert(!r);
628 (void)r;
629 }
630}
631
632/**
633 * @brief Remove node and its hash from set.
634 *
635 * @param[in] set Set to remove from.
636 * @param[in] node Node to remove.
637 * @param[in] type Node type.
638 */
639static void
640set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
641{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200642 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200643 struct lyxp_set_hash_node hnode;
644 uint32_t hash;
645
646 if (set->ht) {
647 hnode.node = node;
648 hnode.type = type;
649
650 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
651 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
652 hash = dict_hash_multi(hash, NULL, 0);
653
654 r = lyht_remove(set->ht, &hnode, hash);
655 assert(!r);
656 (void)r;
657
658 if (!set->ht->used) {
659 lyht_free(set->ht);
660 set->ht = NULL;
661 }
662 }
663}
664
665/**
666 * @brief Check whether node is in set based on its hash.
667 *
668 * @param[in] set Set to search in.
669 * @param[in] node Node to search for.
670 * @param[in] type Node type.
671 * @param[in] skip_idx Index in @p set to skip.
672 * @return LY_ERR
673 */
674static LY_ERR
675set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
676{
677 struct lyxp_set_hash_node hnode, *match_p;
678 uint32_t hash;
679
680 hnode.node = node;
681 hnode.type = type;
682
683 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
684 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
685 hash = dict_hash_multi(hash, NULL, 0);
686
687 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
688 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
689 /* we found it on the index that should be skipped, find another */
690 hnode = *match_p;
691 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
692 /* none other found */
693 return LY_SUCCESS;
694 }
695 }
696
697 return LY_EEXIST;
698 }
699
700 /* not found */
701 return LY_SUCCESS;
702}
703
Michal Vaskod3678892020-05-21 10:06:58 +0200704void
705lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200706{
707 if (!set) {
708 return;
709 }
710
711 if (set->type == LYXP_SET_NODE_SET) {
712 free(set->val.nodes);
713 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200714 } else if (set->type == LYXP_SET_SCNODE_SET) {
715 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200716 lyht_free(set->ht);
717 } else {
718 if (set->type == LYXP_SET_STRING) {
719 free(set->val.str);
720 }
721 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200722 }
Michal Vaskod3678892020-05-21 10:06:58 +0200723
724 set->val.nodes = NULL;
725 set->used = 0;
726 set->size = 0;
727 set->ht = NULL;
728 set->ctx_pos = 0;
729 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200730}
731
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100732/**
733 * @brief Free dynamically-allocated set.
734 *
735 * @param[in] set Set to free.
736 */
737static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200738lyxp_set_free(struct lyxp_set *set)
739{
740 if (!set) {
741 return;
742 }
743
Michal Vaskod3678892020-05-21 10:06:58 +0200744 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200745 free(set);
746}
747
748/**
749 * @brief Initialize set context.
750 *
751 * @param[in] new Set to initialize.
752 * @param[in] set Arbitrary initialized set.
753 */
754static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200755set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200756{
757 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200758 if (set) {
759 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200760 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100761 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200762 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100763 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200764 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200765 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200766 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200767 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200768}
769
770/**
771 * @brief Create a deep copy of a set.
772 *
773 * @param[in] set Set to copy.
774 * @return Copy of @p set.
775 */
776static struct lyxp_set *
777set_copy(struct lyxp_set *set)
778{
779 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100780 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200781
782 if (!set) {
783 return NULL;
784 }
785
786 ret = malloc(sizeof *ret);
787 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
788 set_init(ret, set);
789
790 if (set->type == LYXP_SET_SCNODE_SET) {
791 ret->type = set->type;
792
793 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100794 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
795 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200796 uint32_t idx;
797 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
798 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100799 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200800 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200801 lyxp_set_free(ret);
802 return NULL;
803 }
Michal Vaskoba716542019-12-16 10:01:58 +0100804 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200805 }
806 }
807 } else if (set->type == LYXP_SET_NODE_SET) {
808 ret->type = set->type;
809 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
810 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
811 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
812
813 ret->used = ret->size = set->used;
814 ret->ctx_pos = set->ctx_pos;
815 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100816 if (set->ht) {
817 ret->ht = lyht_dup(set->ht);
818 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200819 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200820 memcpy(ret, set, sizeof *ret);
821 if (set->type == LYXP_SET_STRING) {
822 ret->val.str = strdup(set->val.str);
823 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
824 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200825 }
826
827 return ret;
828}
829
830/**
831 * @brief Fill XPath set with a string. Any current data are disposed of.
832 *
833 * @param[in] set Set to fill.
834 * @param[in] string String to fill into \p set.
835 * @param[in] str_len Length of \p string. 0 is a valid value!
836 */
837static void
838set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
839{
Michal Vaskod3678892020-05-21 10:06:58 +0200840 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200841
842 set->type = LYXP_SET_STRING;
843 if ((str_len == 0) && (string[0] != '\0')) {
844 string = "";
845 }
846 set->val.str = strndup(string, str_len);
847}
848
849/**
850 * @brief Fill XPath set with a number. Any current data are disposed of.
851 *
852 * @param[in] set Set to fill.
853 * @param[in] number Number to fill into \p set.
854 */
855static void
856set_fill_number(struct lyxp_set *set, long double number)
857{
Michal Vaskod3678892020-05-21 10:06:58 +0200858 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200859
860 set->type = LYXP_SET_NUMBER;
861 set->val.num = number;
862}
863
864/**
865 * @brief Fill XPath set with a boolean. Any current data are disposed of.
866 *
867 * @param[in] set Set to fill.
868 * @param[in] boolean Boolean to fill into \p set.
869 */
870static void
Radek Krejci857189e2020-09-01 13:26:36 +0200871set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200872{
Michal Vaskod3678892020-05-21 10:06:58 +0200873 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200874
875 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200876 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877}
878
879/**
880 * @brief Fill XPath set with the value from another set (deep assign).
881 * Any current data are disposed of.
882 *
883 * @param[in] trg Set to fill.
884 * @param[in] src Source set to copy into \p trg.
885 */
886static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200887set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200888{
889 if (!trg || !src) {
890 return;
891 }
892
893 if (trg->type == LYXP_SET_NODE_SET) {
894 free(trg->val.nodes);
895 } else if (trg->type == LYXP_SET_STRING) {
896 free(trg->val.str);
897 }
898 set_init(trg, src);
899
900 if (src->type == LYXP_SET_SCNODE_SET) {
901 trg->type = LYXP_SET_SCNODE_SET;
902 trg->used = src->used;
903 trg->size = src->used;
904
905 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
906 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
907 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
908 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200909 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200910 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200911 set_fill_number(trg, src->val.num);
912 } else if (src->type == LYXP_SET_STRING) {
913 set_fill_string(trg, src->val.str, strlen(src->val.str));
914 } else {
915 if (trg->type == LYXP_SET_NODE_SET) {
916 free(trg->val.nodes);
917 } else if (trg->type == LYXP_SET_STRING) {
918 free(trg->val.str);
919 }
920
Michal Vaskod3678892020-05-21 10:06:58 +0200921 assert(src->type == LYXP_SET_NODE_SET);
922
923 trg->type = LYXP_SET_NODE_SET;
924 trg->used = src->used;
925 trg->size = src->used;
926 trg->ctx_pos = src->ctx_pos;
927 trg->ctx_size = src->ctx_size;
928
929 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
930 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
931 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
932 if (src->ht) {
933 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200934 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200935 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200936 }
937 }
938}
939
940/**
941 * @brief Clear context of all schema nodes.
942 *
943 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200944 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 */
946static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200947set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200948{
949 uint32_t i;
950
951 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100952 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200953 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100954 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
955 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 }
957 }
958}
959
960/**
961 * @brief Remove a node from a set. Removing last node changes
962 * set into LYXP_SET_EMPTY. Context position aware.
963 *
964 * @param[in] set Set to use.
965 * @param[in] idx Index from @p set of the node to be removed.
966 */
967static void
968set_remove_node(struct lyxp_set *set, uint32_t idx)
969{
970 assert(set && (set->type == LYXP_SET_NODE_SET));
971 assert(idx < set->used);
972
973 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
974
975 --set->used;
976 if (set->used) {
977 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
978 (set->used - idx) * sizeof *set->val.nodes);
979 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200980 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200981 }
982}
983
984/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100985 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200986 *
987 * @param[in] set Set to use.
988 * @param[in] idx Index from @p set of the node to be removed.
989 */
990static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100991set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200992{
993 assert(set && (set->type == LYXP_SET_NODE_SET));
994 assert(idx < set->used);
995
Michal Vasko2caefc12019-11-14 16:07:56 +0100996 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
997 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
998 }
999 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001000}
1001
1002/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001003 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001004 * set into LYXP_SET_EMPTY. Context position aware.
1005 *
1006 * @param[in] set Set to consolidate.
1007 */
1008static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001009set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001010{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001011 uint32_t i, orig_used, end = 0;
1012 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001013
Michal Vaskod3678892020-05-21 10:06:58 +02001014 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001015
1016 orig_used = set->used;
1017 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001018 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001019 start = -1;
1020 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001021 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001022 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001023 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001024 end = i;
1025 ++i;
1026 break;
1027 }
1028
1029 ++i;
1030 if (i == orig_used) {
1031 end = i;
1032 }
1033 } while (i < orig_used);
1034
1035 if (start > -1) {
1036 /* move the whole chunk of valid nodes together */
1037 if (set->used != (unsigned)start) {
1038 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1039 }
1040 set->used += end - start;
1041 }
1042 }
Michal Vasko57eab132019-09-24 11:46:26 +02001043}
1044
1045/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001046 * @brief Check for duplicates in a node set.
1047 *
1048 * @param[in] set Set to check.
1049 * @param[in] node Node to look for in @p set.
1050 * @param[in] node_type Type of @p node.
1051 * @param[in] skip_idx Index from @p set to skip.
1052 * @return LY_ERR
1053 */
1054static LY_ERR
1055set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1056{
1057 uint32_t i;
1058
Michal Vasko2caefc12019-11-14 16:07:56 +01001059 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001060 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1061 }
1062
1063 for (i = 0; i < set->used; ++i) {
1064 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1065 continue;
1066 }
1067
1068 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1069 return LY_EEXIST;
1070 }
1071 }
1072
1073 return LY_SUCCESS;
1074}
1075
Radek Krejci857189e2020-09-01 13:26:36 +02001076ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001077lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1078 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001079{
1080 uint32_t i;
1081
1082 for (i = 0; i < set->used; ++i) {
1083 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1084 continue;
1085 }
1086
1087 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001088 if (index_p) {
1089 *index_p = i;
1090 }
1091 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001092 }
1093 }
1094
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001095 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001096}
1097
Michal Vaskoecd62de2019-11-13 12:35:11 +01001098void
1099lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001100{
1101 uint32_t orig_used, i, j;
1102
Michal Vaskod3678892020-05-21 10:06:58 +02001103 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001104
Michal Vaskod3678892020-05-21 10:06:58 +02001105 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106 return;
1107 }
1108
Michal Vaskod3678892020-05-21 10:06:58 +02001109 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110 memcpy(set1, set2, sizeof *set1);
1111 return;
1112 }
1113
1114 if (set1->used + set2->used > set1->size) {
1115 set1->size = set1->used + set2->used;
1116 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1117 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1118 }
1119
1120 orig_used = set1->used;
1121
1122 for (i = 0; i < set2->used; ++i) {
1123 for (j = 0; j < orig_used; ++j) {
1124 /* detect duplicities */
1125 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1126 break;
1127 }
1128 }
1129
Michal Vasko0587bce2020-12-10 12:19:21 +01001130 if (j < orig_used) {
1131 /* node is there, but update its status if needed */
1132 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1133 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001134 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1135 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1136 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001137 }
1138 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001139 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1140 ++set1->used;
1141 }
1142 }
1143
Michal Vaskod3678892020-05-21 10:06:58 +02001144 lyxp_set_free_content(set2);
1145 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001146}
1147
1148/**
1149 * @brief Insert a node into a set. Context position aware.
1150 *
1151 * @param[in] set Set to use.
1152 * @param[in] node Node to insert to @p set.
1153 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1154 * @param[in] node_type Node type of @p node.
1155 * @param[in] idx Index in @p set to insert into.
1156 */
1157static void
1158set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1159{
Michal Vaskod3678892020-05-21 10:06:58 +02001160 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001161
Michal Vaskod3678892020-05-21 10:06:58 +02001162 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001163 /* first item */
1164 if (idx) {
1165 /* no real harm done, but it is a bug */
1166 LOGINT(set->ctx);
1167 idx = 0;
1168 }
1169 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1170 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1171 set->type = LYXP_SET_NODE_SET;
1172 set->used = 0;
1173 set->size = LYXP_SET_SIZE_START;
1174 set->ctx_pos = 1;
1175 set->ctx_size = 1;
1176 set->ht = NULL;
1177 } else {
1178 /* not an empty set */
1179 if (set->used == set->size) {
1180
1181 /* set is full */
1182 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1183 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1184 set->size += LYXP_SET_SIZE_STEP;
1185 }
1186
1187 if (idx > set->used) {
1188 LOGINT(set->ctx);
1189 idx = set->used;
1190 }
1191
1192 /* make space for the new node */
1193 if (idx < set->used) {
1194 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1195 }
1196 }
1197
1198 /* finally assign the value */
1199 set->val.nodes[idx].node = (struct lyd_node *)node;
1200 set->val.nodes[idx].type = node_type;
1201 set->val.nodes[idx].pos = pos;
1202 ++set->used;
1203
Michal Vasko2caefc12019-11-14 16:07:56 +01001204 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1205 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1206 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001207}
1208
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001209LY_ERR
1210lyxp_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 +02001211{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001212 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001213
1214 assert(set->type == LYXP_SET_SCNODE_SET);
1215
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001216 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001217 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001218 } else {
1219 if (set->used == set->size) {
1220 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 +02001221 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001222 set->size += LYXP_SET_SIZE_STEP;
1223 }
1224
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001225 index = set->used;
1226 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1227 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001228 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001229 ++set->used;
1230 }
1231
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001232 if (index_p) {
1233 *index_p = index;
1234 }
1235
1236 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001237}
1238
1239/**
1240 * @brief Replace a node in a set with another. Context position aware.
1241 *
1242 * @param[in] set Set to use.
1243 * @param[in] node Node to insert to @p set.
1244 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1245 * @param[in] node_type Node type of @p node.
1246 * @param[in] idx Index in @p set of the node to replace.
1247 */
1248static void
1249set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1250{
1251 assert(set && (idx < set->used));
1252
Michal Vasko2caefc12019-11-14 16:07:56 +01001253 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1254 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1255 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001256 set->val.nodes[idx].node = (struct lyd_node *)node;
1257 set->val.nodes[idx].type = node_type;
1258 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001259 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1260 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1261 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001262}
1263
1264/**
1265 * @brief Set all nodes with ctx 1 to a new unique context value.
1266 *
1267 * @param[in] set Set to modify.
1268 * @return New context value.
1269 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001270static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001271set_scnode_new_in_ctx(struct lyxp_set *set)
1272{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001273 uint32_t i;
1274 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001275
1276 assert(set->type == LYXP_SET_SCNODE_SET);
1277
Radek Krejcif13b87b2020-12-01 22:02:17 +01001278 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001279retry:
1280 for (i = 0; i < set->used; ++i) {
1281 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1282 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1283 goto retry;
1284 }
1285 }
1286 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001287 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288 set->val.scnodes[i].in_ctx = ret_ctx;
1289 }
1290 }
1291
1292 return ret_ctx;
1293}
1294
1295/**
1296 * @brief Get unique @p node position in the data.
1297 *
1298 * @param[in] node Node to find.
1299 * @param[in] node_type Node type of @p node.
1300 * @param[in] root Root node.
1301 * @param[in] root_type Type of the XPath @p root node.
1302 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1303 * be used to increase efficiency and start the DFS from this node.
1304 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1305 * @return Node position.
1306 */
1307static uint32_t
1308get_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 +02001309 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001310{
Michal Vasko56daf732020-08-10 10:57:18 +02001311 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001313 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314
1315 assert(prev && prev_pos && !root->prev->next);
1316
1317 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1318 return 0;
1319 }
1320
1321 if (*prev) {
1322 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001324 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001325 goto dfs_search;
1326 }
1327
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001328 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001329 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001331 if (*prev && !elem) {
1332 /* resume previous DFS */
1333 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1334 LYD_TREE_DFS_continue = 0;
1335 }
1336
Michal Vasko03ff5a72019-09-11 13:49:33 +02001337 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001338 /* skip */
1339 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001341 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001342 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001343 break;
1344 }
Michal Vasko56daf732020-08-10 10:57:18 +02001345 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001346 }
Michal Vasko56daf732020-08-10 10:57:18 +02001347
1348 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 }
1350
1351 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001352 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001353 break;
1354 }
1355 }
1356
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001357 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358 if (!(*prev)) {
1359 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001360 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 return 0;
1362 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001363 /* start the search again from the beginning */
1364 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365
Michal Vasko56daf732020-08-10 10:57:18 +02001366 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001367 pos = 1;
1368 goto dfs_search;
1369 }
1370 }
1371
1372 /* remember the last found node for next time */
1373 *prev = node;
1374 *prev_pos = pos;
1375
1376 return pos;
1377}
1378
1379/**
1380 * @brief Assign (fill) missing node positions.
1381 *
1382 * @param[in] set Set to fill positions in.
1383 * @param[in] root Context root node.
1384 * @param[in] root_type Context root type.
1385 * @return LY_ERR
1386 */
1387static LY_ERR
1388set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1389{
1390 const struct lyd_node *prev = NULL, *tmp_node;
1391 uint32_t i, tmp_pos = 0;
1392
1393 for (i = 0; i < set->used; ++i) {
1394 if (!set->val.nodes[i].pos) {
1395 tmp_node = NULL;
1396 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001397 case LYXP_NODE_META:
1398 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399 if (!tmp_node) {
1400 LOGINT_RET(root->schema->module->ctx);
1401 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001402 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001403 case LYXP_NODE_ELEM:
1404 case LYXP_NODE_TEXT:
1405 if (!tmp_node) {
1406 tmp_node = set->val.nodes[i].node;
1407 }
1408 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1409 break;
1410 default:
1411 /* all roots have position 0 */
1412 break;
1413 }
1414 }
1415 }
1416
1417 return LY_SUCCESS;
1418}
1419
1420/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001421 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001423 * @param[in] meta Metadata to use.
1424 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425 */
1426static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001427get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001428{
1429 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001430 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001431
Michal Vasko9f96a052020-03-10 09:41:45 +01001432 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 ++pos;
1434 }
1435
Michal Vasko9f96a052020-03-10 09:41:45 +01001436 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 return pos;
1438}
1439
1440/**
1441 * @brief Compare 2 nodes in respect to XPath document order.
1442 *
1443 * @param[in] item1 1st node.
1444 * @param[in] item2 2nd node.
1445 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1446 */
1447static int
1448set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1449{
Michal Vasko9f96a052020-03-10 09:41:45 +01001450 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451
1452 if (item1->pos < item2->pos) {
1453 return -1;
1454 }
1455
1456 if (item1->pos > item2->pos) {
1457 return 1;
1458 }
1459
1460 /* node positions are equal, the fun case */
1461
1462 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1463 /* special case since text nodes are actually saved as their parents */
1464 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1465 if (item1->type == LYXP_NODE_ELEM) {
1466 assert(item2->type == LYXP_NODE_TEXT);
1467 return -1;
1468 } else {
1469 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1470 return 1;
1471 }
1472 }
1473
Michal Vasko9f96a052020-03-10 09:41:45 +01001474 /* we need meta positions now */
1475 if (item1->type == LYXP_NODE_META) {
1476 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001478 if (item2->type == LYXP_NODE_META) {
1479 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480 }
1481
Michal Vasko9f96a052020-03-10 09:41:45 +01001482 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001483 /* check for duplicates */
1484 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001485 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001486 return 0;
1487 }
1488
Michal Vasko9f96a052020-03-10 09:41:45 +01001489 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001490 /* elem is always first, 2nd node is after it */
1491 if (item1->type == LYXP_NODE_ELEM) {
1492 assert(item2->type != LYXP_NODE_ELEM);
1493 return -1;
1494 }
1495
Michal Vasko9f96a052020-03-10 09:41:45 +01001496 /* 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 +02001497 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001498 if (((item1->type == LYXP_NODE_TEXT) &&
1499 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1500 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1501 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1502 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001503 return 1;
1504 }
1505
Michal Vasko9f96a052020-03-10 09:41:45 +01001506 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001507 /* 2nd is after 1st */
1508 return -1;
1509}
1510
1511/**
1512 * @brief Set cast for comparisons.
1513 *
1514 * @param[in] trg Target set to cast source into.
1515 * @param[in] src Source set.
1516 * @param[in] type Target set type.
1517 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518 * @return LY_ERR
1519 */
1520static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001521set_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 +02001522{
1523 assert(src->type == LYXP_SET_NODE_SET);
1524
1525 set_init(trg, src);
1526
1527 /* insert node into target set */
1528 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1529
1530 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001531 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001532}
1533
Michal Vasko4c7763f2020-07-27 17:40:37 +02001534/**
1535 * @brief Set content canonization for comparisons.
1536 *
1537 * @param[in] trg Target set to put the canononized source into.
1538 * @param[in] src Source set.
1539 * @param[in] xp_node Source XPath node/meta to use for canonization.
1540 * @return LY_ERR
1541 */
1542static LY_ERR
1543set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1544{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001545 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001546 struct lyd_value val;
1547 struct ly_err_item *err = NULL;
1548 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001549 LY_ERR rc;
1550
1551 /* is there anything to canonize even? */
1552 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1553 /* do we have a type to use for canonization? */
1554 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1555 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1556 } else if (xp_node->type == LYXP_NODE_META) {
1557 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1558 }
1559 }
1560 if (!type) {
1561 goto fill;
1562 }
1563
1564 if (src->type == LYXP_SET_NUMBER) {
1565 /* canonize number */
1566 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1567 LOGMEM(src->ctx);
1568 return LY_EMEM;
1569 }
1570 } else {
1571 /* canonize string */
1572 str = strdup(src->val.str);
1573 }
1574
1575 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001576 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 +01001577 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001578 ly_err_free(err);
1579 if (rc) {
1580 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001581 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001582 goto fill;
1583 }
1584
Michal Vasko4c7763f2020-07-27 17:40:37 +02001585 /* use the canonized value */
1586 set_init(trg, src);
1587 trg->type = src->type;
1588 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001589 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001590 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1591 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001592 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001593 }
1594 type->plugin->free(src->ctx, &val);
1595 return LY_SUCCESS;
1596
1597fill:
1598 /* no canonization needed/possible */
1599 set_fill_set(trg, src);
1600 return LY_SUCCESS;
1601}
1602
Michal Vasko03ff5a72019-09-11 13:49:33 +02001603#ifndef NDEBUG
1604
1605/**
1606 * @brief Bubble sort @p set into XPath document order.
1607 * Context position aware. Unused in the 'Release' build target.
1608 *
1609 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001610 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1611 */
1612static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001613set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614{
1615 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001616 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001617 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001618 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001619 struct lyxp_set_node item;
1620 struct lyxp_set_hash_node hnode;
1621 uint64_t hash;
1622
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001623 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001624 return 0;
1625 }
1626
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001627 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001628 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001629 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630
1631 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001632 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001633 return -1;
1634 }
1635
1636 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1637 print_set_debug(set);
1638
1639 for (i = 0; i < set->used; ++i) {
1640 inverted = 0;
1641 change = 0;
1642
1643 for (j = 1; j < set->used - i; ++j) {
1644 /* compare node positions */
1645 if (inverted) {
1646 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1647 } else {
1648 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1649 }
1650
1651 /* swap if needed */
1652 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1653 change = 1;
1654
1655 item = set->val.nodes[j - 1];
1656 set->val.nodes[j - 1] = set->val.nodes[j];
1657 set->val.nodes[j] = item;
1658 } else {
1659 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1660 inverted = !inverted;
1661 }
1662 }
1663
1664 ++ret;
1665
1666 if (!change) {
1667 break;
1668 }
1669 }
1670
1671 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1672 print_set_debug(set);
1673
1674 /* check node hashes */
1675 if (set->used >= LYD_HT_MIN_ITEMS) {
1676 assert(set->ht);
1677 for (i = 0; i < set->used; ++i) {
1678 hnode.node = set->val.nodes[i].node;
1679 hnode.type = set->val.nodes[i].type;
1680
1681 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1682 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1683 hash = dict_hash_multi(hash, NULL, 0);
1684
1685 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1686 }
1687 }
1688
1689 return ret - 1;
1690}
1691
1692/**
1693 * @brief Remove duplicate entries in a sorted node set.
1694 *
1695 * @param[in] set Sorted set to check.
1696 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1697 */
1698static LY_ERR
1699set_sorted_dup_node_clean(struct lyxp_set *set)
1700{
1701 uint32_t i = 0;
1702 LY_ERR ret = LY_SUCCESS;
1703
1704 if (set->used > 1) {
1705 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001706 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1707 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001708 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001709 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710 }
Michal Vasko57eab132019-09-24 11:46:26 +02001711 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712 }
1713 }
1714
Michal Vasko2caefc12019-11-14 16:07:56 +01001715 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716 return ret;
1717}
1718
1719#endif
1720
1721/**
1722 * @brief Merge 2 sorted sets into one.
1723 *
1724 * @param[in,out] trg Set to merge into. Duplicates are removed.
1725 * @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 +02001726 * @return LY_ERR
1727 */
1728static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001729set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730{
1731 uint32_t i, j, k, count, dup_count;
1732 int cmp;
1733 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001734
Michal Vaskod3678892020-05-21 10:06:58 +02001735 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001736 return LY_EINVAL;
1737 }
1738
Michal Vaskod3678892020-05-21 10:06:58 +02001739 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001741 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001742 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001743 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001744 return LY_SUCCESS;
1745 }
1746
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001747 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001748 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001749 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750
1751 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001752 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001753 return LY_EINT;
1754 }
1755
1756#ifndef NDEBUG
1757 LOGDBG(LY_LDGXPATH, "MERGE target");
1758 print_set_debug(trg);
1759 LOGDBG(LY_LDGXPATH, "MERGE source");
1760 print_set_debug(src);
1761#endif
1762
1763 /* make memory for the merge (duplicates are not detected yet, so space
1764 * will likely be wasted on them, too bad) */
1765 if (trg->size - trg->used < src->used) {
1766 trg->size = trg->used + src->used;
1767
1768 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1769 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1770 }
1771
1772 i = 0;
1773 j = 0;
1774 count = 0;
1775 dup_count = 0;
1776 do {
1777 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1778 if (!cmp) {
1779 if (!count) {
1780 /* duplicate, just skip it */
1781 ++i;
1782 ++j;
1783 } else {
1784 /* we are copying something already, so let's copy the duplicate too,
1785 * we are hoping that afterwards there are some more nodes to
1786 * copy and this way we can copy them all together */
1787 ++count;
1788 ++dup_count;
1789 ++i;
1790 ++j;
1791 }
1792 } else if (cmp < 0) {
1793 /* inserting src node into trg, just remember it for now */
1794 ++count;
1795 ++i;
1796
1797 /* insert the hash now */
1798 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1799 } else if (count) {
1800copy_nodes:
1801 /* time to actually copy the nodes, we have found the largest block of nodes */
1802 memmove(&trg->val.nodes[j + (count - dup_count)],
1803 &trg->val.nodes[j],
1804 (trg->used - j) * sizeof *trg->val.nodes);
1805 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1806
1807 trg->used += count - dup_count;
1808 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1809 j += count - dup_count;
1810
1811 count = 0;
1812 dup_count = 0;
1813 } else {
1814 ++j;
1815 }
1816 } while ((i < src->used) && (j < trg->used));
1817
1818 if ((i < src->used) || count) {
1819 /* insert all the hashes first */
1820 for (k = i; k < src->used; ++k) {
1821 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1822 }
1823
1824 /* loop ended, but we need to copy something at trg end */
1825 count += src->used - i;
1826 i = src->used;
1827 goto copy_nodes;
1828 }
1829
1830 /* we are inserting hashes before the actual node insert, which causes
1831 * situations when there were initially not enough items for a hash table,
1832 * but even after some were inserted, hash table was not created (during
1833 * insertion the number of items is not updated yet) */
1834 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1835 set_insert_node_hash(trg, NULL, 0);
1836 }
1837
1838#ifndef NDEBUG
1839 LOGDBG(LY_LDGXPATH, "MERGE result");
1840 print_set_debug(trg);
1841#endif
1842
Michal Vaskod3678892020-05-21 10:06:58 +02001843 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001844 return LY_SUCCESS;
1845}
1846
1847/*
1848 * (re)parse functions
1849 *
1850 * Parse functions parse the expression into
1851 * tokens (syntactic analysis).
1852 *
1853 * Reparse functions perform semantic analysis
1854 * (do not save the result, just a check) of
1855 * the expression and fill repeat indices.
1856 */
1857
Michal Vasko14676352020-05-29 11:35:55 +02001858LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001859lyxp_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 +02001860{
Michal Vasko004d3152020-06-11 19:59:22 +02001861 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001862 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001863 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001864 }
Michal Vasko14676352020-05-29 11:35:55 +02001865 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001866 }
1867
Michal Vasko004d3152020-06-11 19:59:22 +02001868 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001869 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001870 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001871 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001872 }
Michal Vasko14676352020-05-29 11:35:55 +02001873 return LY_ENOT;
1874 }
1875
1876 return LY_SUCCESS;
1877}
1878
Michal Vasko004d3152020-06-11 19:59:22 +02001879LY_ERR
1880lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1881{
1882 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1883
1884 /* skip the token */
1885 ++(*tok_idx);
1886
1887 return LY_SUCCESS;
1888}
1889
Michal Vasko14676352020-05-29 11:35:55 +02001890/* just like lyxp_check_token() but tests for 2 tokens */
1891static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001892exp_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 +02001893 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001894{
Michal Vasko004d3152020-06-11 19:59:22 +02001895 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001896 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001897 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001898 }
1899 return LY_EINCOMPLETE;
1900 }
1901
Michal Vasko004d3152020-06-11 19:59:22 +02001902 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001903 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001904 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001905 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001906 }
1907 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001908 }
1909
1910 return LY_SUCCESS;
1911}
1912
1913/**
1914 * @brief Stack operation push on the repeat array.
1915 *
1916 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001917 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1919 */
1920static void
Michal Vasko004d3152020-06-11 19:59:22 +02001921exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922{
1923 uint16_t i;
1924
Michal Vasko004d3152020-06-11 19:59:22 +02001925 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001926 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001927 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1928 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1929 exp->repeat[tok_idx][i] = repeat_op_idx;
1930 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001932 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1933 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1934 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935 }
1936}
1937
1938/**
1939 * @brief Reparse Predicate. Logs directly on error.
1940 *
1941 * [7] Predicate ::= '[' Expr ']'
1942 *
1943 * @param[in] ctx Context for logging.
1944 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001945 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946 * @return LY_ERR
1947 */
1948static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001949reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001950{
1951 LY_ERR rc;
1952
Michal Vasko004d3152020-06-11 19:59:22 +02001953 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001955 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001956
Michal Vasko004d3152020-06-11 19:59:22 +02001957 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958 LY_CHECK_RET(rc);
1959
Michal Vasko004d3152020-06-11 19:59:22 +02001960 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001962 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001963
1964 return LY_SUCCESS;
1965}
1966
1967/**
1968 * @brief Reparse RelativeLocationPath. Logs directly on error.
1969 *
1970 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1971 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1972 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1973 *
1974 * @param[in] ctx Context for logging.
1975 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001976 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1978 */
1979static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001980reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981{
1982 LY_ERR rc;
1983
Michal Vasko004d3152020-06-11 19:59:22 +02001984 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 LY_CHECK_RET(rc);
1986
1987 goto step;
1988 do {
1989 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001990 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991
Michal Vasko004d3152020-06-11 19:59:22 +02001992 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 LY_CHECK_RET(rc);
1994step:
1995 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001996 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001998 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999 break;
2000
2001 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002002 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003 break;
2004
2005 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002006 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007
Michal Vasko004d3152020-06-11 19:59:22 +02002008 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002010 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002011 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 +02002012 return LY_EVALID;
2013 }
Radek Krejci0f969882020-08-21 16:56:47 +02002014 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002016 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 goto reparse_predicate;
2018 break;
2019
2020 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002021 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022
2023 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002024 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002026 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027
2028 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002029 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002031 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032
2033reparse_predicate:
2034 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002035 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2036 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 LY_CHECK_RET(rc);
2038 }
2039 break;
2040 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002041 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 +02002042 return LY_EVALID;
2043 }
Michal Vasko004d3152020-06-11 19:59:22 +02002044 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045
2046 return LY_SUCCESS;
2047}
2048
2049/**
2050 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2051 *
2052 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2053 *
2054 * @param[in] ctx Context for logging.
2055 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002056 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 * @return LY_ERR
2058 */
2059static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002060reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061{
2062 LY_ERR rc;
2063
Michal Vasko004d3152020-06-11 19:59:22 +02002064 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 +02002065
2066 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002067 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002069 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070
Michal Vasko004d3152020-06-11 19:59:22 +02002071 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072 return LY_SUCCESS;
2073 }
Michal Vasko004d3152020-06-11 19:59:22 +02002074 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075 case LYXP_TOKEN_DOT:
2076 case LYXP_TOKEN_DDOT:
2077 case LYXP_TOKEN_AT:
2078 case LYXP_TOKEN_NAMETEST:
2079 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002080 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002082 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 default:
2084 break;
2085 }
2086
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002088 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002089 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090
Michal Vasko004d3152020-06-11 19:59:22 +02002091 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 LY_CHECK_RET(rc);
2093 }
2094
2095 return LY_SUCCESS;
2096}
2097
2098/**
2099 * @brief Reparse FunctionCall. Logs directly on error.
2100 *
2101 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2102 *
2103 * @param[in] ctx Context for logging.
2104 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002105 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106 * @return LY_ERR
2107 */
2108static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002109reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002111 int8_t min_arg_count = -1;
2112 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002113 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 LY_ERR rc;
2115
Michal Vasko004d3152020-06-11 19:59:22 +02002116 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002118 func_tok_idx = *tok_idx;
2119 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002121 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122 min_arg_count = 1;
2123 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002124 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 min_arg_count = 1;
2126 max_arg_count = 1;
2127 }
2128 break;
2129 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002130 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002131 min_arg_count = 1;
2132 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002133 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 min_arg_count = 0;
2135 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002136 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 0;
2138 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 min_arg_count = 0;
2141 max_arg_count = 0;
2142 }
2143 break;
2144 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002145 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 min_arg_count = 1;
2147 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002148 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 0;
2150 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 1;
2153 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002154 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 1;
2156 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002157 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 min_arg_count = 1;
2159 max_arg_count = 1;
2160 }
2161 break;
2162 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002163 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002165 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002166 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 0;
2168 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002169 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 min_arg_count = 0;
2171 max_arg_count = 1;
2172 }
2173 break;
2174 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002175 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 min_arg_count = 1;
2177 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002178 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 1;
2180 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002181 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 0;
2183 max_arg_count = 0;
2184 }
2185 break;
2186 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002187 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002188 min_arg_count = 2;
2189 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002190 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 0;
2192 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002193 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 2;
2195 max_arg_count = 2;
2196 }
2197 break;
2198 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002199 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 min_arg_count = 2;
2201 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002202 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 3;
2204 max_arg_count = 3;
2205 }
2206 break;
2207 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002208 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 min_arg_count = 0;
2210 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002211 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 1;
2213 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002214 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 2;
2216 max_arg_count = 2;
2217 }
2218 break;
2219 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002220 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 2;
2222 max_arg_count = 2;
2223 }
2224 break;
2225 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002226 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 2;
2228 max_arg_count = 2;
2229 }
2230 break;
2231 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002232 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 min_arg_count = 0;
2234 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002235 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002236 min_arg_count = 0;
2237 max_arg_count = 1;
2238 }
2239 break;
2240 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002241 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 0;
2243 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002244 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 min_arg_count = 2;
2246 max_arg_count = 2;
2247 }
2248 break;
2249 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002250 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002251 min_arg_count = 2;
2252 max_arg_count = 2;
2253 }
2254 break;
2255 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002256 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 min_arg_count = 2;
2258 max_arg_count = 2;
2259 }
2260 break;
2261 }
2262 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002263 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 +02002264 return LY_EINVAL;
2265 }
Michal Vasko004d3152020-06-11 19:59:22 +02002266 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267
2268 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002269 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002271 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272
2273 /* ( Expr ( ',' Expr )* )? */
2274 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002275 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002277 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002279 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 LY_CHECK_RET(rc);
2281 }
Michal Vasko004d3152020-06-11 19:59:22 +02002282 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2283 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284
2285 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002286 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287 LY_CHECK_RET(rc);
2288 }
2289
2290 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002291 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002293 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002294
Radek Krejci857189e2020-09-01 13:26:36 +02002295 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002296 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 +02002297 return LY_EVALID;
2298 }
2299
2300 return LY_SUCCESS;
2301}
2302
2303/**
2304 * @brief Reparse PathExpr. Logs directly on error.
2305 *
2306 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2307 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2308 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2309 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2310 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2311 *
2312 * @param[in] ctx Context for logging.
2313 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002314 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 * @return LY_ERR
2316 */
2317static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002318reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002319{
2320 LY_ERR rc;
2321
Michal Vasko004d3152020-06-11 19:59:22 +02002322 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002323 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324 }
2325
Michal Vasko004d3152020-06-11 19:59:22 +02002326 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002327 case LYXP_TOKEN_PAR1:
2328 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002329 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330
Michal Vasko004d3152020-06-11 19:59:22 +02002331 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 LY_CHECK_RET(rc);
2333
Michal Vasko004d3152020-06-11 19:59:22 +02002334 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002336 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 goto predicate;
2338 break;
2339 case LYXP_TOKEN_DOT:
2340 case LYXP_TOKEN_DDOT:
2341 case LYXP_TOKEN_AT:
2342 case LYXP_TOKEN_NAMETEST:
2343 case LYXP_TOKEN_NODETYPE:
2344 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002345 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002346 LY_CHECK_RET(rc);
2347 break;
2348 case LYXP_TOKEN_FUNCNAME:
2349 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002350 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002351 LY_CHECK_RET(rc);
2352 goto predicate;
2353 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002354 case LYXP_TOKEN_OPER_PATH:
2355 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002357 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358 LY_CHECK_RET(rc);
2359 break;
2360 case LYXP_TOKEN_LITERAL:
2361 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002362 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 goto predicate;
2364 break;
2365 case LYXP_TOKEN_NUMBER:
2366 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002367 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 goto predicate;
2369 break;
2370 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002371 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 +02002372 return LY_EVALID;
2373 }
2374
2375 return LY_SUCCESS;
2376
2377predicate:
2378 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002379 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2380 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002381 LY_CHECK_RET(rc);
2382 }
2383
2384 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002385 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386
2387 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002388 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002389
Michal Vasko004d3152020-06-11 19:59:22 +02002390 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391 LY_CHECK_RET(rc);
2392 }
2393
2394 return LY_SUCCESS;
2395}
2396
2397/**
2398 * @brief Reparse UnaryExpr. Logs directly on error.
2399 *
2400 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2401 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2402 *
2403 * @param[in] ctx Context for logging.
2404 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002405 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 * @return LY_ERR
2407 */
2408static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002409reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410{
2411 uint16_t prev_exp;
2412 LY_ERR rc;
2413
2414 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002415 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002416 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2417 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002419 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 }
2421
2422 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002423 prev_exp = *tok_idx;
2424 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002425 LY_CHECK_RET(rc);
2426
2427 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002428 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002429 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002430 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431
Michal Vasko004d3152020-06-11 19:59:22 +02002432 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002433 LY_CHECK_RET(rc);
2434 }
2435
2436 return LY_SUCCESS;
2437}
2438
2439/**
2440 * @brief Reparse AdditiveExpr. Logs directly on error.
2441 *
2442 * [15] AdditiveExpr ::= MultiplicativeExpr
2443 * | AdditiveExpr '+' MultiplicativeExpr
2444 * | AdditiveExpr '-' MultiplicativeExpr
2445 * [16] MultiplicativeExpr ::= UnaryExpr
2446 * | MultiplicativeExpr '*' UnaryExpr
2447 * | MultiplicativeExpr 'div' UnaryExpr
2448 * | MultiplicativeExpr 'mod' UnaryExpr
2449 *
2450 * @param[in] ctx Context for logging.
2451 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002452 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 * @return LY_ERR
2454 */
2455static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002456reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002457{
2458 uint16_t prev_add_exp, prev_mul_exp;
2459 LY_ERR rc;
2460
Michal Vasko004d3152020-06-11 19:59:22 +02002461 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462 goto reparse_multiplicative_expr;
2463
2464 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002465 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2466 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002467 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002468 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469
2470reparse_multiplicative_expr:
2471 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002472 prev_mul_exp = *tok_idx;
2473 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002474 LY_CHECK_RET(rc);
2475
2476 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002477 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2478 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002479 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002480 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481
Michal Vasko004d3152020-06-11 19:59:22 +02002482 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483 LY_CHECK_RET(rc);
2484 }
2485 }
2486
2487 return LY_SUCCESS;
2488}
2489
2490/**
2491 * @brief Reparse EqualityExpr. Logs directly on error.
2492 *
2493 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2494 * | EqualityExpr '!=' RelationalExpr
2495 * [14] RelationalExpr ::= AdditiveExpr
2496 * | RelationalExpr '<' AdditiveExpr
2497 * | RelationalExpr '>' AdditiveExpr
2498 * | RelationalExpr '<=' AdditiveExpr
2499 * | RelationalExpr '>=' AdditiveExpr
2500 *
2501 * @param[in] ctx Context for logging.
2502 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002503 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 * @return LY_ERR
2505 */
2506static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002507reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002508{
2509 uint16_t prev_eq_exp, prev_rel_exp;
2510 LY_ERR rc;
2511
Michal Vasko004d3152020-06-11 19:59:22 +02002512 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 goto reparse_additive_expr;
2514
2515 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002516 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002518 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002519
2520reparse_additive_expr:
2521 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002522 prev_rel_exp = *tok_idx;
2523 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524 LY_CHECK_RET(rc);
2525
2526 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002527 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002528 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002529 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002530
Michal Vasko004d3152020-06-11 19:59:22 +02002531 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002532 LY_CHECK_RET(rc);
2533 }
2534 }
2535
2536 return LY_SUCCESS;
2537}
2538
2539/**
2540 * @brief Reparse OrExpr. Logs directly on error.
2541 *
2542 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2543 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2544 *
2545 * @param[in] ctx Context for logging.
2546 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002547 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548 * @return LY_ERR
2549 */
2550static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002551reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002552{
2553 uint16_t prev_or_exp, prev_and_exp;
2554 LY_ERR rc;
2555
Michal Vasko004d3152020-06-11 19:59:22 +02002556 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002557 goto reparse_equality_expr;
2558
2559 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002560 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 +02002561 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002562 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563
2564reparse_equality_expr:
2565 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002566 prev_and_exp = *tok_idx;
2567 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002568 LY_CHECK_RET(rc);
2569
2570 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002571 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002572 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002573 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002574
Michal Vasko004d3152020-06-11 19:59:22 +02002575 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002576 LY_CHECK_RET(rc);
2577 }
2578 }
2579
2580 return LY_SUCCESS;
2581}
Radek Krejcib1646a92018-11-02 16:08:26 +01002582
2583/**
2584 * @brief Parse NCName.
2585 *
2586 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002587 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002588 */
Radek Krejcid4270262019-01-07 15:07:25 +01002589static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002590parse_ncname(const char *ncname)
2591{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002592 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002593 size_t size;
2594 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002595
2596 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2597 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2598 return len;
2599 }
2600
2601 do {
2602 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002603 if (!*ncname) {
2604 break;
2605 }
Radek Krejcid4270262019-01-07 15:07:25 +01002606 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002607 } while (is_xmlqnamechar(uc) && (uc != ':'));
2608
2609 return len;
2610}
2611
2612/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002613 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002614 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002616 * @param[in] exp Expression to use.
2617 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002618 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002619 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002620 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002621 */
2622static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002623exp_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 +01002624{
2625 uint32_t prev;
2626
2627 if (exp->used == exp->size) {
2628 prev = exp->size;
2629 exp->size += LYXP_EXPR_SIZE_STEP;
2630 if (prev > exp->size) {
2631 LOGINT(ctx);
2632 return LY_EINT;
2633 }
2634
2635 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2636 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2637 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2638 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2639 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2640 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2641 }
2642
2643 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002644 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002645 exp->tok_len[exp->used] = tok_len;
2646 ++exp->used;
2647 return LY_SUCCESS;
2648}
2649
2650void
Michal Vasko14676352020-05-29 11:35:55 +02002651lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002652{
2653 uint16_t i;
2654
2655 if (!expr) {
2656 return;
2657 }
2658
2659 lydict_remove(ctx, expr->expr);
2660 free(expr->tokens);
2661 free(expr->tok_pos);
2662 free(expr->tok_len);
2663 if (expr->repeat) {
2664 for (i = 0; i < expr->used; ++i) {
2665 free(expr->repeat[i]);
2666 }
2667 }
2668 free(expr->repeat);
2669 free(expr);
2670}
2671
Radek Krejcif03a9e22020-09-18 20:09:31 +02002672LY_ERR
2673lyxp_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 +01002674{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002675 LY_ERR ret = LY_SUCCESS;
2676 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002677 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002678 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002679 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002680 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002681
Radek Krejcif03a9e22020-09-18 20:09:31 +02002682 assert(expr_p);
2683
2684 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002685 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002686 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002687 }
2688
2689 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002690 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002691 }
2692 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002693 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002695 }
2696
2697 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002698 expr = calloc(1, sizeof *expr);
2699 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2700 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2701 expr->used = 0;
2702 expr->size = LYXP_EXPR_SIZE_START;
2703 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2704 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002705
Radek Krejcif03a9e22020-09-18 20:09:31 +02002706 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2707 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002708
Radek Krejcif03a9e22020-09-18 20:09:31 +02002709 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2710 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002711
Michal Vasko004d3152020-06-11 19:59:22 +02002712 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002713 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002714
Radek Krejcif03a9e22020-09-18 20:09:31 +02002715 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002716 ++parsed;
2717 }
2718
2719 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002721
2722 /* '(' */
2723 tok_len = 1;
2724 tok_type = LYXP_TOKEN_PAR1;
2725
Radek Krejcif03a9e22020-09-18 20:09:31 +02002726 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002727 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002728 if (((expr->tok_len[expr->used - 1] == 4) &&
2729 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2730 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2731 ((expr->tok_len[expr->used - 1] == 7) &&
2732 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002733 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002734 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002735 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002736 }
2737 prev_function_check = 0;
2738 }
2739
Radek Krejcif03a9e22020-09-18 20:09:31 +02002740 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002741
2742 /* ')' */
2743 tok_len = 1;
2744 tok_type = LYXP_TOKEN_PAR2;
2745
Radek Krejcif03a9e22020-09-18 20:09:31 +02002746 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002747
2748 /* '[' */
2749 tok_len = 1;
2750 tok_type = LYXP_TOKEN_BRACK1;
2751
Radek Krejcif03a9e22020-09-18 20:09:31 +02002752 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002753
2754 /* ']' */
2755 tok_len = 1;
2756 tok_type = LYXP_TOKEN_BRACK2;
2757
Radek Krejcif03a9e22020-09-18 20:09:31 +02002758 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002759
2760 /* '..' */
2761 tok_len = 2;
2762 tok_type = LYXP_TOKEN_DDOT;
2763
Radek Krejcif03a9e22020-09-18 20:09:31 +02002764 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002765
2766 /* '.' */
2767 tok_len = 1;
2768 tok_type = LYXP_TOKEN_DOT;
2769
Radek Krejcif03a9e22020-09-18 20:09:31 +02002770 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002771
2772 /* '@' */
2773 tok_len = 1;
2774 tok_type = LYXP_TOKEN_AT;
2775
Radek Krejcif03a9e22020-09-18 20:09:31 +02002776 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002777
2778 /* ',' */
2779 tok_len = 1;
2780 tok_type = LYXP_TOKEN_COMMA;
2781
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002783
2784 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002785 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2786 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002787 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002788 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002789 ++tok_len;
2790 tok_type = LYXP_TOKEN_LITERAL;
2791
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002793
2794 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002795 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2796 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002797 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002798 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002799 ++tok_len;
2800 tok_type = LYXP_TOKEN_LITERAL;
2801
Radek Krejcif03a9e22020-09-18 20:09:31 +02002802 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002803
2804 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002805 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2806 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002807 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002808 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002809 }
2810 tok_type = LYXP_TOKEN_NUMBER;
2811
Radek Krejcif03a9e22020-09-18 20:09:31 +02002812 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002813
2814 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002815 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002816 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002817 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002818 } else {
2819 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002820 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002821 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002822
Radek Krejcif03a9e22020-09-18 20:09:31 +02002823 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002824
Michal Vasko3e48bf32020-06-01 08:39:07 +02002825 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002826 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002827 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2828
Radek Krejcif03a9e22020-09-18 20:09:31 +02002829 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002830
2831 /* Operator '<=', '>=' */
2832 tok_len = 2;
2833 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002834
Radek Krejcif03a9e22020-09-18 20:09:31 +02002835 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002836
2837 /* Operator '|' */
2838 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002839 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002840
Radek Krejcif03a9e22020-09-18 20:09:31 +02002841 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002842
2843 /* Operator '+', '-' */
2844 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002845 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002846
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002848
Michal Vasko3e48bf32020-06-01 08:39:07 +02002849 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002850 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002851 tok_type = LYXP_TOKEN_OPER_EQUAL;
2852
Radek Krejcif03a9e22020-09-18 20:09:31 +02002853 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002854
2855 /* Operator '<', '>' */
2856 tok_len = 1;
2857 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Michal Vasko69730152020-10-09 16:30:07 +02002859 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2860 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2861 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2862 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2863 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2864 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2865 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2866 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2867 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2868 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2869 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2870 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002871
2872 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002873 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002874 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002875 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002876
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002878 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002879 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002880
Radek Krejcif03a9e22020-09-18 20:09:31 +02002881 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002882 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002883 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002884
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002886 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002887 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002888
2889 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002890 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 +02002891 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 +02002892 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002893 goto error;
2894 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002895 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002896 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002897 goto error;
2898 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002899 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002900
2901 /* NameTest '*' */
2902 tok_len = 1;
2903 tok_type = LYXP_TOKEN_NAMETEST;
2904
2905 } else {
2906
2907 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002908 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002909 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2910 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002911 tok_len = ncname_len;
2912
Radek Krejcif03a9e22020-09-18 20:09:31 +02002913 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002914 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002915 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002916 ++tok_len;
2917 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002918 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vasko774ce402021-04-14 15:35:06 +02002919 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2920 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002921 tok_len += ncname_len;
2922 }
2923 /* remove old flag to prevent ambiguities */
2924 prev_function_check = 0;
2925 tok_type = LYXP_TOKEN_NAMETEST;
2926 } else {
2927 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2928 prev_function_check = 1;
2929 tok_type = LYXP_TOKEN_NAMETEST;
2930 }
2931 }
2932
2933 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002935 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002936 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002937 ++parsed;
2938 }
2939
Radek Krejcif03a9e22020-09-18 20:09:31 +02002940 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002941
Michal Vasko004d3152020-06-11 19:59:22 +02002942 if (reparse) {
2943 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002944 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2945 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002946
Michal Vasko004d3152020-06-11 19:59:22 +02002947 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2949 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002950 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002951 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002952 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002953 goto error;
2954 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002955 }
2956
Radek Krejcif03a9e22020-09-18 20:09:31 +02002957 print_expr_struct_debug(expr);
2958 *expr_p = expr;
2959 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002960
2961error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002962 lyxp_expr_free(ctx, expr);
2963 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002964}
2965
Michal Vasko1734be92020-09-22 08:55:10 +02002966LY_ERR
2967lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002968{
Michal Vasko1734be92020-09-22 08:55:10 +02002969 LY_ERR ret = LY_SUCCESS;
2970 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002971 uint32_t i, j;
2972
Michal Vasko7f45cf22020-10-01 12:49:44 +02002973 if (!exp) {
2974 goto cleanup;
2975 }
2976
Michal Vasko004d3152020-06-11 19:59:22 +02002977 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002978 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002979
2980 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002981 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002982 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2983
2984 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002985 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002986 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2987
2988 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002989 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002990 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2991
2992 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002993 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002994 for (i = 0; i < exp->used; ++i) {
2995 if (!exp->repeat[i]) {
2996 dup->repeat[i] = NULL;
2997 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002998 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002999 /* the ending 0 as well */
3000 ++j;
3001
Michal Vasko99c71642020-07-03 13:33:36 +02003002 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003003 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003004 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3005 dup->repeat[i][j - 1] = 0;
3006 }
3007 }
3008
3009 dup->used = exp->used;
3010 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003011 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003012
Michal Vasko1734be92020-09-22 08:55:10 +02003013cleanup:
3014 if (ret) {
3015 lyxp_expr_free(ctx, dup);
3016 } else {
3017 *dup_p = dup;
3018 }
3019 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003020}
3021
Michal Vasko03ff5a72019-09-11 13:49:33 +02003022/*
3023 * warn functions
3024 *
3025 * Warn functions check specific reasonable conditions for schema XPath
3026 * and print a warning if they are not satisfied.
3027 */
3028
3029/**
3030 * @brief Get the last-added schema node that is currently in the context.
3031 *
3032 * @param[in] set Set to search in.
3033 * @return Last-added schema context node, NULL if no node is in context.
3034 */
3035static struct lysc_node *
3036warn_get_scnode_in_ctx(struct lyxp_set *set)
3037{
3038 uint32_t i;
3039
3040 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3041 return NULL;
3042 }
3043
3044 i = set->used;
3045 do {
3046 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003047 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003048 /* if there are more, simply return the first found (last added) */
3049 return set->val.scnodes[i].scnode;
3050 }
3051 } while (i);
3052
3053 return NULL;
3054}
3055
3056/**
3057 * @brief Test whether a type is numeric - integer type or decimal64.
3058 *
3059 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003060 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003061 */
Radek Krejci857189e2020-09-01 13:26:36 +02003062static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003063warn_is_numeric_type(struct lysc_type *type)
3064{
3065 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003066 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003067 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003068
3069 switch (type->basetype) {
3070 case LY_TYPE_DEC64:
3071 case LY_TYPE_INT8:
3072 case LY_TYPE_UINT8:
3073 case LY_TYPE_INT16:
3074 case LY_TYPE_UINT16:
3075 case LY_TYPE_INT32:
3076 case LY_TYPE_UINT32:
3077 case LY_TYPE_INT64:
3078 case LY_TYPE_UINT64:
3079 return 1;
3080 case LY_TYPE_UNION:
3081 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003082 LY_ARRAY_FOR(uni->types, u) {
3083 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003084 if (ret) {
3085 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003086 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003087 }
3088 }
3089 /* did not find any suitable type */
3090 return 0;
3091 case LY_TYPE_LEAFREF:
3092 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3093 default:
3094 return 0;
3095 }
3096}
3097
3098/**
3099 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3100 *
3101 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003102 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003103 */
Radek Krejci857189e2020-09-01 13:26:36 +02003104static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003105warn_is_string_type(struct lysc_type *type)
3106{
3107 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003108 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003109 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003110
3111 switch (type->basetype) {
3112 case LY_TYPE_BITS:
3113 case LY_TYPE_ENUM:
3114 case LY_TYPE_IDENT:
3115 case LY_TYPE_INST:
3116 case LY_TYPE_STRING:
3117 return 1;
3118 case LY_TYPE_UNION:
3119 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003120 LY_ARRAY_FOR(uni->types, u) {
3121 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003122 if (ret) {
3123 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003124 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003125 }
3126 }
3127 /* did not find any suitable type */
3128 return 0;
3129 case LY_TYPE_LEAFREF:
3130 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3131 default:
3132 return 0;
3133 }
3134}
3135
3136/**
3137 * @brief Test whether a type is one specific type.
3138 *
3139 * @param[in] type Type to test.
3140 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003141 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003142 */
Radek Krejci857189e2020-09-01 13:26:36 +02003143static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003144warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3145{
3146 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003147 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003148 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003149
3150 if (type->basetype == base) {
3151 return 1;
3152 } else if (type->basetype == LY_TYPE_UNION) {
3153 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003154 LY_ARRAY_FOR(uni->types, u) {
3155 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003156 if (ret) {
3157 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003158 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003159 }
3160 }
3161 /* did not find any suitable type */
3162 return 0;
3163 } else if (type->basetype == LY_TYPE_LEAFREF) {
3164 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3165 }
3166
3167 return 0;
3168}
3169
3170/**
3171 * @brief Get next type of a (union) type.
3172 *
3173 * @param[in] type Base type.
3174 * @param[in] prev_type Previously returned type.
3175 * @return Next type or NULL.
3176 */
3177static struct lysc_type *
3178warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3179{
3180 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003181 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003182 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003183
3184 switch (type->basetype) {
3185 case LY_TYPE_UNION:
3186 uni = (struct lysc_type_union *)type;
3187 if (!prev_type) {
3188 return uni->types[0];
3189 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003190 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003191 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003192 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003193 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003194 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003195 found = 1;
3196 }
3197 }
3198 return NULL;
3199 default:
3200 if (prev_type) {
3201 assert(type == prev_type);
3202 return NULL;
3203 } else {
3204 return type;
3205 }
3206 }
3207}
3208
3209/**
3210 * @brief Test whether 2 types have a common type.
3211 *
3212 * @param[in] type1 First type.
3213 * @param[in] type2 Second type.
3214 * @return 1 if they do, 0 otherwise.
3215 */
3216static int
3217warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3218{
3219 struct lysc_type *t1, *rt1, *t2, *rt2;
3220
3221 t1 = NULL;
3222 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3223 if (t1->basetype == LY_TYPE_LEAFREF) {
3224 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3225 } else {
3226 rt1 = t1;
3227 }
3228
3229 t2 = NULL;
3230 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3231 if (t2->basetype == LY_TYPE_LEAFREF) {
3232 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3233 } else {
3234 rt2 = t2;
3235 }
3236
3237 if (rt2->basetype == rt1->basetype) {
3238 /* match found */
3239 return 1;
3240 }
3241 }
3242 }
3243
3244 return 0;
3245}
3246
3247/**
3248 * @brief Check both operands of comparison operators.
3249 *
3250 * @param[in] ctx Context for errors.
3251 * @param[in] set1 First operand set.
3252 * @param[in] set2 Second operand set.
3253 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3254 * @param[in] expr Start of the expression to print with the warning.
3255 * @param[in] tok_pos Token position.
3256 */
3257static void
Radek Krejci857189e2020-09-01 13:26:36 +02003258warn_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 +02003259{
3260 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003261 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003262
3263 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3264 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3265
3266 if (!node1 && !node2) {
3267 /* no node-sets involved, nothing to do */
3268 return;
3269 }
3270
3271 if (node1) {
3272 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3273 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3274 warning = 1;
3275 leaves = 0;
3276 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3277 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3278 warning = 1;
3279 }
3280 }
3281
3282 if (node2) {
3283 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3284 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3285 warning = 1;
3286 leaves = 0;
3287 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3288 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3289 warning = 1;
3290 }
3291 }
3292
3293 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003294 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3295 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3296 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3297 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003298 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3299 warning = 1;
3300 }
3301 }
3302
3303 if (warning) {
3304 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3305 }
3306}
3307
3308/**
3309 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3310 *
3311 * @param[in] exp Parsed XPath expression.
3312 * @param[in] set Set with the leaf/leaf-list.
3313 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3314 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3315 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3316 */
3317static void
Michal Vasko40308e72020-10-20 16:38:40 +02003318warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3319 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003320{
3321 struct lysc_node *scnode;
3322 struct lysc_type *type;
3323 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003324 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003325 LY_ERR rc;
3326 struct ly_err_item *err = NULL;
3327
Michal Vasko69730152020-10-09 16:30:07 +02003328 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3329 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003330 /* check that the node can have the specified value */
3331 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3332 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3333 } else {
3334 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3335 }
3336 if (!value) {
3337 LOGMEM(set->ctx);
3338 return;
3339 }
3340
3341 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3342 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003343 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003345 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003346 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003347 }
3348
3349 type = ((struct lysc_node_leaf *)scnode)->type;
3350 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003351 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003352 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003353 if (rc == LY_EINCOMPLETE) {
3354 rc = LY_SUCCESS;
3355 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003356
3357 if (err) {
3358 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3359 ly_err_free(err);
3360 } else if (rc != LY_SUCCESS) {
3361 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3362 }
3363 if (rc != LY_SUCCESS) {
3364 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003365 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003366 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003367 } else {
3368 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003369 }
3370 }
3371 free(value);
3372 }
3373}
3374
3375/*
3376 * XPath functions
3377 */
3378
3379/**
3380 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3381 * depending on whether the first node bit value from the second argument is set.
3382 *
3383 * @param[in] args Array of arguments.
3384 * @param[in] arg_count Count of elements in @p args.
3385 * @param[in,out] set Context and result set at the same time.
3386 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003387 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003388 */
3389static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003390xpath_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 +02003391{
3392 struct lyd_node_term *leaf;
3393 struct lysc_node_leaf *sleaf;
3394 struct lysc_type_bits *bits;
3395 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003396 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397
3398 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003399 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003400 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003401 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3402 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3403 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3404 sleaf->name);
3405 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3406 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3407 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003408 }
3409
3410 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3411 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003412 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3413 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003414 } else if (!warn_is_string_type(sleaf->type)) {
3415 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003416 }
3417 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003418 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003419 return rc;
3420 }
3421
Michal Vaskod3678892020-05-21 10:06:58 +02003422 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003423 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 +02003424 return LY_EVALID;
3425 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003426 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003427 LY_CHECK_RET(rc);
3428
3429 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003430 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003431 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003432 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3433 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003434 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003435 LY_ARRAY_FOR(bits->bits, u) {
3436 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003437 set_fill_boolean(set, 1);
3438 break;
3439 }
3440 }
3441 }
3442 }
3443
3444 return LY_SUCCESS;
3445}
3446
3447/**
3448 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3449 * with the argument converted to boolean.
3450 *
3451 * @param[in] args Array of arguments.
3452 * @param[in] arg_count Count of elements in @p args.
3453 * @param[in,out] set Context and result set at the same time.
3454 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003455 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003456 */
3457static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003458xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003459{
3460 LY_ERR rc;
3461
3462 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003463 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003464 return LY_SUCCESS;
3465 }
3466
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003467 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468 LY_CHECK_RET(rc);
3469 set_fill_set(set, args[0]);
3470
3471 return LY_SUCCESS;
3472}
3473
3474/**
3475 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3476 * with the first argument rounded up to the nearest integer.
3477 *
3478 * @param[in] args Array of arguments.
3479 * @param[in] arg_count Count of elements in @p args.
3480 * @param[in,out] set Context and result set at the same time.
3481 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003482 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003483 */
3484static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003485xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003486{
3487 struct lysc_node_leaf *sleaf;
3488 LY_ERR rc = LY_SUCCESS;
3489
3490 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003491 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003492 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003493 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3494 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3495 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3496 sleaf->name);
3497 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3498 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3499 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003501 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003502 return rc;
3503 }
3504
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003505 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003506 LY_CHECK_RET(rc);
3507 if ((long long)args[0]->val.num != args[0]->val.num) {
3508 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3509 } else {
3510 set_fill_number(set, args[0]->val.num);
3511 }
3512
3513 return LY_SUCCESS;
3514}
3515
3516/**
3517 * @brief Execute the XPath concat(string, string, string*) function.
3518 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3519 *
3520 * @param[in] args Array of arguments.
3521 * @param[in] arg_count Count of elements in @p args.
3522 * @param[in,out] set Context and result set at the same time.
3523 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003524 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003525 */
3526static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003527xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528{
3529 uint16_t i;
3530 char *str = NULL;
3531 size_t used = 1;
3532 LY_ERR rc = LY_SUCCESS;
3533 struct lysc_node_leaf *sleaf;
3534
3535 if (options & LYXP_SCNODE_ALL) {
3536 for (i = 0; i < arg_count; ++i) {
3537 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3538 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3539 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003540 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003541 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003542 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 +02003543 }
3544 }
3545 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003546 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003547 return rc;
3548 }
3549
3550 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003551 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003552 if (rc != LY_SUCCESS) {
3553 free(str);
3554 return rc;
3555 }
3556
3557 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3558 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3559 strcpy(str + used - 1, args[i]->val.str);
3560 used += strlen(args[i]->val.str);
3561 }
3562
3563 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003564 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003565 set->type = LYXP_SET_STRING;
3566 set->val.str = str;
3567
3568 return LY_SUCCESS;
3569}
3570
3571/**
3572 * @brief Execute the XPath contains(string, string) function.
3573 * Returns LYXP_SET_BOOLEAN whether the second argument can
3574 * be found in the first or not.
3575 *
3576 * @param[in] args Array of arguments.
3577 * @param[in] arg_count Count of elements in @p args.
3578 * @param[in,out] set Context and result set at the same time.
3579 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003580 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003581 */
3582static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003583xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584{
3585 struct lysc_node_leaf *sleaf;
3586 LY_ERR rc = LY_SUCCESS;
3587
3588 if (options & LYXP_SCNODE_ALL) {
3589 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3590 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003591 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3592 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 } else if (!warn_is_string_type(sleaf->type)) {
3594 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003595 }
3596 }
3597
3598 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3599 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003600 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3601 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003602 } else if (!warn_is_string_type(sleaf->type)) {
3603 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003604 }
3605 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003606 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003607 return rc;
3608 }
3609
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003610 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003612 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003613 LY_CHECK_RET(rc);
3614
3615 if (strstr(args[0]->val.str, args[1]->val.str)) {
3616 set_fill_boolean(set, 1);
3617 } else {
3618 set_fill_boolean(set, 0);
3619 }
3620
3621 return LY_SUCCESS;
3622}
3623
3624/**
3625 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3626 * with the size of the node-set from the argument.
3627 *
3628 * @param[in] args Array of arguments.
3629 * @param[in] arg_count Count of elements in @p args.
3630 * @param[in,out] set Context and result set at the same time.
3631 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003632 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003633 */
3634static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003635xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003636{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003637 LY_ERR rc = LY_SUCCESS;
3638
3639 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003640 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003641 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003643 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644 return rc;
3645 }
3646
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003648 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 return LY_EVALID;
3650 }
3651
3652 set_fill_number(set, args[0]->used);
3653 return LY_SUCCESS;
3654}
3655
3656/**
3657 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3658 * with the context with the intial node.
3659 *
3660 * @param[in] args Array of arguments.
3661 * @param[in] arg_count Count of elements in @p args.
3662 * @param[in,out] set Context and result set at the same time.
3663 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003664 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003665 */
3666static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003667xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668{
3669 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003670 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 return LY_EVALID;
3672 }
3673
3674 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003675 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003677 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003679 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680
3681 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003682 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 }
3684
3685 return LY_SUCCESS;
3686}
3687
3688/**
3689 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3690 * leafref or instance-identifier target node(s).
3691 *
3692 * @param[in] args Array of arguments.
3693 * @param[in] arg_count Count of elements in @p args.
3694 * @param[in,out] set Context and result set at the same time.
3695 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003696 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003697 */
3698static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003699xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700{
3701 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003702 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003703 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003704 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003705 struct ly_path *p;
3706 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003708 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 LY_ERR rc = LY_SUCCESS;
3710
3711 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003712 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003713 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003714 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3715 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3716 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3717 sleaf->name);
3718 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3719 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3720 __func__, sleaf->name);
3721 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003722 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003723 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko42e497c2020-01-06 08:38:25 +01003724 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003725 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003726 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003727
3728 /* it was already evaluated on schema, it must succeed */
Radek Krejcid5d37432021-03-12 13:46:40 +01003729 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, NULL, lref->path, LY_PATH_LREF_TRUE, oper,
Radek Krejci8df109d2021-04-23 12:19:08 +02003730 LY_PATH_TARGET_MANY, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003731 assert(!rc);
3732
3733 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003734 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003735 ly_path_free(set->ctx, p);
3736
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003737 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003738 }
3739
Michal Vasko03ff5a72019-09-11 13:49:33 +02003740 return rc;
3741 }
3742
Michal Vaskod3678892020-05-21 10:06:58 +02003743 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003744 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003745 return LY_EVALID;
3746 }
3747
Michal Vaskod3678892020-05-21 10:06:58 +02003748 lyxp_set_free_content(set);
3749 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003750 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3751 sleaf = (struct lysc_node_leaf *)leaf->schema;
3752 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3753 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3754 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003755 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003756 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003757 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003758 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003759 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003760 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003761 } else {
3762 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003763 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003764 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003765 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003766 return LY_EVALID;
3767 }
3768 }
Michal Vasko004d3152020-06-11 19:59:22 +02003769
3770 /* insert it */
3771 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003772 }
3773 }
3774
3775 return LY_SUCCESS;
3776}
3777
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003778static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003779xpath_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 +02003780{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003781 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003782 LY_ARRAY_COUNT_TYPE u;
3783 struct lyd_node_term *leaf;
3784 struct lysc_node_leaf *sleaf;
3785 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003786 struct lyd_value *val;
3787 const struct lys_module *mod;
3788 const char *id_name;
3789 uint16_t id_len;
3790 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003791 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003792 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003793
3794 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003795 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003796 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003797 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3798 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3799 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3800 sleaf->name);
3801 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3802 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3803 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003804 }
3805
3806 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3807 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3808 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003809 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003810 } else if (!warn_is_string_type(sleaf->type)) {
3811 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3812 }
3813 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003814 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003815 return rc;
3816 }
3817
3818 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003819 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 +02003820 return LY_EVALID;
3821 }
3822 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3823 LY_CHECK_RET(rc);
3824
Michal Vasko93923692021-05-07 15:28:02 +02003825 /* parse the identity */
3826 id_name = args[1]->val.str;
3827 id_len = strlen(id_name);
3828 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3829 LY_CHECK_RET(rc);
3830 if (!mod) {
3831 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3832 return LY_EVALID;
3833 }
3834
3835 /* find the identity */
3836 found = 0;
3837 LY_ARRAY_FOR(mod->identities, u) {
3838 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3839 /* we have match */
3840 found = 1;
3841 break;
3842 }
3843 }
3844 if (!found) {
3845 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3846 return LY_EVALID;
3847 }
3848 id = &mod->identities[u];
3849
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003850 set_fill_boolean(set, 0);
3851 found = 0;
3852 for (i = 0; i < args[0]->used; ++i) {
3853 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3854 continue;
3855 }
3856
3857 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3858 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3859 sleaf = (struct lysc_node_leaf *)leaf->schema;
3860 val = &leaf->value;
3861 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3862 /* uninteresting */
3863 continue;
3864 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003865 } else {
3866 meta = args[0]->val.meta[i].meta;
3867 val = &meta->value;
3868 if (val->realtype->basetype != LY_TYPE_IDENT) {
3869 /* uninteresting */
3870 continue;
3871 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003872 }
3873
Michal Vasko93923692021-05-07 15:28:02 +02003874 /* check the identity itself */
3875 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003876 set_fill_boolean(set, 1);
3877 found = 1;
3878 }
Michal Vasko93923692021-05-07 15:28:02 +02003879 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3880 set_fill_boolean(set, 1);
3881 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003882 }
3883
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003884 if (found) {
3885 break;
3886 }
3887 }
3888
3889 return LY_SUCCESS;
3890}
3891
Michal Vasko03ff5a72019-09-11 13:49:33 +02003892/**
3893 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3894 * on whether the first argument nodes contain a node of an identity derived from the second
3895 * argument identity.
3896 *
3897 * @param[in] args Array of arguments.
3898 * @param[in] arg_count Count of elements in @p args.
3899 * @param[in,out] set Context and result set at the same time.
3900 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003901 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003902 */
3903static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003904xpath_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 +02003905{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003906 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003907}
3908
3909/**
3910 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3911 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3912 * the second argument identity.
3913 *
3914 * @param[in] args Array of arguments.
3915 * @param[in] arg_count Count of elements in @p args.
3916 * @param[in,out] set Context and result set at the same time.
3917 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003918 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003919 */
3920static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003921xpath_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 +02003922{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003923 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003924}
3925
3926/**
3927 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3928 * with the integer value of the first node's enum value, otherwise NaN.
3929 *
3930 * @param[in] args Array of arguments.
3931 * @param[in] arg_count Count of elements in @p args.
3932 * @param[in,out] set Context and result set at the same time.
3933 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003934 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003935 */
3936static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003937xpath_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 +02003938{
3939 struct lyd_node_term *leaf;
3940 struct lysc_node_leaf *sleaf;
3941 LY_ERR rc = LY_SUCCESS;
3942
3943 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003944 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003945 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003946 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3947 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3948 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3949 sleaf->name);
3950 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3951 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3952 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003953 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003954 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003955 return rc;
3956 }
3957
Michal Vaskod3678892020-05-21 10:06:58 +02003958 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003959 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003960 return LY_EVALID;
3961 }
3962
3963 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003964 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003965 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3966 sleaf = (struct lysc_node_leaf *)leaf->schema;
3967 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3968 set_fill_number(set, leaf->value.enum_item->value);
3969 }
3970 }
3971
3972 return LY_SUCCESS;
3973}
3974
3975/**
3976 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3977 * with false value.
3978 *
3979 * @param[in] args Array of arguments.
3980 * @param[in] arg_count Count of elements in @p args.
3981 * @param[in,out] set Context and result set at the same time.
3982 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003983 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003984 */
3985static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003986xpath_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 +02003987{
3988 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003989 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 return LY_SUCCESS;
3991 }
3992
3993 set_fill_boolean(set, 0);
3994 return LY_SUCCESS;
3995}
3996
3997/**
3998 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3999 * with the first argument floored (truncated).
4000 *
4001 * @param[in] args Array of arguments.
4002 * @param[in] arg_count Count of elements in @p args.
4003 * @param[in,out] set Context and result set at the same time.
4004 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004005 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004006 */
4007static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004008xpath_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 +02004009{
4010 LY_ERR rc;
4011
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004012 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 LY_CHECK_RET(rc);
4014 if (isfinite(args[0]->val.num)) {
4015 set_fill_number(set, (long long)args[0]->val.num);
4016 }
4017
4018 return LY_SUCCESS;
4019}
4020
4021/**
4022 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4023 * whether the language of the text matches the one from the argument.
4024 *
4025 * @param[in] args Array of arguments.
4026 * @param[in] arg_count Count of elements in @p args.
4027 * @param[in,out] set Context and result set at the same time.
4028 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004029 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 */
4031static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004032xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004033{
4034 const struct lyd_node *node;
4035 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004036 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004037 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004038 LY_ERR rc = LY_SUCCESS;
4039
4040 if (options & LYXP_SCNODE_ALL) {
4041 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4042 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004043 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4044 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 } else if (!warn_is_string_type(sleaf->type)) {
4046 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004047 }
4048 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004049 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 return rc;
4051 }
4052
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004053 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004054 LY_CHECK_RET(rc);
4055
Michal Vasko03ff5a72019-09-11 13:49:33 +02004056 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004057 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004059 } else if (!set->used) {
4060 set_fill_boolean(set, 0);
4061 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 }
4063
4064 switch (set->val.nodes[0].type) {
4065 case LYXP_NODE_ELEM:
4066 case LYXP_NODE_TEXT:
4067 node = set->val.nodes[0].node;
4068 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004069 case LYXP_NODE_META:
4070 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004071 break;
4072 default:
4073 /* nothing to do with roots */
4074 set_fill_boolean(set, 0);
4075 return LY_SUCCESS;
4076 }
4077
Michal Vasko9f96a052020-03-10 09:41:45 +01004078 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004079 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004080 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004081 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004082 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004083 break;
4084 }
4085 }
4086
Michal Vasko9f96a052020-03-10 09:41:45 +01004087 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004088 break;
4089 }
4090 }
4091
4092 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004093 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004094 set_fill_boolean(set, 0);
4095 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004096 uint64_t i;
4097
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004098 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 for (i = 0; args[0]->val.str[i]; ++i) {
4100 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4101 set_fill_boolean(set, 0);
4102 break;
4103 }
4104 }
4105 if (!args[0]->val.str[i]) {
4106 if (!val[i] || (val[i] == '-')) {
4107 set_fill_boolean(set, 1);
4108 } else {
4109 set_fill_boolean(set, 0);
4110 }
4111 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004112 }
4113
4114 return LY_SUCCESS;
4115}
4116
4117/**
4118 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4119 * with the context size.
4120 *
4121 * @param[in] args Array of arguments.
4122 * @param[in] arg_count Count of elements in @p args.
4123 * @param[in,out] set Context and result set at the same time.
4124 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004125 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126 */
4127static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004128xpath_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 +02004129{
4130 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004131 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 return LY_SUCCESS;
4133 }
4134
Michal Vasko03ff5a72019-09-11 13:49:33 +02004135 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004136 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004137 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004138 } else if (!set->used) {
4139 set_fill_number(set, 0);
4140 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004141 }
4142
4143 set_fill_number(set, set->ctx_size);
4144 return LY_SUCCESS;
4145}
4146
4147/**
4148 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4149 * with the node name without namespace from the argument or the context.
4150 *
4151 * @param[in] args Array of arguments.
4152 * @param[in] arg_count Count of elements in @p args.
4153 * @param[in,out] set Context and result set at the same time.
4154 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004155 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004156 */
4157static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004158xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004159{
4160 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004161
Michal Vasko03ff5a72019-09-11 13:49:33 +02004162 /* suppress unused variable warning */
4163 (void)options;
4164
4165 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004166 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004167 return LY_SUCCESS;
4168 }
4169
4170 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004171 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004172 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004173 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004175 } else if (!args[0]->used) {
4176 set_fill_string(set, "", 0);
4177 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 }
4179
4180 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004181 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004182
4183 item = &args[0]->val.nodes[0];
4184 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004185 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004186 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004188 } else if (!set->used) {
4189 set_fill_string(set, "", 0);
4190 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 }
4192
4193 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004194 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004195
4196 item = &set->val.nodes[0];
4197 }
4198
4199 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004200 case LYXP_NODE_NONE:
4201 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 case LYXP_NODE_ROOT:
4203 case LYXP_NODE_ROOT_CONFIG:
4204 case LYXP_NODE_TEXT:
4205 set_fill_string(set, "", 0);
4206 break;
4207 case LYXP_NODE_ELEM:
4208 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4209 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004210 case LYXP_NODE_META:
4211 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 break;
4213 }
4214
4215 return LY_SUCCESS;
4216}
4217
4218/**
4219 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4220 * with the node name fully qualified (with namespace) from the argument or the context.
4221 *
4222 * @param[in] args Array of arguments.
4223 * @param[in] arg_count Count of elements in @p args.
4224 * @param[in,out] set Context and result set at the same time.
4225 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004226 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004227 */
4228static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004229xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230{
4231 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004232 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004233 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004234 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235
4236 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004237 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004238 return LY_SUCCESS;
4239 }
4240
4241 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004243 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004244 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004245 } else if (!args[0]->used) {
4246 set_fill_string(set, "", 0);
4247 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004248 }
4249
4250 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004251 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004252
4253 item = &args[0]->val.nodes[0];
4254 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004255 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004256 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004257 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004258 } else if (!set->used) {
4259 set_fill_string(set, "", 0);
4260 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004261 }
4262
4263 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004264 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004265
4266 item = &set->val.nodes[0];
4267 }
4268
4269 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004270 case LYXP_NODE_NONE:
4271 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272 case LYXP_NODE_ROOT:
4273 case LYXP_NODE_ROOT_CONFIG:
4274 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004275 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276 break;
4277 case LYXP_NODE_ELEM:
4278 mod = item->node->schema->module;
4279 name = item->node->schema->name;
4280 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004281 case LYXP_NODE_META:
4282 mod = ((struct lyd_meta *)item->node)->annotation->module;
4283 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 break;
4285 }
4286
4287 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004288 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4290 set_fill_string(set, str, strlen(str));
4291 free(str);
4292 } else {
4293 set_fill_string(set, "", 0);
4294 }
4295
4296 return LY_SUCCESS;
4297}
4298
4299/**
4300 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4301 * with the namespace of the node from the argument or the context.
4302 *
4303 * @param[in] args Array of arguments.
4304 * @param[in] arg_count Count of elements in @p args.
4305 * @param[in,out] set Context and result set at the same time.
4306 * @param[in] options XPath options.
4307 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4308 */
4309static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004310xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311{
4312 struct lyxp_set_node *item;
4313 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004314
Michal Vasko03ff5a72019-09-11 13:49:33 +02004315 /* suppress unused variable warning */
4316 (void)options;
4317
4318 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004319 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004320 return LY_SUCCESS;
4321 }
4322
4323 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004324 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004325 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004326 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004327 return LY_EVALID;
4328 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329 set_fill_string(set, "", 0);
4330 return LY_SUCCESS;
4331 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332
4333 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004334 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335
4336 item = &args[0]->val.nodes[0];
4337 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004338 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004339 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004340 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004341 } else if (!set->used) {
4342 set_fill_string(set, "", 0);
4343 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004344 }
4345
4346 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004347 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004348
4349 item = &set->val.nodes[0];
4350 }
4351
4352 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004353 case LYXP_NODE_NONE:
4354 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004355 case LYXP_NODE_ROOT:
4356 case LYXP_NODE_ROOT_CONFIG:
4357 case LYXP_NODE_TEXT:
4358 set_fill_string(set, "", 0);
4359 break;
4360 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004361 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004362 if (item->type == LYXP_NODE_ELEM) {
4363 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004364 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004365 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004366 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 }
4368
4369 set_fill_string(set, mod->ns, strlen(mod->ns));
4370 break;
4371 }
4372
4373 return LY_SUCCESS;
4374}
4375
4376/**
4377 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4378 * with only nodes from the context. In practice it either leaves the context
4379 * as it is or returns an empty node set.
4380 *
4381 * @param[in] args Array of arguments.
4382 * @param[in] arg_count Count of elements in @p args.
4383 * @param[in,out] set Context and result set at the same time.
4384 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004385 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004386 */
4387static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004388xpath_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 +02004389{
4390 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004391 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 return LY_SUCCESS;
4393 }
4394
4395 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004396 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004397 }
4398 return LY_SUCCESS;
4399}
4400
4401/**
4402 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4403 * with normalized value (no leading, trailing, double white spaces) of the node
4404 * from the argument or the context.
4405 *
4406 * @param[in] args Array of arguments.
4407 * @param[in] arg_count Count of elements in @p args.
4408 * @param[in,out] set Context and result set at the same time.
4409 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004410 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004411 */
4412static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004413xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004414{
4415 uint16_t i, new_used;
4416 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004417 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004418 struct lysc_node_leaf *sleaf;
4419 LY_ERR rc = LY_SUCCESS;
4420
4421 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004422 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4423 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004424 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004425 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4426 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004427 } else if (!warn_is_string_type(sleaf->type)) {
4428 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429 }
4430 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004431 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004432 return rc;
4433 }
4434
4435 if (arg_count) {
4436 set_fill_set(set, args[0]);
4437 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004438 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 LY_CHECK_RET(rc);
4440
4441 /* is there any normalization necessary? */
4442 for (i = 0; set->val.str[i]; ++i) {
4443 if (is_xmlws(set->val.str[i])) {
4444 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4445 have_spaces = 1;
4446 break;
4447 }
4448 space_before = 1;
4449 } else {
4450 space_before = 0;
4451 }
4452 }
4453
4454 /* yep, there is */
4455 if (have_spaces) {
4456 /* it's enough, at least one character will go, makes space for ending '\0' */
4457 new = malloc(strlen(set->val.str) * sizeof(char));
4458 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4459 new_used = 0;
4460
4461 space_before = 0;
4462 for (i = 0; set->val.str[i]; ++i) {
4463 if (is_xmlws(set->val.str[i])) {
4464 if ((i == 0) || space_before) {
4465 space_before = 1;
4466 continue;
4467 } else {
4468 space_before = 1;
4469 }
4470 } else {
4471 space_before = 0;
4472 }
4473
4474 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4475 ++new_used;
4476 }
4477
4478 /* at worst there is one trailing space now */
4479 if (new_used && is_xmlws(new[new_used - 1])) {
4480 --new_used;
4481 }
4482
4483 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4484 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4485 new[new_used] = '\0';
4486
4487 free(set->val.str);
4488 set->val.str = new;
4489 }
4490
4491 return LY_SUCCESS;
4492}
4493
4494/**
4495 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4496 * with the argument converted to boolean and logically inverted.
4497 *
4498 * @param[in] args Array of arguments.
4499 * @param[in] arg_count Count of elements in @p args.
4500 * @param[in,out] set Context and result set at the same time.
4501 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004502 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004503 */
4504static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004505xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004506{
4507 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004508 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004509 return LY_SUCCESS;
4510 }
4511
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004512 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004513 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004514 set_fill_boolean(set, 0);
4515 } else {
4516 set_fill_boolean(set, 1);
4517 }
4518
4519 return LY_SUCCESS;
4520}
4521
4522/**
4523 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4524 * with the number representation of either the argument or the context.
4525 *
4526 * @param[in] args Array of arguments.
4527 * @param[in] arg_count Count of elements in @p args.
4528 * @param[in,out] set Context and result set at the same time.
4529 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004530 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004531 */
4532static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004533xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534{
4535 LY_ERR rc;
4536
4537 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004538 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004539 return LY_SUCCESS;
4540 }
4541
4542 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004543 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004544 LY_CHECK_RET(rc);
4545 set_fill_set(set, args[0]);
4546 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004547 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004548 LY_CHECK_RET(rc);
4549 }
4550
4551 return LY_SUCCESS;
4552}
4553
4554/**
4555 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4556 * with the context position.
4557 *
4558 * @param[in] args Array of arguments.
4559 * @param[in] arg_count Count of elements in @p args.
4560 * @param[in,out] set Context and result set at the same time.
4561 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004562 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004563 */
4564static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004565xpath_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 +02004566{
4567 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004568 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569 return LY_SUCCESS;
4570 }
4571
Michal Vasko03ff5a72019-09-11 13:49:33 +02004572 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004573 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004575 } else if (!set->used) {
4576 set_fill_number(set, 0);
4577 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 }
4579
4580 set_fill_number(set, set->ctx_pos);
4581
4582 /* UNUSED in 'Release' build type */
4583 (void)options;
4584 return LY_SUCCESS;
4585}
4586
4587/**
4588 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4589 * depending on whether the second argument regex matches the first argument string. For details refer to
4590 * YANG 1.1 RFC section 10.2.1.
4591 *
4592 * @param[in] args Array of arguments.
4593 * @param[in] arg_count Count of elements in @p args.
4594 * @param[in,out] set Context and result set at the same time.
4595 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004596 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004597 */
4598static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004599xpath_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 +02004600{
4601 struct lysc_pattern **patterns = NULL, **pattern;
4602 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603 LY_ERR rc = LY_SUCCESS;
4604 struct ly_err_item *err;
4605
4606 if (options & LYXP_SCNODE_ALL) {
4607 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4608 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4609 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 +02004610 } else if (!warn_is_string_type(sleaf->type)) {
4611 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004612 }
4613 }
4614
4615 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4616 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4617 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 +02004618 } else if (!warn_is_string_type(sleaf->type)) {
4619 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004620 }
4621 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004622 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004623 return rc;
4624 }
4625
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004626 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004627 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004628 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004629 LY_CHECK_RET(rc);
4630
4631 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004632 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004633 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004634 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004635 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636 if (rc != LY_SUCCESS) {
4637 LY_ARRAY_FREE(patterns);
4638 return rc;
4639 }
4640
Radek Krejci0b013302021-03-29 15:22:32 +02004641 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 pcre2_code_free((*pattern)->code);
4643 free(*pattern);
4644 LY_ARRAY_FREE(patterns);
4645 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004646 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 ly_err_free(err);
4648 return rc;
4649 }
4650
4651 if (rc == LY_EVALID) {
4652 ly_err_free(err);
4653 set_fill_boolean(set, 0);
4654 } else {
4655 set_fill_boolean(set, 1);
4656 }
4657
4658 return LY_SUCCESS;
4659}
4660
4661/**
4662 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4663 * with the rounded first argument. For details refer to
4664 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4665 *
4666 * @param[in] args Array of arguments.
4667 * @param[in] arg_count Count of elements in @p args.
4668 * @param[in,out] set Context and result set at the same time.
4669 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004670 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004671 */
4672static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004673xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004674{
4675 struct lysc_node_leaf *sleaf;
4676 LY_ERR rc = LY_SUCCESS;
4677
4678 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004679 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004680 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004681 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4682 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4683 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4684 sleaf->name);
4685 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4686 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4687 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004688 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004689 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004690 return rc;
4691 }
4692
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004693 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004694 LY_CHECK_RET(rc);
4695
4696 /* cover only the cases where floor can't be used */
4697 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4698 set_fill_number(set, -0.0f);
4699 } else {
4700 args[0]->val.num += 0.5;
4701 rc = xpath_floor(args, 1, args[0], options);
4702 LY_CHECK_RET(rc);
4703 set_fill_number(set, args[0]->val.num);
4704 }
4705
4706 return LY_SUCCESS;
4707}
4708
4709/**
4710 * @brief Execute the XPath starts-with(string, string) function.
4711 * Returns LYXP_SET_BOOLEAN whether the second argument is
4712 * the prefix of the first or not.
4713 *
4714 * @param[in] args Array of arguments.
4715 * @param[in] arg_count Count of elements in @p args.
4716 * @param[in,out] set Context and result set at the same time.
4717 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004718 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004719 */
4720static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004721xpath_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 +02004722{
4723 struct lysc_node_leaf *sleaf;
4724 LY_ERR rc = LY_SUCCESS;
4725
4726 if (options & LYXP_SCNODE_ALL) {
4727 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4728 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4729 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 +02004730 } else if (!warn_is_string_type(sleaf->type)) {
4731 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004732 }
4733 }
4734
4735 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4736 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4737 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 +02004738 } else if (!warn_is_string_type(sleaf->type)) {
4739 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004740 }
4741 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004742 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004743 return rc;
4744 }
4745
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004746 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004747 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004748 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004749 LY_CHECK_RET(rc);
4750
4751 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4752 set_fill_boolean(set, 0);
4753 } else {
4754 set_fill_boolean(set, 1);
4755 }
4756
4757 return LY_SUCCESS;
4758}
4759
4760/**
4761 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4762 * with the string representation of either the argument or the context.
4763 *
4764 * @param[in] args Array of arguments.
4765 * @param[in] arg_count Count of elements in @p args.
4766 * @param[in,out] set Context and result set at the same time.
4767 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004768 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 */
4770static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004771xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772{
4773 LY_ERR rc;
4774
4775 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004776 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 return LY_SUCCESS;
4778 }
4779
4780 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004781 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004782 LY_CHECK_RET(rc);
4783 set_fill_set(set, args[0]);
4784 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004785 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 LY_CHECK_RET(rc);
4787 }
4788
4789 return LY_SUCCESS;
4790}
4791
4792/**
4793 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4794 * with the length of the string in either the argument or the context.
4795 *
4796 * @param[in] args Array of arguments.
4797 * @param[in] arg_count Count of elements in @p args.
4798 * @param[in,out] set Context and result set at the same time.
4799 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004800 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004801 */
4802static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004803xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004804{
4805 struct lysc_node_leaf *sleaf;
4806 LY_ERR rc = LY_SUCCESS;
4807
4808 if (options & LYXP_SCNODE_ALL) {
4809 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4810 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4811 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 +02004812 } else if (!warn_is_string_type(sleaf->type)) {
4813 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 }
4815 }
4816 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4817 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4818 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 +02004819 } else if (!warn_is_string_type(sleaf->type)) {
4820 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821 }
4822 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004823 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 return rc;
4825 }
4826
4827 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004828 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004829 LY_CHECK_RET(rc);
4830 set_fill_number(set, strlen(args[0]->val.str));
4831 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004832 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 LY_CHECK_RET(rc);
4834 set_fill_number(set, strlen(set->val.str));
4835 }
4836
4837 return LY_SUCCESS;
4838}
4839
4840/**
4841 * @brief Execute the XPath substring(string, number, number?) function.
4842 * Returns LYXP_SET_STRING substring of the first argument starting
4843 * on the second argument index ending on the third argument index,
4844 * indexed from 1. For exact definition refer to
4845 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4846 *
4847 * @param[in] args Array of arguments.
4848 * @param[in] arg_count Count of elements in @p args.
4849 * @param[in,out] set Context and result set at the same time.
4850 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004851 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004852 */
4853static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004854xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004855{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004856 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004857 uint16_t str_start, str_len, pos;
4858 struct lysc_node_leaf *sleaf;
4859 LY_ERR rc = LY_SUCCESS;
4860
4861 if (options & LYXP_SCNODE_ALL) {
4862 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4863 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4864 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 +02004865 } else if (!warn_is_string_type(sleaf->type)) {
4866 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867 }
4868 }
4869
4870 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4871 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4872 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 +02004873 } else if (!warn_is_numeric_type(sleaf->type)) {
4874 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004875 }
4876 }
4877
Michal Vasko69730152020-10-09 16:30:07 +02004878 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4879 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004880 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4881 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 +02004882 } else if (!warn_is_numeric_type(sleaf->type)) {
4883 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004884 }
4885 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004886 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004887 return rc;
4888 }
4889
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004890 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004891 LY_CHECK_RET(rc);
4892
4893 /* start */
4894 if (xpath_round(&args[1], 1, args[1], options)) {
4895 return -1;
4896 }
4897 if (isfinite(args[1]->val.num)) {
4898 start = args[1]->val.num - 1;
4899 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004900 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004901 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004902 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 }
4904
4905 /* len */
4906 if (arg_count == 3) {
4907 rc = xpath_round(&args[2], 1, args[2], options);
4908 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004909 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004910 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004911 } else if (isfinite(args[2]->val.num)) {
4912 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004913 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004914 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004915 }
4916 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004917 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004918 }
4919
4920 /* find matching character positions */
4921 str_start = 0;
4922 str_len = 0;
4923 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4924 if (pos < start) {
4925 ++str_start;
4926 } else if (pos < start + len) {
4927 ++str_len;
4928 } else {
4929 break;
4930 }
4931 }
4932
4933 set_fill_string(set, args[0]->val.str + str_start, str_len);
4934 return LY_SUCCESS;
4935}
4936
4937/**
4938 * @brief Execute the XPath substring-after(string, string) function.
4939 * Returns LYXP_SET_STRING with the string succeeding the occurance
4940 * of the second argument in the first or an empty string.
4941 *
4942 * @param[in] args Array of arguments.
4943 * @param[in] arg_count Count of elements in @p args.
4944 * @param[in,out] set Context and result set at the same time.
4945 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004946 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 */
4948static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004949xpath_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 +02004950{
4951 char *ptr;
4952 struct lysc_node_leaf *sleaf;
4953 LY_ERR rc = LY_SUCCESS;
4954
4955 if (options & LYXP_SCNODE_ALL) {
4956 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4957 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4958 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 +02004959 } else if (!warn_is_string_type(sleaf->type)) {
4960 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004961 }
4962 }
4963
4964 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4965 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4966 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 +02004967 } else if (!warn_is_string_type(sleaf->type)) {
4968 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004969 }
4970 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004971 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004972 return rc;
4973 }
4974
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004975 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004976 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004977 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004978 LY_CHECK_RET(rc);
4979
4980 ptr = strstr(args[0]->val.str, args[1]->val.str);
4981 if (ptr) {
4982 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4983 } else {
4984 set_fill_string(set, "", 0);
4985 }
4986
4987 return LY_SUCCESS;
4988}
4989
4990/**
4991 * @brief Execute the XPath substring-before(string, string) function.
4992 * Returns LYXP_SET_STRING with the string preceding the occurance
4993 * of the second argument in the first or an empty string.
4994 *
4995 * @param[in] args Array of arguments.
4996 * @param[in] arg_count Count of elements in @p args.
4997 * @param[in,out] set Context and result set at the same time.
4998 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004999 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005000 */
5001static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005002xpath_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 +02005003{
5004 char *ptr;
5005 struct lysc_node_leaf *sleaf;
5006 LY_ERR rc = LY_SUCCESS;
5007
5008 if (options & LYXP_SCNODE_ALL) {
5009 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5010 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5011 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 +02005012 } else if (!warn_is_string_type(sleaf->type)) {
5013 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005014 }
5015 }
5016
5017 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5018 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5019 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 +02005020 } else if (!warn_is_string_type(sleaf->type)) {
5021 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005022 }
5023 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005024 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 return rc;
5026 }
5027
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005028 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005030 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005031 LY_CHECK_RET(rc);
5032
5033 ptr = strstr(args[0]->val.str, args[1]->val.str);
5034 if (ptr) {
5035 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5036 } else {
5037 set_fill_string(set, "", 0);
5038 }
5039
5040 return LY_SUCCESS;
5041}
5042
5043/**
5044 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5045 * with the sum of all the nodes in the context.
5046 *
5047 * @param[in] args Array of arguments.
5048 * @param[in] arg_count Count of elements in @p args.
5049 * @param[in,out] set Context and result set at the same time.
5050 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005051 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005052 */
5053static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005054xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005055{
5056 long double num;
5057 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005058 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005059 struct lyxp_set set_item;
5060 struct lysc_node_leaf *sleaf;
5061 LY_ERR rc = LY_SUCCESS;
5062
5063 if (options & LYXP_SCNODE_ALL) {
5064 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5065 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005066 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005067 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5068 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5069 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005070 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071 } else if (!warn_is_numeric_type(sleaf->type)) {
5072 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005073 }
5074 }
5075 }
5076 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005077 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005078 return rc;
5079 }
5080
5081 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005082
5083 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005084 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005085 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005086 } else if (!args[0]->used) {
5087 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005088 }
5089
Michal Vasko5c4e5892019-11-14 12:31:38 +01005090 set_init(&set_item, set);
5091
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 set_item.type = LYXP_SET_NODE_SET;
5093 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5094 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5095
5096 set_item.used = 1;
5097 set_item.size = 1;
5098
5099 for (i = 0; i < args[0]->used; ++i) {
5100 set_item.val.nodes[0] = args[0]->val.nodes[i];
5101
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005102 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103 LY_CHECK_RET(rc);
5104 num = cast_string_to_number(str);
5105 free(str);
5106 set->val.num += num;
5107 }
5108
5109 free(set_item.val.nodes);
5110
5111 return LY_SUCCESS;
5112}
5113
5114/**
5115 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5116 * with the text content of the nodes in the context.
5117 *
5118 * @param[in] args Array of arguments.
5119 * @param[in] arg_count Count of elements in @p args.
5120 * @param[in,out] set Context and result set at the same time.
5121 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005122 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 */
5124static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005125xpath_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 +02005126{
5127 uint32_t i;
5128
5129 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005130 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 return LY_SUCCESS;
5132 }
5133
Michal Vasko03ff5a72019-09-11 13:49:33 +02005134 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005135 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005136 return LY_EVALID;
5137 }
5138
Michal Vaskod989ba02020-08-24 10:59:24 +02005139 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005141 case LYXP_NODE_NONE:
5142 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005143 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005144 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5145 set->val.nodes[i].type = LYXP_NODE_TEXT;
5146 ++i;
5147 break;
5148 }
Radek Krejci0f969882020-08-21 16:56:47 +02005149 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005150 case LYXP_NODE_ROOT:
5151 case LYXP_NODE_ROOT_CONFIG:
5152 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005153 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005154 set_remove_node(set, i);
5155 break;
5156 }
5157 }
5158
5159 return LY_SUCCESS;
5160}
5161
5162/**
5163 * @brief Execute the XPath translate(string, string, string) function.
5164 * Returns LYXP_SET_STRING with the first argument with the characters
5165 * from the second argument replaced by those on the corresponding
5166 * positions in the third argument.
5167 *
5168 * @param[in] args Array of arguments.
5169 * @param[in] arg_count Count of elements in @p args.
5170 * @param[in,out] set Context and result set at the same time.
5171 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005172 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005173 */
5174static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005175xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005176{
5177 uint16_t i, j, new_used;
5178 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005179 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 struct lysc_node_leaf *sleaf;
5181 LY_ERR rc = LY_SUCCESS;
5182
5183 if (options & LYXP_SCNODE_ALL) {
5184 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5185 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5186 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 +02005187 } else if (!warn_is_string_type(sleaf->type)) {
5188 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005189 }
5190 }
5191
5192 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5193 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5194 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 +02005195 } else if (!warn_is_string_type(sleaf->type)) {
5196 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005197 }
5198 }
5199
5200 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5201 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5202 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 +02005203 } else if (!warn_is_string_type(sleaf->type)) {
5204 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005205 }
5206 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005207 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005208 return rc;
5209 }
5210
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005211 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005212 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005213 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005214 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005215 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005216 LY_CHECK_RET(rc);
5217
5218 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5219 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5220 new_used = 0;
5221
5222 have_removed = 0;
5223 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005224 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005225
5226 for (j = 0; args[1]->val.str[j]; ++j) {
5227 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5228 /* removing this char */
5229 if (j >= strlen(args[2]->val.str)) {
5230 have_removed = 1;
5231 found = 1;
5232 break;
5233 }
5234 /* replacing this char */
5235 new[new_used] = args[2]->val.str[j];
5236 ++new_used;
5237 found = 1;
5238 break;
5239 }
5240 }
5241
5242 /* copying this char */
5243 if (!found) {
5244 new[new_used] = args[0]->val.str[i];
5245 ++new_used;
5246 }
5247 }
5248
5249 if (have_removed) {
5250 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5251 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5252 }
5253 new[new_used] = '\0';
5254
Michal Vaskod3678892020-05-21 10:06:58 +02005255 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005256 set->type = LYXP_SET_STRING;
5257 set->val.str = new;
5258
5259 return LY_SUCCESS;
5260}
5261
5262/**
5263 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5264 * with true value.
5265 *
5266 * @param[in] args Array of arguments.
5267 * @param[in] arg_count Count of elements in @p args.
5268 * @param[in,out] set Context and result set at the same time.
5269 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005270 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 */
5272static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005273xpath_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 +02005274{
5275 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005276 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005277 return LY_SUCCESS;
5278 }
5279
5280 set_fill_boolean(set, 1);
5281 return LY_SUCCESS;
5282}
5283
5284/*
5285 * moveto functions
5286 *
5287 * They and only they actually change the context (set).
5288 */
5289
5290/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005291 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005292 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005293 * XPath @p set is expected to be a (sc)node set!
5294 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005295 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5296 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005297 * @param[in] set Set with general XPath context.
5298 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005299 * @param[out] moveto_mod Expected module of a matching node.
5300 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005301 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005302static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005303moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5304 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005305{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005306 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005307 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005308 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309
Michal Vasko2104e9f2020-03-06 08:23:25 +01005310 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5311
Michal Vasko6346ece2019-09-24 13:12:53 +02005312 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5313 /* specific module */
5314 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005315 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005316
Michal Vasko004d3152020-06-11 19:59:22 +02005317 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005318 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005319 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005320 return LY_EVALID;
5321 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005322
Michal Vasko6346ece2019-09-24 13:12:53 +02005323 *qname += pref_len + 1;
5324 *qname_len -= pref_len + 1;
5325 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5326 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005328 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005329 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005330 case LY_VALUE_SCHEMA:
5331 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005332 /* current module */
5333 mod = set->cur_mod;
5334 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005335 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005336 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005337 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005338 /* inherit parent (context node) module */
5339 if (ctx_scnode) {
5340 mod = ctx_scnode->module;
5341 } else {
5342 mod = NULL;
5343 }
5344 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005345 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005346 /* all nodes need to be prefixed */
5347 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5348 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005349 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005350 }
5351
Michal Vasko6346ece2019-09-24 13:12:53 +02005352 *moveto_mod = mod;
5353 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005354}
5355
5356/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005357 * @brief Move context @p set to the root. Handles absolute path.
5358 * Result is LYXP_SET_NODE_SET.
5359 *
5360 * @param[in,out] set Set to use.
5361 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005362 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005363 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005364static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005365moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005366{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005367 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005368 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005369 }
5370
5371 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005372 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005373 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005375 set->type = LYXP_SET_NODE_SET;
5376 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005377 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005379
5380 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381}
5382
5383/**
5384 * @brief Check @p node as a part of NameTest processing.
5385 *
5386 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005387 * @param[in] ctx_node Context node.
5388 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005389 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005390 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005391 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005392 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5393 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 */
5395static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005396moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005397 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005399 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5400 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005401 case LY_VALUE_SCHEMA:
5402 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005403 /* use current module */
5404 moveto_mod = set->cur_mod;
5405 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005406 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005407 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005408 /* inherit module of the context node, if any */
5409 if (ctx_node) {
5410 moveto_mod = ctx_node->schema->module;
5411 }
5412 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005413 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005414 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005415 /* not defined */
5416 LOGINT(set->ctx);
5417 return LY_EINVAL;
5418 }
5419 }
5420
Michal Vasko03ff5a72019-09-11 13:49:33 +02005421 /* module check */
5422 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005423 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005424 }
5425
Michal Vasko5c4e5892019-11-14 12:31:38 +01005426 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005427 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005429 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5430 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005431 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005432 }
5433
5434 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005435 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005436 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005437 }
5438
Michal Vaskoa1424542019-11-14 16:08:52 +01005439 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005440 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005441 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005442 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443
5444 /* match */
5445 return LY_SUCCESS;
5446}
5447
5448/**
5449 * @brief Check @p node as a part of schema NameTest processing.
5450 *
5451 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005452 * @param[in] ctx_scnode Context node.
5453 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005454 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005455 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005456 * @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 +02005457 */
5458static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005459moveto_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 +02005460 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005462 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5463 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005464 case LY_VALUE_SCHEMA:
5465 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005466 /* use current module */
5467 moveto_mod = set->cur_mod;
5468 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005469 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005470 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005471 /* inherit module of the context node, if any */
5472 if (ctx_scnode) {
5473 moveto_mod = ctx_scnode->module;
5474 }
5475 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005476 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005477 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005478 /* not defined */
5479 LOGINT(set->ctx);
5480 return LY_EINVAL;
5481 }
5482 }
5483
Michal Vasko03ff5a72019-09-11 13:49:33 +02005484 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005485 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005486 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005487 }
5488
5489 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005490 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005491 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005492 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005493 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005494 }
5495
5496 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005497 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005498 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005499 }
5500
5501 /* match */
5502 return LY_SUCCESS;
5503}
5504
5505/**
Michal Vaskod3678892020-05-21 10:06:58 +02005506 * @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 +02005507 *
5508 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005509 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005510 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005511 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005512 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5513 */
5514static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005515moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516{
Michal Vaskof03ed032020-03-04 13:31:44 +01005517 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005518 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005519 LY_ERR rc;
5520
Michal Vaskod3678892020-05-21 10:06:58 +02005521 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005522 return LY_SUCCESS;
5523 }
5524
5525 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005526 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005527 return LY_EVALID;
5528 }
5529
Michal Vasko03ff5a72019-09-11 13:49:33 +02005530 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005531 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005532
5533 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 +01005534 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005535
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005536 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005537 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005538 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005539 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005540 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005541 ctx_node = set->val.nodes[i].node;
5542 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005543 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005544
Michal Vaskod3678892020-05-21 10:06:58 +02005545 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005546 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005547 if (rc == LY_SUCCESS) {
5548 if (!replaced) {
5549 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5550 replaced = 1;
5551 } else {
5552 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005553 }
Michal Vaskod3678892020-05-21 10:06:58 +02005554 ++i;
5555 } else if (rc == LY_EINCOMPLETE) {
5556 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005557 }
5558 }
5559
5560 if (!replaced) {
5561 /* no match */
5562 set_remove_node(set, i);
5563 }
5564 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005565
5566 return LY_SUCCESS;
5567}
5568
5569/**
Michal Vaskod3678892020-05-21 10:06:58 +02005570 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5571 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572 *
5573 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005574 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005575 * @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 +01005576 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005577 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5578 */
5579static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005580moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5581 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005582{
Michal Vasko004d3152020-06-11 19:59:22 +02005583 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005584 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005585 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005586 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005587
Michal Vasko004d3152020-06-11 19:59:22 +02005588 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005589
5590 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005591 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005592 }
5593
5594 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005595 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005596 ret = LY_EVALID;
5597 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005598 }
5599
5600 /* context check for all the nodes since we have the schema node */
5601 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5602 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005603 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005604 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5605 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005606 lyxp_set_free_content(set);
5607 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005608 }
5609
5610 /* create specific data instance if needed */
5611 if (scnode->nodetype == LYS_LIST) {
5612 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5613 } else if (scnode->nodetype == LYS_LEAFLIST) {
5614 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005615 }
5616
5617 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005618 siblings = NULL;
5619
5620 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5621 assert(!set->val.nodes[i].node);
5622
5623 /* search in all the trees */
5624 siblings = set->tree;
5625 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5626 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005627 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005628 }
5629
5630 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005631 if (inst) {
5632 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005633 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005634 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005635 }
5636
5637 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005638 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005639 ret = LY_EINCOMPLETE;
5640 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005641 }
5642
5643 if (sub) {
5644 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005645 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005646 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005647 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005648 /* no match */
5649 set_remove_node(set, i);
5650 }
5651 }
5652
Michal Vasko004d3152020-06-11 19:59:22 +02005653cleanup:
5654 lyd_free_tree(inst);
5655 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005656}
5657
5658/**
5659 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5660 *
5661 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005662 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005663 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005664 * @param[in] options XPath options.
5665 * @return LY_ERR
5666 */
5667static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005668moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005669{
Radek Krejci857189e2020-09-01 13:26:36 +02005670 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005671 uint32_t getnext_opts;
5672 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005674 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005675 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005676
Michal Vaskod3678892020-05-21 10:06:58 +02005677 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678 return LY_SUCCESS;
5679 }
5680
5681 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005682 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 return LY_EVALID;
5684 }
5685
Michal Vaskocafad9d2019-11-07 15:20:03 +01005686 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005687 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005688 if (options & LYXP_SCNODE_OUTPUT) {
5689 getnext_opts |= LYS_GETNEXT_OUTPUT;
5690 }
5691
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692 orig_used = set->used;
5693 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005694 uint32_t idx;
5695
Radek Krejcif13b87b2020-12-01 22:02:17 +01005696 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5697 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005698 continue;
5699 }
5700
5701 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005702 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005703 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005704 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005705 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706
5707 start_parent = set->val.scnodes[i].scnode;
5708
5709 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 +02005710 /* 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 +02005711 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005712 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005713 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005714 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005715 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005716 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005717 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005718 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5719
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005721 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005722 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723 temp_ctx = 1;
5724 }
5725 }
5726 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 }
5728
Michal Vasko519fd602020-05-26 12:17:39 +02005729 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5730 iter = NULL;
5731 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005732 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005733 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5734
5735 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005736 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005737 temp_ctx = 1;
5738 }
5739 }
5740 }
5741 }
5742 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743
5744 /* correct temporary in_ctx values */
5745 if (temp_ctx) {
5746 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005747 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5748 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 }
5750 }
5751 }
5752
5753 return LY_SUCCESS;
5754}
5755
5756/**
Michal Vaskod3678892020-05-21 10:06:58 +02005757 * @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 +02005758 * Context position aware.
5759 *
5760 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005761 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005762 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005763 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005764 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5765 */
5766static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005767moveto_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 +02005768{
5769 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005770 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005771 struct lyxp_set ret_set;
5772 LY_ERR rc;
5773
Michal Vaskod3678892020-05-21 10:06:58 +02005774 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005775 return LY_SUCCESS;
5776 }
5777
5778 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005779 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780 return LY_EVALID;
5781 }
5782
Michal Vasko9f96a052020-03-10 09:41:45 +01005783 /* 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 +01005784 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005785 LY_CHECK_RET(rc);
5786
Michal Vasko6346ece2019-09-24 13:12:53 +02005787 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005788 set_init(&ret_set, set);
5789 for (i = 0; i < set->used; ++i) {
5790
5791 /* TREE DFS */
5792 start = set->val.nodes[i].node;
5793 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005794 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005795 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005796 /* add matching node into result set */
5797 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5798 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5799 /* the node is a duplicate, we'll process it later in the set */
5800 goto skip_children;
5801 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005802 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005803 return rc;
5804 } else if (rc == LY_EINVAL) {
5805 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 }
5807
5808 /* TREE DFS NEXT ELEM */
5809 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005810 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 if (!next) {
5812skip_children:
5813 /* no children, so try siblings, but only if it's not the start,
5814 * that is considered to be the root and it's siblings are not traversed */
5815 if (elem != start) {
5816 next = elem->next;
5817 } else {
5818 break;
5819 }
5820 }
5821 while (!next) {
5822 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005823 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005824 /* we are done, no next element to process */
5825 break;
5826 }
5827 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005828 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829 next = elem->next;
5830 }
5831 }
5832 }
5833
5834 /* make the temporary set the current one */
5835 ret_set.ctx_pos = set->ctx_pos;
5836 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005837 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 memcpy(set, &ret_set, sizeof *set);
5839
5840 return LY_SUCCESS;
5841}
5842
5843/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005844 * @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 +02005845 *
5846 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005847 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005848 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005849 * @param[in] options XPath options.
5850 * @return LY_ERR
5851 */
5852static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005853moveto_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 +02005854{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005855 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005857 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858
Michal Vaskod3678892020-05-21 10:06:58 +02005859 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005860 return LY_SUCCESS;
5861 }
5862
5863 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005864 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 return LY_EVALID;
5866 }
5867
Michal Vasko03ff5a72019-09-11 13:49:33 +02005868 orig_used = set->used;
5869 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005870 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5871 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005872 continue;
5873 }
5874
5875 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005876 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005877 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005878 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005880
5881 /* TREE DFS */
5882 start = set->val.scnodes[i].scnode;
5883 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005884 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5885 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005886 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005887 }
5888
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005889 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005890 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005891 uint32_t idx;
5892
5893 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005894 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005895 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896 /* we will process it later in the set */
5897 goto skip_children;
5898 }
5899 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005900 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005901 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005902 } else if (rc == LY_EINVAL) {
5903 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005904 }
5905
5906next_iter:
5907 /* TREE DFS NEXT ELEM */
5908 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005909 next = lysc_node_child(elem);
5910 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5911 next = next->next;
5912 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5913 next = next->next;
5914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915 if (!next) {
5916skip_children:
5917 /* no children, so try siblings, but only if it's not the start,
5918 * that is considered to be the root and it's siblings are not traversed */
5919 if (elem != start) {
5920 next = elem->next;
5921 } else {
5922 break;
5923 }
5924 }
5925 while (!next) {
5926 /* no siblings, go back through the parents */
5927 if (elem->parent == start) {
5928 /* we are done, no next element to process */
5929 break;
5930 }
5931 /* parent is already processed, go to its sibling */
5932 elem = elem->parent;
5933 next = elem->next;
5934 }
5935 }
5936 }
5937
5938 return LY_SUCCESS;
5939}
5940
5941/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005942 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943 * Indirectly context position aware.
5944 *
5945 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005946 * @param[in] mod Matching metadata module, NULL for any.
5947 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005948 * @return LY_ERR
5949 */
5950static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005951moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005952{
Michal Vasko9f96a052020-03-10 09:41:45 +01005953 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005954
Michal Vaskod3678892020-05-21 10:06:58 +02005955 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005956 return LY_SUCCESS;
5957 }
5958
5959 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005960 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005961 return LY_EVALID;
5962 }
5963
Radek Krejci1deb5be2020-08-26 16:43:36 +02005964 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005965 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005966
5967 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5968 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005969 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005970 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005971
5972 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005973 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005974 continue;
5975 }
5976
Michal Vaskod3678892020-05-21 10:06:58 +02005977 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 /* match */
5979 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005980 set->val.meta[i].meta = sub;
5981 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005982 /* pos does not change */
5983 replaced = 1;
5984 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005985 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 +02005986 }
5987 ++i;
5988 }
5989 }
5990 }
5991
5992 if (!replaced) {
5993 /* no match */
5994 set_remove_node(set, i);
5995 }
5996 }
5997
5998 return LY_SUCCESS;
5999}
6000
6001/**
6002 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006003 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004 *
6005 * @param[in,out] set1 Set to use for the result.
6006 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007 * @return LY_ERR
6008 */
6009static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006010moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006011{
6012 LY_ERR rc;
6013
Michal Vaskod3678892020-05-21 10:06:58 +02006014 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006015 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 return LY_EVALID;
6017 }
6018
6019 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006020 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021 return LY_SUCCESS;
6022 }
6023
Michal Vaskod3678892020-05-21 10:06:58 +02006024 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006025 memcpy(set1, set2, sizeof *set1);
6026 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006027 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028 return LY_SUCCESS;
6029 }
6030
6031 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006032 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033
6034 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006035 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006036 LY_CHECK_RET(rc);
6037
6038 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006039 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040
6041 return LY_SUCCESS;
6042}
6043
6044/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006045 * @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 +02006046 * Context position aware.
6047 *
6048 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006049 * @param[in] mod Matching metadata module, NULL for any.
6050 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006051 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6053 */
6054static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006055moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006056{
Michal Vasko9f96a052020-03-10 09:41:45 +01006057 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006058 struct lyxp_set *set_all_desc = NULL;
6059 LY_ERR rc;
6060
Michal Vaskod3678892020-05-21 10:06:58 +02006061 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006062 return LY_SUCCESS;
6063 }
6064
6065 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006066 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067 return LY_EVALID;
6068 }
6069
Michal Vasko03ff5a72019-09-11 13:49:33 +02006070 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6071 * but it likely won't be used much, so it's a waste of time */
6072 /* copy the context */
6073 set_all_desc = set_copy(set);
6074 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006075 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006076 if (rc != LY_SUCCESS) {
6077 lyxp_set_free(set_all_desc);
6078 return rc;
6079 }
6080 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006081 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006082 if (rc != LY_SUCCESS) {
6083 lyxp_set_free(set_all_desc);
6084 return rc;
6085 }
6086 lyxp_set_free(set_all_desc);
6087
Radek Krejci1deb5be2020-08-26 16:43:36 +02006088 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006089 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006090
6091 /* only attributes of an elem can be in the result, skip all the rest,
6092 * we have all attributes qualified in lyd tree */
6093 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006094 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006095 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006096 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097 continue;
6098 }
6099
Michal Vaskod3678892020-05-21 10:06:58 +02006100 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101 /* match */
6102 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006103 set->val.meta[i].meta = sub;
6104 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 /* pos does not change */
6106 replaced = 1;
6107 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006108 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 +02006109 }
6110 ++i;
6111 }
6112 }
6113 }
6114
6115 if (!replaced) {
6116 /* no match */
6117 set_remove_node(set, i);
6118 }
6119 }
6120
6121 return LY_SUCCESS;
6122}
6123
6124/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006125 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6126 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006127 *
6128 * @param[in] parent Current parent.
6129 * @param[in] parent_pos Position of @p parent.
6130 * @param[in] parent_type Node type of @p parent.
6131 * @param[in,out] to_set Set to use.
6132 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006133 * @param[in] options XPath options.
6134 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6135 */
6136static LY_ERR
6137moveto_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 +02006138 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006139{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006140 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006141 LY_ERR rc;
6142
6143 switch (parent_type) {
6144 case LYXP_NODE_ROOT:
6145 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006146 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006147
Michal Vasko61ac2f62020-05-25 12:39:51 +02006148 /* add all top-level nodes as elements */
6149 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006150 break;
6151 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006152 /* add just the text node of this term element node */
6153 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006154 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6155 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6156 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006157 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006158 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006159
6160 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006161 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006162 break;
6163 default:
6164 LOGINT_RET(parent->schema->module->ctx);
6165 }
6166
Michal Vasko61ac2f62020-05-25 12:39:51 +02006167 /* add all top-level nodes as elements */
6168 LY_LIST_FOR(first, iter) {
6169 /* context check */
6170 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6171 continue;
6172 }
6173
6174 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006175 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006176 return LY_EINCOMPLETE;
6177 }
6178
6179 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6180 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6181
6182 /* also add all the children of this node, recursively */
6183 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6184 LY_CHECK_RET(rc);
6185 }
6186 }
6187
Michal Vasko03ff5a72019-09-11 13:49:33 +02006188 return LY_SUCCESS;
6189}
6190
6191/**
6192 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6193 * (or LYXP_SET_EMPTY). Context position aware.
6194 *
6195 * @param[in,out] set Set to use.
6196 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6197 * @param[in] options XPath options.
6198 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6199 */
6200static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006201moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006202{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006203 struct lyxp_set ret_set;
6204 LY_ERR rc;
6205
Michal Vaskod3678892020-05-21 10:06:58 +02006206 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006207 return LY_SUCCESS;
6208 }
6209
6210 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006211 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006212 return LY_EVALID;
6213 }
6214
6215 /* nothing to do */
6216 if (!all_desc) {
6217 return LY_SUCCESS;
6218 }
6219
Michal Vasko03ff5a72019-09-11 13:49:33 +02006220 /* add all the children, they get added recursively */
6221 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006222 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006223 /* copy the current node to tmp */
6224 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6225
6226 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006227 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006228 continue;
6229 }
6230
Michal Vasko03ff5a72019-09-11 13:49:33 +02006231 /* add all the children */
6232 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 +02006233 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006234 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006235 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236 return rc;
6237 }
6238 }
6239
6240 /* use the temporary set as the current one */
6241 ret_set.ctx_pos = set->ctx_pos;
6242 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006243 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244 memcpy(set, &ret_set, sizeof *set);
6245
6246 return LY_SUCCESS;
6247}
6248
6249/**
6250 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6251 * (or LYXP_SET_EMPTY).
6252 *
6253 * @param[in,out] set Set to use.
6254 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6255 * @param[in] options XPath options.
6256 * @return LY_ERR
6257 */
6258static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006259moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006260{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006261 uint32_t getnext_opts;
6262 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006263 const struct lysc_node *iter, *start_parent;
6264 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006265
Michal Vaskod3678892020-05-21 10:06:58 +02006266 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267 return LY_SUCCESS;
6268 }
6269
6270 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006271 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006272 return LY_EVALID;
6273 }
6274
Michal Vasko519fd602020-05-26 12:17:39 +02006275 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006276 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006277 if (options & LYXP_SCNODE_OUTPUT) {
6278 getnext_opts |= LYS_GETNEXT_OUTPUT;
6279 }
6280
6281 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006282 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006283 if (!all_desc) {
6284 /* traverse the start node */
6285 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6286 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6287 }
6288 continue;
6289 }
6290
Radek Krejcif13b87b2020-12-01 22:02:17 +01006291 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6292 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006293 continue;
6294 }
6295
Michal Vasko519fd602020-05-26 12:17:39 +02006296 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006297 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006298 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006299 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006300 }
6301
Michal Vasko519fd602020-05-26 12:17:39 +02006302 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303
Michal Vasko519fd602020-05-26 12:17:39 +02006304 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6305 /* it can actually be in any module, it's all <running> */
6306 mod_idx = 0;
6307 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6308 iter = NULL;
6309 /* module may not be implemented */
6310 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6311 /* context check */
6312 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6313 continue;
6314 }
6315
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006316 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006317 /* throw away the insert index, we want to consider that node again, recursively */
6318 }
6319 }
6320
6321 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6322 iter = NULL;
6323 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006324 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006325 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006326 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 Vasko03ff5a72019-09-11 13:49:33 +02006330 }
6331 }
6332 }
6333
6334 return LY_SUCCESS;
6335}
6336
6337/**
6338 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6339 * (or LYXP_SET_EMPTY). Context position aware.
6340 *
6341 * @param[in] set Set to use.
6342 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6343 * @param[in] options XPath options.
6344 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6345 */
6346static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006347moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006348{
6349 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006351 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006352
Michal Vaskod3678892020-05-21 10:06:58 +02006353 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006354 return LY_SUCCESS;
6355 }
6356
6357 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006358 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006359 return LY_EVALID;
6360 }
6361
6362 if (all_desc) {
6363 /* <path>//.. == <path>//./.. */
6364 rc = moveto_self(set, 1, options);
6365 LY_CHECK_RET(rc);
6366 }
6367
Radek Krejci1deb5be2020-08-26 16:43:36 +02006368 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006369 node = set->val.nodes[i].node;
6370
6371 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006372 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006373 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6374 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006375 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6376 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006377 if (!new_node) {
6378 LOGINT_RET(set->ctx);
6379 }
6380 } else {
6381 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006382 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006383 continue;
6384 }
6385
Michal Vaskoa1424542019-11-14 16:08:52 +01006386 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006387 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 +02006388 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006389 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006390
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006391 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006392 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006393 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006394
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006396 /* 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 +02006397 new_type = LYXP_NODE_ELEM;
6398 }
6399
Michal Vasko03ff5a72019-09-11 13:49:33 +02006400 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006401 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006402 } else {
6403 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006404 }
6405 }
6406
Michal Vasko2caefc12019-11-14 16:07:56 +01006407 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006408 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006409
6410 return LY_SUCCESS;
6411}
6412
6413/**
6414 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6415 * (or LYXP_SET_EMPTY).
6416 *
6417 * @param[in] set Set to use.
6418 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6419 * @param[in] options XPath options.
6420 * @return LY_ERR
6421 */
6422static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006423moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006425 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006426 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006427 const struct lysc_node *node, *new_node;
6428 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006429
Michal Vaskod3678892020-05-21 10:06:58 +02006430 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431 return LY_SUCCESS;
6432 }
6433
6434 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006435 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006436 return LY_EVALID;
6437 }
6438
6439 if (all_desc) {
6440 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006441 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006442 }
6443
Michal Vasko03ff5a72019-09-11 13:49:33 +02006444 orig_used = set->used;
6445 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006446 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6447 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006448 continue;
6449 }
6450
6451 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006452 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006453 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006454 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006455 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006456
6457 node = set->val.scnodes[i].scnode;
6458
6459 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006460 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006461 } else {
6462 /* root does not have a parent */
6463 continue;
6464 }
6465
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006466 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006467 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006468 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006469
Michal Vasko03ff5a72019-09-11 13:49:33 +02006470 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006471 /* 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 +02006472 new_type = LYXP_NODE_ELEM;
6473 }
6474
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006475 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6476 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006477 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478 temp_ctx = 1;
6479 }
6480 }
6481
6482 if (temp_ctx) {
6483 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006484 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6485 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006486 }
6487 }
6488 }
6489
6490 return LY_SUCCESS;
6491}
6492
6493/**
6494 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6495 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6496 *
6497 * @param[in,out] set1 Set to use for the result.
6498 * @param[in] set2 Set acting as the second operand for @p op.
6499 * @param[in] op Comparison operator to process.
6500 * @param[in] options XPath options.
6501 * @return LY_ERR
6502 */
6503static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006504moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505{
6506 /*
6507 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6508 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6509 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6510 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6511 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6512 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6513 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6514 *
6515 * '=' or '!='
6516 * BOOLEAN + BOOLEAN
6517 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6518 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6519 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6520 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6521 * NUMBER + NUMBER
6522 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6523 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6524 * STRING + STRING
6525 *
6526 * '<=', '<', '>=', '>'
6527 * NUMBER + NUMBER
6528 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6529 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6530 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6531 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6532 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6533 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6534 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6535 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6536 */
6537 struct lyxp_set iter1, iter2;
6538 int result;
6539 int64_t i;
6540 LY_ERR rc;
6541
Michal Vasko004d3152020-06-11 19:59:22 +02006542 memset(&iter1, 0, sizeof iter1);
6543 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006544
6545 /* iterative evaluation with node-sets */
6546 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6547 if (set1->type == LYXP_SET_NODE_SET) {
6548 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006549 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006550 switch (set2->type) {
6551 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006552 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006553 break;
6554 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006555 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006556 break;
6557 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006558 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006559 break;
6560 }
6561 LY_CHECK_RET(rc);
6562
Michal Vasko4c7763f2020-07-27 17:40:37 +02006563 /* canonize set2 */
6564 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6565
6566 /* compare recursively */
6567 rc = moveto_op_comp(&iter1, &iter2, op, options);
6568 lyxp_set_free_content(&iter2);
6569 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006570
6571 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006572 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006573 set_fill_boolean(set1, 1);
6574 return LY_SUCCESS;
6575 }
6576 }
6577 } else {
6578 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006579 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006581 case LYXP_SET_NUMBER:
6582 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6583 break;
6584 case LYXP_SET_BOOLEAN:
6585 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6586 break;
6587 default:
6588 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6589 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 }
6591 LY_CHECK_RET(rc);
6592
Michal Vasko4c7763f2020-07-27 17:40:37 +02006593 /* canonize set1 */
6594 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 +02006595
Michal Vasko4c7763f2020-07-27 17:40:37 +02006596 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006597 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006598 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006599 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006600
6601 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006602 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006603 set_fill_boolean(set1, 1);
6604 return LY_SUCCESS;
6605 }
6606 }
6607 }
6608
6609 /* false for all nodes */
6610 set_fill_boolean(set1, 0);
6611 return LY_SUCCESS;
6612 }
6613
6614 /* first convert properly */
6615 if ((op[0] == '=') || (op[0] == '!')) {
6616 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006617 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6618 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006619 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006620 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006621 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006622 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006623 LY_CHECK_RET(rc);
6624 } /* else we have 2 strings */
6625 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006626 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006627 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006628 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006629 LY_CHECK_RET(rc);
6630 }
6631
6632 assert(set1->type == set2->type);
6633
6634 /* compute result */
6635 if (op[0] == '=') {
6636 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006637 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638 } else if (set1->type == LYXP_SET_NUMBER) {
6639 result = (set1->val.num == set2->val.num);
6640 } else {
6641 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006642 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006643 }
6644 } else if (op[0] == '!') {
6645 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006646 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006647 } else if (set1->type == LYXP_SET_NUMBER) {
6648 result = (set1->val.num != set2->val.num);
6649 } else {
6650 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006651 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652 }
6653 } else {
6654 assert(set1->type == LYXP_SET_NUMBER);
6655 if (op[0] == '<') {
6656 if (op[1] == '=') {
6657 result = (set1->val.num <= set2->val.num);
6658 } else {
6659 result = (set1->val.num < set2->val.num);
6660 }
6661 } else {
6662 if (op[1] == '=') {
6663 result = (set1->val.num >= set2->val.num);
6664 } else {
6665 result = (set1->val.num > set2->val.num);
6666 }
6667 }
6668 }
6669
6670 /* assign result */
6671 if (result) {
6672 set_fill_boolean(set1, 1);
6673 } else {
6674 set_fill_boolean(set1, 0);
6675 }
6676
6677 return LY_SUCCESS;
6678}
6679
6680/**
6681 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6682 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6683 *
6684 * @param[in,out] set1 Set to use for the result.
6685 * @param[in] set2 Set acting as the second operand for @p op.
6686 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006687 * @return LY_ERR
6688 */
6689static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006690moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006691{
6692 LY_ERR rc;
6693
6694 /* unary '-' */
6695 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006696 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 LY_CHECK_RET(rc);
6698 set1->val.num *= -1;
6699 lyxp_set_free(set2);
6700 return LY_SUCCESS;
6701 }
6702
6703 assert(set1 && set2);
6704
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006705 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006706 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006707 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006708 LY_CHECK_RET(rc);
6709
6710 switch (op[0]) {
6711 /* '+' */
6712 case '+':
6713 set1->val.num += set2->val.num;
6714 break;
6715
6716 /* '-' */
6717 case '-':
6718 set1->val.num -= set2->val.num;
6719 break;
6720
6721 /* '*' */
6722 case '*':
6723 set1->val.num *= set2->val.num;
6724 break;
6725
6726 /* 'div' */
6727 case 'd':
6728 set1->val.num /= set2->val.num;
6729 break;
6730
6731 /* 'mod' */
6732 case 'm':
6733 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6734 break;
6735
6736 default:
6737 LOGINT_RET(set1->ctx);
6738 }
6739
6740 return LY_SUCCESS;
6741}
6742
6743/*
6744 * eval functions
6745 *
6746 * They execute a parsed XPath expression on some data subtree.
6747 */
6748
6749/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006750 * @brief Evaluate Predicate. Logs directly on error.
6751 *
Michal Vaskod3678892020-05-21 10:06:58 +02006752 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006753 *
6754 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006755 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006756 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6757 * @param[in] options XPath options.
6758 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6759 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6760 */
6761static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006762eval_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 +02006763{
6764 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006765 uint16_t orig_exp;
6766 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006767 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006768 struct lyxp_set set2;
6769 struct lyd_node *orig_parent;
6770
6771 /* '[' */
6772 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006773 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006774 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006775
6776 if (!set) {
6777only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006778 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 LY_CHECK_RET(rc);
6780 } else if (set->type == LYXP_SET_NODE_SET) {
6781 /* 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 +01006782 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006783
6784 /* empty set, nothing to evaluate */
6785 if (!set->used) {
6786 goto only_parse;
6787 }
6788
Michal Vasko004d3152020-06-11 19:59:22 +02006789 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 orig_pos = 0;
6791 orig_size = set->used;
6792 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006793 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006794 set_init(&set2, set);
6795 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6796 /* remember the node context position for position() and context size for last(),
6797 * predicates should always be evaluated with respect to the child axis (since we do
6798 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006799 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6800 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801 orig_pos = 1;
6802 } else {
6803 ++orig_pos;
6804 }
6805
6806 set2.ctx_pos = orig_pos;
6807 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006808 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006809
Michal Vasko004d3152020-06-11 19:59:22 +02006810 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006811 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006812 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006813 return rc;
6814 }
6815
6816 /* number is a position */
6817 if (set2.type == LYXP_SET_NUMBER) {
6818 if ((long long)set2.val.num == orig_pos) {
6819 set2.val.num = 1;
6820 } else {
6821 set2.val.num = 0;
6822 }
6823 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006824 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006825
6826 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006827 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006828 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006829 }
6830 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006831 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832
6833 } else if (set->type == LYXP_SET_SCNODE_SET) {
6834 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006835 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 /* there is a currently-valid node */
6837 break;
6838 }
6839 }
6840 /* empty set, nothing to evaluate */
6841 if (i == set->used) {
6842 goto only_parse;
6843 }
6844
Michal Vasko004d3152020-06-11 19:59:22 +02006845 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006846
Michal Vasko03ff5a72019-09-11 13:49:33 +02006847 /* set special in_ctx to all the valid snodes */
6848 pred_in_ctx = set_scnode_new_in_ctx(set);
6849
6850 /* use the valid snodes one-by-one */
6851 for (i = 0; i < set->used; ++i) {
6852 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6853 continue;
6854 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006855 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856
Michal Vasko004d3152020-06-11 19:59:22 +02006857 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006858
Michal Vasko004d3152020-06-11 19:59:22 +02006859 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006860 LY_CHECK_RET(rc);
6861
6862 set->val.scnodes[i].in_ctx = pred_in_ctx;
6863 }
6864
6865 /* restore the state as it was before the predicate */
6866 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006867 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006868 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006869 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006870 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006871 }
6872 }
6873
6874 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006875 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006876 set_fill_set(&set2, set);
6877
Michal Vasko004d3152020-06-11 19:59:22 +02006878 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006879 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006880 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006881 return rc;
6882 }
6883
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006884 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006885 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006886 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006887 }
Michal Vaskod3678892020-05-21 10:06:58 +02006888 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006889 }
6890
6891 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006892 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006893 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006894 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006895 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006896
6897 return LY_SUCCESS;
6898}
6899
6900/**
Michal Vaskod3678892020-05-21 10:06:58 +02006901 * @brief Evaluate Literal. Logs directly on error.
6902 *
6903 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006904 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006905 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6906 */
6907static void
Michal Vasko40308e72020-10-20 16:38:40 +02006908eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006909{
6910 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006911 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006912 set_fill_string(set, "", 0);
6913 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006914 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 +02006915 }
6916 }
6917 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006918 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006919 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006920}
6921
6922/**
Michal Vasko004d3152020-06-11 19:59:22 +02006923 * @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 +02006924 *
Michal Vasko004d3152020-06-11 19:59:22 +02006925 * @param[in] exp Full parsed XPath expression.
6926 * @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 +02006927 * @param[in] ctx_node Found schema node as the context for the predicate.
6928 * @param[in] cur_mod Current module for the expression.
6929 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006930 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006931 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006932 * @param[out] predicates Parsed predicates.
6933 * @param[out] pred_type Type of @p predicates.
6934 * @return LY_SUCCESS on success,
6935 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006936 */
6937static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006938eval_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 +02006939 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 +02006940 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006941{
Michal Vasko004d3152020-06-11 19:59:22 +02006942 LY_ERR ret = LY_SUCCESS;
6943 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006944 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006945 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006946 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006947 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006948
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006949 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006950
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006951 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006952 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006953 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006954 return LY_EINVAL;
6955 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006956 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 +02006957 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006958
Michal Vasko004d3152020-06-11 19:59:22 +02006959 /* learn where the predicates end */
6960 e_idx = *tok_idx;
6961 while (key_count) {
6962 /* '[' */
6963 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6964 return LY_EINVAL;
6965 }
6966 ++e_idx;
6967
Michal Vasko3354d272021-04-06 09:40:06 +02006968 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6969 /* definitely not a key */
6970 return LY_EINVAL;
6971 }
6972
Michal Vasko004d3152020-06-11 19:59:22 +02006973 /* ']' */
6974 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6975 ++e_idx;
6976 }
6977 ++e_idx;
6978
6979 /* another presumably key predicate parsed */
6980 --key_count;
6981 }
Michal Vasko004d3152020-06-11 19:59:22 +02006982 } else {
6983 /* learn just where this single predicate ends */
6984 e_idx = *tok_idx;
6985
Michal Vaskod3678892020-05-21 10:06:58 +02006986 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006987 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6988 return LY_EINVAL;
6989 }
6990 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006991
Michal Vasko3354d272021-04-06 09:40:06 +02006992 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
6993 /* definitely not the value */
6994 return LY_EINVAL;
6995 }
6996
Michal Vaskod3678892020-05-21 10:06:58 +02006997 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006998 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6999 ++e_idx;
7000 }
7001 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007002 }
7003
Michal Vasko004d3152020-06-11 19:59:22 +02007004 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7005 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7006
7007 /* turn logging off */
7008 prev_lo = ly_log_options(0);
7009
7010 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007011 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7012 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007013
7014 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007015 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7016 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007017 LY_CHECK_GOTO(ret, cleanup);
7018
7019 /* success, the predicate must include all the needed information for hash-based search */
7020 *tok_idx = e_idx;
7021
7022cleanup:
7023 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007024 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007025 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007026}
7027
7028/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007029 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7030 * data node(s) could be found using a single hash-based search.
7031 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007032 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007033 * @param[in] node Next context node to check.
7034 * @param[in] name Expected node name.
7035 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007036 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007037 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007038 * @param[in] format Prefix format.
7039 * @param[in,out] found Previously found node, is updated.
7040 * @return LY_SUCCESS on success,
7041 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7042 */
7043static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007044eval_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 +02007045 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 +02007046 const struct lysc_node **found)
7047{
7048 const struct lysc_node *scnode;
7049 const struct lys_module *mod;
7050 uint32_t idx = 0;
7051
Radek Krejci8df109d2021-04-23 12:19:08 +02007052 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007053
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007054continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007055 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007056 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007057 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007058 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007059 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007060 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7061 if (scnode) {
7062 /* we have found a match */
7063 break;
7064 }
7065 }
7066
Michal Vasko7d1d0912020-10-16 08:38:30 +02007067 if (!scnode) {
7068 /* all modules searched */
7069 idx = 0;
7070 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007071 } else {
7072 /* search in top-level */
7073 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7074 }
7075 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007076 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007077 /* we must adjust the module to inherit the one from the context node */
7078 moveto_mod = node->schema->module;
7079 }
7080
7081 /* search in children, do not repeat the same search */
7082 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007083 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007084
7085 /* additional context check */
7086 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7087 scnode = NULL;
7088 }
7089
7090 if (scnode) {
7091 if (*found) {
7092 /* we found a schema node with the same name but at different level, give up, too complicated
7093 * (more hash-based searches would be required, not supported) */
7094 return LY_ENOT;
7095 } else {
7096 /* remember the found schema node and continue to make sure it can be used */
7097 *found = scnode;
7098 }
7099 }
7100
7101 if (idx) {
7102 /* continue searching all the following models */
7103 goto continue_search;
7104 }
7105
7106 return LY_SUCCESS;
7107}
7108
7109/**
Michal Vaskod3678892020-05-21 10:06:58 +02007110 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7111 *
7112 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7113 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7114 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7115 *
7116 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007117 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007118 * @param[in] attr_axis Whether to search attributes or standard nodes.
7119 * @param[in] all_desc Whether to search all the descendants or children only.
7120 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7121 * @param[in] options XPath options.
7122 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7123 */
7124static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007125eval_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 +02007126 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007127{
Michal Vaskod3678892020-05-21 10:06:58 +02007128 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007129 const char *ncname, *ncname_dict = NULL;
7130 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007131 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007132 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007133 struct ly_path_predicate *predicates = NULL;
7134 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007135 LY_ERR rc = LY_SUCCESS;
7136
7137 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007138 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007139 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007140
7141 if (!set) {
7142 goto moveto;
7143 }
7144
Michal Vasko004d3152020-06-11 19:59:22 +02007145 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7146 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007147
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007148 if ((ncname[0] == '*') && (ncname_len == 1)) {
7149 /* all nodes will match */
7150 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007151 }
7152
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007153 /* parse (and skip) module name */
7154 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007155 LY_CHECK_GOTO(rc, cleanup);
7156
Radek Krejci8df109d2021-04-23 12:19:08 +02007157 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 +02007158 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007159 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007160 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7161 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007162 /* check failed */
7163 scnode = NULL;
7164 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007165 }
7166 }
7167
Michal Vasko004d3152020-06-11 19:59:22 +02007168 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7169 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007170 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7171 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007172 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007173 scnode = NULL;
7174 }
7175 }
7176 }
7177
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007178 if (!scnode) {
7179 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007180 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007181 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007182 }
7183
7184moveto:
7185 /* move to the attribute(s), data node(s), or schema node(s) */
7186 if (attr_axis) {
7187 if (set && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007188 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007189 } else {
7190 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007191 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007192 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007193 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007194 }
7195 LY_CHECK_GOTO(rc, cleanup);
7196 }
7197 } else {
7198 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007199 int64_t i;
7200
Michal Vaskod3678892020-05-21 10:06:58 +02007201 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007202 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007203 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007204 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007205 }
7206 LY_CHECK_GOTO(rc, cleanup);
7207
7208 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007209 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007210 break;
7211 }
7212 }
7213 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007214 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007215 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7216 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007217 free(path);
7218 }
7219 } else {
7220 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007221 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007222 } else {
7223 if (scnode) {
7224 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007225 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007226 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007227 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007228 }
7229 }
7230 LY_CHECK_GOTO(rc, cleanup);
7231 }
7232 }
7233
7234 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007235 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7236 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007237 LY_CHECK_RET(rc);
7238 }
7239
7240cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007241 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007242 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007243 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007244 }
Michal Vaskod3678892020-05-21 10:06:58 +02007245 return rc;
7246}
7247
7248/**
7249 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7250 *
7251 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7252 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7253 * [8] NodeType ::= 'text' | 'node'
7254 *
7255 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007256 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007257 * @param[in] attr_axis Whether to search attributes or standard nodes.
7258 * @param[in] all_desc Whether to search all the descendants or children only.
7259 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7260 * @param[in] options XPath options.
7261 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7262 */
7263static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007264eval_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 +02007265 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007266{
7267 LY_ERR rc;
7268
7269 /* TODO */
7270 (void)attr_axis;
7271 (void)all_desc;
7272
7273 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007274 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007275 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007276 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007277 /* just for the debug message below */
7278 set = NULL;
7279 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007280 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007281 rc = xpath_node(NULL, 0, set, options);
7282 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007283 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007284 rc = xpath_text(NULL, 0, set, options);
7285 }
7286 LY_CHECK_RET(rc);
7287 }
7288 }
7289 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007290 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007291 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007292
7293 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007294 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007295 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007296 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007297 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007298
7299 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007300 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007301 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007302 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007303 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007304
7305 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007306 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7307 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007308 LY_CHECK_RET(rc);
7309 }
7310
7311 return LY_SUCCESS;
7312}
7313
7314/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007315 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7316 *
7317 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7318 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007319 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007320 *
7321 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007322 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007323 * @param[in] all_desc Whether to search all the descendants or children only.
7324 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7325 * @param[in] options XPath options.
7326 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7327 */
7328static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007329eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7330 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007331{
Radek Krejci857189e2020-09-01 13:26:36 +02007332 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 LY_ERR rc;
7334
7335 goto step;
7336 do {
7337 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007338 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 all_desc = 0;
7340 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007341 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 all_desc = 1;
7343 }
7344 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007345 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007346 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347
7348step:
Michal Vaskod3678892020-05-21 10:06:58 +02007349 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007350 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007351 attr_axis = 1;
7352
7353 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007354 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007355 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007356 } else {
7357 attr_axis = 0;
7358 }
7359
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007361 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007362 case LYXP_TOKEN_DOT:
7363 /* evaluate '.' */
7364 if (set && (options & LYXP_SCNODE_ALL)) {
7365 rc = moveto_scnode_self(set, all_desc, options);
7366 } else {
7367 rc = moveto_self(set, all_desc, options);
7368 }
7369 LY_CHECK_RET(rc);
7370 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007371 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007372 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007373 break;
7374
7375 case LYXP_TOKEN_DDOT:
7376 /* evaluate '..' */
7377 if (set && (options & LYXP_SCNODE_ALL)) {
7378 rc = moveto_scnode_parent(set, all_desc, options);
7379 } else {
7380 rc = moveto_parent(set, all_desc, options);
7381 }
7382 LY_CHECK_RET(rc);
7383 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007384 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007385 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007386 break;
7387
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007389 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007390 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007392 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393
Michal Vaskod3678892020-05-21 10:06:58 +02007394 case LYXP_TOKEN_NODETYPE:
7395 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007396 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007397 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 break;
7399
7400 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007401 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 }
Michal Vasko004d3152020-06-11 19:59:22 +02007403 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404
7405 return LY_SUCCESS;
7406}
7407
7408/**
7409 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7410 *
7411 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7412 *
7413 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007414 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7416 * @param[in] options XPath options.
7417 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7418 */
7419static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007420eval_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 +02007421{
Radek Krejci857189e2020-09-01 13:26:36 +02007422 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423
7424 if (set) {
7425 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007426 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427 }
7428
7429 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007430 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431 /* evaluate '/' - deferred */
7432 all_desc = 0;
7433 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007434 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007435 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436
Michal Vasko004d3152020-06-11 19:59:22 +02007437 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 return LY_SUCCESS;
7439 }
Michal Vasko004d3152020-06-11 19:59:22 +02007440 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 case LYXP_TOKEN_DOT:
7442 case LYXP_TOKEN_DDOT:
7443 case LYXP_TOKEN_AT:
7444 case LYXP_TOKEN_NAMETEST:
7445 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007446 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447 break;
7448 default:
7449 break;
7450 }
7451
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007453 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7455 all_desc = 1;
7456 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007457 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007458 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459
Michal Vaskob0099a92020-08-31 14:55:23 +02007460 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 }
7462
7463 return LY_SUCCESS;
7464}
7465
7466/**
7467 * @brief Evaluate FunctionCall. Logs directly on error.
7468 *
Michal Vaskod3678892020-05-21 10:06:58 +02007469 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 *
7471 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007472 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7474 * @param[in] options XPath options.
7475 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7476 */
7477static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007478eval_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 +02007479{
7480 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007481
Radek Krejci1deb5be2020-08-26 16:43:36 +02007482 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007483 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 struct lyxp_set **args = NULL, **args_aux;
7485
7486 if (set) {
7487 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007488 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007490 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007492 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 xpath_func = &xpath_sum;
7494 }
7495 break;
7496 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007497 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007499 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007501 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007503 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 xpath_func = &xpath_true;
7505 }
7506 break;
7507 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007508 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007510 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007512 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007514 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007516 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517 xpath_func = &xpath_deref;
7518 }
7519 break;
7520 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007521 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007523 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007525 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_string;
7527 }
7528 break;
7529 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007530 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007532 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007534 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_current;
7536 }
7537 break;
7538 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007539 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007541 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007542 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007543 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007544 xpath_func = &xpath_re_match;
7545 }
7546 break;
7547 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007548 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007550 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007551 xpath_func = &xpath_translate;
7552 }
7553 break;
7554 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007555 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007557 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007559 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560 xpath_func = &xpath_bit_is_set;
7561 }
7562 break;
7563 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007564 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565 xpath_func = &xpath_starts_with;
7566 }
7567 break;
7568 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007569 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 xpath_func = &xpath_derived_from;
7571 }
7572 break;
7573 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007574 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007576 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 xpath_func = &xpath_string_length;
7578 }
7579 break;
7580 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007581 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007582 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007583 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 xpath_func = &xpath_substring_after;
7585 }
7586 break;
7587 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007588 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007589 xpath_func = &xpath_substring_before;
7590 }
7591 break;
7592 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007593 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 xpath_func = &xpath_derived_from_or_self;
7595 }
7596 break;
7597 }
7598
7599 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007600 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 +02007601 return LY_EVALID;
7602 }
7603 }
7604
7605 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007606 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007607 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007608
7609 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007610 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007611 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007612 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007613 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614
7615 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007616 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007617 if (set) {
7618 args = malloc(sizeof *args);
7619 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7620 arg_count = 1;
7621 args[0] = set_copy(set);
7622 if (!args[0]) {
7623 rc = LY_EMEM;
7624 goto cleanup;
7625 }
7626
Michal Vasko004d3152020-06-11 19:59:22 +02007627 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007628 LY_CHECK_GOTO(rc, cleanup);
7629 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007630 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007631 LY_CHECK_GOTO(rc, cleanup);
7632 }
7633 }
Michal Vasko004d3152020-06-11 19:59:22 +02007634 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007635 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007636 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007637 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007638
7639 if (set) {
7640 ++arg_count;
7641 args_aux = realloc(args, arg_count * sizeof *args);
7642 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7643 args = args_aux;
7644 args[arg_count - 1] = set_copy(set);
7645 if (!args[arg_count - 1]) {
7646 rc = LY_EMEM;
7647 goto cleanup;
7648 }
7649
Michal Vasko004d3152020-06-11 19:59:22 +02007650 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007651 LY_CHECK_GOTO(rc, cleanup);
7652 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007653 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007654 LY_CHECK_GOTO(rc, cleanup);
7655 }
7656 }
7657
7658 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007659 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007660 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007661 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007662 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663
7664 if (set) {
7665 /* evaluate function */
7666 rc = xpath_func(args, arg_count, set, options);
7667
7668 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007669 /* merge all nodes from arg evaluations */
7670 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007671 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007672 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007673 }
7674 }
7675 } else {
7676 rc = LY_SUCCESS;
7677 }
7678
7679cleanup:
7680 for (i = 0; i < arg_count; ++i) {
7681 lyxp_set_free(args[i]);
7682 }
7683 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684 return rc;
7685}
7686
7687/**
7688 * @brief Evaluate Number. Logs directly on error.
7689 *
7690 * @param[in] ctx Context for errors.
7691 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007692 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007693 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7694 * @return LY_ERR
7695 */
7696static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007697eval_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 +02007698{
7699 long double num;
7700 char *endptr;
7701
7702 if (set) {
7703 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007704 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007705 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007706 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7707 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007708 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007710 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007711 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7712 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007713 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007714 return LY_EVALID;
7715 }
7716
7717 set_fill_number(set, num);
7718 }
7719
7720 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007721 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007722 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 return LY_SUCCESS;
7724}
7725
7726/**
7727 * @brief Evaluate PathExpr. Logs directly on error.
7728 *
Michal Vaskod3678892020-05-21 10:06:58 +02007729 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7731 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7732 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007733 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734 *
7735 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007736 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007737 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7738 * @param[in] options XPath options.
7739 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7740 */
7741static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007742eval_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 +02007743{
Radek Krejci857189e2020-09-01 13:26:36 +02007744 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007745 LY_ERR rc;
7746
Michal Vasko004d3152020-06-11 19:59:22 +02007747 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 case LYXP_TOKEN_PAR1:
7749 /* '(' Expr ')' */
7750
7751 /* '(' */
7752 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007753 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007754 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007755
7756 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007757 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 LY_CHECK_RET(rc);
7759
7760 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007761 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007762 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
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 parent_pos_pred = 0;
7767 goto predicate;
7768
7769 case LYXP_TOKEN_DOT:
7770 case LYXP_TOKEN_DDOT:
7771 case LYXP_TOKEN_AT:
7772 case LYXP_TOKEN_NAMETEST:
7773 case LYXP_TOKEN_NODETYPE:
7774 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007775 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007776 LY_CHECK_RET(rc);
7777 break;
7778
7779 case LYXP_TOKEN_FUNCNAME:
7780 /* FunctionCall */
7781 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007782 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007783 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007784 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007785 }
7786 LY_CHECK_RET(rc);
7787
7788 parent_pos_pred = 1;
7789 goto predicate;
7790
Michal Vasko3e48bf32020-06-01 08:39:07 +02007791 case LYXP_TOKEN_OPER_PATH:
7792 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007793 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007794 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 LY_CHECK_RET(rc);
7796 break;
7797
7798 case LYXP_TOKEN_LITERAL:
7799 /* Literal */
7800 if (!set || (options & LYXP_SCNODE_ALL)) {
7801 if (set) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007802 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007803 }
Michal Vasko004d3152020-06-11 19:59:22 +02007804 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007805 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007806 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007807 }
7808
7809 parent_pos_pred = 1;
7810 goto predicate;
7811
7812 case LYXP_TOKEN_NUMBER:
7813 /* Number */
7814 if (!set || (options & LYXP_SCNODE_ALL)) {
7815 if (set) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007816 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007817 }
Michal Vasko004d3152020-06-11 19:59:22 +02007818 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007819 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007820 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007821 }
7822 LY_CHECK_RET(rc);
7823
7824 parent_pos_pred = 1;
7825 goto predicate;
7826
7827 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007828 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 +02007829 return LY_EVALID;
7830 }
7831
7832 return LY_SUCCESS;
7833
7834predicate:
7835 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007836 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7837 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007838 LY_CHECK_RET(rc);
7839 }
7840
7841 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007842 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843
7844 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007845 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007846 all_desc = 0;
7847 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848 all_desc = 1;
7849 }
7850
7851 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007852 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007853 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007854
Michal Vasko004d3152020-06-11 19:59:22 +02007855 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007856 LY_CHECK_RET(rc);
7857 }
7858
7859 return LY_SUCCESS;
7860}
7861
7862/**
7863 * @brief Evaluate UnionExpr. Logs directly on error.
7864 *
Michal Vaskod3678892020-05-21 10:06:58 +02007865 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007866 *
7867 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007868 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007869 * @param[in] repeat How many times this expression is repeated.
7870 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7871 * @param[in] options XPath options.
7872 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7873 */
7874static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007875eval_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 +02007876{
7877 LY_ERR rc = LY_SUCCESS;
7878 struct lyxp_set orig_set, set2;
7879 uint16_t i;
7880
7881 assert(repeat);
7882
7883 set_init(&orig_set, set);
7884 set_init(&set2, set);
7885
7886 set_fill_set(&orig_set, set);
7887
Michal Vasko004d3152020-06-11 19:59:22 +02007888 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007889 LY_CHECK_GOTO(rc, cleanup);
7890
7891 /* ('|' PathExpr)* */
7892 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007893 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007894 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007895 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007896 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897
7898 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007899 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007900 LY_CHECK_GOTO(rc, cleanup);
7901 continue;
7902 }
7903
7904 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007905 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007906 LY_CHECK_GOTO(rc, cleanup);
7907
7908 /* eval */
7909 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007910 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007911 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007912 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007913 LY_CHECK_GOTO(rc, cleanup);
7914 }
7915 }
7916
7917cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007918 lyxp_set_free_content(&orig_set);
7919 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007920 return rc;
7921}
7922
7923/**
7924 * @brief Evaluate UnaryExpr. Logs directly on error.
7925 *
Michal Vaskod3678892020-05-21 10:06:58 +02007926 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007927 *
7928 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007929 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930 * @param[in] repeat How many times this expression is repeated.
7931 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7932 * @param[in] options XPath options.
7933 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7934 */
7935static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007936eval_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 +02007937{
7938 LY_ERR rc;
7939 uint16_t this_op, i;
7940
7941 assert(repeat);
7942
7943 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007944 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007945 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007946 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 +02007947
7948 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007949 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007950 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 }
7952
Michal Vasko004d3152020-06-11 19:59:22 +02007953 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007954 LY_CHECK_RET(rc);
7955
7956 if (set && (repeat % 2)) {
7957 if (options & LYXP_SCNODE_ALL) {
7958 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7959 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007960 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007961 LY_CHECK_RET(rc);
7962 }
7963 }
7964
7965 return LY_SUCCESS;
7966}
7967
7968/**
7969 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7970 *
Michal Vaskod3678892020-05-21 10:06:58 +02007971 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007972 * | MultiplicativeExpr '*' UnaryExpr
7973 * | MultiplicativeExpr 'div' UnaryExpr
7974 * | MultiplicativeExpr 'mod' UnaryExpr
7975 *
7976 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007977 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 * @param[in] repeat How many times this expression is repeated.
7979 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7980 * @param[in] options XPath options.
7981 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7982 */
7983static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007984eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7985 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007986{
7987 LY_ERR rc;
7988 uint16_t this_op;
7989 struct lyxp_set orig_set, set2;
7990 uint16_t i;
7991
7992 assert(repeat);
7993
7994 set_init(&orig_set, set);
7995 set_init(&set2, set);
7996
7997 set_fill_set(&orig_set, set);
7998
Michal Vasko004d3152020-06-11 19:59:22 +02007999 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008000 LY_CHECK_GOTO(rc, cleanup);
8001
8002 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8003 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008004 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008005
Michal Vasko004d3152020-06-11 19:59:22 +02008006 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008007 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008008 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008009 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008010
8011 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008012 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008013 LY_CHECK_GOTO(rc, cleanup);
8014 continue;
8015 }
8016
8017 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008018 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019 LY_CHECK_GOTO(rc, cleanup);
8020
8021 /* eval */
8022 if (options & LYXP_SCNODE_ALL) {
8023 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008024 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008025 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008027 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028 LY_CHECK_GOTO(rc, cleanup);
8029 }
8030 }
8031
8032cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008033 lyxp_set_free_content(&orig_set);
8034 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008035 return rc;
8036}
8037
8038/**
8039 * @brief Evaluate AdditiveExpr. Logs directly on error.
8040 *
Michal Vaskod3678892020-05-21 10:06:58 +02008041 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008042 * | AdditiveExpr '+' MultiplicativeExpr
8043 * | AdditiveExpr '-' MultiplicativeExpr
8044 *
8045 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008046 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047 * @param[in] repeat How many times this expression is repeated.
8048 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8049 * @param[in] options XPath options.
8050 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8051 */
8052static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008053eval_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 +02008054{
8055 LY_ERR rc;
8056 uint16_t this_op;
8057 struct lyxp_set orig_set, set2;
8058 uint16_t i;
8059
8060 assert(repeat);
8061
8062 set_init(&orig_set, set);
8063 set_init(&set2, set);
8064
8065 set_fill_set(&orig_set, set);
8066
Michal Vasko004d3152020-06-11 19:59:22 +02008067 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 LY_CHECK_GOTO(rc, cleanup);
8069
8070 /* ('+' / '-' MultiplicativeExpr)* */
8071 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008072 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008073
Michal Vasko004d3152020-06-11 19:59:22 +02008074 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008075 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008076 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008077 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008078
8079 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008080 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008081 LY_CHECK_GOTO(rc, cleanup);
8082 continue;
8083 }
8084
8085 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008086 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 LY_CHECK_GOTO(rc, cleanup);
8088
8089 /* eval */
8090 if (options & LYXP_SCNODE_ALL) {
8091 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008092 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008093 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008095 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096 LY_CHECK_GOTO(rc, cleanup);
8097 }
8098 }
8099
8100cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008101 lyxp_set_free_content(&orig_set);
8102 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008103 return rc;
8104}
8105
8106/**
8107 * @brief Evaluate RelationalExpr. Logs directly on error.
8108 *
Michal Vaskod3678892020-05-21 10:06:58 +02008109 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008110 * | RelationalExpr '<' AdditiveExpr
8111 * | RelationalExpr '>' AdditiveExpr
8112 * | RelationalExpr '<=' AdditiveExpr
8113 * | RelationalExpr '>=' AdditiveExpr
8114 *
8115 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008116 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117 * @param[in] repeat How many times this expression is repeated.
8118 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8119 * @param[in] options XPath options.
8120 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8121 */
8122static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008123eval_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 +02008124{
8125 LY_ERR rc;
8126 uint16_t this_op;
8127 struct lyxp_set orig_set, set2;
8128 uint16_t i;
8129
8130 assert(repeat);
8131
8132 set_init(&orig_set, set);
8133 set_init(&set2, set);
8134
8135 set_fill_set(&orig_set, set);
8136
Michal Vasko004d3152020-06-11 19:59:22 +02008137 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008138 LY_CHECK_GOTO(rc, cleanup);
8139
8140 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8141 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008142 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143
Michal Vasko004d3152020-06-11 19:59:22 +02008144 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008145 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008146 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008147 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008148
8149 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008150 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008151 LY_CHECK_GOTO(rc, cleanup);
8152 continue;
8153 }
8154
8155 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008156 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 LY_CHECK_GOTO(rc, cleanup);
8158
8159 /* eval */
8160 if (options & LYXP_SCNODE_ALL) {
8161 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008162 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008163 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008164 } else {
8165 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8166 LY_CHECK_GOTO(rc, cleanup);
8167 }
8168 }
8169
8170cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008171 lyxp_set_free_content(&orig_set);
8172 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008173 return rc;
8174}
8175
8176/**
8177 * @brief Evaluate EqualityExpr. Logs directly on error.
8178 *
Michal Vaskod3678892020-05-21 10:06:58 +02008179 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008180 * | EqualityExpr '!=' RelationalExpr
8181 *
8182 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008183 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184 * @param[in] repeat How many times this expression is repeated.
8185 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8186 * @param[in] options XPath options.
8187 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8188 */
8189static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008190eval_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 +02008191{
8192 LY_ERR rc;
8193 uint16_t this_op;
8194 struct lyxp_set orig_set, set2;
8195 uint16_t i;
8196
8197 assert(repeat);
8198
8199 set_init(&orig_set, set);
8200 set_init(&set2, set);
8201
8202 set_fill_set(&orig_set, set);
8203
Michal Vasko004d3152020-06-11 19:59:22 +02008204 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 LY_CHECK_GOTO(rc, cleanup);
8206
8207 /* ('=' / '!=' RelationalExpr)* */
8208 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008209 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008210
Michal Vasko004d3152020-06-11 19:59:22 +02008211 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008212 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008213 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008214 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215
8216 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008217 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008218 LY_CHECK_GOTO(rc, cleanup);
8219 continue;
8220 }
8221
8222 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008223 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 LY_CHECK_GOTO(rc, cleanup);
8225
8226 /* eval */
8227 if (options & LYXP_SCNODE_ALL) {
8228 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008229 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8230 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008231 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008232 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008233 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008234 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8235 LY_CHECK_GOTO(rc, cleanup);
8236 }
8237 }
8238
8239cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008240 lyxp_set_free_content(&orig_set);
8241 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242 return rc;
8243}
8244
8245/**
8246 * @brief Evaluate AndExpr. Logs directly on error.
8247 *
Michal Vaskod3678892020-05-21 10:06:58 +02008248 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 *
8250 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008251 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 * @param[in] repeat How many times this expression is repeated.
8253 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8254 * @param[in] options XPath options.
8255 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8256 */
8257static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008258eval_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 +02008259{
8260 LY_ERR rc;
8261 struct lyxp_set orig_set, set2;
8262 uint16_t i;
8263
8264 assert(repeat);
8265
8266 set_init(&orig_set, set);
8267 set_init(&set2, set);
8268
8269 set_fill_set(&orig_set, set);
8270
Michal Vasko004d3152020-06-11 19:59:22 +02008271 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 LY_CHECK_GOTO(rc, cleanup);
8273
8274 /* cast to boolean, we know that will be the final result */
8275 if (set && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008276 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008278 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008279 }
8280
8281 /* ('and' EqualityExpr)* */
8282 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008283 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8284 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008285 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008286 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008287
8288 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008289 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8290 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008291 LY_CHECK_GOTO(rc, cleanup);
8292 continue;
8293 }
8294
8295 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008296 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008297 LY_CHECK_GOTO(rc, cleanup);
8298
8299 /* eval - just get boolean value actually */
8300 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008301 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008302 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008304 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305 set_fill_set(set, &set2);
8306 }
8307 }
8308
8309cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008310 lyxp_set_free_content(&orig_set);
8311 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008312 return rc;
8313}
8314
8315/**
8316 * @brief Evaluate OrExpr. Logs directly on error.
8317 *
Michal Vaskod3678892020-05-21 10:06:58 +02008318 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008319 *
8320 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008321 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008322 * @param[in] repeat How many times this expression is repeated.
8323 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8324 * @param[in] options XPath options.
8325 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8326 */
8327static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008328eval_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 +02008329{
8330 LY_ERR rc;
8331 struct lyxp_set orig_set, set2;
8332 uint16_t i;
8333
8334 assert(repeat);
8335
8336 set_init(&orig_set, set);
8337 set_init(&set2, set);
8338
8339 set_fill_set(&orig_set, set);
8340
Michal Vasko004d3152020-06-11 19:59:22 +02008341 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342 LY_CHECK_GOTO(rc, cleanup);
8343
8344 /* cast to boolean, we know that will be the final result */
8345 if (set && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008346 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008347 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008348 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 }
8350
8351 /* ('or' AndExpr)* */
8352 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008353 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8354 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008355 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008356 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357
8358 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008359 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8360 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361 LY_CHECK_GOTO(rc, cleanup);
8362 continue;
8363 }
8364
8365 set_fill_set(&set2, &orig_set);
8366 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8367 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008368 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369 LY_CHECK_GOTO(rc, cleanup);
8370
8371 /* eval - just get boolean value actually */
8372 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008373 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008374 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008376 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377 set_fill_set(set, &set2);
8378 }
8379 }
8380
8381cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008382 lyxp_set_free_content(&orig_set);
8383 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 return rc;
8385}
8386
8387/**
Michal Vasko004d3152020-06-11 19:59:22 +02008388 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 *
8390 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008391 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 * @param[in] etype Expression type to evaluate.
8393 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8394 * @param[in] options XPath options.
8395 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8396 */
8397static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008398eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8399 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400{
8401 uint16_t i, count;
8402 enum lyxp_expr_type next_etype;
8403 LY_ERR rc;
8404
8405 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008406 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008407 next_etype = LYXP_EXPR_NONE;
8408 } else {
8409 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008410 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008411
8412 /* select one-priority lower because etype expression called us */
8413 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008414 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008416 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417 } else {
8418 next_etype = LYXP_EXPR_NONE;
8419 }
8420 }
8421
8422 /* decide what expression are we parsing based on the repeat */
8423 switch (next_etype) {
8424 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008425 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426 break;
8427 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008428 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 break;
8430 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008431 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 break;
8433 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008434 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 break;
8436 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008437 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 break;
8439 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008440 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441 break;
8442 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008443 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 break;
8445 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008446 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 break;
8448 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008449 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 break;
8451 default:
8452 LOGINT_RET(set->ctx);
8453 }
8454
8455 return rc;
8456}
8457
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008458/**
8459 * @brief Get root type.
8460 *
8461 * @param[in] ctx_node Context node.
8462 * @param[in] ctx_scnode Schema context node.
8463 * @param[in] options XPath options.
8464 * @return Root type.
8465 */
8466static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008467lyxp_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 +01008468{
Michal Vasko6b26e742020-07-17 15:02:10 +02008469 const struct lysc_node *op;
8470
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008471 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008472 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008473 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008474
8475 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008476 /* general root that can access everything */
8477 return LYXP_NODE_ROOT;
8478 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8479 /* root context node can access only config data (because we said so, it is unspecified) */
8480 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008481 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008482 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008483 }
8484
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008485 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008486 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008487 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008488
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008489 if (op || !(options & LYXP_SCHEMA)) {
8490 /* general root that can access everything */
8491 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008492 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008493 /* root context node can access only config data (because we said so, it is unspecified) */
8494 return LYXP_NODE_ROOT_CONFIG;
8495 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008496 return LYXP_NODE_ROOT;
8497}
8498
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008500lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008501 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 +01008502 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008503{
Michal Vasko004d3152020-06-11 19:59:22 +02008504 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008505 LY_ERR rc;
8506
Michal Vasko400e9672021-01-11 13:39:17 +01008507 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008508 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008509 LOGARG(NULL, "Current module must be set if schema format is used.");
8510 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008511 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008512
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008513 if (tree) {
8514 /* adjust the pointer to be the first top-level sibling */
8515 while (tree->parent) {
8516 tree = lyd_parent(tree);
8517 }
8518 tree = lyd_first_sibling(tree);
8519 }
8520
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008523 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008524 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8525 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8526
Michal Vasko400e9672021-01-11 13:39:17 +01008527 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008528 set->cur_node = ctx_node;
8529 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008530 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8531 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008532 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008533 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008534 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008535 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008536
Radek Krejciddace2c2021-01-08 11:30:56 +01008537 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008538
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008540 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008542 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 }
8544
Radek Krejciddace2c2021-01-08 11:30:56 +01008545 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 return rc;
8547}
8548
8549#if 0
8550
8551/* full xml printing of set elements, not used currently */
8552
8553void
8554lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8555{
8556 uint32_t i;
8557 char *str_num;
8558 struct lyout out;
8559
8560 memset(&out, 0, sizeof out);
8561
8562 out.type = LYOUT_STREAM;
8563 out.method.f = f;
8564
8565 switch (set->type) {
8566 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008567 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568 break;
8569 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008570 ly_print_(&out, "Boolean XPath set:\n");
8571 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 break;
8573 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008574 ly_print_(&out, "String XPath set:\n");
8575 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 break;
8577 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008578 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579
8580 if (isnan(set->value.num)) {
8581 str_num = strdup("NaN");
8582 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8583 str_num = strdup("0");
8584 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8585 str_num = strdup("Infinity");
8586 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8587 str_num = strdup("-Infinity");
8588 } else if ((long long)set->value.num == set->value.num) {
8589 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8590 str_num = NULL;
8591 }
8592 } else {
8593 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8594 str_num = NULL;
8595 }
8596 }
8597 if (!str_num) {
8598 LOGMEM;
8599 return;
8600 }
Michal Vasko5233e962020-08-14 14:26:20 +02008601 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008602 free(str_num);
8603 break;
8604 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008605 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606
8607 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008608 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 switch (set->node_type[i]) {
8610 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008611 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612 break;
8613 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008614 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 break;
8616 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008617 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 break;
8619 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008620 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 break;
8622 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008623 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 break;
8625 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008626 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 break;
8628 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008629 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008631 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008632 break;
8633 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008634 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 +02008635 break;
8636 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008637 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 +02008638 break;
8639 }
8640 }
8641 break;
8642 }
8643}
8644
8645#endif
8646
8647LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008648lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649{
8650 long double num;
8651 char *str;
8652 LY_ERR rc;
8653
8654 if (!set || (set->type == target)) {
8655 return LY_SUCCESS;
8656 }
8657
8658 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008659 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008660
8661 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008662 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 return LY_EINVAL;
8664 }
8665
8666 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008667 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 switch (set->type) {
8669 case LYXP_SET_NUMBER:
8670 if (isnan(set->val.num)) {
8671 set->val.str = strdup("NaN");
8672 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8673 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8674 set->val.str = strdup("0");
8675 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8676 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8677 set->val.str = strdup("Infinity");
8678 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8679 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8680 set->val.str = strdup("-Infinity");
8681 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8682 } else if ((long long)set->val.num == set->val.num) {
8683 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8684 LOGMEM_RET(set->ctx);
8685 }
8686 set->val.str = str;
8687 } else {
8688 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8689 LOGMEM_RET(set->ctx);
8690 }
8691 set->val.str = str;
8692 }
8693 break;
8694 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008695 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008696 set->val.str = strdup("true");
8697 } else {
8698 set->val.str = strdup("false");
8699 }
8700 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8701 break;
8702 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008703 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008704 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008705
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008706 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008707 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008708 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008709 set->val.str = str;
8710 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008711 default:
8712 LOGINT_RET(set->ctx);
8713 }
8714 set->type = LYXP_SET_STRING;
8715 }
8716
8717 /* to NUMBER */
8718 if (target == LYXP_SET_NUMBER) {
8719 switch (set->type) {
8720 case LYXP_SET_STRING:
8721 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008722 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008723 set->val.num = num;
8724 break;
8725 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008726 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008727 set->val.num = 1;
8728 } else {
8729 set->val.num = 0;
8730 }
8731 break;
8732 default:
8733 LOGINT_RET(set->ctx);
8734 }
8735 set->type = LYXP_SET_NUMBER;
8736 }
8737
8738 /* to BOOLEAN */
8739 if (target == LYXP_SET_BOOLEAN) {
8740 switch (set->type) {
8741 case LYXP_SET_NUMBER:
8742 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008743 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008745 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008746 }
8747 break;
8748 case LYXP_SET_STRING:
8749 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008750 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008751 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008753 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008754 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008755 }
8756 break;
8757 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008758 if (set->used) {
8759 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008760 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008761 } else {
8762 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008763 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008764 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008765 break;
8766 default:
8767 LOGINT_RET(set->ctx);
8768 }
8769 set->type = LYXP_SET_BOOLEAN;
8770 }
8771
Michal Vasko03ff5a72019-09-11 13:49:33 +02008772 return LY_SUCCESS;
8773}
8774
8775LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008776lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008777 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008778 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008779{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008780 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008781 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008782
Michal Vasko400e9672021-01-11 13:39:17 +01008783 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008784 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008785 LOGARG(NULL, "Current module must be set if schema format is used.");
8786 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008787 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008788
8789 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008790 memset(set, 0, sizeof *set);
8791 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008792 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8793 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 +01008794 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008795
Michal Vasko400e9672021-01-11 13:39:17 +01008796 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008797 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008798 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008799 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8800 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008801 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008802 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008803 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008804
Radek Krejciddace2c2021-01-08 11:30:56 +01008805 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008806
Michal Vasko03ff5a72019-09-11 13:49:33 +02008807 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008808 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8809
Radek Krejciddace2c2021-01-08 11:30:56 +01008810 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008811 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008812}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008813
8814API const char *
8815lyxp_get_expr(const struct lyxp_expr *path)
8816{
8817 if (!path) {
8818 return NULL;
8819 }
8820
8821 return path->expr;
8822}