blob: acfeaf4659ed6461dc13cae76d4505d8dcba432a [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 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
24#include <limits.h>
25#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "hash_table.h"
Radek Krejci7931b192020-06-25 17:05:03 +020036#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020037#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020038#include "plugins_types.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020039#include "out.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020041#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree.h"
43#include "tree_data_internal.h"
44#include "tree_schema_internal.h"
45#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020046
Michal Vasko004d3152020-06-11 19:59:22 +020047static 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 +020048static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
49 struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
136 uint16_t i, j;
137 char tmp[128];
138
Radek Krejci52b6d512020-10-12 12:33:17 +0200139 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140 return;
141 }
142
143 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
144 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200145 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200146 &exp->expr[exp->tok_pos[i]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200147 if (exp->repeat[i]) {
148 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
149 for (j = 1; exp->repeat[i][j]; ++j) {
150 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
151 }
152 strcat(tmp, ")");
153 }
154 LOGDBG(LY_LDGXPATH, tmp);
155 }
156}
157
158#ifndef NDEBUG
159
160/**
161 * @brief Print XPath set content to debug output.
162 *
163 * @param[in] set Set to print.
164 */
165static void
166print_set_debug(struct lyxp_set *set)
167{
168 uint32_t i;
169 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200170 struct lyxp_set_node *item;
171 struct lyxp_set_scnode *sitem;
172
Radek Krejci52b6d512020-10-12 12:33:17 +0200173 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200174 return;
175 }
176
177 switch (set->type) {
178 case LYXP_SET_NODE_SET:
179 LOGDBG(LY_LDGXPATH, "set NODE SET:");
180 for (i = 0; i < set->used; ++i) {
181 item = &set->val.nodes[i];
182
183 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100184 case LYXP_NODE_NONE:
185 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
186 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200187 case LYXP_NODE_ROOT:
188 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
189 break;
190 case LYXP_NODE_ROOT_CONFIG:
191 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
192 break;
193 case LYXP_NODE_ELEM:
Michal Vasko69730152020-10-09 16:30:07 +0200194 if ((item->node->schema->nodetype == LYS_LIST) &&
195 (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200196 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200197 item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200198 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
199 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200200 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200201 } else {
202 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
203 }
204 break;
205 case LYXP_NODE_TEXT:
206 if (item->node->schema->nodetype & LYS_ANYDATA) {
207 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200208 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200209 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200210 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200211 }
212 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100213 case LYXP_NODE_META:
214 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 +0200215 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200216 break;
217 }
218 }
219 break;
220
221 case LYXP_SET_SCNODE_SET:
222 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
223 for (i = 0; i < set->used; ++i) {
224 sitem = &set->val.scnodes[i];
225
226 switch (sitem->type) {
227 case LYXP_NODE_ROOT:
228 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
229 break;
230 case LYXP_NODE_ROOT_CONFIG:
231 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
232 break;
233 case LYXP_NODE_ELEM:
234 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
235 break;
236 default:
237 LOGINT(NULL);
238 break;
239 }
240 }
241 break;
242
Michal Vasko03ff5a72019-09-11 13:49:33 +0200243 case LYXP_SET_BOOLEAN:
244 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200245 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200246 break;
247
248 case LYXP_SET_STRING:
249 LOGDBG(LY_LDGXPATH, "set STRING");
250 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
251 break;
252
253 case LYXP_SET_NUMBER:
254 LOGDBG(LY_LDGXPATH, "set NUMBER");
255
256 if (isnan(set->val.num)) {
257 str = strdup("NaN");
258 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
259 str = strdup("0");
260 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
261 str = strdup("Infinity");
262 } else if (isinf(set->val.num) && signbit(set->val.num)) {
263 str = strdup("-Infinity");
264 } else if ((long long)set->val.num == set->val.num) {
265 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
266 str = NULL;
267 }
268 } else {
269 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
270 str = NULL;
271 }
272 }
273 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
274
275 LOGDBG(LY_LDGXPATH, "\t%s", str);
276 free(str);
277 }
278}
279
280#endif
281
282/**
283 * @brief Realloc the string \p str.
284 *
285 * @param[in] ctx libyang context for logging.
286 * @param[in] needed How much free space is required.
287 * @param[in,out] str Pointer to the string to use.
288 * @param[in,out] used Used bytes in \p str.
289 * @param[in,out] size Allocated bytes in \p str.
290 * @return LY_ERR
291 */
292static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100293cast_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 +0200294{
295 if (*size - *used < needed) {
296 do {
297 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
298 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
299 return LY_EINVAL;
300 }
301 *size += LYXP_STRING_CAST_SIZE_STEP;
302 } while (*size - *used < needed);
303 *str = ly_realloc(*str, *size * sizeof(char));
304 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
305 }
306
307 return LY_SUCCESS;
308}
309
310/**
311 * @brief Cast nodes recursively to one string @p str.
312 *
313 * @param[in] node Node to cast.
314 * @param[in] fake_cont Whether to put the data into a "fake" container.
315 * @param[in] root_type Type of the XPath root.
316 * @param[in] indent Current indent.
317 * @param[in,out] str Resulting string.
318 * @param[in,out] used Used bytes in @p str.
319 * @param[in,out] size Allocated bytes in @p str.
320 * @return LY_ERR
321 */
322static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200323cast_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 +0200324 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200325{
Radek Krejci7f769d72020-07-11 23:13:56 +0200326 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200327 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200329 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200330 struct lyd_node_any *any;
331 LY_ERR rc;
332
333 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
334 return LY_SUCCESS;
335 }
336
337 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200338 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200339 LY_CHECK_RET(rc);
340 strcpy(*str + (*used - 1), "\n");
341 ++(*used);
342
343 ++indent;
344 }
345
346 switch (node->schema->nodetype) {
347 case LYS_CONTAINER:
348 case LYS_LIST:
349 case LYS_RPC:
350 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200351 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200352 LY_CHECK_RET(rc);
353 strcpy(*str + (*used - 1), "\n");
354 ++(*used);
355
Radek Krejcia1c1e542020-09-29 16:06:52 +0200356 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200357 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
358 LY_CHECK_RET(rc);
359 }
360
361 break;
362
363 case LYS_LEAF:
364 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200365 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200366
367 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200368 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 +0200369 memset(*str + (*used - 1), ' ', indent * 2);
370 *used += indent * 2;
371
372 /* print value */
373 if (*used == 1) {
374 sprintf(*str + (*used - 1), "%s", value_str);
375 *used += strlen(value_str);
376 } else {
377 sprintf(*str + (*used - 1), "%s\n", value_str);
378 *used += strlen(value_str) + 1;
379 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200380
381 break;
382
383 case LYS_ANYXML:
384 case LYS_ANYDATA:
385 any = (struct lyd_node_any *)node;
386 if (!(void *)any->value.tree) {
387 /* no content */
388 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200389 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200390 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200391 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100392
Michal Vasko60ea6352020-06-29 13:39:39 +0200393 if (any->value_type == LYD_ANYDATA_LYB) {
394 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200395 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 +0200396 /* successfully parsed */
397 free(any->value.mem);
398 any->value.tree = tree;
399 any->value_type = LYD_ANYDATA_DATATREE;
400 }
Radek Krejci7931b192020-06-25 17:05:03 +0200401 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200402 }
403
Michal Vasko03ff5a72019-09-11 13:49:33 +0200404 switch (any->value_type) {
405 case LYD_ANYDATA_STRING:
406 case LYD_ANYDATA_XML:
407 case LYD_ANYDATA_JSON:
408 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200409 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200410 break;
411 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200412 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200413 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200414 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100415 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200416 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200418 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200419 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200420 }
421 }
422
423 line = strtok_r(buf, "\n", &ptr);
424 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200425 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200426 if (rc != LY_SUCCESS) {
427 free(buf);
428 return rc;
429 }
430 memset(*str + (*used - 1), ' ', indent * 2);
431 *used += indent * 2;
432
433 strcpy(*str + (*used - 1), line);
434 *used += strlen(line);
435
436 strcpy(*str + (*used - 1), "\n");
437 *used += 1;
438 } while ((line = strtok_r(NULL, "\n", &ptr)));
439
440 free(buf);
441 break;
442
443 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200444 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200445 }
446
447 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200448 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200449 LY_CHECK_RET(rc);
450 strcpy(*str + (*used - 1), "\n");
451 ++(*used);
452
453 --indent;
454 }
455
456 return LY_SUCCESS;
457}
458
459/**
460 * @brief Cast an element into a string.
461 *
462 * @param[in] node Node to cast.
463 * @param[in] fake_cont Whether to put the data into a "fake" container.
464 * @param[in] root_type Type of the XPath root.
465 * @param[out] str Element cast to dynamically-allocated string.
466 * @return LY_ERR
467 */
468static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200469cast_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 +0200470{
471 uint16_t used, size;
472 LY_ERR rc;
473
474 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200475 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200476 (*str)[0] = '\0';
477 used = 1;
478 size = LYXP_STRING_CAST_SIZE_START;
479
480 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
481 if (rc != LY_SUCCESS) {
482 free(*str);
483 return rc;
484 }
485
486 if (size > used) {
487 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200488 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200489 }
490 return LY_SUCCESS;
491}
492
493/**
494 * @brief Cast a LYXP_SET_NODE_SET set into a string.
495 * Context position aware.
496 *
497 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200498 * @param[out] str Cast dynamically-allocated string.
499 * @return LY_ERR
500 */
501static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100502cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200503{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100505 case LYXP_NODE_NONE:
506 /* invalid */
507 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200508 case LYXP_NODE_ROOT:
509 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100510 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200511 case LYXP_NODE_ELEM:
512 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100513 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100514 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200515 *str = strdup(set->val.meta[0].meta->value.canonical);
516 if (!*str) {
517 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200518 }
519 return LY_SUCCESS;
520 }
521
522 LOGINT_RET(set->ctx);
523}
524
525/**
526 * @brief Cast a string into an XPath number.
527 *
528 * @param[in] str String to use.
529 * @return Cast number.
530 */
531static long double
532cast_string_to_number(const char *str)
533{
534 long double num;
535 char *ptr;
536
537 errno = 0;
538 num = strtold(str, &ptr);
539 if (errno || *ptr) {
540 num = NAN;
541 }
542 return num;
543}
544
545/**
546 * @brief Callback for checking value equality.
547 *
Radek Krejci857189e2020-09-01 13:26:36 +0200548 * Implementation of ::values_equal_cb.
549 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200550 * @param[in] val1_p First value.
551 * @param[in] val2_p Second value.
552 * @param[in] mod Whether hash table is being modified.
553 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200554 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200555 */
Radek Krejci857189e2020-09-01 13:26:36 +0200556static ly_bool
557set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200558{
559 struct lyxp_set_hash_node *val1, *val2;
560
561 val1 = (struct lyxp_set_hash_node *)val1_p;
562 val2 = (struct lyxp_set_hash_node *)val2_p;
563
564 if ((val1->node == val2->node) && (val1->type == val2->type)) {
565 return 1;
566 }
567
568 return 0;
569}
570
571/**
572 * @brief Insert node and its hash into set.
573 *
574 * @param[in] set et to insert to.
575 * @param[in] node Node with hash.
576 * @param[in] type Node type.
577 */
578static void
579set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
580{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200581 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200582 uint32_t i, hash;
583 struct lyxp_set_hash_node hnode;
584
585 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
586 /* create hash table and add all the nodes */
587 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
588 for (i = 0; i < set->used; ++i) {
589 hnode.node = set->val.nodes[i].node;
590 hnode.type = set->val.nodes[i].type;
591
592 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
593 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
594 hash = dict_hash_multi(hash, NULL, 0);
595
596 r = lyht_insert(set->ht, &hnode, hash, NULL);
597 assert(!r);
598 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200599
Michal Vasko9d6befd2019-12-11 14:56:56 +0100600 if (hnode.node == node) {
601 /* it was just added, do not add it twice */
602 node = NULL;
603 }
604 }
605 }
606
607 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608 /* add the new node into hash table */
609 hnode.node = node;
610 hnode.type = type;
611
612 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
613 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
614 hash = dict_hash_multi(hash, NULL, 0);
615
616 r = lyht_insert(set->ht, &hnode, hash, NULL);
617 assert(!r);
618 (void)r;
619 }
620}
621
622/**
623 * @brief Remove node and its hash from set.
624 *
625 * @param[in] set Set to remove from.
626 * @param[in] node Node to remove.
627 * @param[in] type Node type.
628 */
629static void
630set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
631{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200632 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200633 struct lyxp_set_hash_node hnode;
634 uint32_t hash;
635
636 if (set->ht) {
637 hnode.node = node;
638 hnode.type = type;
639
640 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
641 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
642 hash = dict_hash_multi(hash, NULL, 0);
643
644 r = lyht_remove(set->ht, &hnode, hash);
645 assert(!r);
646 (void)r;
647
648 if (!set->ht->used) {
649 lyht_free(set->ht);
650 set->ht = NULL;
651 }
652 }
653}
654
655/**
656 * @brief Check whether node is in set based on its hash.
657 *
658 * @param[in] set Set to search in.
659 * @param[in] node Node to search for.
660 * @param[in] type Node type.
661 * @param[in] skip_idx Index in @p set to skip.
662 * @return LY_ERR
663 */
664static LY_ERR
665set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
666{
667 struct lyxp_set_hash_node hnode, *match_p;
668 uint32_t hash;
669
670 hnode.node = node;
671 hnode.type = type;
672
673 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
674 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
675 hash = dict_hash_multi(hash, NULL, 0);
676
677 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
678 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
679 /* we found it on the index that should be skipped, find another */
680 hnode = *match_p;
681 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
682 /* none other found */
683 return LY_SUCCESS;
684 }
685 }
686
687 return LY_EEXIST;
688 }
689
690 /* not found */
691 return LY_SUCCESS;
692}
693
Michal Vaskod3678892020-05-21 10:06:58 +0200694void
695lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200696{
697 if (!set) {
698 return;
699 }
700
701 if (set->type == LYXP_SET_NODE_SET) {
702 free(set->val.nodes);
703 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200704 } else if (set->type == LYXP_SET_SCNODE_SET) {
705 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200706 lyht_free(set->ht);
707 } else {
708 if (set->type == LYXP_SET_STRING) {
709 free(set->val.str);
710 }
711 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200712 }
Michal Vaskod3678892020-05-21 10:06:58 +0200713
714 set->val.nodes = NULL;
715 set->used = 0;
716 set->size = 0;
717 set->ht = NULL;
718 set->ctx_pos = 0;
719 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200720}
721
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100722/**
723 * @brief Free dynamically-allocated set.
724 *
725 * @param[in] set Set to free.
726 */
727static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200728lyxp_set_free(struct lyxp_set *set)
729{
730 if (!set) {
731 return;
732 }
733
Michal Vaskod3678892020-05-21 10:06:58 +0200734 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200735 free(set);
736}
737
738/**
739 * @brief Initialize set context.
740 *
741 * @param[in] new Set to initialize.
742 * @param[in] set Arbitrary initialized set.
743 */
744static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200745set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200746{
747 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200748 if (set) {
749 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200750 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100751 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200752 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100753 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200754 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200755 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200756 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200757 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200758}
759
760/**
761 * @brief Create a deep copy of a set.
762 *
763 * @param[in] set Set to copy.
764 * @return Copy of @p set.
765 */
766static struct lyxp_set *
767set_copy(struct lyxp_set *set)
768{
769 struct lyxp_set *ret;
770 uint16_t i;
771
772 if (!set) {
773 return NULL;
774 }
775
776 ret = malloc(sizeof *ret);
777 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
778 set_init(ret, set);
779
780 if (set->type == LYXP_SET_SCNODE_SET) {
781 ret->type = set->type;
782
783 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100784 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200785 uint32_t idx;
786 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
787 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100788 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200789 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200790 lyxp_set_free(ret);
791 return NULL;
792 }
Michal Vaskoba716542019-12-16 10:01:58 +0100793 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200794 }
795 }
796 } else if (set->type == LYXP_SET_NODE_SET) {
797 ret->type = set->type;
798 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
799 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
800 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
801
802 ret->used = ret->size = set->used;
803 ret->ctx_pos = set->ctx_pos;
804 ret->ctx_size = set->ctx_size;
805 ret->ht = lyht_dup(set->ht);
806 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200807 memcpy(ret, set, sizeof *ret);
808 if (set->type == LYXP_SET_STRING) {
809 ret->val.str = strdup(set->val.str);
810 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
811 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200812 }
813
814 return ret;
815}
816
817/**
818 * @brief Fill XPath set with a string. Any current data are disposed of.
819 *
820 * @param[in] set Set to fill.
821 * @param[in] string String to fill into \p set.
822 * @param[in] str_len Length of \p string. 0 is a valid value!
823 */
824static void
825set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
826{
Michal Vaskod3678892020-05-21 10:06:58 +0200827 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828
829 set->type = LYXP_SET_STRING;
830 if ((str_len == 0) && (string[0] != '\0')) {
831 string = "";
832 }
833 set->val.str = strndup(string, str_len);
834}
835
836/**
837 * @brief Fill XPath set with a number. Any current data are disposed of.
838 *
839 * @param[in] set Set to fill.
840 * @param[in] number Number to fill into \p set.
841 */
842static void
843set_fill_number(struct lyxp_set *set, long double number)
844{
Michal Vaskod3678892020-05-21 10:06:58 +0200845 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200846
847 set->type = LYXP_SET_NUMBER;
848 set->val.num = number;
849}
850
851/**
852 * @brief Fill XPath set with a boolean. Any current data are disposed of.
853 *
854 * @param[in] set Set to fill.
855 * @param[in] boolean Boolean to fill into \p set.
856 */
857static void
Radek Krejci857189e2020-09-01 13:26:36 +0200858set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200859{
Michal Vaskod3678892020-05-21 10:06:58 +0200860 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200861
862 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200863 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200864}
865
866/**
867 * @brief Fill XPath set with the value from another set (deep assign).
868 * Any current data are disposed of.
869 *
870 * @param[in] trg Set to fill.
871 * @param[in] src Source set to copy into \p trg.
872 */
873static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200874set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875{
876 if (!trg || !src) {
877 return;
878 }
879
880 if (trg->type == LYXP_SET_NODE_SET) {
881 free(trg->val.nodes);
882 } else if (trg->type == LYXP_SET_STRING) {
883 free(trg->val.str);
884 }
885 set_init(trg, src);
886
887 if (src->type == LYXP_SET_SCNODE_SET) {
888 trg->type = LYXP_SET_SCNODE_SET;
889 trg->used = src->used;
890 trg->size = src->used;
891
892 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
893 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
894 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
895 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200896 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200897 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200898 set_fill_number(trg, src->val.num);
899 } else if (src->type == LYXP_SET_STRING) {
900 set_fill_string(trg, src->val.str, strlen(src->val.str));
901 } else {
902 if (trg->type == LYXP_SET_NODE_SET) {
903 free(trg->val.nodes);
904 } else if (trg->type == LYXP_SET_STRING) {
905 free(trg->val.str);
906 }
907
Michal Vaskod3678892020-05-21 10:06:58 +0200908 assert(src->type == LYXP_SET_NODE_SET);
909
910 trg->type = LYXP_SET_NODE_SET;
911 trg->used = src->used;
912 trg->size = src->used;
913 trg->ctx_pos = src->ctx_pos;
914 trg->ctx_size = src->ctx_size;
915
916 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
917 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
918 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
919 if (src->ht) {
920 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200921 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200922 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200923 }
924 }
925}
926
927/**
928 * @brief Clear context of all schema nodes.
929 *
930 * @param[in] set Set to clear.
931 */
932static void
933set_scnode_clear_ctx(struct lyxp_set *set)
934{
935 uint32_t i;
936
937 for (i = 0; i < set->used; ++i) {
938 if (set->val.scnodes[i].in_ctx == 1) {
939 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100940 } else if (set->val.scnodes[i].in_ctx == -2) {
941 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200942 }
943 }
944}
945
946/**
947 * @brief Remove a node from a set. Removing last node changes
948 * set into LYXP_SET_EMPTY. Context position aware.
949 *
950 * @param[in] set Set to use.
951 * @param[in] idx Index from @p set of the node to be removed.
952 */
953static void
954set_remove_node(struct lyxp_set *set, uint32_t idx)
955{
956 assert(set && (set->type == LYXP_SET_NODE_SET));
957 assert(idx < set->used);
958
959 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
960
961 --set->used;
962 if (set->used) {
963 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
964 (set->used - idx) * sizeof *set->val.nodes);
965 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200966 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968}
969
970/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100971 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200972 *
973 * @param[in] set Set to use.
974 * @param[in] idx Index from @p set of the node to be removed.
975 */
976static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100977set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200978{
979 assert(set && (set->type == LYXP_SET_NODE_SET));
980 assert(idx < set->used);
981
Michal Vasko2caefc12019-11-14 16:07:56 +0100982 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
983 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
984 }
985 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200986}
987
988/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100989 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200990 * set into LYXP_SET_EMPTY. Context position aware.
991 *
992 * @param[in] set Set to consolidate.
993 */
994static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100995set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200996{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200997 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +0200998 int32_t start;
999
Michal Vaskod3678892020-05-21 10:06:58 +02001000 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001001
1002 orig_used = set->used;
1003 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001004 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001005 start = -1;
1006 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001007 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001008 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001009 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001010 end = i;
1011 ++i;
1012 break;
1013 }
1014
1015 ++i;
1016 if (i == orig_used) {
1017 end = i;
1018 }
1019 } while (i < orig_used);
1020
1021 if (start > -1) {
1022 /* move the whole chunk of valid nodes together */
1023 if (set->used != (unsigned)start) {
1024 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1025 }
1026 set->used += end - start;
1027 }
1028 }
Michal Vasko57eab132019-09-24 11:46:26 +02001029}
1030
1031/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001032 * @brief Check for duplicates in a node set.
1033 *
1034 * @param[in] set Set to check.
1035 * @param[in] node Node to look for in @p set.
1036 * @param[in] node_type Type of @p node.
1037 * @param[in] skip_idx Index from @p set to skip.
1038 * @return LY_ERR
1039 */
1040static LY_ERR
1041set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1042{
1043 uint32_t i;
1044
Michal Vasko2caefc12019-11-14 16:07:56 +01001045 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001046 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1047 }
1048
1049 for (i = 0; i < set->used; ++i) {
1050 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1051 continue;
1052 }
1053
1054 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1055 return LY_EEXIST;
1056 }
1057 }
1058
1059 return LY_SUCCESS;
1060}
1061
Radek Krejci857189e2020-09-01 13:26:36 +02001062ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001063lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1064 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001065{
1066 uint32_t i;
1067
1068 for (i = 0; i < set->used; ++i) {
1069 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1070 continue;
1071 }
1072
1073 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001074 if (index_p) {
1075 *index_p = i;
1076 }
1077 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001078 }
1079 }
1080
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001081 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001082}
1083
Michal Vaskoecd62de2019-11-13 12:35:11 +01001084void
1085lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001086{
1087 uint32_t orig_used, i, j;
1088
Michal Vaskod3678892020-05-21 10:06:58 +02001089 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001090
Michal Vaskod3678892020-05-21 10:06:58 +02001091 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001092 return;
1093 }
1094
Michal Vaskod3678892020-05-21 10:06:58 +02001095 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001096 memcpy(set1, set2, sizeof *set1);
1097 return;
1098 }
1099
1100 if (set1->used + set2->used > set1->size) {
1101 set1->size = set1->used + set2->used;
1102 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1103 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1104 }
1105
1106 orig_used = set1->used;
1107
1108 for (i = 0; i < set2->used; ++i) {
1109 for (j = 0; j < orig_used; ++j) {
1110 /* detect duplicities */
1111 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1112 break;
1113 }
1114 }
1115
1116 if (j == orig_used) {
1117 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1118 ++set1->used;
1119 }
1120 }
1121
Michal Vaskod3678892020-05-21 10:06:58 +02001122 lyxp_set_free_content(set2);
1123 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001124}
1125
1126/**
1127 * @brief Insert a node into a set. Context position aware.
1128 *
1129 * @param[in] set Set to use.
1130 * @param[in] node Node to insert to @p set.
1131 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1132 * @param[in] node_type Node type of @p node.
1133 * @param[in] idx Index in @p set to insert into.
1134 */
1135static void
1136set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1137{
Michal Vaskod3678892020-05-21 10:06:58 +02001138 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001139
Michal Vaskod3678892020-05-21 10:06:58 +02001140 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001141 /* first item */
1142 if (idx) {
1143 /* no real harm done, but it is a bug */
1144 LOGINT(set->ctx);
1145 idx = 0;
1146 }
1147 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1148 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1149 set->type = LYXP_SET_NODE_SET;
1150 set->used = 0;
1151 set->size = LYXP_SET_SIZE_START;
1152 set->ctx_pos = 1;
1153 set->ctx_size = 1;
1154 set->ht = NULL;
1155 } else {
1156 /* not an empty set */
1157 if (set->used == set->size) {
1158
1159 /* set is full */
1160 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1161 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1162 set->size += LYXP_SET_SIZE_STEP;
1163 }
1164
1165 if (idx > set->used) {
1166 LOGINT(set->ctx);
1167 idx = set->used;
1168 }
1169
1170 /* make space for the new node */
1171 if (idx < set->used) {
1172 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1173 }
1174 }
1175
1176 /* finally assign the value */
1177 set->val.nodes[idx].node = (struct lyd_node *)node;
1178 set->val.nodes[idx].type = node_type;
1179 set->val.nodes[idx].pos = pos;
1180 ++set->used;
1181
Michal Vasko2caefc12019-11-14 16:07:56 +01001182 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1183 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1184 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001185}
1186
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001187LY_ERR
1188lyxp_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 +02001189{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001190 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001191
1192 assert(set->type == LYXP_SET_SCNODE_SET);
1193
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001194 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
1195 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001196 } else {
1197 if (set->used == set->size) {
1198 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 +02001199 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001200 set->size += LYXP_SET_SIZE_STEP;
1201 }
1202
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001203 index = set->used;
1204 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1205 set->val.scnodes[index].type = node_type;
1206 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001207 ++set->used;
1208 }
1209
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001210 if (index_p) {
1211 *index_p = index;
1212 }
1213
1214 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001215}
1216
1217/**
1218 * @brief Replace a node in a set with another. Context position aware.
1219 *
1220 * @param[in] set Set to use.
1221 * @param[in] node Node to insert to @p set.
1222 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1223 * @param[in] node_type Node type of @p node.
1224 * @param[in] idx Index in @p set of the node to replace.
1225 */
1226static void
1227set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1228{
1229 assert(set && (idx < set->used));
1230
Michal Vasko2caefc12019-11-14 16:07:56 +01001231 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1232 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1233 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001234 set->val.nodes[idx].node = (struct lyd_node *)node;
1235 set->val.nodes[idx].type = node_type;
1236 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001237 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1238 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1239 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001240}
1241
1242/**
1243 * @brief Set all nodes with ctx 1 to a new unique context value.
1244 *
1245 * @param[in] set Set to modify.
1246 * @return New context value.
1247 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001248static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001249set_scnode_new_in_ctx(struct lyxp_set *set)
1250{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001251 uint32_t i;
1252 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001253
1254 assert(set->type == LYXP_SET_SCNODE_SET);
1255
1256 ret_ctx = 3;
1257retry:
1258 for (i = 0; i < set->used; ++i) {
1259 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1260 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1261 goto retry;
1262 }
1263 }
1264 for (i = 0; i < set->used; ++i) {
1265 if (set->val.scnodes[i].in_ctx == 1) {
1266 set->val.scnodes[i].in_ctx = ret_ctx;
1267 }
1268 }
1269
1270 return ret_ctx;
1271}
1272
1273/**
1274 * @brief Get unique @p node position in the data.
1275 *
1276 * @param[in] node Node to find.
1277 * @param[in] node_type Node type of @p node.
1278 * @param[in] root Root node.
1279 * @param[in] root_type Type of the XPath @p root node.
1280 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1281 * be used to increase efficiency and start the DFS from this node.
1282 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1283 * @return Node position.
1284 */
1285static uint32_t
1286get_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 +02001287 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288{
Michal Vasko56daf732020-08-10 10:57:18 +02001289 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001290 uint32_t pos = 1;
1291
1292 assert(prev && prev_pos && !root->prev->next);
1293
1294 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1295 return 0;
1296 }
1297
1298 if (*prev) {
1299 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001300 pos = *prev_pos;
Radek Krejci1e008d22020-08-17 11:37:37 +02001301 for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001302 goto dfs_search;
1303 }
1304
1305 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
Michal Vasko56daf732020-08-10 10:57:18 +02001306 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001307dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001308 if (*prev && !elem) {
1309 /* resume previous DFS */
1310 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1311 LYD_TREE_DFS_continue = 0;
1312 }
1313
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001315 /* skip */
1316 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001317 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001318 if (elem == node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001319 break;
1320 }
Michal Vasko56daf732020-08-10 10:57:18 +02001321 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 }
Michal Vasko56daf732020-08-10 10:57:18 +02001323
1324 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001325 }
1326
1327 /* node found */
1328 if (elem) {
1329 break;
1330 }
1331 }
1332
1333 if (!elem) {
1334 if (!(*prev)) {
1335 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001336 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001337 return 0;
1338 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001339 /* start the search again from the beginning */
1340 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001341
Michal Vasko56daf732020-08-10 10:57:18 +02001342 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001343 pos = 1;
1344 goto dfs_search;
1345 }
1346 }
1347
1348 /* remember the last found node for next time */
1349 *prev = node;
1350 *prev_pos = pos;
1351
1352 return pos;
1353}
1354
1355/**
1356 * @brief Assign (fill) missing node positions.
1357 *
1358 * @param[in] set Set to fill positions in.
1359 * @param[in] root Context root node.
1360 * @param[in] root_type Context root type.
1361 * @return LY_ERR
1362 */
1363static LY_ERR
1364set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1365{
1366 const struct lyd_node *prev = NULL, *tmp_node;
1367 uint32_t i, tmp_pos = 0;
1368
1369 for (i = 0; i < set->used; ++i) {
1370 if (!set->val.nodes[i].pos) {
1371 tmp_node = NULL;
1372 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001373 case LYXP_NODE_META:
1374 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001375 if (!tmp_node) {
1376 LOGINT_RET(root->schema->module->ctx);
1377 }
Radek Krejci0f969882020-08-21 16:56:47 +02001378 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001379 case LYXP_NODE_ELEM:
1380 case LYXP_NODE_TEXT:
1381 if (!tmp_node) {
1382 tmp_node = set->val.nodes[i].node;
1383 }
1384 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1385 break;
1386 default:
1387 /* all roots have position 0 */
1388 break;
1389 }
1390 }
1391 }
1392
1393 return LY_SUCCESS;
1394}
1395
1396/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001397 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001398 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001399 * @param[in] meta Metadata to use.
1400 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001401 */
1402static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001403get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001404{
1405 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001406 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001407
Michal Vasko9f96a052020-03-10 09:41:45 +01001408 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001409 ++pos;
1410 }
1411
Michal Vasko9f96a052020-03-10 09:41:45 +01001412 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001413 return pos;
1414}
1415
1416/**
1417 * @brief Compare 2 nodes in respect to XPath document order.
1418 *
1419 * @param[in] item1 1st node.
1420 * @param[in] item2 2nd node.
1421 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1422 */
1423static int
1424set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1425{
Michal Vasko9f96a052020-03-10 09:41:45 +01001426 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001427
1428 if (item1->pos < item2->pos) {
1429 return -1;
1430 }
1431
1432 if (item1->pos > item2->pos) {
1433 return 1;
1434 }
1435
1436 /* node positions are equal, the fun case */
1437
1438 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1439 /* special case since text nodes are actually saved as their parents */
1440 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1441 if (item1->type == LYXP_NODE_ELEM) {
1442 assert(item2->type == LYXP_NODE_TEXT);
1443 return -1;
1444 } else {
1445 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1446 return 1;
1447 }
1448 }
1449
Michal Vasko9f96a052020-03-10 09:41:45 +01001450 /* we need meta positions now */
1451 if (item1->type == LYXP_NODE_META) {
1452 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001453 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001454 if (item2->type == LYXP_NODE_META) {
1455 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001456 }
1457
Michal Vasko9f96a052020-03-10 09:41:45 +01001458 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001459 /* check for duplicates */
1460 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001461 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001462 return 0;
1463 }
1464
Michal Vasko9f96a052020-03-10 09:41:45 +01001465 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001466 /* elem is always first, 2nd node is after it */
1467 if (item1->type == LYXP_NODE_ELEM) {
1468 assert(item2->type != LYXP_NODE_ELEM);
1469 return -1;
1470 }
1471
Michal Vasko9f96a052020-03-10 09:41:45 +01001472 /* 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 +02001473 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001474 if (((item1->type == LYXP_NODE_TEXT) &&
1475 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1476 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1477 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1478 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001479 return 1;
1480 }
1481
Michal Vasko9f96a052020-03-10 09:41:45 +01001482 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001483 /* 2nd is after 1st */
1484 return -1;
1485}
1486
1487/**
1488 * @brief Set cast for comparisons.
1489 *
1490 * @param[in] trg Target set to cast source into.
1491 * @param[in] src Source set.
1492 * @param[in] type Target set type.
1493 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001494 * @return LY_ERR
1495 */
1496static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001497set_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 +02001498{
1499 assert(src->type == LYXP_SET_NODE_SET);
1500
1501 set_init(trg, src);
1502
1503 /* insert node into target set */
1504 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1505
1506 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001507 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001508}
1509
Michal Vasko4c7763f2020-07-27 17:40:37 +02001510/**
1511 * @brief Set content canonization for comparisons.
1512 *
1513 * @param[in] trg Target set to put the canononized source into.
1514 * @param[in] src Source set.
1515 * @param[in] xp_node Source XPath node/meta to use for canonization.
1516 * @return LY_ERR
1517 */
1518static LY_ERR
1519set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1520{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001521 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001522 struct lyd_value val;
1523 struct ly_err_item *err = NULL;
1524 char *str, *ptr;
Radek Krejci857189e2020-09-01 13:26:36 +02001525 ly_bool dynamic;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001526 LY_ERR rc;
1527
1528 /* is there anything to canonize even? */
1529 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1530 /* do we have a type to use for canonization? */
1531 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1532 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1533 } else if (xp_node->type == LYXP_NODE_META) {
1534 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1535 }
1536 }
1537 if (!type) {
1538 goto fill;
1539 }
1540
1541 if (src->type == LYXP_SET_NUMBER) {
1542 /* canonize number */
1543 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1544 LOGMEM(src->ctx);
1545 return LY_EMEM;
1546 }
1547 } else {
1548 /* canonize string */
1549 str = strdup(src->val.str);
1550 }
1551
1552 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko25d6ad02020-10-22 12:20:22 +02001553 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, LY_PREF_JSON, NULL, LYD_HINT_DATA,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001554 xp_node->node->schema, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001555 ly_err_free(err);
1556 if (rc) {
1557 /* invalid value */
1558 free(str);
1559 goto fill;
1560 }
1561
1562 /* storing successful, now print the canonical value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001563 str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001564
1565 /* use the canonized value */
1566 set_init(trg, src);
1567 trg->type = src->type;
1568 if (src->type == LYXP_SET_NUMBER) {
1569 trg->val.num = strtold(str, &ptr);
1570 if (dynamic) {
1571 free(str);
1572 }
1573 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1574 } else {
1575 trg->val.str = (dynamic ? str : strdup(str));
1576 }
1577 type->plugin->free(src->ctx, &val);
1578 return LY_SUCCESS;
1579
1580fill:
1581 /* no canonization needed/possible */
1582 set_fill_set(trg, src);
1583 return LY_SUCCESS;
1584}
1585
Michal Vasko03ff5a72019-09-11 13:49:33 +02001586#ifndef NDEBUG
1587
1588/**
1589 * @brief Bubble sort @p set into XPath document order.
1590 * Context position aware. Unused in the 'Release' build target.
1591 *
1592 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001593 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1594 */
1595static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001596set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001597{
1598 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001599 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001600 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001602 struct lyxp_set_node item;
1603 struct lyxp_set_hash_node hnode;
1604 uint64_t hash;
1605
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001606 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001607 return 0;
1608 }
1609
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001610 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001611 for (root = set->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001612 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613
1614 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001615 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001616 return -1;
1617 }
1618
1619 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1620 print_set_debug(set);
1621
1622 for (i = 0; i < set->used; ++i) {
1623 inverted = 0;
1624 change = 0;
1625
1626 for (j = 1; j < set->used - i; ++j) {
1627 /* compare node positions */
1628 if (inverted) {
1629 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1630 } else {
1631 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1632 }
1633
1634 /* swap if needed */
1635 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1636 change = 1;
1637
1638 item = set->val.nodes[j - 1];
1639 set->val.nodes[j - 1] = set->val.nodes[j];
1640 set->val.nodes[j] = item;
1641 } else {
1642 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1643 inverted = !inverted;
1644 }
1645 }
1646
1647 ++ret;
1648
1649 if (!change) {
1650 break;
1651 }
1652 }
1653
1654 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1655 print_set_debug(set);
1656
1657 /* check node hashes */
1658 if (set->used >= LYD_HT_MIN_ITEMS) {
1659 assert(set->ht);
1660 for (i = 0; i < set->used; ++i) {
1661 hnode.node = set->val.nodes[i].node;
1662 hnode.type = set->val.nodes[i].type;
1663
1664 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1665 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1666 hash = dict_hash_multi(hash, NULL, 0);
1667
1668 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1669 }
1670 }
1671
1672 return ret - 1;
1673}
1674
1675/**
1676 * @brief Remove duplicate entries in a sorted node set.
1677 *
1678 * @param[in] set Sorted set to check.
1679 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1680 */
1681static LY_ERR
1682set_sorted_dup_node_clean(struct lyxp_set *set)
1683{
1684 uint32_t i = 0;
1685 LY_ERR ret = LY_SUCCESS;
1686
1687 if (set->used > 1) {
1688 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001689 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1690 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001691 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001692 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001693 }
Michal Vasko57eab132019-09-24 11:46:26 +02001694 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001695 }
1696 }
1697
Michal Vasko2caefc12019-11-14 16:07:56 +01001698 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001699 return ret;
1700}
1701
1702#endif
1703
1704/**
1705 * @brief Merge 2 sorted sets into one.
1706 *
1707 * @param[in,out] trg Set to merge into. Duplicates are removed.
1708 * @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 +02001709 * @return LY_ERR
1710 */
1711static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001712set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001713{
1714 uint32_t i, j, k, count, dup_count;
1715 int cmp;
1716 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001717
Michal Vaskod3678892020-05-21 10:06:58 +02001718 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719 return LY_EINVAL;
1720 }
1721
Michal Vaskod3678892020-05-21 10:06:58 +02001722 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001723 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001724 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001725 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001726 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727 return LY_SUCCESS;
1728 }
1729
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001730 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001731 for (root = trg->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001732 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001733
1734 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001735 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001736 return LY_EINT;
1737 }
1738
1739#ifndef NDEBUG
1740 LOGDBG(LY_LDGXPATH, "MERGE target");
1741 print_set_debug(trg);
1742 LOGDBG(LY_LDGXPATH, "MERGE source");
1743 print_set_debug(src);
1744#endif
1745
1746 /* make memory for the merge (duplicates are not detected yet, so space
1747 * will likely be wasted on them, too bad) */
1748 if (trg->size - trg->used < src->used) {
1749 trg->size = trg->used + src->used;
1750
1751 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1752 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1753 }
1754
1755 i = 0;
1756 j = 0;
1757 count = 0;
1758 dup_count = 0;
1759 do {
1760 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1761 if (!cmp) {
1762 if (!count) {
1763 /* duplicate, just skip it */
1764 ++i;
1765 ++j;
1766 } else {
1767 /* we are copying something already, so let's copy the duplicate too,
1768 * we are hoping that afterwards there are some more nodes to
1769 * copy and this way we can copy them all together */
1770 ++count;
1771 ++dup_count;
1772 ++i;
1773 ++j;
1774 }
1775 } else if (cmp < 0) {
1776 /* inserting src node into trg, just remember it for now */
1777 ++count;
1778 ++i;
1779
1780 /* insert the hash now */
1781 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1782 } else if (count) {
1783copy_nodes:
1784 /* time to actually copy the nodes, we have found the largest block of nodes */
1785 memmove(&trg->val.nodes[j + (count - dup_count)],
1786 &trg->val.nodes[j],
1787 (trg->used - j) * sizeof *trg->val.nodes);
1788 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1789
1790 trg->used += count - dup_count;
1791 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1792 j += count - dup_count;
1793
1794 count = 0;
1795 dup_count = 0;
1796 } else {
1797 ++j;
1798 }
1799 } while ((i < src->used) && (j < trg->used));
1800
1801 if ((i < src->used) || count) {
1802 /* insert all the hashes first */
1803 for (k = i; k < src->used; ++k) {
1804 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1805 }
1806
1807 /* loop ended, but we need to copy something at trg end */
1808 count += src->used - i;
1809 i = src->used;
1810 goto copy_nodes;
1811 }
1812
1813 /* we are inserting hashes before the actual node insert, which causes
1814 * situations when there were initially not enough items for a hash table,
1815 * but even after some were inserted, hash table was not created (during
1816 * insertion the number of items is not updated yet) */
1817 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1818 set_insert_node_hash(trg, NULL, 0);
1819 }
1820
1821#ifndef NDEBUG
1822 LOGDBG(LY_LDGXPATH, "MERGE result");
1823 print_set_debug(trg);
1824#endif
1825
Michal Vaskod3678892020-05-21 10:06:58 +02001826 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001827 return LY_SUCCESS;
1828}
1829
1830/*
1831 * (re)parse functions
1832 *
1833 * Parse functions parse the expression into
1834 * tokens (syntactic analysis).
1835 *
1836 * Reparse functions perform semantic analysis
1837 * (do not save the result, just a check) of
1838 * the expression and fill repeat indices.
1839 */
1840
Michal Vasko14676352020-05-29 11:35:55 +02001841LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001842lyxp_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 +02001843{
Michal Vasko004d3152020-06-11 19:59:22 +02001844 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001845 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001846 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1847 }
Michal Vasko14676352020-05-29 11:35:55 +02001848 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001849 }
1850
Michal Vasko004d3152020-06-11 19:59:22 +02001851 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001852 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001853 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
1854 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001855 }
Michal Vasko14676352020-05-29 11:35:55 +02001856 return LY_ENOT;
1857 }
1858
1859 return LY_SUCCESS;
1860}
1861
Michal Vasko004d3152020-06-11 19:59:22 +02001862LY_ERR
1863lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1864{
1865 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1866
1867 /* skip the token */
1868 ++(*tok_idx);
1869
1870 return LY_SUCCESS;
1871}
1872
Michal Vasko14676352020-05-29 11:35:55 +02001873/* just like lyxp_check_token() but tests for 2 tokens */
1874static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001875exp_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 +02001876 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001877{
Michal Vasko004d3152020-06-11 19:59:22 +02001878 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001879 if (ctx) {
1880 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1881 }
1882 return LY_EINCOMPLETE;
1883 }
1884
Michal Vasko004d3152020-06-11 19:59:22 +02001885 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001886 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001887 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
1888 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001889 }
1890 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001891 }
1892
1893 return LY_SUCCESS;
1894}
1895
1896/**
1897 * @brief Stack operation push on the repeat array.
1898 *
1899 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001900 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001901 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1902 */
1903static void
Michal Vasko004d3152020-06-11 19:59:22 +02001904exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001905{
1906 uint16_t i;
1907
Michal Vasko004d3152020-06-11 19:59:22 +02001908 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001909 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001910 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1911 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1912 exp->repeat[tok_idx][i] = repeat_op_idx;
1913 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001914 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001915 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1916 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1917 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 }
1919}
1920
1921/**
1922 * @brief Reparse Predicate. Logs directly on error.
1923 *
1924 * [7] Predicate ::= '[' Expr ']'
1925 *
1926 * @param[in] ctx Context for logging.
1927 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001928 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001929 * @return LY_ERR
1930 */
1931static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001932reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001933{
1934 LY_ERR rc;
1935
Michal Vasko004d3152020-06-11 19:59:22 +02001936 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001938 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001939
Michal Vasko004d3152020-06-11 19:59:22 +02001940 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 LY_CHECK_RET(rc);
1942
Michal Vasko004d3152020-06-11 19:59:22 +02001943 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001944 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001945 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946
1947 return LY_SUCCESS;
1948}
1949
1950/**
1951 * @brief Reparse RelativeLocationPath. Logs directly on error.
1952 *
1953 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1954 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1955 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1956 *
1957 * @param[in] ctx Context for logging.
1958 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001959 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001960 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1961 */
1962static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001963reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001964{
1965 LY_ERR rc;
1966
Michal Vasko004d3152020-06-11 19:59:22 +02001967 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968 LY_CHECK_RET(rc);
1969
1970 goto step;
1971 do {
1972 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001973 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974
Michal Vasko004d3152020-06-11 19:59:22 +02001975 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976 LY_CHECK_RET(rc);
1977step:
1978 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001979 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001981 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 break;
1983
1984 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001985 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 break;
1987
1988 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001989 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990
Michal Vasko004d3152020-06-11 19:59:22 +02001991 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001993 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02001995 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001996 return LY_EVALID;
1997 }
Radek Krejci0f969882020-08-21 16:56:47 +02001998 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002000 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 goto reparse_predicate;
2002 break;
2003
2004 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002005 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002006
2007 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002008 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002010 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011
2012 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002013 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002015 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016
2017reparse_predicate:
2018 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002019 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2020 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 LY_CHECK_RET(rc);
2022 }
2023 break;
2024 default:
2025 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002026 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027 return LY_EVALID;
2028 }
Michal Vasko004d3152020-06-11 19:59:22 +02002029 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030
2031 return LY_SUCCESS;
2032}
2033
2034/**
2035 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2036 *
2037 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2038 *
2039 * @param[in] ctx Context for logging.
2040 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002041 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042 * @return LY_ERR
2043 */
2044static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002045reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046{
2047 LY_ERR rc;
2048
Michal Vasko004d3152020-06-11 19:59:22 +02002049 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 +02002050
2051 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002052 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002054 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055
Michal Vasko004d3152020-06-11 19:59:22 +02002056 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 return LY_SUCCESS;
2058 }
Michal Vasko004d3152020-06-11 19:59:22 +02002059 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 case LYXP_TOKEN_DOT:
2061 case LYXP_TOKEN_DDOT:
2062 case LYXP_TOKEN_AT:
2063 case LYXP_TOKEN_NAMETEST:
2064 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002065 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002067 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068 default:
2069 break;
2070 }
2071
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002073 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002074 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075
Michal Vasko004d3152020-06-11 19:59:22 +02002076 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 LY_CHECK_RET(rc);
2078 }
2079
2080 return LY_SUCCESS;
2081}
2082
2083/**
2084 * @brief Reparse FunctionCall. Logs directly on error.
2085 *
2086 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2087 *
2088 * @param[in] ctx Context for logging.
2089 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002090 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 * @return LY_ERR
2092 */
2093static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002094reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002096 int8_t min_arg_count = -1;
2097 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002098 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 LY_ERR rc;
2100
Michal Vasko004d3152020-06-11 19:59:22 +02002101 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002103 func_tok_idx = *tok_idx;
2104 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002106 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107 min_arg_count = 1;
2108 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002109 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 min_arg_count = 1;
2111 max_arg_count = 1;
2112 }
2113 break;
2114 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002115 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 min_arg_count = 1;
2117 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002118 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 min_arg_count = 0;
2120 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002121 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122 min_arg_count = 0;
2123 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002124 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 min_arg_count = 0;
2126 max_arg_count = 0;
2127 }
2128 break;
2129 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002130 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
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]], "false", 5)) {
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]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 1;
2138 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 min_arg_count = 1;
2141 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002142 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 min_arg_count = 1;
2144 max_arg_count = 1;
2145 }
2146 break;
2147 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002148 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002150 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 0;
2153 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002154 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 0;
2156 max_arg_count = 1;
2157 }
2158 break;
2159 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002160 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 1;
2162 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002163 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 1;
2165 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002166 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 0;
2168 max_arg_count = 0;
2169 }
2170 break;
2171 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002172 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 2;
2174 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002175 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 min_arg_count = 0;
2177 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002178 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 2;
2180 max_arg_count = 2;
2181 }
2182 break;
2183 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002184 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 2;
2186 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002187 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002188 min_arg_count = 3;
2189 max_arg_count = 3;
2190 }
2191 break;
2192 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002193 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 min_arg_count = 0;
2195 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002196 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 1;
2198 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002199 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 min_arg_count = 2;
2201 max_arg_count = 2;
2202 }
2203 break;
2204 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002205 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 2;
2207 max_arg_count = 2;
2208 }
2209 break;
2210 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002211 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 2;
2213 max_arg_count = 2;
2214 }
2215 break;
2216 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002217 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 0;
2219 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002220 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 0;
2222 max_arg_count = 1;
2223 }
2224 break;
2225 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002226 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 0;
2228 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002229 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 2;
2231 max_arg_count = 2;
2232 }
2233 break;
2234 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002235 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002236 min_arg_count = 2;
2237 max_arg_count = 2;
2238 }
2239 break;
2240 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002241 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 2;
2243 max_arg_count = 2;
2244 }
2245 break;
2246 }
2247 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002248 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 return LY_EINVAL;
2250 }
Michal Vasko004d3152020-06-11 19:59:22 +02002251 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252
2253 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002254 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002256 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257
2258 /* ( Expr ( ',' Expr )* )? */
2259 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002260 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002262 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002264 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 LY_CHECK_RET(rc);
2266 }
Michal Vasko004d3152020-06-11 19:59:22 +02002267 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2268 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269
2270 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002271 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 LY_CHECK_RET(rc);
2273 }
2274
2275 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002276 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002278 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279
Radek Krejci857189e2020-09-01 13:26:36 +02002280 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002281 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +02002282 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 return LY_EVALID;
2284 }
2285
2286 return LY_SUCCESS;
2287}
2288
2289/**
2290 * @brief Reparse PathExpr. Logs directly on error.
2291 *
2292 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2293 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2294 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2295 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2296 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2297 *
2298 * @param[in] ctx Context for logging.
2299 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002300 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002301 * @return LY_ERR
2302 */
2303static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002304reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305{
2306 LY_ERR rc;
2307
Michal Vasko004d3152020-06-11 19:59:22 +02002308 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002309 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310 }
2311
Michal Vasko004d3152020-06-11 19:59:22 +02002312 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 case LYXP_TOKEN_PAR1:
2314 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002315 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316
Michal Vasko004d3152020-06-11 19:59:22 +02002317 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 LY_CHECK_RET(rc);
2319
Michal Vasko004d3152020-06-11 19:59:22 +02002320 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002322 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002323 goto predicate;
2324 break;
2325 case LYXP_TOKEN_DOT:
2326 case LYXP_TOKEN_DDOT:
2327 case LYXP_TOKEN_AT:
2328 case LYXP_TOKEN_NAMETEST:
2329 case LYXP_TOKEN_NODETYPE:
2330 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002331 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 LY_CHECK_RET(rc);
2333 break;
2334 case LYXP_TOKEN_FUNCNAME:
2335 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002336 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 LY_CHECK_RET(rc);
2338 goto predicate;
2339 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002340 case LYXP_TOKEN_OPER_PATH:
2341 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002343 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 LY_CHECK_RET(rc);
2345 break;
2346 case LYXP_TOKEN_LITERAL:
2347 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002348 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349 goto predicate;
2350 break;
2351 case LYXP_TOKEN_NUMBER:
2352 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002353 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 goto predicate;
2355 break;
2356 default:
2357 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002358 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359 return LY_EVALID;
2360 }
2361
2362 return LY_SUCCESS;
2363
2364predicate:
2365 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002366 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2367 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 LY_CHECK_RET(rc);
2369 }
2370
2371 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002372 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373
2374 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002375 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002376
Michal Vasko004d3152020-06-11 19:59:22 +02002377 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002378 LY_CHECK_RET(rc);
2379 }
2380
2381 return LY_SUCCESS;
2382}
2383
2384/**
2385 * @brief Reparse UnaryExpr. Logs directly on error.
2386 *
2387 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2388 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2389 *
2390 * @param[in] ctx Context for logging.
2391 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002392 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393 * @return LY_ERR
2394 */
2395static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002396reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397{
2398 uint16_t prev_exp;
2399 LY_ERR rc;
2400
2401 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002402 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002403 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2404 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002406 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002407 }
2408
2409 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002410 prev_exp = *tok_idx;
2411 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412 LY_CHECK_RET(rc);
2413
2414 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002415 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418
Michal Vasko004d3152020-06-11 19:59:22 +02002419 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002420 LY_CHECK_RET(rc);
2421 }
2422
2423 return LY_SUCCESS;
2424}
2425
2426/**
2427 * @brief Reparse AdditiveExpr. Logs directly on error.
2428 *
2429 * [15] AdditiveExpr ::= MultiplicativeExpr
2430 * | AdditiveExpr '+' MultiplicativeExpr
2431 * | AdditiveExpr '-' MultiplicativeExpr
2432 * [16] MultiplicativeExpr ::= UnaryExpr
2433 * | MultiplicativeExpr '*' UnaryExpr
2434 * | MultiplicativeExpr 'div' UnaryExpr
2435 * | MultiplicativeExpr 'mod' UnaryExpr
2436 *
2437 * @param[in] ctx Context for logging.
2438 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002439 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440 * @return LY_ERR
2441 */
2442static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002443reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002444{
2445 uint16_t prev_add_exp, prev_mul_exp;
2446 LY_ERR rc;
2447
Michal Vasko004d3152020-06-11 19:59:22 +02002448 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 goto reparse_multiplicative_expr;
2450
2451 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002452 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2453 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002455 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002456
2457reparse_multiplicative_expr:
2458 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002459 prev_mul_exp = *tok_idx;
2460 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 LY_CHECK_RET(rc);
2462
2463 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002464 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2465 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002467 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468
Michal Vasko004d3152020-06-11 19:59:22 +02002469 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002470 LY_CHECK_RET(rc);
2471 }
2472 }
2473
2474 return LY_SUCCESS;
2475}
2476
2477/**
2478 * @brief Reparse EqualityExpr. Logs directly on error.
2479 *
2480 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2481 * | EqualityExpr '!=' RelationalExpr
2482 * [14] RelationalExpr ::= AdditiveExpr
2483 * | RelationalExpr '<' AdditiveExpr
2484 * | RelationalExpr '>' AdditiveExpr
2485 * | RelationalExpr '<=' AdditiveExpr
2486 * | RelationalExpr '>=' AdditiveExpr
2487 *
2488 * @param[in] ctx Context for logging.
2489 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002490 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002491 * @return LY_ERR
2492 */
2493static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002494reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002495{
2496 uint16_t prev_eq_exp, prev_rel_exp;
2497 LY_ERR rc;
2498
Michal Vasko004d3152020-06-11 19:59:22 +02002499 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 goto reparse_additive_expr;
2501
2502 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002503 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002504 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002505 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506
2507reparse_additive_expr:
2508 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002509 prev_rel_exp = *tok_idx;
2510 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 LY_CHECK_RET(rc);
2512
2513 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002514 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002515 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002516 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517
Michal Vasko004d3152020-06-11 19:59:22 +02002518 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002519 LY_CHECK_RET(rc);
2520 }
2521 }
2522
2523 return LY_SUCCESS;
2524}
2525
2526/**
2527 * @brief Reparse OrExpr. Logs directly on error.
2528 *
2529 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2530 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2531 *
2532 * @param[in] ctx Context for logging.
2533 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002534 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 * @return LY_ERR
2536 */
2537static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002538reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002539{
2540 uint16_t prev_or_exp, prev_and_exp;
2541 LY_ERR rc;
2542
Michal Vasko004d3152020-06-11 19:59:22 +02002543 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002544 goto reparse_equality_expr;
2545
2546 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002547 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 +02002548 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002549 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550
2551reparse_equality_expr:
2552 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002553 prev_and_exp = *tok_idx;
2554 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 LY_CHECK_RET(rc);
2556
2557 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002558 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 +02002559 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002560 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561
Michal Vasko004d3152020-06-11 19:59:22 +02002562 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 LY_CHECK_RET(rc);
2564 }
2565 }
2566
2567 return LY_SUCCESS;
2568}
Radek Krejcib1646a92018-11-02 16:08:26 +01002569
2570/**
2571 * @brief Parse NCName.
2572 *
2573 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002574 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002575 */
Radek Krejcid4270262019-01-07 15:07:25 +01002576static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002577parse_ncname(const char *ncname)
2578{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002579 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002580 size_t size;
2581 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002582
2583 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2584 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2585 return len;
2586 }
2587
2588 do {
2589 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002590 if (!*ncname) {
2591 break;
2592 }
Radek Krejcid4270262019-01-07 15:07:25 +01002593 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002594 } while (is_xmlqnamechar(uc) && (uc != ':'));
2595
2596 return len;
2597}
2598
2599/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002600 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002601 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002603 * @param[in] exp Expression to use.
2604 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002606 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002607 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002608 */
2609static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002610exp_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 +01002611{
2612 uint32_t prev;
2613
2614 if (exp->used == exp->size) {
2615 prev = exp->size;
2616 exp->size += LYXP_EXPR_SIZE_STEP;
2617 if (prev > exp->size) {
2618 LOGINT(ctx);
2619 return LY_EINT;
2620 }
2621
2622 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2623 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2624 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2625 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2626 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2627 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2628 }
2629
2630 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002631 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002632 exp->tok_len[exp->used] = tok_len;
2633 ++exp->used;
2634 return LY_SUCCESS;
2635}
2636
2637void
Michal Vasko14676352020-05-29 11:35:55 +02002638lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002639{
2640 uint16_t i;
2641
2642 if (!expr) {
2643 return;
2644 }
2645
2646 lydict_remove(ctx, expr->expr);
2647 free(expr->tokens);
2648 free(expr->tok_pos);
2649 free(expr->tok_len);
2650 if (expr->repeat) {
2651 for (i = 0; i < expr->used; ++i) {
2652 free(expr->repeat[i]);
2653 }
2654 }
2655 free(expr->repeat);
2656 free(expr);
2657}
2658
Radek Krejcif03a9e22020-09-18 20:09:31 +02002659LY_ERR
2660lyxp_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 +01002661{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002662 LY_ERR ret = LY_SUCCESS;
2663 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002664 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002665 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002666 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002667 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002668
Radek Krejcif03a9e22020-09-18 20:09:31 +02002669 assert(expr_p);
2670
2671 if (!expr_str[0]) {
Michal Vasko004d3152020-06-11 19:59:22 +02002672 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002673 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002674 }
2675
2676 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002677 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002678 }
2679 if (expr_len > UINT16_MAX) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002680 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2681 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002682 }
2683
2684 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002685 expr = calloc(1, sizeof *expr);
2686 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2687 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2688 expr->used = 0;
2689 expr->size = LYXP_EXPR_SIZE_START;
2690 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2691 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002692
Radek Krejcif03a9e22020-09-18 20:09:31 +02002693 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2694 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002695
Radek Krejcif03a9e22020-09-18 20:09:31 +02002696 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2697 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002698
Michal Vasko004d3152020-06-11 19:59:22 +02002699 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002700 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002701
Radek Krejcif03a9e22020-09-18 20:09:31 +02002702 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002703 ++parsed;
2704 }
2705
2706 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002707 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002708
2709 /* '(' */
2710 tok_len = 1;
2711 tok_type = LYXP_TOKEN_PAR1;
2712
Radek Krejcif03a9e22020-09-18 20:09:31 +02002713 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002714 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002715 if (((expr->tok_len[expr->used - 1] == 4) &&
2716 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2717 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2718 ((expr->tok_len[expr->used - 1] == 7) &&
2719 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002721 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002722 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002723 }
2724 prev_function_check = 0;
2725 }
2726
Radek Krejcif03a9e22020-09-18 20:09:31 +02002727 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002728
2729 /* ')' */
2730 tok_len = 1;
2731 tok_type = LYXP_TOKEN_PAR2;
2732
Radek Krejcif03a9e22020-09-18 20:09:31 +02002733 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002734
2735 /* '[' */
2736 tok_len = 1;
2737 tok_type = LYXP_TOKEN_BRACK1;
2738
Radek Krejcif03a9e22020-09-18 20:09:31 +02002739 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002740
2741 /* ']' */
2742 tok_len = 1;
2743 tok_type = LYXP_TOKEN_BRACK2;
2744
Radek Krejcif03a9e22020-09-18 20:09:31 +02002745 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002746
2747 /* '..' */
2748 tok_len = 2;
2749 tok_type = LYXP_TOKEN_DDOT;
2750
Radek Krejcif03a9e22020-09-18 20:09:31 +02002751 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002752
2753 /* '.' */
2754 tok_len = 1;
2755 tok_type = LYXP_TOKEN_DOT;
2756
Radek Krejcif03a9e22020-09-18 20:09:31 +02002757 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002758
2759 /* '@' */
2760 tok_len = 1;
2761 tok_type = LYXP_TOKEN_AT;
2762
Radek Krejcif03a9e22020-09-18 20:09:31 +02002763 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002764
2765 /* ',' */
2766 tok_len = 1;
2767 tok_type = LYXP_TOKEN_COMMA;
2768
Radek Krejcif03a9e22020-09-18 20:09:31 +02002769 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002770
2771 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002772 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2773 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002774 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2775 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002776 ++tok_len;
2777 tok_type = LYXP_TOKEN_LITERAL;
2778
Radek Krejcif03a9e22020-09-18 20:09:31 +02002779 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002780
2781 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2783 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002784 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2785 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002786 ++tok_len;
2787 tok_type = LYXP_TOKEN_LITERAL;
2788
Radek Krejcif03a9e22020-09-18 20:09:31 +02002789 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002790
2791 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2793 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002794 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002795 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002796 }
2797 tok_type = LYXP_TOKEN_NUMBER;
2798
Radek Krejcif03a9e22020-09-18 20:09:31 +02002799 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002800
2801 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002802 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002803 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002804 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002805 } else {
2806 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002807 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002808 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002809
Radek Krejcif03a9e22020-09-18 20:09:31 +02002810 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002811
Michal Vasko3e48bf32020-06-01 08:39:07 +02002812 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002813 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002814 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2815
Radek Krejcif03a9e22020-09-18 20:09:31 +02002816 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002817
2818 /* Operator '<=', '>=' */
2819 tok_len = 2;
2820 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002821
Radek Krejcif03a9e22020-09-18 20:09:31 +02002822 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002823
2824 /* Operator '|' */
2825 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002826 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002827
Radek Krejcif03a9e22020-09-18 20:09:31 +02002828 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002829
2830 /* Operator '+', '-' */
2831 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002832 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002833
Radek Krejcif03a9e22020-09-18 20:09:31 +02002834 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002835
Michal Vasko3e48bf32020-06-01 08:39:07 +02002836 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002837 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002838 tok_type = LYXP_TOKEN_OPER_EQUAL;
2839
Radek Krejcif03a9e22020-09-18 20:09:31 +02002840 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002841
2842 /* Operator '<', '>' */
2843 tok_len = 1;
2844 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002845
Michal Vasko69730152020-10-09 16:30:07 +02002846 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2852 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2853 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2854 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2855 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2856 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2857 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
2859 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002860 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002861 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002862 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002863
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002865 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002866 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002867
Radek Krejcif03a9e22020-09-18 20:09:31 +02002868 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002869 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002870 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002871
Radek Krejcif03a9e22020-09-18 20:09:31 +02002872 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002873 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002874 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002875
2876 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002877 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002878 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 +02002879 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002880 goto error;
2881 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002882 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
2883 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002884 goto error;
2885 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002886 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002887
2888 /* NameTest '*' */
2889 tok_len = 1;
2890 tok_type = LYXP_TOKEN_NAMETEST;
2891
2892 } else {
2893
2894 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002895 long int ncname_len = parse_ncname(&expr_str[parsed]);
2896 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002897 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2898 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002899 tok_len = ncname_len;
2900
Radek Krejcif03a9e22020-09-18 20:09:31 +02002901 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002902 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002903 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002904 ++tok_len;
2905 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002906 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2907 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002908 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2909 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002910 tok_len += ncname_len;
2911 }
2912 /* remove old flag to prevent ambiguities */
2913 prev_function_check = 0;
2914 tok_type = LYXP_TOKEN_NAMETEST;
2915 } else {
2916 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2917 prev_function_check = 1;
2918 tok_type = LYXP_TOKEN_NAMETEST;
2919 }
2920 }
2921
2922 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002923 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002924 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002925 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002926 ++parsed;
2927 }
2928
Radek Krejcif03a9e22020-09-18 20:09:31 +02002929 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002930
Michal Vasko004d3152020-06-11 19:59:22 +02002931 if (reparse) {
2932 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002933 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2934 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002935
Michal Vasko004d3152020-06-11 19:59:22 +02002936 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2938 if (expr->used > tok_idx) {
Michal Vasko004d3152020-06-11 19:59:22 +02002939 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002940 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002941 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002942 goto error;
2943 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002944 }
2945
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 print_expr_struct_debug(expr);
2947 *expr_p = expr;
2948 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002949
2950error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002951 lyxp_expr_free(ctx, expr);
2952 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002953}
2954
Michal Vasko1734be92020-09-22 08:55:10 +02002955LY_ERR
2956lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002957{
Michal Vasko1734be92020-09-22 08:55:10 +02002958 LY_ERR ret = LY_SUCCESS;
2959 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002960 uint32_t i, j;
2961
Michal Vasko7f45cf22020-10-01 12:49:44 +02002962 if (!exp) {
2963 goto cleanup;
2964 }
2965
Michal Vasko004d3152020-06-11 19:59:22 +02002966 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002967 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002968
2969 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002970 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002971 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2972
2973 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002974 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002975 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2976
2977 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002978 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002979 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2980
2981 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002982 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002983 for (i = 0; i < exp->used; ++i) {
2984 if (!exp->repeat[i]) {
2985 dup->repeat[i] = NULL;
2986 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002987 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002988 /* the ending 0 as well */
2989 ++j;
2990
Michal Vasko99c71642020-07-03 13:33:36 +02002991 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002992 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002993 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2994 dup->repeat[i][j - 1] = 0;
2995 }
2996 }
2997
2998 dup->used = exp->used;
2999 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003000 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003001
Michal Vasko1734be92020-09-22 08:55:10 +02003002cleanup:
3003 if (ret) {
3004 lyxp_expr_free(ctx, dup);
3005 } else {
3006 *dup_p = dup;
3007 }
3008 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003009}
3010
Michal Vasko03ff5a72019-09-11 13:49:33 +02003011/*
3012 * warn functions
3013 *
3014 * Warn functions check specific reasonable conditions for schema XPath
3015 * and print a warning if they are not satisfied.
3016 */
3017
3018/**
3019 * @brief Get the last-added schema node that is currently in the context.
3020 *
3021 * @param[in] set Set to search in.
3022 * @return Last-added schema context node, NULL if no node is in context.
3023 */
3024static struct lysc_node *
3025warn_get_scnode_in_ctx(struct lyxp_set *set)
3026{
3027 uint32_t i;
3028
3029 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3030 return NULL;
3031 }
3032
3033 i = set->used;
3034 do {
3035 --i;
3036 if (set->val.scnodes[i].in_ctx == 1) {
3037 /* if there are more, simply return the first found (last added) */
3038 return set->val.scnodes[i].scnode;
3039 }
3040 } while (i);
3041
3042 return NULL;
3043}
3044
3045/**
3046 * @brief Test whether a type is numeric - integer type or decimal64.
3047 *
3048 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003049 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003050 */
Radek Krejci857189e2020-09-01 13:26:36 +02003051static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003052warn_is_numeric_type(struct lysc_type *type)
3053{
3054 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003055 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003056 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003057
3058 switch (type->basetype) {
3059 case LY_TYPE_DEC64:
3060 case LY_TYPE_INT8:
3061 case LY_TYPE_UINT8:
3062 case LY_TYPE_INT16:
3063 case LY_TYPE_UINT16:
3064 case LY_TYPE_INT32:
3065 case LY_TYPE_UINT32:
3066 case LY_TYPE_INT64:
3067 case LY_TYPE_UINT64:
3068 return 1;
3069 case LY_TYPE_UNION:
3070 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003071 LY_ARRAY_FOR(uni->types, u) {
3072 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003073 if (ret) {
3074 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003075 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003076 }
3077 }
3078 /* did not find any suitable type */
3079 return 0;
3080 case LY_TYPE_LEAFREF:
3081 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3082 default:
3083 return 0;
3084 }
3085}
3086
3087/**
3088 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3089 *
3090 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003091 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003092 */
Radek Krejci857189e2020-09-01 13:26:36 +02003093static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003094warn_is_string_type(struct lysc_type *type)
3095{
3096 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003097 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003098 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003099
3100 switch (type->basetype) {
3101 case LY_TYPE_BITS:
3102 case LY_TYPE_ENUM:
3103 case LY_TYPE_IDENT:
3104 case LY_TYPE_INST:
3105 case LY_TYPE_STRING:
3106 return 1;
3107 case LY_TYPE_UNION:
3108 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003109 LY_ARRAY_FOR(uni->types, u) {
3110 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003111 if (ret) {
3112 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003113 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003114 }
3115 }
3116 /* did not find any suitable type */
3117 return 0;
3118 case LY_TYPE_LEAFREF:
3119 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3120 default:
3121 return 0;
3122 }
3123}
3124
3125/**
3126 * @brief Test whether a type is one specific type.
3127 *
3128 * @param[in] type Type to test.
3129 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003130 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003131 */
Radek Krejci857189e2020-09-01 13:26:36 +02003132static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003133warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3134{
3135 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003136 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003137 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003138
3139 if (type->basetype == base) {
3140 return 1;
3141 } else if (type->basetype == LY_TYPE_UNION) {
3142 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003143 LY_ARRAY_FOR(uni->types, u) {
3144 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003145 if (ret) {
3146 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003147 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003148 }
3149 }
3150 /* did not find any suitable type */
3151 return 0;
3152 } else if (type->basetype == LY_TYPE_LEAFREF) {
3153 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3154 }
3155
3156 return 0;
3157}
3158
3159/**
3160 * @brief Get next type of a (union) type.
3161 *
3162 * @param[in] type Base type.
3163 * @param[in] prev_type Previously returned type.
3164 * @return Next type or NULL.
3165 */
3166static struct lysc_type *
3167warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3168{
3169 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003170 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003171 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003172
3173 switch (type->basetype) {
3174 case LY_TYPE_UNION:
3175 uni = (struct lysc_type_union *)type;
3176 if (!prev_type) {
3177 return uni->types[0];
3178 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003179 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003181 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003183 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003184 found = 1;
3185 }
3186 }
3187 return NULL;
3188 default:
3189 if (prev_type) {
3190 assert(type == prev_type);
3191 return NULL;
3192 } else {
3193 return type;
3194 }
3195 }
3196}
3197
3198/**
3199 * @brief Test whether 2 types have a common type.
3200 *
3201 * @param[in] type1 First type.
3202 * @param[in] type2 Second type.
3203 * @return 1 if they do, 0 otherwise.
3204 */
3205static int
3206warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3207{
3208 struct lysc_type *t1, *rt1, *t2, *rt2;
3209
3210 t1 = NULL;
3211 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3212 if (t1->basetype == LY_TYPE_LEAFREF) {
3213 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3214 } else {
3215 rt1 = t1;
3216 }
3217
3218 t2 = NULL;
3219 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3220 if (t2->basetype == LY_TYPE_LEAFREF) {
3221 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3222 } else {
3223 rt2 = t2;
3224 }
3225
3226 if (rt2->basetype == rt1->basetype) {
3227 /* match found */
3228 return 1;
3229 }
3230 }
3231 }
3232
3233 return 0;
3234}
3235
3236/**
3237 * @brief Check both operands of comparison operators.
3238 *
3239 * @param[in] ctx Context for errors.
3240 * @param[in] set1 First operand set.
3241 * @param[in] set2 Second operand set.
3242 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3243 * @param[in] expr Start of the expression to print with the warning.
3244 * @param[in] tok_pos Token position.
3245 */
3246static void
Radek Krejci857189e2020-09-01 13:26:36 +02003247warn_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 +02003248{
3249 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003250 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003251
3252 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3253 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3254
3255 if (!node1 && !node2) {
3256 /* no node-sets involved, nothing to do */
3257 return;
3258 }
3259
3260 if (node1) {
3261 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3262 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3263 warning = 1;
3264 leaves = 0;
3265 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3266 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3267 warning = 1;
3268 }
3269 }
3270
3271 if (node2) {
3272 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3273 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3274 warning = 1;
3275 leaves = 0;
3276 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3277 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3278 warning = 1;
3279 }
3280 }
3281
3282 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003283 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3284 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3285 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3286 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003287 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3288 warning = 1;
3289 }
3290 }
3291
3292 if (warning) {
3293 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3294 }
3295}
3296
3297/**
3298 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3299 *
3300 * @param[in] exp Parsed XPath expression.
3301 * @param[in] set Set with the leaf/leaf-list.
3302 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3303 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3304 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3305 */
3306static void
Michal Vasko40308e72020-10-20 16:38:40 +02003307warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3308 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309{
3310 struct lysc_node *scnode;
3311 struct lysc_type *type;
3312 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003313 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003314 LY_ERR rc;
3315 struct ly_err_item *err = NULL;
3316
Michal Vasko69730152020-10-09 16:30:07 +02003317 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3318 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003319 /* check that the node can have the specified value */
3320 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3321 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3322 } else {
3323 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3324 }
3325 if (!value) {
3326 LOGMEM(set->ctx);
3327 return;
3328 }
3329
3330 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3331 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003332 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003333 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003334 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003335 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336 }
3337
3338 type = ((struct lysc_node_leaf *)scnode)->type;
3339 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003340 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003341 LYD_HINT_DATA, scnode, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003342
3343 if (err) {
3344 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3345 ly_err_free(err);
3346 } else if (rc != LY_SUCCESS) {
3347 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3348 }
3349 if (rc != LY_SUCCESS) {
3350 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003351 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003352 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003353 } else {
3354 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003355 }
3356 }
3357 free(value);
3358 }
3359}
3360
3361/*
3362 * XPath functions
3363 */
3364
3365/**
3366 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3367 * depending on whether the first node bit value from the second argument is set.
3368 *
3369 * @param[in] args Array of arguments.
3370 * @param[in] arg_count Count of elements in @p args.
3371 * @param[in,out] set Context and result set at the same time.
3372 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003373 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003374 */
3375static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003376xpath_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 +02003377{
3378 struct lyd_node_term *leaf;
3379 struct lysc_node_leaf *sleaf;
3380 struct lysc_type_bits *bits;
3381 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003382 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003383
3384 if (options & LYXP_SCNODE_ALL) {
3385 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3386 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3388 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 +02003389 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3390 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003391 }
3392
3393 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3394 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3395 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 +02003396 } else if (!warn_is_string_type(sleaf->type)) {
3397 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003398 }
3399 }
3400 set_scnode_clear_ctx(set);
3401 return rc;
3402 }
3403
Michal Vaskod3678892020-05-21 10:06:58 +02003404 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003405 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3406 "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003407 return LY_EVALID;
3408 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003409 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003410 LY_CHECK_RET(rc);
3411
3412 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003413 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003414 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003415 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3416 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003417 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003418 LY_ARRAY_FOR(bits->bits, u) {
3419 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003420 set_fill_boolean(set, 1);
3421 break;
3422 }
3423 }
3424 }
3425 }
3426
3427 return LY_SUCCESS;
3428}
3429
3430/**
3431 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3432 * with the argument converted to boolean.
3433 *
3434 * @param[in] args Array of arguments.
3435 * @param[in] arg_count Count of elements in @p args.
3436 * @param[in,out] set Context and result set at the same time.
3437 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003438 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003439 */
3440static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003441xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003442{
3443 LY_ERR rc;
3444
3445 if (options & LYXP_SCNODE_ALL) {
3446 set_scnode_clear_ctx(set);
3447 return LY_SUCCESS;
3448 }
3449
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003450 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003451 LY_CHECK_RET(rc);
3452 set_fill_set(set, args[0]);
3453
3454 return LY_SUCCESS;
3455}
3456
3457/**
3458 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3459 * with the first argument rounded up to the nearest integer.
3460 *
3461 * @param[in] args Array of arguments.
3462 * @param[in] arg_count Count of elements in @p args.
3463 * @param[in,out] set Context and result set at the same time.
3464 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003465 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003466 */
3467static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003468xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003469{
3470 struct lysc_node_leaf *sleaf;
3471 LY_ERR rc = LY_SUCCESS;
3472
3473 if (options & LYXP_SCNODE_ALL) {
3474 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3475 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003476 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3477 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 +02003478 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3479 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003480 }
3481 set_scnode_clear_ctx(set);
3482 return rc;
3483 }
3484
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003485 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003486 LY_CHECK_RET(rc);
3487 if ((long long)args[0]->val.num != args[0]->val.num) {
3488 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3489 } else {
3490 set_fill_number(set, args[0]->val.num);
3491 }
3492
3493 return LY_SUCCESS;
3494}
3495
3496/**
3497 * @brief Execute the XPath concat(string, string, string*) function.
3498 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3499 *
3500 * @param[in] args Array of arguments.
3501 * @param[in] arg_count Count of elements in @p args.
3502 * @param[in,out] set Context and result set at the same time.
3503 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003504 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003505 */
3506static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003507xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003508{
3509 uint16_t i;
3510 char *str = NULL;
3511 size_t used = 1;
3512 LY_ERR rc = LY_SUCCESS;
3513 struct lysc_node_leaf *sleaf;
3514
3515 if (options & LYXP_SCNODE_ALL) {
3516 for (i = 0; i < arg_count; ++i) {
3517 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3518 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3519 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003520 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003521 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003522 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 +02003523 }
3524 }
3525 }
3526 set_scnode_clear_ctx(set);
3527 return rc;
3528 }
3529
3530 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003531 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003532 if (rc != LY_SUCCESS) {
3533 free(str);
3534 return rc;
3535 }
3536
3537 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3538 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3539 strcpy(str + used - 1, args[i]->val.str);
3540 used += strlen(args[i]->val.str);
3541 }
3542
3543 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003544 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003545 set->type = LYXP_SET_STRING;
3546 set->val.str = str;
3547
3548 return LY_SUCCESS;
3549}
3550
3551/**
3552 * @brief Execute the XPath contains(string, string) function.
3553 * Returns LYXP_SET_BOOLEAN whether the second argument can
3554 * be found in the first or not.
3555 *
3556 * @param[in] args Array of arguments.
3557 * @param[in] arg_count Count of elements in @p args.
3558 * @param[in,out] set Context and result set at the same time.
3559 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003560 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003561 */
3562static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003563xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003564{
3565 struct lysc_node_leaf *sleaf;
3566 LY_ERR rc = LY_SUCCESS;
3567
3568 if (options & LYXP_SCNODE_ALL) {
3569 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3570 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3571 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 +02003572 } else if (!warn_is_string_type(sleaf->type)) {
3573 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003574 }
3575 }
3576
3577 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3578 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3579 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 +02003580 } else if (!warn_is_string_type(sleaf->type)) {
3581 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003582 }
3583 }
3584 set_scnode_clear_ctx(set);
3585 return rc;
3586 }
3587
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003588 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003589 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003590 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003591 LY_CHECK_RET(rc);
3592
3593 if (strstr(args[0]->val.str, args[1]->val.str)) {
3594 set_fill_boolean(set, 1);
3595 } else {
3596 set_fill_boolean(set, 0);
3597 }
3598
3599 return LY_SUCCESS;
3600}
3601
3602/**
3603 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3604 * with the size of the node-set from the argument.
3605 *
3606 * @param[in] args Array of arguments.
3607 * @param[in] arg_count Count of elements in @p args.
3608 * @param[in,out] set Context and result set at the same time.
3609 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003610 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611 */
3612static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003613xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003614{
3615 struct lysc_node *scnode = NULL;
3616 LY_ERR rc = LY_SUCCESS;
3617
3618 if (options & LYXP_SCNODE_ALL) {
3619 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3620 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621 }
3622 set_scnode_clear_ctx(set);
3623 return rc;
3624 }
3625
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003627 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003628 return LY_EVALID;
3629 }
3630
3631 set_fill_number(set, args[0]->used);
3632 return LY_SUCCESS;
3633}
3634
3635/**
3636 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3637 * with the context with the intial node.
3638 *
3639 * @param[in] args Array of arguments.
3640 * @param[in] arg_count Count of elements in @p args.
3641 * @param[in,out] set Context and result set at the same time.
3642 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003643 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644 */
3645static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003646xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647{
3648 if (arg_count || args) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003649 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003650 return LY_EVALID;
3651 }
3652
3653 if (options & LYXP_SCNODE_ALL) {
3654 set_scnode_clear_ctx(set);
3655
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003656 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003658 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659
3660 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003661 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003662 }
3663
3664 return LY_SUCCESS;
3665}
3666
3667/**
3668 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3669 * leafref or instance-identifier target node(s).
3670 *
3671 * @param[in] args Array of arguments.
3672 * @param[in] arg_count Count of elements in @p args.
3673 * @param[in,out] set Context and result set at the same time.
3674 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003675 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003676 */
3677static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003678xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679{
3680 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003681 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003682 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003683 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003684 struct ly_path *p;
3685 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003687 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 LY_ERR rc = LY_SUCCESS;
3689
3690 if (options & LYXP_SCNODE_ALL) {
3691 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3692 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003693 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3694 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 +02003695 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3696 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003697 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698 }
3699 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003700 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003701 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003702 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003703
3704 /* it was already evaluated on schema, it must succeed */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003705 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path,
3706 LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003707 assert(!rc);
3708
3709 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003710 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003711 ly_path_free(set->ctx, p);
3712
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003713 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003714 }
3715
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716 return rc;
3717 }
3718
Michal Vaskod3678892020-05-21 10:06:58 +02003719 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003720 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 return LY_EVALID;
3722 }
3723
Michal Vaskod3678892020-05-21 10:06:58 +02003724 lyxp_set_free_content(set);
3725 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003726 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3727 sleaf = (struct lysc_node_leaf *)leaf->schema;
3728 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3729 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3730 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003731 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
Michal Vasko69730152020-10-09 16:30:07 +02003732 &leaf->value, set->tree, &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003733 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003735 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 } else {
3738 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003739 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003740 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003741 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003742 return LY_EVALID;
3743 }
3744 }
Michal Vasko004d3152020-06-11 19:59:22 +02003745
3746 /* insert it */
3747 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748 }
3749 }
3750
3751 return LY_SUCCESS;
3752}
3753
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003754static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003755xpath_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 +02003756{
3757 uint16_t i;
3758 LY_ARRAY_COUNT_TYPE u;
3759 struct lyd_node_term *leaf;
3760 struct lysc_node_leaf *sleaf;
3761 struct lyd_meta *meta;
3762 struct lyd_value data = {0}, *val;
3763 struct ly_err_item *err = NULL;
3764 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003765 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003766
3767 if (options & LYXP_SCNODE_ALL) {
3768 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3769 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3770 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3771 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003772 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003773 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3774 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3775 }
3776
3777 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3778 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3779 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003780 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003781 } else if (!warn_is_string_type(sleaf->type)) {
3782 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3783 }
3784 }
3785 set_scnode_clear_ctx(set);
3786 return rc;
3787 }
3788
3789 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003790 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02003791 "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003792 return LY_EVALID;
3793 }
3794 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3795 LY_CHECK_RET(rc);
3796
3797 set_fill_boolean(set, 0);
3798 found = 0;
3799 for (i = 0; i < args[0]->used; ++i) {
3800 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3801 continue;
3802 }
3803
3804 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3805 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3806 sleaf = (struct lysc_node_leaf *)leaf->schema;
3807 val = &leaf->value;
3808 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3809 /* uninteresting */
3810 continue;
3811 }
3812
3813 /* store args[1] as ident */
3814 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003815 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003816 } else {
3817 meta = args[0]->val.meta[i].meta;
3818 val = &meta->value;
3819 if (val->realtype->basetype != LY_TYPE_IDENT) {
3820 /* uninteresting */
3821 continue;
3822 }
3823
3824 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003825 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003826 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003827 }
3828
3829 if (err) {
3830 ly_err_print(err);
3831 ly_err_free(err);
3832 }
3833 LY_CHECK_RET(rc);
3834
3835 /* finally check the identity itself */
3836 if (self_match && (data.ident == val->ident)) {
3837 set_fill_boolean(set, 1);
3838 found = 1;
3839 }
3840 if (!found) {
3841 LY_ARRAY_FOR(data.ident->derived, u) {
3842 if (data.ident->derived[u] == val->ident) {
3843 set_fill_boolean(set, 1);
3844 found = 1;
3845 break;
3846 }
3847 }
3848 }
3849
3850 /* free temporary value */
3851 val->realtype->plugin->free(set->ctx, &data);
3852 if (found) {
3853 break;
3854 }
3855 }
3856
3857 return LY_SUCCESS;
3858}
3859
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860/**
3861 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3862 * on whether the first argument nodes contain a node of an identity derived from the second
3863 * argument identity.
3864 *
3865 * @param[in] args Array of arguments.
3866 * @param[in] arg_count Count of elements in @p args.
3867 * @param[in,out] set Context and result set at the same time.
3868 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003869 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870 */
3871static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003872xpath_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 +02003873{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003874 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003875}
3876
3877/**
3878 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3879 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3880 * the second argument identity.
3881 *
3882 * @param[in] args Array of arguments.
3883 * @param[in] arg_count Count of elements in @p args.
3884 * @param[in,out] set Context and result set at the same time.
3885 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003886 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003887 */
3888static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003889xpath_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 +02003890{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003891 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003892}
3893
3894/**
3895 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3896 * with the integer value of the first node's enum value, otherwise NaN.
3897 *
3898 * @param[in] args Array of arguments.
3899 * @param[in] arg_count Count of elements in @p args.
3900 * @param[in,out] set Context and result set at the same time.
3901 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003902 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903 */
3904static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003905xpath_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 +02003906{
3907 struct lyd_node_term *leaf;
3908 struct lysc_node_leaf *sleaf;
3909 LY_ERR rc = LY_SUCCESS;
3910
3911 if (options & LYXP_SCNODE_ALL) {
3912 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3913 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003914 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3915 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 +02003916 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3917 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003918 }
3919 set_scnode_clear_ctx(set);
3920 return rc;
3921 }
3922
Michal Vaskod3678892020-05-21 10:06:58 +02003923 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003924 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003925 return LY_EVALID;
3926 }
3927
3928 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003929 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3931 sleaf = (struct lysc_node_leaf *)leaf->schema;
3932 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3933 set_fill_number(set, leaf->value.enum_item->value);
3934 }
3935 }
3936
3937 return LY_SUCCESS;
3938}
3939
3940/**
3941 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3942 * with false value.
3943 *
3944 * @param[in] args Array of arguments.
3945 * @param[in] arg_count Count of elements in @p args.
3946 * @param[in,out] set Context and result set at the same time.
3947 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003948 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003949 */
3950static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003951xpath_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 +02003952{
3953 if (options & LYXP_SCNODE_ALL) {
3954 set_scnode_clear_ctx(set);
3955 return LY_SUCCESS;
3956 }
3957
3958 set_fill_boolean(set, 0);
3959 return LY_SUCCESS;
3960}
3961
3962/**
3963 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3964 * with the first argument floored (truncated).
3965 *
3966 * @param[in] args Array of arguments.
3967 * @param[in] arg_count Count of elements in @p args.
3968 * @param[in,out] set Context and result set at the same time.
3969 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003970 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003971 */
3972static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003973xpath_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 +02003974{
3975 LY_ERR rc;
3976
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003977 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003978 LY_CHECK_RET(rc);
3979 if (isfinite(args[0]->val.num)) {
3980 set_fill_number(set, (long long)args[0]->val.num);
3981 }
3982
3983 return LY_SUCCESS;
3984}
3985
3986/**
3987 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3988 * whether the language of the text matches the one from the argument.
3989 *
3990 * @param[in] args Array of arguments.
3991 * @param[in] arg_count Count of elements in @p args.
3992 * @param[in,out] set Context and result set at the same time.
3993 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003994 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995 */
3996static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003997xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003998{
3999 const struct lyd_node *node;
4000 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004001 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004003 LY_ERR rc = LY_SUCCESS;
4004
4005 if (options & LYXP_SCNODE_ALL) {
4006 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4007 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4008 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 +02004009 } else if (!warn_is_string_type(sleaf->type)) {
4010 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004011 }
4012 }
4013 set_scnode_clear_ctx(set);
4014 return rc;
4015 }
4016
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004017 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004018 LY_CHECK_RET(rc);
4019
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004021 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004022 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004023 } else if (!set->used) {
4024 set_fill_boolean(set, 0);
4025 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 }
4027
4028 switch (set->val.nodes[0].type) {
4029 case LYXP_NODE_ELEM:
4030 case LYXP_NODE_TEXT:
4031 node = set->val.nodes[0].node;
4032 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004033 case LYXP_NODE_META:
4034 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004035 break;
4036 default:
4037 /* nothing to do with roots */
4038 set_fill_boolean(set, 0);
4039 return LY_SUCCESS;
4040 }
4041
Michal Vasko9f96a052020-03-10 09:41:45 +01004042 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004043 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004044 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004046 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004047 break;
4048 }
4049 }
4050
Michal Vasko9f96a052020-03-10 09:41:45 +01004051 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004052 break;
4053 }
4054 }
4055
4056 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004057 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 set_fill_boolean(set, 0);
4059 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004060 uint64_t i;
4061
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004062 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004063 for (i = 0; args[0]->val.str[i]; ++i) {
4064 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4065 set_fill_boolean(set, 0);
4066 break;
4067 }
4068 }
4069 if (!args[0]->val.str[i]) {
4070 if (!val[i] || (val[i] == '-')) {
4071 set_fill_boolean(set, 1);
4072 } else {
4073 set_fill_boolean(set, 0);
4074 }
4075 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004076 }
4077
4078 return LY_SUCCESS;
4079}
4080
4081/**
4082 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4083 * with the context size.
4084 *
4085 * @param[in] args Array of arguments.
4086 * @param[in] arg_count Count of elements in @p args.
4087 * @param[in,out] set Context and result set at the same time.
4088 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004089 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004090 */
4091static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004092xpath_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 +02004093{
4094 if (options & LYXP_SCNODE_ALL) {
4095 set_scnode_clear_ctx(set);
4096 return LY_SUCCESS;
4097 }
4098
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004100 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004101 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004102 } else if (!set->used) {
4103 set_fill_number(set, 0);
4104 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004105 }
4106
4107 set_fill_number(set, set->ctx_size);
4108 return LY_SUCCESS;
4109}
4110
4111/**
4112 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4113 * with the node name without namespace from the argument or the context.
4114 *
4115 * @param[in] args Array of arguments.
4116 * @param[in] arg_count Count of elements in @p args.
4117 * @param[in,out] set Context and result set at the same time.
4118 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004119 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004120 */
4121static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004122xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004123{
4124 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004125
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126 /* suppress unused variable warning */
4127 (void)options;
4128
4129 if (options & LYXP_SCNODE_ALL) {
4130 set_scnode_clear_ctx(set);
4131 return LY_SUCCESS;
4132 }
4133
4134 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004135 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004136 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4137 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004138 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004139 } else if (!args[0]->used) {
4140 set_fill_string(set, "", 0);
4141 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004142 }
4143
4144 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004145 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004146
4147 item = &args[0]->val.nodes[0];
4148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004150 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004151 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004152 } else if (!set->used) {
4153 set_fill_string(set, "", 0);
4154 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004155 }
4156
4157 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004158 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004159
4160 item = &set->val.nodes[0];
4161 }
4162
4163 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004164 case LYXP_NODE_NONE:
4165 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166 case LYXP_NODE_ROOT:
4167 case LYXP_NODE_ROOT_CONFIG:
4168 case LYXP_NODE_TEXT:
4169 set_fill_string(set, "", 0);
4170 break;
4171 case LYXP_NODE_ELEM:
4172 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4173 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004174 case LYXP_NODE_META:
4175 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004176 break;
4177 }
4178
4179 return LY_SUCCESS;
4180}
4181
4182/**
4183 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4184 * with the node name fully qualified (with namespace) from the argument or the context.
4185 *
4186 * @param[in] args Array of arguments.
4187 * @param[in] arg_count Count of elements in @p args.
4188 * @param[in,out] set Context and result set at the same time.
4189 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004190 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 */
4192static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004193xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194{
4195 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004196 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004198 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199
4200 if (options & LYXP_SCNODE_ALL) {
4201 set_scnode_clear_ctx(set);
4202 return LY_SUCCESS;
4203 }
4204
4205 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004207 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004209 } else if (!args[0]->used) {
4210 set_fill_string(set, "", 0);
4211 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 }
4213
4214 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004215 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216
4217 item = &args[0]->val.nodes[0];
4218 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004220 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004222 } else if (!set->used) {
4223 set_fill_string(set, "", 0);
4224 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 }
4226
4227 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004228 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229
4230 item = &set->val.nodes[0];
4231 }
4232
4233 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004234 case LYXP_NODE_NONE:
4235 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004236 case LYXP_NODE_ROOT:
4237 case LYXP_NODE_ROOT_CONFIG:
4238 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004239 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004240 break;
4241 case LYXP_NODE_ELEM:
4242 mod = item->node->schema->module;
4243 name = item->node->schema->name;
4244 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004245 case LYXP_NODE_META:
4246 mod = ((struct lyd_meta *)item->node)->annotation->module;
4247 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004248 break;
4249 }
4250
4251 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004252 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004253 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4254 set_fill_string(set, str, strlen(str));
4255 free(str);
4256 } else {
4257 set_fill_string(set, "", 0);
4258 }
4259
4260 return LY_SUCCESS;
4261}
4262
4263/**
4264 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4265 * with the namespace of the node from the argument or the context.
4266 *
4267 * @param[in] args Array of arguments.
4268 * @param[in] arg_count Count of elements in @p args.
4269 * @param[in,out] set Context and result set at the same time.
4270 * @param[in] options XPath options.
4271 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4272 */
4273static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004274xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275{
4276 struct lyxp_set_node *item;
4277 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004278
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 /* suppress unused variable warning */
4280 (void)options;
4281
4282 if (options & LYXP_SCNODE_ALL) {
4283 set_scnode_clear_ctx(set);
4284 return LY_SUCCESS;
4285 }
4286
4287 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004288 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004289 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004290 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004291 return LY_EVALID;
4292 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293 set_fill_string(set, "", 0);
4294 return LY_SUCCESS;
4295 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296
4297 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004298 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299
4300 item = &args[0]->val.nodes[0];
4301 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004303 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004304 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004305 } else if (!set->used) {
4306 set_fill_string(set, "", 0);
4307 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 }
4309
4310 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004311 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004312
4313 item = &set->val.nodes[0];
4314 }
4315
4316 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004317 case LYXP_NODE_NONE:
4318 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 case LYXP_NODE_ROOT:
4320 case LYXP_NODE_ROOT_CONFIG:
4321 case LYXP_NODE_TEXT:
4322 set_fill_string(set, "", 0);
4323 break;
4324 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004325 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 if (item->type == LYXP_NODE_ELEM) {
4327 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004328 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004330 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004331 }
4332
4333 set_fill_string(set, mod->ns, strlen(mod->ns));
4334 break;
4335 }
4336
4337 return LY_SUCCESS;
4338}
4339
4340/**
4341 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4342 * with only nodes from the context. In practice it either leaves the context
4343 * as it is or returns an empty node set.
4344 *
4345 * @param[in] args Array of arguments.
4346 * @param[in] arg_count Count of elements in @p args.
4347 * @param[in,out] set Context and result set at the same time.
4348 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004349 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004350 */
4351static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004352xpath_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 +02004353{
4354 if (options & LYXP_SCNODE_ALL) {
4355 set_scnode_clear_ctx(set);
4356 return LY_SUCCESS;
4357 }
4358
4359 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004360 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004361 }
4362 return LY_SUCCESS;
4363}
4364
4365/**
4366 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4367 * with normalized value (no leading, trailing, double white spaces) of the node
4368 * from the argument or the context.
4369 *
4370 * @param[in] args Array of arguments.
4371 * @param[in] arg_count Count of elements in @p args.
4372 * @param[in,out] set Context and result set at the same time.
4373 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004374 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 */
4376static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004377xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004378{
4379 uint16_t i, new_used;
4380 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004381 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004382 struct lysc_node_leaf *sleaf;
4383 LY_ERR rc = LY_SUCCESS;
4384
4385 if (options & LYXP_SCNODE_ALL) {
4386 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4387 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4388 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 +02004389 } else if (!warn_is_string_type(sleaf->type)) {
4390 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004391 }
4392 }
4393 set_scnode_clear_ctx(set);
4394 return rc;
4395 }
4396
4397 if (arg_count) {
4398 set_fill_set(set, args[0]);
4399 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004400 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004401 LY_CHECK_RET(rc);
4402
4403 /* is there any normalization necessary? */
4404 for (i = 0; set->val.str[i]; ++i) {
4405 if (is_xmlws(set->val.str[i])) {
4406 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4407 have_spaces = 1;
4408 break;
4409 }
4410 space_before = 1;
4411 } else {
4412 space_before = 0;
4413 }
4414 }
4415
4416 /* yep, there is */
4417 if (have_spaces) {
4418 /* it's enough, at least one character will go, makes space for ending '\0' */
4419 new = malloc(strlen(set->val.str) * sizeof(char));
4420 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4421 new_used = 0;
4422
4423 space_before = 0;
4424 for (i = 0; set->val.str[i]; ++i) {
4425 if (is_xmlws(set->val.str[i])) {
4426 if ((i == 0) || space_before) {
4427 space_before = 1;
4428 continue;
4429 } else {
4430 space_before = 1;
4431 }
4432 } else {
4433 space_before = 0;
4434 }
4435
4436 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4437 ++new_used;
4438 }
4439
4440 /* at worst there is one trailing space now */
4441 if (new_used && is_xmlws(new[new_used - 1])) {
4442 --new_used;
4443 }
4444
4445 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4446 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4447 new[new_used] = '\0';
4448
4449 free(set->val.str);
4450 set->val.str = new;
4451 }
4452
4453 return LY_SUCCESS;
4454}
4455
4456/**
4457 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4458 * with the argument converted to boolean and logically inverted.
4459 *
4460 * @param[in] args Array of arguments.
4461 * @param[in] arg_count Count of elements in @p args.
4462 * @param[in,out] set Context and result set at the same time.
4463 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004464 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465 */
4466static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004467xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468{
4469 if (options & LYXP_SCNODE_ALL) {
4470 set_scnode_clear_ctx(set);
4471 return LY_SUCCESS;
4472 }
4473
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004474 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004475 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 set_fill_boolean(set, 0);
4477 } else {
4478 set_fill_boolean(set, 1);
4479 }
4480
4481 return LY_SUCCESS;
4482}
4483
4484/**
4485 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4486 * with the number representation of either the argument or the context.
4487 *
4488 * @param[in] args Array of arguments.
4489 * @param[in] arg_count Count of elements in @p args.
4490 * @param[in,out] set Context and result set at the same time.
4491 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004492 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004493 */
4494static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004495xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004496{
4497 LY_ERR rc;
4498
4499 if (options & LYXP_SCNODE_ALL) {
4500 set_scnode_clear_ctx(set);
4501 return LY_SUCCESS;
4502 }
4503
4504 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004505 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004506 LY_CHECK_RET(rc);
4507 set_fill_set(set, args[0]);
4508 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004509 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 LY_CHECK_RET(rc);
4511 }
4512
4513 return LY_SUCCESS;
4514}
4515
4516/**
4517 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4518 * with the context position.
4519 *
4520 * @param[in] args Array of arguments.
4521 * @param[in] arg_count Count of elements in @p args.
4522 * @param[in,out] set Context and result set at the same time.
4523 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004524 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 */
4526static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004527xpath_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 +02004528{
4529 if (options & LYXP_SCNODE_ALL) {
4530 set_scnode_clear_ctx(set);
4531 return LY_SUCCESS;
4532 }
4533
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004535 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004536 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004537 } else if (!set->used) {
4538 set_fill_number(set, 0);
4539 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 }
4541
4542 set_fill_number(set, set->ctx_pos);
4543
4544 /* UNUSED in 'Release' build type */
4545 (void)options;
4546 return LY_SUCCESS;
4547}
4548
4549/**
4550 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4551 * depending on whether the second argument regex matches the first argument string. For details refer to
4552 * YANG 1.1 RFC section 10.2.1.
4553 *
4554 * @param[in] args Array of arguments.
4555 * @param[in] arg_count Count of elements in @p args.
4556 * @param[in,out] set Context and result set at the same time.
4557 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004558 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004559 */
4560static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004561xpath_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 +02004562{
4563 struct lysc_pattern **patterns = NULL, **pattern;
4564 struct lysc_node_leaf *sleaf;
4565 char *path;
4566 LY_ERR rc = LY_SUCCESS;
4567 struct ly_err_item *err;
4568
4569 if (options & LYXP_SCNODE_ALL) {
4570 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4571 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4572 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 +02004573 } else if (!warn_is_string_type(sleaf->type)) {
4574 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004575 }
4576 }
4577
4578 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4579 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4580 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 +02004581 } else if (!warn_is_string_type(sleaf->type)) {
4582 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 }
4584 }
4585 set_scnode_clear_ctx(set);
4586 return rc;
4587 }
4588
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004589 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004591 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004592 LY_CHECK_RET(rc);
4593
4594 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4595 *pattern = malloc(sizeof **pattern);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004596 path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004597 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4598 free(path);
4599 if (rc != LY_SUCCESS) {
4600 LY_ARRAY_FREE(patterns);
4601 return rc;
4602 }
4603
4604 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4605 pcre2_code_free((*pattern)->code);
4606 free(*pattern);
4607 LY_ARRAY_FREE(patterns);
4608 if (rc && (rc != LY_EVALID)) {
4609 ly_err_print(err);
4610 ly_err_free(err);
4611 return rc;
4612 }
4613
4614 if (rc == LY_EVALID) {
4615 ly_err_free(err);
4616 set_fill_boolean(set, 0);
4617 } else {
4618 set_fill_boolean(set, 1);
4619 }
4620
4621 return LY_SUCCESS;
4622}
4623
4624/**
4625 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4626 * with the rounded first argument. For details refer to
4627 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4628 *
4629 * @param[in] args Array of arguments.
4630 * @param[in] arg_count Count of elements in @p args.
4631 * @param[in,out] set Context and result set at the same time.
4632 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004633 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 */
4635static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004636xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004637{
4638 struct lysc_node_leaf *sleaf;
4639 LY_ERR rc = LY_SUCCESS;
4640
4641 if (options & LYXP_SCNODE_ALL) {
4642 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4643 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004644 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4645 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 +02004646 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4647 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 }
4649 set_scnode_clear_ctx(set);
4650 return rc;
4651 }
4652
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004653 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 LY_CHECK_RET(rc);
4655
4656 /* cover only the cases where floor can't be used */
4657 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4658 set_fill_number(set, -0.0f);
4659 } else {
4660 args[0]->val.num += 0.5;
4661 rc = xpath_floor(args, 1, args[0], options);
4662 LY_CHECK_RET(rc);
4663 set_fill_number(set, args[0]->val.num);
4664 }
4665
4666 return LY_SUCCESS;
4667}
4668
4669/**
4670 * @brief Execute the XPath starts-with(string, string) function.
4671 * Returns LYXP_SET_BOOLEAN whether the second argument is
4672 * the prefix of the first or not.
4673 *
4674 * @param[in] args Array of arguments.
4675 * @param[in] arg_count Count of elements in @p args.
4676 * @param[in,out] set Context and result set at the same time.
4677 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004678 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 */
4680static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004681xpath_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 +02004682{
4683 struct lysc_node_leaf *sleaf;
4684 LY_ERR rc = LY_SUCCESS;
4685
4686 if (options & LYXP_SCNODE_ALL) {
4687 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4688 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4689 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 +02004690 } else if (!warn_is_string_type(sleaf->type)) {
4691 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004692 }
4693 }
4694
4695 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4696 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4697 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 +02004698 } else if (!warn_is_string_type(sleaf->type)) {
4699 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004700 }
4701 }
4702 set_scnode_clear_ctx(set);
4703 return rc;
4704 }
4705
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004706 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004707 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004708 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004709 LY_CHECK_RET(rc);
4710
4711 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4712 set_fill_boolean(set, 0);
4713 } else {
4714 set_fill_boolean(set, 1);
4715 }
4716
4717 return LY_SUCCESS;
4718}
4719
4720/**
4721 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4722 * with the string representation of either the argument or the context.
4723 *
4724 * @param[in] args Array of arguments.
4725 * @param[in] arg_count Count of elements in @p args.
4726 * @param[in,out] set Context and result set at the same time.
4727 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004728 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004729 */
4730static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004731xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004732{
4733 LY_ERR rc;
4734
4735 if (options & LYXP_SCNODE_ALL) {
4736 set_scnode_clear_ctx(set);
4737 return LY_SUCCESS;
4738 }
4739
4740 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004741 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004742 LY_CHECK_RET(rc);
4743 set_fill_set(set, args[0]);
4744 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004745 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004746 LY_CHECK_RET(rc);
4747 }
4748
4749 return LY_SUCCESS;
4750}
4751
4752/**
4753 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4754 * with the length of the string in either the argument or the context.
4755 *
4756 * @param[in] args Array of arguments.
4757 * @param[in] arg_count Count of elements in @p args.
4758 * @param[in,out] set Context and result set at the same time.
4759 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004760 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004761 */
4762static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004763xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004764{
4765 struct lysc_node_leaf *sleaf;
4766 LY_ERR rc = LY_SUCCESS;
4767
4768 if (options & LYXP_SCNODE_ALL) {
4769 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4770 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4771 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 +02004772 } else if (!warn_is_string_type(sleaf->type)) {
4773 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004774 }
4775 }
4776 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4777 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4778 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 +02004779 } else if (!warn_is_string_type(sleaf->type)) {
4780 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004781 }
4782 }
4783 set_scnode_clear_ctx(set);
4784 return rc;
4785 }
4786
4787 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004788 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004789 LY_CHECK_RET(rc);
4790 set_fill_number(set, strlen(args[0]->val.str));
4791 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004792 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 LY_CHECK_RET(rc);
4794 set_fill_number(set, strlen(set->val.str));
4795 }
4796
4797 return LY_SUCCESS;
4798}
4799
4800/**
4801 * @brief Execute the XPath substring(string, number, number?) function.
4802 * Returns LYXP_SET_STRING substring of the first argument starting
4803 * on the second argument index ending on the third argument index,
4804 * indexed from 1. For exact definition refer to
4805 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4806 *
4807 * @param[in] args Array of arguments.
4808 * @param[in] arg_count Count of elements in @p args.
4809 * @param[in,out] set Context and result set at the same time.
4810 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004811 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812 */
4813static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004814xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004816 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004817 uint16_t str_start, str_len, pos;
4818 struct lysc_node_leaf *sleaf;
4819 LY_ERR rc = LY_SUCCESS;
4820
4821 if (options & LYXP_SCNODE_ALL) {
4822 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4823 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4824 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 +02004825 } else if (!warn_is_string_type(sleaf->type)) {
4826 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004827 }
4828 }
4829
4830 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4831 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4832 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 +02004833 } else if (!warn_is_numeric_type(sleaf->type)) {
4834 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004835 }
4836 }
4837
Michal Vasko69730152020-10-09 16:30:07 +02004838 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4839 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004840 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4841 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 +02004842 } else if (!warn_is_numeric_type(sleaf->type)) {
4843 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004844 }
4845 }
4846 set_scnode_clear_ctx(set);
4847 return rc;
4848 }
4849
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004850 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 LY_CHECK_RET(rc);
4852
4853 /* start */
4854 if (xpath_round(&args[1], 1, args[1], options)) {
4855 return -1;
4856 }
4857 if (isfinite(args[1]->val.num)) {
4858 start = args[1]->val.num - 1;
4859 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004860 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004862 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004863 }
4864
4865 /* len */
4866 if (arg_count == 3) {
4867 rc = xpath_round(&args[2], 1, args[2], options);
4868 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004869 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004871 } else if (isfinite(args[2]->val.num)) {
4872 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004873 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004874 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004875 }
4876 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004877 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004878 }
4879
4880 /* find matching character positions */
4881 str_start = 0;
4882 str_len = 0;
4883 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4884 if (pos < start) {
4885 ++str_start;
4886 } else if (pos < start + len) {
4887 ++str_len;
4888 } else {
4889 break;
4890 }
4891 }
4892
4893 set_fill_string(set, args[0]->val.str + str_start, str_len);
4894 return LY_SUCCESS;
4895}
4896
4897/**
4898 * @brief Execute the XPath substring-after(string, string) function.
4899 * Returns LYXP_SET_STRING with the string succeeding the occurance
4900 * of the second argument in the first or an empty string.
4901 *
4902 * @param[in] args Array of arguments.
4903 * @param[in] arg_count Count of elements in @p args.
4904 * @param[in,out] set Context and result set at the same time.
4905 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004906 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004907 */
4908static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004909xpath_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 +02004910{
4911 char *ptr;
4912 struct lysc_node_leaf *sleaf;
4913 LY_ERR rc = LY_SUCCESS;
4914
4915 if (options & LYXP_SCNODE_ALL) {
4916 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4917 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4918 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 +02004919 } else if (!warn_is_string_type(sleaf->type)) {
4920 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 }
4922 }
4923
4924 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4925 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4926 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 +02004927 } else if (!warn_is_string_type(sleaf->type)) {
4928 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004929 }
4930 }
4931 set_scnode_clear_ctx(set);
4932 return rc;
4933 }
4934
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004935 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004936 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004937 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 LY_CHECK_RET(rc);
4939
4940 ptr = strstr(args[0]->val.str, args[1]->val.str);
4941 if (ptr) {
4942 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4943 } else {
4944 set_fill_string(set, "", 0);
4945 }
4946
4947 return LY_SUCCESS;
4948}
4949
4950/**
4951 * @brief Execute the XPath substring-before(string, string) function.
4952 * Returns LYXP_SET_STRING with the string preceding the occurance
4953 * of the second argument in the first or an empty string.
4954 *
4955 * @param[in] args Array of arguments.
4956 * @param[in] arg_count Count of elements in @p args.
4957 * @param[in,out] set Context and result set at the same time.
4958 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004959 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004960 */
4961static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004962xpath_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 +02004963{
4964 char *ptr;
4965 struct lysc_node_leaf *sleaf;
4966 LY_ERR rc = LY_SUCCESS;
4967
4968 if (options & LYXP_SCNODE_ALL) {
4969 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4970 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4971 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 +02004972 } else if (!warn_is_string_type(sleaf->type)) {
4973 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004974 }
4975 }
4976
4977 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4978 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4979 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 +02004980 } else if (!warn_is_string_type(sleaf->type)) {
4981 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004982 }
4983 }
4984 set_scnode_clear_ctx(set);
4985 return rc;
4986 }
4987
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004988 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004990 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004991 LY_CHECK_RET(rc);
4992
4993 ptr = strstr(args[0]->val.str, args[1]->val.str);
4994 if (ptr) {
4995 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4996 } else {
4997 set_fill_string(set, "", 0);
4998 }
4999
5000 return LY_SUCCESS;
5001}
5002
5003/**
5004 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5005 * with the sum of all the nodes in the context.
5006 *
5007 * @param[in] args Array of arguments.
5008 * @param[in] arg_count Count of elements in @p args.
5009 * @param[in,out] set Context and result set at the same time.
5010 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005011 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005012 */
5013static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005014xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015{
5016 long double num;
5017 char *str;
5018 uint16_t i;
5019 struct lyxp_set set_item;
5020 struct lysc_node_leaf *sleaf;
5021 LY_ERR rc = LY_SUCCESS;
5022
5023 if (options & LYXP_SCNODE_ALL) {
5024 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5025 for (i = 0; i < args[0]->used; ++i) {
5026 if (args[0]->val.scnodes[i].in_ctx == 1) {
5027 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5028 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5029 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005030 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005031 } else if (!warn_is_numeric_type(sleaf->type)) {
5032 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033 }
5034 }
5035 }
5036 }
5037 set_scnode_clear_ctx(set);
5038 return rc;
5039 }
5040
5041 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005042
5043 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005044 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005045 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005046 } else if (!args[0]->used) {
5047 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005048 }
5049
Michal Vasko5c4e5892019-11-14 12:31:38 +01005050 set_init(&set_item, set);
5051
Michal Vasko03ff5a72019-09-11 13:49:33 +02005052 set_item.type = LYXP_SET_NODE_SET;
5053 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5054 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5055
5056 set_item.used = 1;
5057 set_item.size = 1;
5058
5059 for (i = 0; i < args[0]->used; ++i) {
5060 set_item.val.nodes[0] = args[0]->val.nodes[i];
5061
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005062 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005063 LY_CHECK_RET(rc);
5064 num = cast_string_to_number(str);
5065 free(str);
5066 set->val.num += num;
5067 }
5068
5069 free(set_item.val.nodes);
5070
5071 return LY_SUCCESS;
5072}
5073
5074/**
5075 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5076 * with the text content of the nodes in the context.
5077 *
5078 * @param[in] args Array of arguments.
5079 * @param[in] arg_count Count of elements in @p args.
5080 * @param[in,out] set Context and result set at the same time.
5081 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005082 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005083 */
5084static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005085xpath_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 +02005086{
5087 uint32_t i;
5088
5089 if (options & LYXP_SCNODE_ALL) {
5090 set_scnode_clear_ctx(set);
5091 return LY_SUCCESS;
5092 }
5093
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005095 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 return LY_EVALID;
5097 }
5098
Michal Vaskod989ba02020-08-24 10:59:24 +02005099 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005101 case LYXP_NODE_NONE:
5102 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5105 set->val.nodes[i].type = LYXP_NODE_TEXT;
5106 ++i;
5107 break;
5108 }
Radek Krejci0f969882020-08-21 16:56:47 +02005109 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 case LYXP_NODE_ROOT:
5111 case LYXP_NODE_ROOT_CONFIG:
5112 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005113 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005114 set_remove_node(set, i);
5115 break;
5116 }
5117 }
5118
5119 return LY_SUCCESS;
5120}
5121
5122/**
5123 * @brief Execute the XPath translate(string, string, string) function.
5124 * Returns LYXP_SET_STRING with the first argument with the characters
5125 * from the second argument replaced by those on the corresponding
5126 * positions in the third argument.
5127 *
5128 * @param[in] args Array of arguments.
5129 * @param[in] arg_count Count of elements in @p args.
5130 * @param[in,out] set Context and result set at the same time.
5131 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005132 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005133 */
5134static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005135xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005136{
5137 uint16_t i, j, new_used;
5138 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005139 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 struct lysc_node_leaf *sleaf;
5141 LY_ERR rc = LY_SUCCESS;
5142
5143 if (options & LYXP_SCNODE_ALL) {
5144 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5145 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5146 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 +02005147 } else if (!warn_is_string_type(sleaf->type)) {
5148 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005149 }
5150 }
5151
5152 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5153 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5154 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 +02005155 } else if (!warn_is_string_type(sleaf->type)) {
5156 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005157 }
5158 }
5159
5160 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5161 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5162 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 +02005163 } else if (!warn_is_string_type(sleaf->type)) {
5164 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165 }
5166 }
5167 set_scnode_clear_ctx(set);
5168 return rc;
5169 }
5170
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005171 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005173 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005174 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005175 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005176 LY_CHECK_RET(rc);
5177
5178 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5179 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5180 new_used = 0;
5181
5182 have_removed = 0;
5183 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005184 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005185
5186 for (j = 0; args[1]->val.str[j]; ++j) {
5187 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5188 /* removing this char */
5189 if (j >= strlen(args[2]->val.str)) {
5190 have_removed = 1;
5191 found = 1;
5192 break;
5193 }
5194 /* replacing this char */
5195 new[new_used] = args[2]->val.str[j];
5196 ++new_used;
5197 found = 1;
5198 break;
5199 }
5200 }
5201
5202 /* copying this char */
5203 if (!found) {
5204 new[new_used] = args[0]->val.str[i];
5205 ++new_used;
5206 }
5207 }
5208
5209 if (have_removed) {
5210 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5211 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5212 }
5213 new[new_used] = '\0';
5214
Michal Vaskod3678892020-05-21 10:06:58 +02005215 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005216 set->type = LYXP_SET_STRING;
5217 set->val.str = new;
5218
5219 return LY_SUCCESS;
5220}
5221
5222/**
5223 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5224 * with true value.
5225 *
5226 * @param[in] args Array of arguments.
5227 * @param[in] arg_count Count of elements in @p args.
5228 * @param[in,out] set Context and result set at the same time.
5229 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005230 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005231 */
5232static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005233xpath_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 +02005234{
5235 if (options & LYXP_SCNODE_ALL) {
5236 set_scnode_clear_ctx(set);
5237 return LY_SUCCESS;
5238 }
5239
5240 set_fill_boolean(set, 1);
5241 return LY_SUCCESS;
5242}
5243
5244/*
5245 * moveto functions
5246 *
5247 * They and only they actually change the context (set).
5248 */
5249
5250/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005251 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005252 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005253 * XPath @p set is expected to be a (sc)node set!
5254 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005255 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5256 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005257 * @param[in] set Set with general XPath context.
5258 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005259 * @param[out] moveto_mod Expected module of a matching node.
5260 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005261 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005262static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005263moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5264 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005266 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005267 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005268 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005269
Michal Vasko2104e9f2020-03-06 08:23:25 +01005270 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5271
Michal Vasko6346ece2019-09-24 13:12:53 +02005272 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5273 /* specific module */
5274 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005275 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005276
Michal Vasko004d3152020-06-11 19:59:22 +02005277 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005278 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005279 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005280 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005281 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005282 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005283 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005284 return LY_EVALID;
5285 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005286
Michal Vasko6346ece2019-09-24 13:12:53 +02005287 *qname += pref_len + 1;
5288 *qname_len -= pref_len + 1;
5289 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5290 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005291 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005292 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005293 switch (set->format) {
5294 case LY_PREF_SCHEMA:
5295 case LY_PREF_SCHEMA_RESOLVED:
5296 /* current module */
5297 mod = set->cur_mod;
5298 break;
5299 case LY_PREF_JSON:
5300 /* inherit parent (context node) module */
5301 if (ctx_scnode) {
5302 mod = ctx_scnode->module;
5303 } else {
5304 mod = NULL;
5305 }
5306 break;
5307 case LY_PREF_XML:
5308 /* not defined */
5309 LOGINT_RET(set->ctx);
5310 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005311 }
5312
Michal Vasko6346ece2019-09-24 13:12:53 +02005313 *moveto_mod = mod;
5314 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315}
5316
5317/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005318 * @brief Move context @p set to the root. Handles absolute path.
5319 * Result is LYXP_SET_NODE_SET.
5320 *
5321 * @param[in,out] set Set to use.
5322 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005323 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005325static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005326moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005328 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005329 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 }
5331
5332 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005334 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005335 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005336 set->type = LYXP_SET_NODE_SET;
5337 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005338 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005340
5341 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342}
5343
5344/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005345 * @brief Check whether a node has some unresolved "when".
5346 *
5347 * @param[in] node Node to check.
5348 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5349 */
5350static LY_ERR
5351moveto_when_check(const struct lyd_node *node)
5352{
5353 const struct lysc_node *schema;
5354
5355 if (!node) {
5356 return LY_SUCCESS;
5357 }
5358
5359 schema = node->schema;
5360 do {
5361 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5362 return LY_EINCOMPLETE;
5363 }
5364 schema = schema->parent;
5365 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5366
5367 return LY_SUCCESS;
5368}
5369
5370/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005371 * @brief Check @p node as a part of NameTest processing.
5372 *
5373 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005374 * @param[in] ctx_node Context node.
5375 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005376 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005377 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005378 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5379 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005380 */
5381static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005382moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Radek Krejci0f969882020-08-21 16:56:47 +02005383 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005384{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005385 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5386 switch (set->format) {
5387 case LY_PREF_SCHEMA:
5388 case LY_PREF_SCHEMA_RESOLVED:
5389 /* use current module */
5390 moveto_mod = set->cur_mod;
5391 break;
5392 case LY_PREF_JSON:
5393 /* inherit module of the context node, if any */
5394 if (ctx_node) {
5395 moveto_mod = ctx_node->schema->module;
5396 }
5397 break;
5398 case LY_PREF_XML:
5399 /* not defined */
5400 LOGINT(set->ctx);
5401 return LY_EINVAL;
5402 }
5403 }
5404
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405 /* module check */
5406 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005407 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408 }
5409
Michal Vasko5c4e5892019-11-14 12:31:38 +01005410 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005411 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005413 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5414 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005415 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005416 }
5417
5418 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005419 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005420 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005421 }
5422
Michal Vaskoa1424542019-11-14 16:08:52 +01005423 /* when check */
5424 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005425 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005426 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005427
5428 /* match */
5429 return LY_SUCCESS;
5430}
5431
5432/**
5433 * @brief Check @p node as a part of schema NameTest processing.
5434 *
5435 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005436 * @param[in] ctx_scnode Context node.
5437 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005438 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005439 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005440 * @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 +02005441 */
5442static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005443moveto_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 +02005444 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005445{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005446 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5447 switch (set->format) {
5448 case LY_PREF_SCHEMA:
5449 case LY_PREF_SCHEMA_RESOLVED:
5450 /* use current module */
5451 moveto_mod = set->cur_mod;
5452 break;
5453 case LY_PREF_JSON:
5454 /* inherit module of the context node, if any */
5455 if (ctx_scnode) {
5456 moveto_mod = ctx_scnode->module;
5457 }
5458 break;
5459 case LY_PREF_XML:
5460 /* not defined */
5461 LOGINT(set->ctx);
5462 return LY_EINVAL;
5463 }
5464 }
5465
Michal Vasko03ff5a72019-09-11 13:49:33 +02005466 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005467 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005468 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005469 }
5470
5471 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005472 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005474 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005475 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005476 }
5477
5478 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005479 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005480 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481 }
5482
5483 /* match */
5484 return LY_SUCCESS;
5485}
5486
5487/**
Michal Vaskod3678892020-05-21 10:06:58 +02005488 * @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 +02005489 *
5490 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005491 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005492 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005493 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5494 */
5495static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005496moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005497{
Michal Vaskof03ed032020-03-04 13:31:44 +01005498 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005499 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005500 LY_ERR rc;
5501
Michal Vaskod3678892020-05-21 10:06:58 +02005502 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005503 return LY_SUCCESS;
5504 }
5505
5506 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005507 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005508 return LY_EVALID;
5509 }
5510
Michal Vasko03ff5a72019-09-11 13:49:33 +02005511 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005512 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005513
5514 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 +01005515 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005516
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005517 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005518 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005519 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005520 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005521 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005522 ctx_node = set->val.nodes[i].node;
5523 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005524 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005525
Michal Vaskod3678892020-05-21 10:06:58 +02005526 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005527 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005528 if (rc == LY_SUCCESS) {
5529 if (!replaced) {
5530 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5531 replaced = 1;
5532 } else {
5533 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005534 }
Michal Vaskod3678892020-05-21 10:06:58 +02005535 ++i;
5536 } else if (rc == LY_EINCOMPLETE) {
5537 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005538 }
5539 }
5540
5541 if (!replaced) {
5542 /* no match */
5543 set_remove_node(set, i);
5544 }
5545 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005546
5547 return LY_SUCCESS;
5548}
5549
5550/**
Michal Vaskod3678892020-05-21 10:06:58 +02005551 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5552 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005553 *
5554 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005555 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005556 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskod3678892020-05-21 10:06:58 +02005557 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5558 */
5559static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005560moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005561{
Michal Vasko004d3152020-06-11 19:59:22 +02005562 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005563 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005564 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005565 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005566
Michal Vasko004d3152020-06-11 19:59:22 +02005567 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005568
5569 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005570 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005571 }
5572
5573 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005574 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005575 ret = LY_EVALID;
5576 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005577 }
5578
5579 /* context check for all the nodes since we have the schema node */
5580 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5581 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005582 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005583 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5584 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005585 lyxp_set_free_content(set);
5586 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005587 }
5588
5589 /* create specific data instance if needed */
5590 if (scnode->nodetype == LYS_LIST) {
5591 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5592 } else if (scnode->nodetype == LYS_LEAFLIST) {
5593 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005594 }
5595
5596 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005597 siblings = NULL;
5598
5599 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5600 assert(!set->val.nodes[i].node);
5601
5602 /* search in all the trees */
5603 siblings = set->tree;
5604 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5605 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005606 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005607 }
5608
5609 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005610 if (inst) {
5611 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005612 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005613 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005614 }
5615
5616 /* when check */
5617 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005618 ret = LY_EINCOMPLETE;
5619 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005620 }
5621
5622 if (sub) {
5623 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005624 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005625 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005626 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005627 /* no match */
5628 set_remove_node(set, i);
5629 }
5630 }
5631
Michal Vasko004d3152020-06-11 19:59:22 +02005632cleanup:
5633 lyd_free_tree(inst);
5634 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005635}
5636
5637/**
5638 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5639 *
5640 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005641 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005642 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005643 * @param[in] options XPath options.
5644 * @return LY_ERR
5645 */
5646static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005647moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005648{
Radek Krejci857189e2020-09-01 13:26:36 +02005649 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005650 uint32_t getnext_opts;
5651 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005652 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005653 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005654 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005655
Michal Vaskod3678892020-05-21 10:06:58 +02005656 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005657 return LY_SUCCESS;
5658 }
5659
5660 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005661 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005662 return LY_EVALID;
5663 }
5664
Michal Vaskocafad9d2019-11-07 15:20:03 +01005665 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005666 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005667 if (options & LYXP_SCNODE_OUTPUT) {
5668 getnext_opts |= LYS_GETNEXT_OUTPUT;
5669 }
5670
Michal Vasko03ff5a72019-09-11 13:49:33 +02005671 orig_used = set->used;
5672 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005673 uint32_t idx;
5674
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005676 if (set->val.scnodes[i].in_ctx != -2) {
5677 continue;
5678 }
5679
5680 /* remember context node */
5681 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005682 } else {
5683 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005684 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685
5686 start_parent = set->val.scnodes[i].scnode;
5687
5688 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 +02005689 /* 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 +02005690 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005691 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005692 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005693 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005694 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005695 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005696 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005697 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5698
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005700 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 set->val.scnodes[idx].in_ctx = 2;
5702 temp_ctx = 1;
5703 }
5704 }
5705 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706 }
5707
Michal Vasko519fd602020-05-26 12:17:39 +02005708 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5709 iter = NULL;
5710 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005711 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005712 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5713
5714 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715 set->val.scnodes[idx].in_ctx = 2;
5716 temp_ctx = 1;
5717 }
5718 }
5719 }
5720 }
5721 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005722
5723 /* correct temporary in_ctx values */
5724 if (temp_ctx) {
5725 for (i = 0; i < orig_used; ++i) {
5726 if (set->val.scnodes[i].in_ctx == 2) {
5727 set->val.scnodes[i].in_ctx = 1;
5728 }
5729 }
5730 }
5731
5732 return LY_SUCCESS;
5733}
5734
5735/**
Michal Vaskod3678892020-05-21 10:06:58 +02005736 * @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 +02005737 * Context position aware.
5738 *
5739 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005740 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005741 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005742 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5743 */
5744static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005745moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746{
5747 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005748 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 struct lyxp_set ret_set;
5750 LY_ERR rc;
5751
Michal Vaskod3678892020-05-21 10:06:58 +02005752 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005753 return LY_SUCCESS;
5754 }
5755
5756 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005757 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 return LY_EVALID;
5759 }
5760
Michal Vasko9f96a052020-03-10 09:41:45 +01005761 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskod3678892020-05-21 10:06:58 +02005762 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005763 LY_CHECK_RET(rc);
5764
Michal Vasko6346ece2019-09-24 13:12:53 +02005765 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766 set_init(&ret_set, set);
5767 for (i = 0; i < set->used; ++i) {
5768
5769 /* TREE DFS */
5770 start = set->val.nodes[i].node;
5771 for (elem = next = start; elem; elem = next) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005772 rc = moveto_node_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005773 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005774 /* add matching node into result set */
5775 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5776 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5777 /* the node is a duplicate, we'll process it later in the set */
5778 goto skip_children;
5779 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005780 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005781 return rc;
5782 } else if (rc == LY_EINVAL) {
5783 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 }
5785
5786 /* TREE DFS NEXT ELEM */
5787 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005788 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789 if (!next) {
5790skip_children:
5791 /* no children, so try siblings, but only if it's not the start,
5792 * that is considered to be the root and it's siblings are not traversed */
5793 if (elem != start) {
5794 next = elem->next;
5795 } else {
5796 break;
5797 }
5798 }
5799 while (!next) {
5800 /* no siblings, go back through the parents */
5801 if ((struct lyd_node *)elem->parent == start) {
5802 /* we are done, no next element to process */
5803 break;
5804 }
5805 /* parent is already processed, go to its sibling */
5806 elem = (struct lyd_node *)elem->parent;
5807 next = elem->next;
5808 }
5809 }
5810 }
5811
5812 /* make the temporary set the current one */
5813 ret_set.ctx_pos = set->ctx_pos;
5814 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005815 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005816 memcpy(set, &ret_set, sizeof *set);
5817
5818 return LY_SUCCESS;
5819}
5820
5821/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005822 * @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 +02005823 *
5824 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005825 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005826 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005827 * @param[in] options XPath options.
5828 * @return LY_ERR
5829 */
5830static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005831moveto_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 +02005832{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005833 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005834 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005835 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005836
Michal Vaskod3678892020-05-21 10:06:58 +02005837 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 return LY_SUCCESS;
5839 }
5840
5841 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005842 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005843 return LY_EVALID;
5844 }
5845
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846 orig_used = set->used;
5847 for (i = 0; i < orig_used; ++i) {
5848 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005849 if (set->val.scnodes[i].in_ctx != -2) {
5850 continue;
5851 }
5852
5853 /* remember context node */
5854 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005855 } else {
5856 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005857 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858
5859 /* TREE DFS */
5860 start = set->val.scnodes[i].scnode;
5861 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005862 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5863 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 }
5866
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005867 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005868 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005869 uint32_t idx;
5870
5871 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005872 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005873 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874 /* we will process it later in the set */
5875 goto skip_children;
5876 }
5877 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005878 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005880 } else if (rc == LY_EINVAL) {
5881 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005882 }
5883
5884next_iter:
5885 /* TREE DFS NEXT ELEM */
5886 /* select element for the next run - children first */
5887 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005888 if (!next) {
5889skip_children:
5890 /* no children, so try siblings, but only if it's not the start,
5891 * that is considered to be the root and it's siblings are not traversed */
5892 if (elem != start) {
5893 next = elem->next;
5894 } else {
5895 break;
5896 }
5897 }
5898 while (!next) {
5899 /* no siblings, go back through the parents */
5900 if (elem->parent == start) {
5901 /* we are done, no next element to process */
5902 break;
5903 }
5904 /* parent is already processed, go to its sibling */
5905 elem = elem->parent;
5906 next = elem->next;
5907 }
5908 }
5909 }
5910
5911 return LY_SUCCESS;
5912}
5913
5914/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005915 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916 * Indirectly context position aware.
5917 *
5918 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005919 * @param[in] mod Matching metadata module, NULL for any.
5920 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921 * @return LY_ERR
5922 */
5923static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005924moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925{
Michal Vasko9f96a052020-03-10 09:41:45 +01005926 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927
Michal Vaskod3678892020-05-21 10:06:58 +02005928 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929 return LY_SUCCESS;
5930 }
5931
5932 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005933 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005934 return LY_EVALID;
5935 }
5936
Radek Krejci1deb5be2020-08-26 16:43:36 +02005937 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005938 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005939
5940 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5941 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005942 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005943 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944
5945 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005946 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005947 continue;
5948 }
5949
Michal Vaskod3678892020-05-21 10:06:58 +02005950 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005951 /* match */
5952 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005953 set->val.meta[i].meta = sub;
5954 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005955 /* pos does not change */
5956 replaced = 1;
5957 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005958 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 +02005959 }
5960 ++i;
5961 }
5962 }
5963 }
5964
5965 if (!replaced) {
5966 /* no match */
5967 set_remove_node(set, i);
5968 }
5969 }
5970
5971 return LY_SUCCESS;
5972}
5973
5974/**
5975 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005976 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977 *
5978 * @param[in,out] set1 Set to use for the result.
5979 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005980 * @return LY_ERR
5981 */
5982static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005983moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984{
5985 LY_ERR rc;
5986
Michal Vaskod3678892020-05-21 10:06:58 +02005987 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005988 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->cur_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005989 return LY_EVALID;
5990 }
5991
5992 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005993 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994 return LY_SUCCESS;
5995 }
5996
Michal Vaskod3678892020-05-21 10:06:58 +02005997 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 memcpy(set1, set2, sizeof *set1);
5999 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006000 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006001 return LY_SUCCESS;
6002 }
6003
6004 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006005 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006006
6007 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006008 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006009 LY_CHECK_RET(rc);
6010
6011 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006012 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006013
6014 return LY_SUCCESS;
6015}
6016
6017/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006018 * @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 +02006019 * Context position aware.
6020 *
6021 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006022 * @param[in] mod Matching metadata module, NULL for any.
6023 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6025 */
6026static int
Michal Vaskod3678892020-05-21 10:06:58 +02006027moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028{
Michal Vasko9f96a052020-03-10 09:41:45 +01006029 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030 struct lyxp_set *set_all_desc = NULL;
6031 LY_ERR rc;
6032
Michal Vaskod3678892020-05-21 10:06:58 +02006033 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034 return LY_SUCCESS;
6035 }
6036
6037 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006038 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006039 return LY_EVALID;
6040 }
6041
Michal Vasko03ff5a72019-09-11 13:49:33 +02006042 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6043 * but it likely won't be used much, so it's a waste of time */
6044 /* copy the context */
6045 set_all_desc = set_copy(set);
6046 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02006047 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 if (rc != LY_SUCCESS) {
6049 lyxp_set_free(set_all_desc);
6050 return rc;
6051 }
6052 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006053 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006054 if (rc != LY_SUCCESS) {
6055 lyxp_set_free(set_all_desc);
6056 return rc;
6057 }
6058 lyxp_set_free(set_all_desc);
6059
Radek Krejci1deb5be2020-08-26 16:43:36 +02006060 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006061 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006062
6063 /* only attributes of an elem can be in the result, skip all the rest,
6064 * we have all attributes qualified in lyd tree */
6065 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006066 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006068 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069 continue;
6070 }
6071
Michal Vaskod3678892020-05-21 10:06:58 +02006072 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006073 /* match */
6074 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006075 set->val.meta[i].meta = sub;
6076 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006077 /* pos does not change */
6078 replaced = 1;
6079 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006080 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 +02006081 }
6082 ++i;
6083 }
6084 }
6085 }
6086
6087 if (!replaced) {
6088 /* no match */
6089 set_remove_node(set, i);
6090 }
6091 }
6092
6093 return LY_SUCCESS;
6094}
6095
6096/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006097 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6098 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099 *
6100 * @param[in] parent Current parent.
6101 * @param[in] parent_pos Position of @p parent.
6102 * @param[in] parent_type Node type of @p parent.
6103 * @param[in,out] to_set Set to use.
6104 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 * @param[in] options XPath options.
6106 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6107 */
6108static LY_ERR
6109moveto_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 +02006110 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006111{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006112 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006113 LY_ERR rc;
6114
6115 switch (parent_type) {
6116 case LYXP_NODE_ROOT:
6117 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006118 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006119
Michal Vasko61ac2f62020-05-25 12:39:51 +02006120 /* add all top-level nodes as elements */
6121 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006122 break;
6123 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006124 /* add just the text node of this term element node */
6125 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006126 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6127 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6128 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006129 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006130 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006131
6132 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006133 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006134 break;
6135 default:
6136 LOGINT_RET(parent->schema->module->ctx);
6137 }
6138
Michal Vasko61ac2f62020-05-25 12:39:51 +02006139 /* add all top-level nodes as elements */
6140 LY_LIST_FOR(first, iter) {
6141 /* context check */
6142 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6143 continue;
6144 }
6145
6146 /* when check */
6147 if (moveto_when_check(iter)) {
6148 return LY_EINCOMPLETE;
6149 }
6150
6151 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6152 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6153
6154 /* also add all the children of this node, recursively */
6155 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6156 LY_CHECK_RET(rc);
6157 }
6158 }
6159
Michal Vasko03ff5a72019-09-11 13:49:33 +02006160 return LY_SUCCESS;
6161}
6162
6163/**
6164 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6165 * (or LYXP_SET_EMPTY). Context position aware.
6166 *
6167 * @param[in,out] set Set to use.
6168 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6169 * @param[in] options XPath options.
6170 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6171 */
6172static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006173moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006175 struct lyxp_set ret_set;
6176 LY_ERR rc;
6177
Michal Vaskod3678892020-05-21 10:06:58 +02006178 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006179 return LY_SUCCESS;
6180 }
6181
6182 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006183 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006184 return LY_EVALID;
6185 }
6186
6187 /* nothing to do */
6188 if (!all_desc) {
6189 return LY_SUCCESS;
6190 }
6191
Michal Vasko03ff5a72019-09-11 13:49:33 +02006192 /* add all the children, they get added recursively */
6193 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006194 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006195 /* copy the current node to tmp */
6196 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6197
6198 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006199 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006200 continue;
6201 }
6202
Michal Vasko03ff5a72019-09-11 13:49:33 +02006203 /* add all the children */
6204 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 +02006205 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006206 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006207 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006208 return rc;
6209 }
6210 }
6211
6212 /* use the temporary set as the current one */
6213 ret_set.ctx_pos = set->ctx_pos;
6214 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006215 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006216 memcpy(set, &ret_set, sizeof *set);
6217
6218 return LY_SUCCESS;
6219}
6220
6221/**
6222 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6223 * (or LYXP_SET_EMPTY).
6224 *
6225 * @param[in,out] set Set to use.
6226 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6227 * @param[in] options XPath options.
6228 * @return LY_ERR
6229 */
6230static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006231moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006232{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006233 uint32_t getnext_opts;
6234 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006235 const struct lysc_node *iter, *start_parent;
6236 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006237
Michal Vaskod3678892020-05-21 10:06:58 +02006238 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006239 return LY_SUCCESS;
6240 }
6241
6242 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006243 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244 return LY_EVALID;
6245 }
6246
6247 /* nothing to do */
6248 if (!all_desc) {
6249 return LY_SUCCESS;
6250 }
6251
Michal Vasko519fd602020-05-26 12:17:39 +02006252 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006253 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006254 if (options & LYXP_SCNODE_OUTPUT) {
6255 getnext_opts |= LYS_GETNEXT_OUTPUT;
6256 }
6257
6258 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006259 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006260 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006261 if (set->val.scnodes[i].in_ctx != -2) {
6262 continue;
6263 }
6264
Michal Vasko519fd602020-05-26 12:17:39 +02006265 /* remember context node */
6266 set->val.scnodes[i].in_ctx = -1;
6267 } else {
6268 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006269 }
6270
Michal Vasko519fd602020-05-26 12:17:39 +02006271 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006272
Michal Vasko519fd602020-05-26 12:17:39 +02006273 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6274 /* it can actually be in any module, it's all <running> */
6275 mod_idx = 0;
6276 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6277 iter = NULL;
6278 /* module may not be implemented */
6279 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6280 /* context check */
6281 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6282 continue;
6283 }
6284
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006285 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006286 /* throw away the insert index, we want to consider that node again, recursively */
6287 }
6288 }
6289
6290 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6291 iter = NULL;
6292 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006293 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006294 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006295 continue;
6296 }
6297
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006298 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006299 }
6300 }
6301 }
6302
6303 return LY_SUCCESS;
6304}
6305
6306/**
6307 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6308 * (or LYXP_SET_EMPTY). Context position aware.
6309 *
6310 * @param[in] set Set to use.
6311 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6312 * @param[in] options XPath options.
6313 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6314 */
6315static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006316moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006317{
6318 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006319 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006320 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006321
Michal Vaskod3678892020-05-21 10:06:58 +02006322 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006323 return LY_SUCCESS;
6324 }
6325
6326 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006327 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006328 return LY_EVALID;
6329 }
6330
6331 if (all_desc) {
6332 /* <path>//.. == <path>//./.. */
6333 rc = moveto_self(set, 1, options);
6334 LY_CHECK_RET(rc);
6335 }
6336
Radek Krejci1deb5be2020-08-26 16:43:36 +02006337 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006338 node = set->val.nodes[i].node;
6339
6340 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6341 new_node = (struct lyd_node *)node->parent;
6342 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6343 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006344 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6345 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346 if (!new_node) {
6347 LOGINT_RET(set->ctx);
6348 }
6349 } else {
6350 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006351 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006352 continue;
6353 }
6354
Michal Vaskoa1424542019-11-14 16:08:52 +01006355 /* when check */
6356 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006357 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006358 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006359
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006360 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006361 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006362 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006365 /* 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 +02006366 new_type = LYXP_NODE_ELEM;
6367 }
6368
Michal Vasko03ff5a72019-09-11 13:49:33 +02006369 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006370 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006371 } else {
6372 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006373 }
6374 }
6375
Michal Vasko2caefc12019-11-14 16:07:56 +01006376 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006377 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006378
6379 return LY_SUCCESS;
6380}
6381
6382/**
6383 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6384 * (or LYXP_SET_EMPTY).
6385 *
6386 * @param[in] set Set to use.
6387 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6388 * @param[in] options XPath options.
6389 * @return LY_ERR
6390 */
6391static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006392moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006394 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006395 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006396 const struct lysc_node *node, *new_node;
6397 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006398
Michal Vaskod3678892020-05-21 10:06:58 +02006399 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006400 return LY_SUCCESS;
6401 }
6402
6403 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006404 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405 return LY_EVALID;
6406 }
6407
6408 if (all_desc) {
6409 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006410 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006411 }
6412
Michal Vasko03ff5a72019-09-11 13:49:33 +02006413 orig_used = set->used;
6414 for (i = 0; i < orig_used; ++i) {
6415 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006416 if (set->val.scnodes[i].in_ctx != -2) {
6417 continue;
6418 }
6419
6420 /* remember context node */
6421 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006422 } else {
6423 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006425
6426 node = set->val.scnodes[i].scnode;
6427
6428 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006429 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006430 } else {
6431 /* root does not have a parent */
6432 continue;
6433 }
6434
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006435 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006436 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006437 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438
Michal Vasko03ff5a72019-09-11 13:49:33 +02006439 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006440 /* 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 +02006441 new_type = LYXP_NODE_ELEM;
6442 }
6443
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006444 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6445 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006446 set->val.scnodes[idx].in_ctx = 2;
6447 temp_ctx = 1;
6448 }
6449 }
6450
6451 if (temp_ctx) {
6452 for (i = 0; i < orig_used; ++i) {
6453 if (set->val.scnodes[i].in_ctx == 2) {
6454 set->val.scnodes[i].in_ctx = 1;
6455 }
6456 }
6457 }
6458
6459 return LY_SUCCESS;
6460}
6461
6462/**
6463 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6464 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6465 *
6466 * @param[in,out] set1 Set to use for the result.
6467 * @param[in] set2 Set acting as the second operand for @p op.
6468 * @param[in] op Comparison operator to process.
6469 * @param[in] options XPath options.
6470 * @return LY_ERR
6471 */
6472static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006473moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006474{
6475 /*
6476 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6477 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6478 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6479 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6480 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6481 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6482 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6483 *
6484 * '=' or '!='
6485 * BOOLEAN + BOOLEAN
6486 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6487 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6488 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6489 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6490 * NUMBER + NUMBER
6491 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6492 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6493 * STRING + STRING
6494 *
6495 * '<=', '<', '>=', '>'
6496 * NUMBER + NUMBER
6497 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6498 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6499 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6500 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6501 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6502 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6503 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6504 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6505 */
6506 struct lyxp_set iter1, iter2;
6507 int result;
6508 int64_t i;
6509 LY_ERR rc;
6510
Michal Vasko004d3152020-06-11 19:59:22 +02006511 memset(&iter1, 0, sizeof iter1);
6512 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006513
6514 /* iterative evaluation with node-sets */
6515 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6516 if (set1->type == LYXP_SET_NODE_SET) {
6517 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006518 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006519 switch (set2->type) {
6520 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006521 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006522 break;
6523 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006524 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006525 break;
6526 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006527 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006528 break;
6529 }
6530 LY_CHECK_RET(rc);
6531
Michal Vasko4c7763f2020-07-27 17:40:37 +02006532 /* canonize set2 */
6533 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6534
6535 /* compare recursively */
6536 rc = moveto_op_comp(&iter1, &iter2, op, options);
6537 lyxp_set_free_content(&iter2);
6538 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006539
6540 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006541 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006542 set_fill_boolean(set1, 1);
6543 return LY_SUCCESS;
6544 }
6545 }
6546 } else {
6547 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006548 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006549 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006550 case LYXP_SET_NUMBER:
6551 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6552 break;
6553 case LYXP_SET_BOOLEAN:
6554 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6555 break;
6556 default:
6557 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6558 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006559 }
6560 LY_CHECK_RET(rc);
6561
Michal Vasko4c7763f2020-07-27 17:40:37 +02006562 /* canonize set1 */
6563 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 +02006564
Michal Vasko4c7763f2020-07-27 17:40:37 +02006565 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006567 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006568 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006569
6570 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006571 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006572 set_fill_boolean(set1, 1);
6573 return LY_SUCCESS;
6574 }
6575 }
6576 }
6577
6578 /* false for all nodes */
6579 set_fill_boolean(set1, 0);
6580 return LY_SUCCESS;
6581 }
6582
6583 /* first convert properly */
6584 if ((op[0] == '=') || (op[0] == '!')) {
6585 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006586 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6587 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006588 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006589 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006591 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592 LY_CHECK_RET(rc);
6593 } /* else we have 2 strings */
6594 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006595 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006596 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006597 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006598 LY_CHECK_RET(rc);
6599 }
6600
6601 assert(set1->type == set2->type);
6602
6603 /* compute result */
6604 if (op[0] == '=') {
6605 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006606 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006607 } else if (set1->type == LYXP_SET_NUMBER) {
6608 result = (set1->val.num == set2->val.num);
6609 } else {
6610 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006611 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006612 }
6613 } else if (op[0] == '!') {
6614 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006615 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006616 } else if (set1->type == LYXP_SET_NUMBER) {
6617 result = (set1->val.num != set2->val.num);
6618 } else {
6619 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006620 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006621 }
6622 } else {
6623 assert(set1->type == LYXP_SET_NUMBER);
6624 if (op[0] == '<') {
6625 if (op[1] == '=') {
6626 result = (set1->val.num <= set2->val.num);
6627 } else {
6628 result = (set1->val.num < set2->val.num);
6629 }
6630 } else {
6631 if (op[1] == '=') {
6632 result = (set1->val.num >= set2->val.num);
6633 } else {
6634 result = (set1->val.num > set2->val.num);
6635 }
6636 }
6637 }
6638
6639 /* assign result */
6640 if (result) {
6641 set_fill_boolean(set1, 1);
6642 } else {
6643 set_fill_boolean(set1, 0);
6644 }
6645
6646 return LY_SUCCESS;
6647}
6648
6649/**
6650 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6651 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6652 *
6653 * @param[in,out] set1 Set to use for the result.
6654 * @param[in] set2 Set acting as the second operand for @p op.
6655 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006656 * @return LY_ERR
6657 */
6658static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006659moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660{
6661 LY_ERR rc;
6662
6663 /* unary '-' */
6664 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006665 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006666 LY_CHECK_RET(rc);
6667 set1->val.num *= -1;
6668 lyxp_set_free(set2);
6669 return LY_SUCCESS;
6670 }
6671
6672 assert(set1 && set2);
6673
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006674 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006675 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006676 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006677 LY_CHECK_RET(rc);
6678
6679 switch (op[0]) {
6680 /* '+' */
6681 case '+':
6682 set1->val.num += set2->val.num;
6683 break;
6684
6685 /* '-' */
6686 case '-':
6687 set1->val.num -= set2->val.num;
6688 break;
6689
6690 /* '*' */
6691 case '*':
6692 set1->val.num *= set2->val.num;
6693 break;
6694
6695 /* 'div' */
6696 case 'd':
6697 set1->val.num /= set2->val.num;
6698 break;
6699
6700 /* 'mod' */
6701 case 'm':
6702 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6703 break;
6704
6705 default:
6706 LOGINT_RET(set1->ctx);
6707 }
6708
6709 return LY_SUCCESS;
6710}
6711
6712/*
6713 * eval functions
6714 *
6715 * They execute a parsed XPath expression on some data subtree.
6716 */
6717
6718/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 * @brief Evaluate Predicate. Logs directly on error.
6720 *
Michal Vaskod3678892020-05-21 10:06:58 +02006721 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006722 *
6723 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006724 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006725 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6726 * @param[in] options XPath options.
6727 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6728 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6729 */
6730static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006731eval_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 +02006732{
6733 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006734 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006735 uint32_t orig_pos, orig_size;
6736 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737 struct lyxp_set set2;
6738 struct lyd_node *orig_parent;
6739
6740 /* '[' */
6741 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006742 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006743 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006744
6745 if (!set) {
6746only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006747 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748 LY_CHECK_RET(rc);
6749 } else if (set->type == LYXP_SET_NODE_SET) {
6750 /* 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 +01006751 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006752
6753 /* empty set, nothing to evaluate */
6754 if (!set->used) {
6755 goto only_parse;
6756 }
6757
Michal Vasko004d3152020-06-11 19:59:22 +02006758 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006759 orig_pos = 0;
6760 orig_size = set->used;
6761 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006762 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006763 set_init(&set2, set);
6764 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6765 /* remember the node context position for position() and context size for last(),
6766 * predicates should always be evaluated with respect to the child axis (since we do
6767 * not support explicit axes) so we assign positions based on their parents */
6768 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6769 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6770 orig_pos = 1;
6771 } else {
6772 ++orig_pos;
6773 }
6774
6775 set2.ctx_pos = orig_pos;
6776 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006777 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006778
Michal Vasko004d3152020-06-11 19:59:22 +02006779 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006781 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006782 return rc;
6783 }
6784
6785 /* number is a position */
6786 if (set2.type == LYXP_SET_NUMBER) {
6787 if ((long long)set2.val.num == orig_pos) {
6788 set2.val.num = 1;
6789 } else {
6790 set2.val.num = 0;
6791 }
6792 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006793 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006794
6795 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006796 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006797 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006798 }
6799 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006800 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801
6802 } else if (set->type == LYXP_SET_SCNODE_SET) {
6803 for (i = 0; i < set->used; ++i) {
6804 if (set->val.scnodes[i].in_ctx == 1) {
6805 /* there is a currently-valid node */
6806 break;
6807 }
6808 }
6809 /* empty set, nothing to evaluate */
6810 if (i == set->used) {
6811 goto only_parse;
6812 }
6813
Michal Vasko004d3152020-06-11 19:59:22 +02006814 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006815
Michal Vasko03ff5a72019-09-11 13:49:33 +02006816 /* set special in_ctx to all the valid snodes */
6817 pred_in_ctx = set_scnode_new_in_ctx(set);
6818
6819 /* use the valid snodes one-by-one */
6820 for (i = 0; i < set->used; ++i) {
6821 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6822 continue;
6823 }
6824 set->val.scnodes[i].in_ctx = 1;
6825
Michal Vasko004d3152020-06-11 19:59:22 +02006826 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006827
Michal Vasko004d3152020-06-11 19:59:22 +02006828 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006829 LY_CHECK_RET(rc);
6830
6831 set->val.scnodes[i].in_ctx = pred_in_ctx;
6832 }
6833
6834 /* restore the state as it was before the predicate */
6835 for (i = 0; i < set->used; ++i) {
6836 if (set->val.scnodes[i].in_ctx == 1) {
6837 set->val.scnodes[i].in_ctx = 0;
6838 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6839 set->val.scnodes[i].in_ctx = 1;
6840 }
6841 }
6842
6843 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006844 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006845 set_fill_set(&set2, set);
6846
Michal Vasko004d3152020-06-11 19:59:22 +02006847 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006849 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006850 return rc;
6851 }
6852
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006853 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006854 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006855 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856 }
Michal Vaskod3678892020-05-21 10:06:58 +02006857 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006858 }
6859
6860 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006861 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006862 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006863 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006864 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006865
6866 return LY_SUCCESS;
6867}
6868
6869/**
Michal Vaskod3678892020-05-21 10:06:58 +02006870 * @brief Evaluate Literal. Logs directly on error.
6871 *
6872 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006873 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006874 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6875 */
6876static void
Michal Vasko40308e72020-10-20 16:38:40 +02006877eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006878{
6879 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006880 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006881 set_fill_string(set, "", 0);
6882 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006883 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 +02006884 }
6885 }
6886 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006887 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006888 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006889}
6890
6891/**
Michal Vasko004d3152020-06-11 19:59:22 +02006892 * @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 +02006893 *
Michal Vasko004d3152020-06-11 19:59:22 +02006894 * @param[in] exp Full parsed XPath expression.
6895 * @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 +02006896 * @param[in] ctx_node Found schema node as the context for the predicate.
6897 * @param[in] cur_mod Current module for the expression.
6898 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006899 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006900 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006901 * @param[out] predicates Parsed predicates.
6902 * @param[out] pred_type Type of @p predicates.
6903 * @return LY_SUCCESS on success,
6904 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006905 */
6906static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006907eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006908 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6909 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006910{
Michal Vasko004d3152020-06-11 19:59:22 +02006911 LY_ERR ret = LY_SUCCESS;
6912 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006913 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006914 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006915 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006916 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006917
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006918 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006919
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006920 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006921 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006922 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006923 return LY_EINVAL;
6924 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006925 for (key_count = 0, key = lysc_node_children(ctx_node, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006926 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006927
Michal Vasko004d3152020-06-11 19:59:22 +02006928 /* learn where the predicates end */
6929 e_idx = *tok_idx;
6930 while (key_count) {
6931 /* '[' */
6932 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6933 return LY_EINVAL;
6934 }
6935 ++e_idx;
6936
6937 /* ']' */
6938 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6939 ++e_idx;
6940 }
6941 ++e_idx;
6942
6943 /* another presumably key predicate parsed */
6944 --key_count;
6945 }
Michal Vasko004d3152020-06-11 19:59:22 +02006946 } else {
6947 /* learn just where this single predicate ends */
6948 e_idx = *tok_idx;
6949
Michal Vaskod3678892020-05-21 10:06:58 +02006950 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006951 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6952 return LY_EINVAL;
6953 }
6954 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006955
6956 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006957 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6958 ++e_idx;
6959 }
6960 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006961 }
6962
Michal Vasko004d3152020-06-11 19:59:22 +02006963 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6964 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6965
6966 /* turn logging off */
6967 prev_lo = ly_log_options(0);
6968
6969 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006970 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6971 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006972
6973 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006974 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6975 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006976 LY_CHECK_GOTO(ret, cleanup);
6977
6978 /* success, the predicate must include all the needed information for hash-based search */
6979 *tok_idx = e_idx;
6980
6981cleanup:
6982 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006983 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006984 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006985}
6986
6987/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006988 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6989 * data node(s) could be found using a single hash-based search.
6990 *
6991 * @param[in] node Next context node to check.
6992 * @param[in] name Expected node name.
6993 * @param[in] name_len Length of @p name.
6994 * @param[in] moveto_mod Expected node module.
6995 * @param[in] root_type XPath root type.
6996 * @param[in] no_prefix Whether the node name was unprefixed.
6997 * @param[in] format Prefix format.
6998 * @param[in,out] found Previously found node, is updated.
6999 * @return LY_SUCCESS on success,
7000 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7001 */
7002static LY_ERR
7003eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len,
7004 const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format,
7005 const struct lysc_node **found)
7006{
7007 const struct lysc_node *scnode;
7008 const struct lys_module *mod;
7009 uint32_t idx = 0;
7010
7011continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007012 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007013 if (!node) {
7014 if (no_prefix && (format == LY_PREF_JSON)) {
7015 /* search all modules for a single match */
7016 while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) {
7017 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7018 if (scnode) {
7019 /* we have found a match */
7020 break;
7021 }
7022 }
7023
Michal Vasko7d1d0912020-10-16 08:38:30 +02007024 if (!scnode) {
7025 /* all modules searched */
7026 idx = 0;
7027 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007028 } else {
7029 /* search in top-level */
7030 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7031 }
7032 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
7033 if (no_prefix && (format == LY_PREF_JSON)) {
7034 /* we must adjust the module to inherit the one from the context node */
7035 moveto_mod = node->schema->module;
7036 }
7037
7038 /* search in children, do not repeat the same search */
7039 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007040 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007041
7042 /* additional context check */
7043 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7044 scnode = NULL;
7045 }
7046
7047 if (scnode) {
7048 if (*found) {
7049 /* we found a schema node with the same name but at different level, give up, too complicated
7050 * (more hash-based searches would be required, not supported) */
7051 return LY_ENOT;
7052 } else {
7053 /* remember the found schema node and continue to make sure it can be used */
7054 *found = scnode;
7055 }
7056 }
7057
7058 if (idx) {
7059 /* continue searching all the following models */
7060 goto continue_search;
7061 }
7062
7063 return LY_SUCCESS;
7064}
7065
7066/**
Michal Vaskod3678892020-05-21 10:06:58 +02007067 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7068 *
7069 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7070 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7071 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7072 *
7073 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007074 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007075 * @param[in] attr_axis Whether to search attributes or standard nodes.
7076 * @param[in] all_desc Whether to search all the descendants or children only.
7077 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7078 * @param[in] options XPath options.
7079 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7080 */
7081static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007082eval_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 +02007083 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007084{
Michal Vaskod3678892020-05-21 10:06:58 +02007085 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007086 const char *ncname, *ncname_dict = NULL;
7087 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007088 const struct lys_module *moveto_mod = NULL;
7089 const struct lysc_node *scnode = NULL, *ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02007090 struct ly_path_predicate *predicates = NULL;
7091 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007092 LY_ERR rc = LY_SUCCESS;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007093 ly_bool no_prefix;
Michal Vaskod3678892020-05-21 10:06:58 +02007094
7095 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007096 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007097 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007098
7099 if (!set) {
7100 goto moveto;
7101 }
7102
Michal Vasko004d3152020-06-11 19:59:22 +02007103 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7104 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007105
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007106 /* get the first schema context node */
7107 if (set->used) {
7108 if (set->type == LYXP_SET_NODE_SET) {
7109 ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL;
7110 } else {
7111 ctx_scnode = set->val.scnodes[0].scnode;
7112 }
7113 } else {
7114 ctx_scnode = NULL;
7115 }
7116
7117 /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there
7118 * is no prefix for ::LY_PREF_JSON */
7119 rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007120 LY_CHECK_GOTO(rc, cleanup);
7121
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007122 if (ncname_len == exp->tok_len[*tok_idx - 1]) {
7123 /* remember that there is no prefix */
7124 no_prefix = 1;
7125 } else {
7126 no_prefix = 0;
7127 }
7128
Michal Vaskod3678892020-05-21 10:06:58 +02007129 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
7130 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007131 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007132 if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod,
7133 set->root_type, no_prefix, set->format, &scnode)) {
7134 /* check failed */
7135 scnode = NULL;
7136 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007137 }
7138 }
7139
Michal Vasko004d3152020-06-11 19:59:22 +02007140 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7141 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007142 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7143 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007144 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007145 scnode = NULL;
7146 }
7147 }
7148 }
7149
Michal Vasko004d3152020-06-11 19:59:22 +02007150 if (!scnode && moveto_mod) {
7151 /* we are not able to match based on a schema node and not all the modules match,
7152 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007153 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007154 }
7155
7156moveto:
7157 /* move to the attribute(s), data node(s), or schema node(s) */
7158 if (attr_axis) {
7159 if (set && (options & LYXP_SCNODE_ALL)) {
7160 set_scnode_clear_ctx(set);
7161 } else {
7162 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007163 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007164 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007165 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007166 }
7167 LY_CHECK_GOTO(rc, cleanup);
7168 }
7169 } else {
7170 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007171 int64_t i;
7172
Michal Vaskod3678892020-05-21 10:06:58 +02007173 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007174 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007175 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007176 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007177 }
7178 LY_CHECK_GOTO(rc, cleanup);
7179
7180 for (i = set->used - 1; i > -1; --i) {
7181 if (set->val.scnodes[i].in_ctx > 0) {
7182 break;
7183 }
7184 }
7185 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007186 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02007187 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02007188 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007189 free(path);
7190 }
7191 } else {
7192 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007193 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007194 } else {
7195 if (scnode) {
7196 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007197 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007198 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007199 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007200 }
7201 }
7202 LY_CHECK_GOTO(rc, cleanup);
7203 }
7204 }
7205
7206 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007207 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7208 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007209 LY_CHECK_RET(rc);
7210 }
7211
7212cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007213 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007214 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007215 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007216 }
Michal Vaskod3678892020-05-21 10:06:58 +02007217 return rc;
7218}
7219
7220/**
7221 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7222 *
7223 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7224 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7225 * [8] NodeType ::= 'text' | 'node'
7226 *
7227 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007228 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007229 * @param[in] attr_axis Whether to search attributes or standard nodes.
7230 * @param[in] all_desc Whether to search all the descendants or children only.
7231 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7232 * @param[in] options XPath options.
7233 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7234 */
7235static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007236eval_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 +02007237 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007238{
7239 LY_ERR rc;
7240
7241 /* TODO */
7242 (void)attr_axis;
7243 (void)all_desc;
7244
7245 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007246 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007247 if (set->type == LYXP_SET_SCNODE_SET) {
7248 set_scnode_clear_ctx(set);
7249 /* just for the debug message below */
7250 set = NULL;
7251 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007252 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007253 rc = xpath_node(NULL, 0, set, options);
7254 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007255 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007256 rc = xpath_text(NULL, 0, set, options);
7257 }
7258 LY_CHECK_RET(rc);
7259 }
7260 }
7261 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007262 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007263 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007264
7265 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007266 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007267 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007268 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007269 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007270
7271 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007272 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007273 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007274 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007275 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007276
7277 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007278 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7279 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007280 LY_CHECK_RET(rc);
7281 }
7282
7283 return LY_SUCCESS;
7284}
7285
7286/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007287 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7288 *
7289 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7290 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007291 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007292 *
7293 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007294 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007295 * @param[in] all_desc Whether to search all the descendants or children only.
7296 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7297 * @param[in] options XPath options.
7298 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7299 */
7300static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007301eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7302 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303{
Radek Krejci857189e2020-09-01 13:26:36 +02007304 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 LY_ERR rc;
7306
7307 goto step;
7308 do {
7309 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007310 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007311 all_desc = 0;
7312 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007313 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007314 all_desc = 1;
7315 }
7316 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007317 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007318 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319
7320step:
Michal Vaskod3678892020-05-21 10:06:58 +02007321 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007322 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007323 attr_axis = 1;
7324
7325 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007326 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007327 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007328 } else {
7329 attr_axis = 0;
7330 }
7331
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007333 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 case LYXP_TOKEN_DOT:
7335 /* evaluate '.' */
7336 if (set && (options & LYXP_SCNODE_ALL)) {
7337 rc = moveto_scnode_self(set, all_desc, options);
7338 } else {
7339 rc = moveto_self(set, all_desc, options);
7340 }
7341 LY_CHECK_RET(rc);
7342 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007343 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007344 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 break;
7346
7347 case LYXP_TOKEN_DDOT:
7348 /* evaluate '..' */
7349 if (set && (options & LYXP_SCNODE_ALL)) {
7350 rc = moveto_scnode_parent(set, all_desc, options);
7351 } else {
7352 rc = moveto_parent(set, all_desc, options);
7353 }
7354 LY_CHECK_RET(rc);
7355 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007356 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007357 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 break;
7359
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007361 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007362 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007364 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365
Michal Vaskod3678892020-05-21 10:06:58 +02007366 case LYXP_TOKEN_NODETYPE:
7367 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007368 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007369 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 break;
7371
7372 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007373 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374 }
Michal Vasko004d3152020-06-11 19:59:22 +02007375 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007376
7377 return LY_SUCCESS;
7378}
7379
7380/**
7381 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7382 *
7383 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7384 *
7385 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007386 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7388 * @param[in] options XPath options.
7389 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7390 */
7391static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007392eval_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 +02007393{
Radek Krejci857189e2020-09-01 13:26:36 +02007394 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395
7396 if (set) {
7397 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007398 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399 }
7400
7401 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007402 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 /* evaluate '/' - deferred */
7404 all_desc = 0;
7405 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007406 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007407 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408
Michal Vasko004d3152020-06-11 19:59:22 +02007409 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 return LY_SUCCESS;
7411 }
Michal Vasko004d3152020-06-11 19:59:22 +02007412 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413 case LYXP_TOKEN_DOT:
7414 case LYXP_TOKEN_DDOT:
7415 case LYXP_TOKEN_AT:
7416 case LYXP_TOKEN_NAMETEST:
7417 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007418 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 break;
7420 default:
7421 break;
7422 }
7423
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007425 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7427 all_desc = 1;
7428 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007429 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007430 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431
Michal Vaskob0099a92020-08-31 14:55:23 +02007432 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433 }
7434
7435 return LY_SUCCESS;
7436}
7437
7438/**
7439 * @brief Evaluate FunctionCall. Logs directly on error.
7440 *
Michal Vaskod3678892020-05-21 10:06:58 +02007441 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442 *
7443 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007444 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7446 * @param[in] options XPath options.
7447 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7448 */
7449static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007450eval_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 +02007451{
7452 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007453
Radek Krejci1deb5be2020-08-26 16:43:36 +02007454 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007455 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 struct lyxp_set **args = NULL, **args_aux;
7457
7458 if (set) {
7459 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007460 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007462 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007464 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 xpath_func = &xpath_sum;
7466 }
7467 break;
7468 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007469 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007471 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007473 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007475 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007476 xpath_func = &xpath_true;
7477 }
7478 break;
7479 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007480 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007482 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007484 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007486 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007488 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 xpath_func = &xpath_deref;
7490 }
7491 break;
7492 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007493 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007495 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007497 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 xpath_func = &xpath_string;
7499 }
7500 break;
7501 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007502 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007504 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007505 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007506 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 xpath_func = &xpath_current;
7508 }
7509 break;
7510 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007511 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007513 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007515 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 xpath_func = &xpath_re_match;
7517 }
7518 break;
7519 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007520 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007522 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_translate;
7524 }
7525 break;
7526 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007527 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007529 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007530 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007531 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532 xpath_func = &xpath_bit_is_set;
7533 }
7534 break;
7535 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007536 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 xpath_func = &xpath_starts_with;
7538 }
7539 break;
7540 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007541 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007542 xpath_func = &xpath_derived_from;
7543 }
7544 break;
7545 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007546 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007548 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 xpath_func = &xpath_string_length;
7550 }
7551 break;
7552 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007553 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007555 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 xpath_func = &xpath_substring_after;
7557 }
7558 break;
7559 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007560 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_substring_before;
7562 }
7563 break;
7564 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007565 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 xpath_func = &xpath_derived_from_or_self;
7567 }
7568 break;
7569 }
7570
7571 if (!xpath_func) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007572 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007573 return LY_EVALID;
7574 }
7575 }
7576
7577 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007578 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007579 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580
7581 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007582 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007583 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007584 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007585 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586
7587 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007588 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007589 if (set) {
7590 args = malloc(sizeof *args);
7591 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7592 arg_count = 1;
7593 args[0] = set_copy(set);
7594 if (!args[0]) {
7595 rc = LY_EMEM;
7596 goto cleanup;
7597 }
7598
Michal Vasko004d3152020-06-11 19:59:22 +02007599 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 LY_CHECK_GOTO(rc, cleanup);
7601 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007602 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 LY_CHECK_GOTO(rc, cleanup);
7604 }
7605 }
Michal Vasko004d3152020-06-11 19:59:22 +02007606 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007608 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007609 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610
7611 if (set) {
7612 ++arg_count;
7613 args_aux = realloc(args, arg_count * sizeof *args);
7614 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7615 args = args_aux;
7616 args[arg_count - 1] = set_copy(set);
7617 if (!args[arg_count - 1]) {
7618 rc = LY_EMEM;
7619 goto cleanup;
7620 }
7621
Michal Vasko004d3152020-06-11 19:59:22 +02007622 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007623 LY_CHECK_GOTO(rc, cleanup);
7624 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007625 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007626 LY_CHECK_GOTO(rc, cleanup);
7627 }
7628 }
7629
7630 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007631 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007632 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007633 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007634 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007635
7636 if (set) {
7637 /* evaluate function */
7638 rc = xpath_func(args, arg_count, set, options);
7639
7640 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 /* merge all nodes from arg evaluations */
7642 for (i = 0; i < arg_count; ++i) {
7643 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007644 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007645 }
7646 }
7647 } else {
7648 rc = LY_SUCCESS;
7649 }
7650
7651cleanup:
7652 for (i = 0; i < arg_count; ++i) {
7653 lyxp_set_free(args[i]);
7654 }
7655 free(args);
7656
7657 return rc;
7658}
7659
7660/**
7661 * @brief Evaluate Number. Logs directly on error.
7662 *
7663 * @param[in] ctx Context for errors.
7664 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007665 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7667 * @return LY_ERR
7668 */
7669static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007670eval_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 +02007671{
7672 long double num;
7673 char *endptr;
7674
7675 if (set) {
7676 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007677 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007678 if (errno) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007679 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7680 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007681 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007682 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007683 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007684 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7685 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007686 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007687 return LY_EVALID;
7688 }
7689
7690 set_fill_number(set, num);
7691 }
7692
7693 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007694 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007695 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007696 return LY_SUCCESS;
7697}
7698
7699/**
7700 * @brief Evaluate PathExpr. Logs directly on error.
7701 *
Michal Vaskod3678892020-05-21 10:06:58 +02007702 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7704 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7705 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007706 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707 *
7708 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007709 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7711 * @param[in] options XPath options.
7712 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7713 */
7714static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007715eval_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 +02007716{
Radek Krejci857189e2020-09-01 13:26:36 +02007717 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007718 LY_ERR rc;
7719
Michal Vasko004d3152020-06-11 19:59:22 +02007720 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721 case LYXP_TOKEN_PAR1:
7722 /* '(' Expr ')' */
7723
7724 /* '(' */
7725 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007726 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007727 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728
7729 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007730 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007731 LY_CHECK_RET(rc);
7732
7733 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007734 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007735 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007736 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007737 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007738
7739 parent_pos_pred = 0;
7740 goto predicate;
7741
7742 case LYXP_TOKEN_DOT:
7743 case LYXP_TOKEN_DDOT:
7744 case LYXP_TOKEN_AT:
7745 case LYXP_TOKEN_NAMETEST:
7746 case LYXP_TOKEN_NODETYPE:
7747 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007748 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749 LY_CHECK_RET(rc);
7750 break;
7751
7752 case LYXP_TOKEN_FUNCNAME:
7753 /* FunctionCall */
7754 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007755 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007756 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007757 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 }
7759 LY_CHECK_RET(rc);
7760
7761 parent_pos_pred = 1;
7762 goto predicate;
7763
Michal Vasko3e48bf32020-06-01 08:39:07 +02007764 case LYXP_TOKEN_OPER_PATH:
7765 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007766 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007767 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 LY_CHECK_RET(rc);
7769 break;
7770
7771 case LYXP_TOKEN_LITERAL:
7772 /* Literal */
7773 if (!set || (options & LYXP_SCNODE_ALL)) {
7774 if (set) {
7775 set_scnode_clear_ctx(set);
7776 }
Michal Vasko004d3152020-06-11 19:59:22 +02007777 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007778 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007779 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007780 }
7781
7782 parent_pos_pred = 1;
7783 goto predicate;
7784
7785 case LYXP_TOKEN_NUMBER:
7786 /* Number */
7787 if (!set || (options & LYXP_SCNODE_ALL)) {
7788 if (set) {
7789 set_scnode_clear_ctx(set);
7790 }
Michal Vasko004d3152020-06-11 19:59:22 +02007791 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007793 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007794 }
7795 LY_CHECK_RET(rc);
7796
7797 parent_pos_pred = 1;
7798 goto predicate;
7799
7800 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007801 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
Michal Vasko69730152020-10-09 16:30:07 +02007802 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007803 return LY_EVALID;
7804 }
7805
7806 return LY_SUCCESS;
7807
7808predicate:
7809 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007810 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7811 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 LY_CHECK_RET(rc);
7813 }
7814
7815 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007816 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007817
7818 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007819 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007820 all_desc = 0;
7821 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 all_desc = 1;
7823 }
7824
7825 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007826 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007827 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007828
Michal Vasko004d3152020-06-11 19:59:22 +02007829 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007830 LY_CHECK_RET(rc);
7831 }
7832
7833 return LY_SUCCESS;
7834}
7835
7836/**
7837 * @brief Evaluate UnionExpr. Logs directly on error.
7838 *
Michal Vaskod3678892020-05-21 10:06:58 +02007839 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840 *
7841 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007842 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843 * @param[in] repeat How many times this expression is repeated.
7844 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7845 * @param[in] options XPath options.
7846 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7847 */
7848static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007849eval_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 +02007850{
7851 LY_ERR rc = LY_SUCCESS;
7852 struct lyxp_set orig_set, set2;
7853 uint16_t i;
7854
7855 assert(repeat);
7856
7857 set_init(&orig_set, set);
7858 set_init(&set2, set);
7859
7860 set_fill_set(&orig_set, set);
7861
Michal Vasko004d3152020-06-11 19:59:22 +02007862 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863 LY_CHECK_GOTO(rc, cleanup);
7864
7865 /* ('|' PathExpr)* */
7866 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007867 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007868 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007869 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007870 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007871
7872 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007873 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007874 LY_CHECK_GOTO(rc, cleanup);
7875 continue;
7876 }
7877
7878 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007879 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007880 LY_CHECK_GOTO(rc, cleanup);
7881
7882 /* eval */
7883 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007884 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007885 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007886 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887 LY_CHECK_GOTO(rc, cleanup);
7888 }
7889 }
7890
7891cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007892 lyxp_set_free_content(&orig_set);
7893 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007894 return rc;
7895}
7896
7897/**
7898 * @brief Evaluate UnaryExpr. Logs directly on error.
7899 *
Michal Vaskod3678892020-05-21 10:06:58 +02007900 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007901 *
7902 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007903 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007904 * @param[in] repeat How many times this expression is repeated.
7905 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7906 * @param[in] options XPath options.
7907 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7908 */
7909static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007910eval_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 +02007911{
7912 LY_ERR rc;
7913 uint16_t this_op, i;
7914
7915 assert(repeat);
7916
7917 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007918 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007919 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007920 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 +02007921
7922 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007923 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007924 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 }
7926
Michal Vasko004d3152020-06-11 19:59:22 +02007927 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007928 LY_CHECK_RET(rc);
7929
7930 if (set && (repeat % 2)) {
7931 if (options & LYXP_SCNODE_ALL) {
7932 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7933 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007934 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007935 LY_CHECK_RET(rc);
7936 }
7937 }
7938
7939 return LY_SUCCESS;
7940}
7941
7942/**
7943 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7944 *
Michal Vaskod3678892020-05-21 10:06:58 +02007945 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007946 * | MultiplicativeExpr '*' UnaryExpr
7947 * | MultiplicativeExpr 'div' UnaryExpr
7948 * | MultiplicativeExpr 'mod' UnaryExpr
7949 *
7950 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007951 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952 * @param[in] repeat How many times this expression is repeated.
7953 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7954 * @param[in] options XPath options.
7955 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7956 */
7957static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007958eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7959 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960{
7961 LY_ERR rc;
7962 uint16_t this_op;
7963 struct lyxp_set orig_set, set2;
7964 uint16_t i;
7965
7966 assert(repeat);
7967
7968 set_init(&orig_set, set);
7969 set_init(&set2, set);
7970
7971 set_fill_set(&orig_set, set);
7972
Michal Vasko004d3152020-06-11 19:59:22 +02007973 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007974 LY_CHECK_GOTO(rc, cleanup);
7975
7976 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7977 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007978 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007979
Michal Vasko004d3152020-06-11 19:59:22 +02007980 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007981 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007982 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007983 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007984
7985 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007986 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007987 LY_CHECK_GOTO(rc, cleanup);
7988 continue;
7989 }
7990
7991 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007992 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 LY_CHECK_GOTO(rc, cleanup);
7994
7995 /* eval */
7996 if (options & LYXP_SCNODE_ALL) {
7997 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007998 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 set_scnode_clear_ctx(set);
8000 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008001 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008002 LY_CHECK_GOTO(rc, cleanup);
8003 }
8004 }
8005
8006cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008007 lyxp_set_free_content(&orig_set);
8008 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008009 return rc;
8010}
8011
8012/**
8013 * @brief Evaluate AdditiveExpr. Logs directly on error.
8014 *
Michal Vaskod3678892020-05-21 10:06:58 +02008015 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016 * | AdditiveExpr '+' MultiplicativeExpr
8017 * | AdditiveExpr '-' MultiplicativeExpr
8018 *
8019 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008020 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021 * @param[in] repeat How many times this expression is repeated.
8022 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8023 * @param[in] options XPath options.
8024 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8025 */
8026static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008027eval_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 +02008028{
8029 LY_ERR rc;
8030 uint16_t this_op;
8031 struct lyxp_set orig_set, set2;
8032 uint16_t i;
8033
8034 assert(repeat);
8035
8036 set_init(&orig_set, set);
8037 set_init(&set2, set);
8038
8039 set_fill_set(&orig_set, set);
8040
Michal Vasko004d3152020-06-11 19:59:22 +02008041 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008042 LY_CHECK_GOTO(rc, cleanup);
8043
8044 /* ('+' / '-' MultiplicativeExpr)* */
8045 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008046 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047
Michal Vasko004d3152020-06-11 19:59:22 +02008048 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008049 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008050 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008051 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052
8053 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008054 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008055 LY_CHECK_GOTO(rc, cleanup);
8056 continue;
8057 }
8058
8059 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008060 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 LY_CHECK_GOTO(rc, cleanup);
8062
8063 /* eval */
8064 if (options & LYXP_SCNODE_ALL) {
8065 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008066 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008067 set_scnode_clear_ctx(set);
8068 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008069 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008070 LY_CHECK_GOTO(rc, cleanup);
8071 }
8072 }
8073
8074cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008075 lyxp_set_free_content(&orig_set);
8076 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008077 return rc;
8078}
8079
8080/**
8081 * @brief Evaluate RelationalExpr. Logs directly on error.
8082 *
Michal Vaskod3678892020-05-21 10:06:58 +02008083 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084 * | RelationalExpr '<' AdditiveExpr
8085 * | RelationalExpr '>' AdditiveExpr
8086 * | RelationalExpr '<=' AdditiveExpr
8087 * | RelationalExpr '>=' AdditiveExpr
8088 *
8089 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008090 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008091 * @param[in] repeat How many times this expression is repeated.
8092 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8093 * @param[in] options XPath options.
8094 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8095 */
8096static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008097eval_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 +02008098{
8099 LY_ERR rc;
8100 uint16_t this_op;
8101 struct lyxp_set orig_set, set2;
8102 uint16_t i;
8103
8104 assert(repeat);
8105
8106 set_init(&orig_set, set);
8107 set_init(&set2, set);
8108
8109 set_fill_set(&orig_set, set);
8110
Michal Vasko004d3152020-06-11 19:59:22 +02008111 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008112 LY_CHECK_GOTO(rc, cleanup);
8113
8114 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8115 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008116 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117
Michal Vasko004d3152020-06-11 19:59:22 +02008118 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008120 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008121 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122
8123 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008124 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 LY_CHECK_GOTO(rc, cleanup);
8126 continue;
8127 }
8128
8129 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008130 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008131 LY_CHECK_GOTO(rc, cleanup);
8132
8133 /* eval */
8134 if (options & LYXP_SCNODE_ALL) {
8135 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008136 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008137 set_scnode_clear_ctx(set);
8138 } else {
8139 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8140 LY_CHECK_GOTO(rc, cleanup);
8141 }
8142 }
8143
8144cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008145 lyxp_set_free_content(&orig_set);
8146 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008147 return rc;
8148}
8149
8150/**
8151 * @brief Evaluate EqualityExpr. Logs directly on error.
8152 *
Michal Vaskod3678892020-05-21 10:06:58 +02008153 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008154 * | EqualityExpr '!=' RelationalExpr
8155 *
8156 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008157 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158 * @param[in] repeat How many times this expression is repeated.
8159 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8160 * @param[in] options XPath options.
8161 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8162 */
8163static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008164eval_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 +02008165{
8166 LY_ERR rc;
8167 uint16_t this_op;
8168 struct lyxp_set orig_set, set2;
8169 uint16_t i;
8170
8171 assert(repeat);
8172
8173 set_init(&orig_set, set);
8174 set_init(&set2, set);
8175
8176 set_fill_set(&orig_set, set);
8177
Michal Vasko004d3152020-06-11 19:59:22 +02008178 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179 LY_CHECK_GOTO(rc, cleanup);
8180
8181 /* ('=' / '!=' RelationalExpr)* */
8182 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008183 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184
Michal Vasko004d3152020-06-11 19:59:22 +02008185 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008186 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008187 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008188 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008189
8190 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008191 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 LY_CHECK_GOTO(rc, cleanup);
8193 continue;
8194 }
8195
8196 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008197 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 LY_CHECK_GOTO(rc, cleanup);
8199
8200 /* eval */
8201 if (options & LYXP_SCNODE_ALL) {
8202 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008203 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8204 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008205 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008206 set_scnode_clear_ctx(set);
8207 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008208 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8209 LY_CHECK_GOTO(rc, cleanup);
8210 }
8211 }
8212
8213cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008214 lyxp_set_free_content(&orig_set);
8215 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008216 return rc;
8217}
8218
8219/**
8220 * @brief Evaluate AndExpr. Logs directly on error.
8221 *
Michal Vaskod3678892020-05-21 10:06:58 +02008222 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008223 *
8224 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008225 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008226 * @param[in] repeat How many times this expression is repeated.
8227 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8228 * @param[in] options XPath options.
8229 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8230 */
8231static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008232eval_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 +02008233{
8234 LY_ERR rc;
8235 struct lyxp_set orig_set, set2;
8236 uint16_t i;
8237
8238 assert(repeat);
8239
8240 set_init(&orig_set, set);
8241 set_init(&set2, set);
8242
8243 set_fill_set(&orig_set, set);
8244
Michal Vasko004d3152020-06-11 19:59:22 +02008245 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 LY_CHECK_GOTO(rc, cleanup);
8247
8248 /* cast to boolean, we know that will be the final result */
8249 if (set && (options & LYXP_SCNODE_ALL)) {
8250 set_scnode_clear_ctx(set);
8251 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008252 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 }
8254
8255 /* ('and' EqualityExpr)* */
8256 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008257 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8258 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008259 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008260 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261
8262 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008263 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8264 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 LY_CHECK_GOTO(rc, cleanup);
8266 continue;
8267 }
8268
8269 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008270 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 LY_CHECK_GOTO(rc, cleanup);
8272
8273 /* eval - just get boolean value actually */
8274 if (set->type == LYXP_SET_SCNODE_SET) {
8275 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008276 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008278 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008279 set_fill_set(set, &set2);
8280 }
8281 }
8282
8283cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008284 lyxp_set_free_content(&orig_set);
8285 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008286 return rc;
8287}
8288
8289/**
8290 * @brief Evaluate OrExpr. Logs directly on error.
8291 *
Michal Vaskod3678892020-05-21 10:06:58 +02008292 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293 *
8294 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008295 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008296 * @param[in] repeat How many times this expression is repeated.
8297 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8298 * @param[in] options XPath options.
8299 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8300 */
8301static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008302eval_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 +02008303{
8304 LY_ERR rc;
8305 struct lyxp_set orig_set, set2;
8306 uint16_t i;
8307
8308 assert(repeat);
8309
8310 set_init(&orig_set, set);
8311 set_init(&set2, set);
8312
8313 set_fill_set(&orig_set, set);
8314
Michal Vasko004d3152020-06-11 19:59:22 +02008315 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316 LY_CHECK_GOTO(rc, cleanup);
8317
8318 /* cast to boolean, we know that will be the final result */
8319 if (set && (options & LYXP_SCNODE_ALL)) {
8320 set_scnode_clear_ctx(set);
8321 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008322 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008323 }
8324
8325 /* ('or' AndExpr)* */
8326 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008327 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8328 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008329 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008330 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008331
8332 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008333 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8334 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 LY_CHECK_GOTO(rc, cleanup);
8336 continue;
8337 }
8338
8339 set_fill_set(&set2, &orig_set);
8340 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8341 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008342 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 LY_CHECK_GOTO(rc, cleanup);
8344
8345 /* eval - just get boolean value actually */
8346 if (set->type == LYXP_SET_SCNODE_SET) {
8347 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008348 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008350 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008351 set_fill_set(set, &set2);
8352 }
8353 }
8354
8355cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008356 lyxp_set_free_content(&orig_set);
8357 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358 return rc;
8359}
8360
8361/**
Michal Vasko004d3152020-06-11 19:59:22 +02008362 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 *
8364 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008365 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366 * @param[in] etype Expression type to evaluate.
8367 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8368 * @param[in] options XPath options.
8369 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8370 */
8371static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008372eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8373 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008374{
8375 uint16_t i, count;
8376 enum lyxp_expr_type next_etype;
8377 LY_ERR rc;
8378
8379 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008380 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 next_etype = LYXP_EXPR_NONE;
8382 } else {
8383 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008384 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008385
8386 /* select one-priority lower because etype expression called us */
8387 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008388 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008390 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008391 } else {
8392 next_etype = LYXP_EXPR_NONE;
8393 }
8394 }
8395
8396 /* decide what expression are we parsing based on the repeat */
8397 switch (next_etype) {
8398 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008399 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400 break;
8401 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008402 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 break;
8404 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008405 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406 break;
8407 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008408 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409 break;
8410 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008411 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412 break;
8413 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008414 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 break;
8416 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008417 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 break;
8419 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008420 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 break;
8422 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008423 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008424 break;
8425 default:
8426 LOGINT_RET(set->ctx);
8427 }
8428
8429 return rc;
8430}
8431
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008432/**
8433 * @brief Get root type.
8434 *
8435 * @param[in] ctx_node Context node.
8436 * @param[in] ctx_scnode Schema context node.
8437 * @param[in] options XPath options.
8438 * @return Root type.
8439 */
8440static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008441lyxp_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 +01008442{
Michal Vasko6b26e742020-07-17 15:02:10 +02008443 const struct lysc_node *op;
8444
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008445 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008446 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008447 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008448
8449 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008450 /* general root that can access everything */
8451 return LYXP_NODE_ROOT;
8452 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8453 /* root context node can access only config data (because we said so, it is unspecified) */
8454 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008455 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008456 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008457 }
8458
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008459 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008460 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008461 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008462
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008463 if (op || !(options & LYXP_SCHEMA)) {
8464 /* general root that can access everything */
8465 return LYXP_NODE_ROOT;
8466 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008467 /* root context node can access only config data (because we said so, it is unspecified) */
8468 return LYXP_NODE_ROOT_CONFIG;
8469 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008470 return LYXP_NODE_ROOT;
8471}
8472
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008474lyxp_eval(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008475 const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476{
Michal Vasko004d3152020-06-11 19:59:22 +02008477 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478 LY_ERR rc;
8479
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008480 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8481 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8482 LOGARG(NULL, "Current module must be set if schema format is used.");
8483 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008484 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008485
8486 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008488 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008489 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8490 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8491
8492 set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL)));
8493 set->cur_node = ctx_node;
8494 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008495 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8496 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008497 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008498 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008500 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008501
8502 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008503 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008505 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 }
8507
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 return rc;
8509}
8510
8511#if 0
8512
8513/* full xml printing of set elements, not used currently */
8514
8515void
8516lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8517{
8518 uint32_t i;
8519 char *str_num;
8520 struct lyout out;
8521
8522 memset(&out, 0, sizeof out);
8523
8524 out.type = LYOUT_STREAM;
8525 out.method.f = f;
8526
8527 switch (set->type) {
8528 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008529 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530 break;
8531 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008532 ly_print_(&out, "Boolean XPath set:\n");
8533 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008534 break;
8535 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008536 ly_print_(&out, "String XPath set:\n");
8537 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008538 break;
8539 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008540 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541
8542 if (isnan(set->value.num)) {
8543 str_num = strdup("NaN");
8544 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8545 str_num = strdup("0");
8546 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8547 str_num = strdup("Infinity");
8548 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8549 str_num = strdup("-Infinity");
8550 } else if ((long long)set->value.num == set->value.num) {
8551 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8552 str_num = NULL;
8553 }
8554 } else {
8555 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8556 str_num = NULL;
8557 }
8558 }
8559 if (!str_num) {
8560 LOGMEM;
8561 return;
8562 }
Michal Vasko5233e962020-08-14 14:26:20 +02008563 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 free(str_num);
8565 break;
8566 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008567 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568
8569 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008570 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008571 switch (set->node_type[i]) {
8572 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008573 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 break;
8575 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008576 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 break;
8578 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008579 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 break;
8581 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008582 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 break;
8584 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008585 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 break;
8587 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008588 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 break;
8590 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008591 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008592 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008593 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594 break;
8595 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008596 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 +02008597 break;
8598 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008599 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 +02008600 break;
8601 }
8602 }
8603 break;
8604 }
8605}
8606
8607#endif
8608
8609LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008610lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008611{
8612 long double num;
8613 char *str;
8614 LY_ERR rc;
8615
8616 if (!set || (set->type == target)) {
8617 return LY_SUCCESS;
8618 }
8619
8620 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008621 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008622
8623 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008624 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008625 return LY_EINVAL;
8626 }
8627
8628 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008629 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 switch (set->type) {
8631 case LYXP_SET_NUMBER:
8632 if (isnan(set->val.num)) {
8633 set->val.str = strdup("NaN");
8634 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8635 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8636 set->val.str = strdup("0");
8637 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8638 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8639 set->val.str = strdup("Infinity");
8640 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8641 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8642 set->val.str = strdup("-Infinity");
8643 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8644 } else if ((long long)set->val.num == set->val.num) {
8645 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8646 LOGMEM_RET(set->ctx);
8647 }
8648 set->val.str = str;
8649 } else {
8650 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8651 LOGMEM_RET(set->ctx);
8652 }
8653 set->val.str = str;
8654 }
8655 break;
8656 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008657 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658 set->val.str = strdup("true");
8659 } else {
8660 set->val.str = strdup("false");
8661 }
8662 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8663 break;
8664 case LYXP_SET_NODE_SET:
8665 assert(set->used);
8666
8667 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008668 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008669
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008670 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008671 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008672 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008673 set->val.str = str;
8674 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675 default:
8676 LOGINT_RET(set->ctx);
8677 }
8678 set->type = LYXP_SET_STRING;
8679 }
8680
8681 /* to NUMBER */
8682 if (target == LYXP_SET_NUMBER) {
8683 switch (set->type) {
8684 case LYXP_SET_STRING:
8685 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008686 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008687 set->val.num = num;
8688 break;
8689 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008690 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008691 set->val.num = 1;
8692 } else {
8693 set->val.num = 0;
8694 }
8695 break;
8696 default:
8697 LOGINT_RET(set->ctx);
8698 }
8699 set->type = LYXP_SET_NUMBER;
8700 }
8701
8702 /* to BOOLEAN */
8703 if (target == LYXP_SET_BOOLEAN) {
8704 switch (set->type) {
8705 case LYXP_SET_NUMBER:
8706 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008707 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008709 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008710 }
8711 break;
8712 case LYXP_SET_STRING:
8713 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008714 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008715 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008717 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008718 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008719 }
8720 break;
8721 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008722 if (set->used) {
8723 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008724 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008725 } else {
8726 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008727 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008728 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008729 break;
8730 default:
8731 LOGINT_RET(set->ctx);
8732 }
8733 set->type = LYXP_SET_BOOLEAN;
8734 }
8735
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736 return LY_SUCCESS;
8737}
8738
8739LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008740lyxp_atomize(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008741 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008742{
Michal Vasko004d3152020-06-11 19:59:22 +02008743 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008745 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8746 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8747 LOGARG(NULL, "Current module must be set if schema format is used.");
8748 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008749 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008750
8751 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008752 memset(set, 0, sizeof *set);
8753 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008754 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8755 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL));
Michal Vasko5c4e5892019-11-14 12:31:38 +01008756 set->val.scnodes[0].in_ctx = -2;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008757
8758 set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL);
8759 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008760 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008761 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8762 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008763 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008765 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008766
8767 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008768 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008769}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008770
8771API const char *
8772lyxp_get_expr(const struct lyxp_expr *path)
8773{
8774 if (!path) {
8775 return NULL;
8776 }
8777
8778 return path->expr;
8779}