blob: 14738c7d03f7904e0c1aed05a895b549a2aa74fd [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
Radek Krejcif8dc59a2020-11-25 13:47:44 +010015#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Michal Vasko03ff5a72019-09-11 13:49:33 +020016
17/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
18#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010019
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010021
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#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 Krejci47fab892020-11-05 17:02:41 +010036#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020038#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020039#include "plugins_types.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;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001291 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001292
1293 assert(prev && prev_pos && !root->prev->next);
1294
1295 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1296 return 0;
1297 }
1298
1299 if (*prev) {
1300 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001301 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001302 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001303 goto dfs_search;
1304 }
1305
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001306 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001307 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001308dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001309 if (*prev && !elem) {
1310 /* resume previous DFS */
1311 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1312 LYD_TREE_DFS_continue = 0;
1313 }
1314
Michal Vasko03ff5a72019-09-11 13:49:33 +02001315 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001316 /* skip */
1317 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001318 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001319 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001320 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001321 break;
1322 }
Michal Vasko56daf732020-08-10 10:57:18 +02001323 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324 }
Michal Vasko56daf732020-08-10 10:57:18 +02001325
1326 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001327 }
1328
1329 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001330 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001331 break;
1332 }
1333 }
1334
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001335 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001336 if (!(*prev)) {
1337 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001338 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001339 return 0;
1340 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001341 /* start the search again from the beginning */
1342 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001343
Michal Vasko56daf732020-08-10 10:57:18 +02001344 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001345 pos = 1;
1346 goto dfs_search;
1347 }
1348 }
1349
1350 /* remember the last found node for next time */
1351 *prev = node;
1352 *prev_pos = pos;
1353
1354 return pos;
1355}
1356
1357/**
1358 * @brief Assign (fill) missing node positions.
1359 *
1360 * @param[in] set Set to fill positions in.
1361 * @param[in] root Context root node.
1362 * @param[in] root_type Context root type.
1363 * @return LY_ERR
1364 */
1365static LY_ERR
1366set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1367{
1368 const struct lyd_node *prev = NULL, *tmp_node;
1369 uint32_t i, tmp_pos = 0;
1370
1371 for (i = 0; i < set->used; ++i) {
1372 if (!set->val.nodes[i].pos) {
1373 tmp_node = NULL;
1374 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001375 case LYXP_NODE_META:
1376 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377 if (!tmp_node) {
1378 LOGINT_RET(root->schema->module->ctx);
1379 }
Radek Krejci0f969882020-08-21 16:56:47 +02001380 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001381 case LYXP_NODE_ELEM:
1382 case LYXP_NODE_TEXT:
1383 if (!tmp_node) {
1384 tmp_node = set->val.nodes[i].node;
1385 }
1386 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1387 break;
1388 default:
1389 /* all roots have position 0 */
1390 break;
1391 }
1392 }
1393 }
1394
1395 return LY_SUCCESS;
1396}
1397
1398/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001399 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001401 * @param[in] meta Metadata to use.
1402 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001403 */
1404static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001405get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001406{
1407 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001408 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001409
Michal Vasko9f96a052020-03-10 09:41:45 +01001410 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001411 ++pos;
1412 }
1413
Michal Vasko9f96a052020-03-10 09:41:45 +01001414 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001415 return pos;
1416}
1417
1418/**
1419 * @brief Compare 2 nodes in respect to XPath document order.
1420 *
1421 * @param[in] item1 1st node.
1422 * @param[in] item2 2nd node.
1423 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1424 */
1425static int
1426set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1427{
Michal Vasko9f96a052020-03-10 09:41:45 +01001428 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001429
1430 if (item1->pos < item2->pos) {
1431 return -1;
1432 }
1433
1434 if (item1->pos > item2->pos) {
1435 return 1;
1436 }
1437
1438 /* node positions are equal, the fun case */
1439
1440 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1441 /* special case since text nodes are actually saved as their parents */
1442 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1443 if (item1->type == LYXP_NODE_ELEM) {
1444 assert(item2->type == LYXP_NODE_TEXT);
1445 return -1;
1446 } else {
1447 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1448 return 1;
1449 }
1450 }
1451
Michal Vasko9f96a052020-03-10 09:41:45 +01001452 /* we need meta positions now */
1453 if (item1->type == LYXP_NODE_META) {
1454 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001455 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001456 if (item2->type == LYXP_NODE_META) {
1457 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001458 }
1459
Michal Vasko9f96a052020-03-10 09:41:45 +01001460 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461 /* check for duplicates */
1462 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001463 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001464 return 0;
1465 }
1466
Michal Vasko9f96a052020-03-10 09:41:45 +01001467 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001468 /* elem is always first, 2nd node is after it */
1469 if (item1->type == LYXP_NODE_ELEM) {
1470 assert(item2->type != LYXP_NODE_ELEM);
1471 return -1;
1472 }
1473
Michal Vasko9f96a052020-03-10 09:41:45 +01001474 /* 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 +02001475 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001476 if (((item1->type == LYXP_NODE_TEXT) &&
1477 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1478 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1479 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1480 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001481 return 1;
1482 }
1483
Michal Vasko9f96a052020-03-10 09:41:45 +01001484 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001485 /* 2nd is after 1st */
1486 return -1;
1487}
1488
1489/**
1490 * @brief Set cast for comparisons.
1491 *
1492 * @param[in] trg Target set to cast source into.
1493 * @param[in] src Source set.
1494 * @param[in] type Target set type.
1495 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001496 * @return LY_ERR
1497 */
1498static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001499set_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 +02001500{
1501 assert(src->type == LYXP_SET_NODE_SET);
1502
1503 set_init(trg, src);
1504
1505 /* insert node into target set */
1506 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1507
1508 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001509 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001510}
1511
Michal Vasko4c7763f2020-07-27 17:40:37 +02001512/**
1513 * @brief Set content canonization for comparisons.
1514 *
1515 * @param[in] trg Target set to put the canononized source into.
1516 * @param[in] src Source set.
1517 * @param[in] xp_node Source XPath node/meta to use for canonization.
1518 * @return LY_ERR
1519 */
1520static LY_ERR
1521set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1522{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001523 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001524 struct lyd_value val;
1525 struct ly_err_item *err = NULL;
1526 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001527 LY_ERR rc;
1528
1529 /* is there anything to canonize even? */
1530 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1531 /* do we have a type to use for canonization? */
1532 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1533 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1534 } else if (xp_node->type == LYXP_NODE_META) {
1535 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1536 }
1537 }
1538 if (!type) {
1539 goto fill;
1540 }
1541
1542 if (src->type == LYXP_SET_NUMBER) {
1543 /* canonize number */
1544 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1545 LOGMEM(src->ctx);
1546 return LY_EMEM;
1547 }
1548 } else {
1549 /* canonize string */
1550 str = strdup(src->val.str);
1551 }
1552
1553 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001554 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001555 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001556 ly_err_free(err);
1557 if (rc) {
1558 /* invalid value */
1559 free(str);
1560 goto fill;
1561 }
1562
Michal Vasko4c7763f2020-07-27 17:40:37 +02001563 /* use the canonized value */
1564 set_init(trg, src);
1565 trg->type = src->type;
1566 if (src->type == LYXP_SET_NUMBER) {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001567 trg->val.num = strtold(val.canonical, &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001568 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1569 } else {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001570 trg->val.str = strdup(val.canonical);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001571 }
1572 type->plugin->free(src->ctx, &val);
1573 return LY_SUCCESS;
1574
1575fill:
1576 /* no canonization needed/possible */
1577 set_fill_set(trg, src);
1578 return LY_SUCCESS;
1579}
1580
Michal Vasko03ff5a72019-09-11 13:49:33 +02001581#ifndef NDEBUG
1582
1583/**
1584 * @brief Bubble sort @p set into XPath document order.
1585 * Context position aware. Unused in the 'Release' build target.
1586 *
1587 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1589 */
1590static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001591set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001592{
1593 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001594 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001595 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001596 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001597 struct lyxp_set_node item;
1598 struct lyxp_set_hash_node hnode;
1599 uint64_t hash;
1600
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001601 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001602 return 0;
1603 }
1604
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001605 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001606 for (root = set->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001607 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001608
1609 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001610 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001611 return -1;
1612 }
1613
1614 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1615 print_set_debug(set);
1616
1617 for (i = 0; i < set->used; ++i) {
1618 inverted = 0;
1619 change = 0;
1620
1621 for (j = 1; j < set->used - i; ++j) {
1622 /* compare node positions */
1623 if (inverted) {
1624 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1625 } else {
1626 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1627 }
1628
1629 /* swap if needed */
1630 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1631 change = 1;
1632
1633 item = set->val.nodes[j - 1];
1634 set->val.nodes[j - 1] = set->val.nodes[j];
1635 set->val.nodes[j] = item;
1636 } else {
1637 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1638 inverted = !inverted;
1639 }
1640 }
1641
1642 ++ret;
1643
1644 if (!change) {
1645 break;
1646 }
1647 }
1648
1649 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1650 print_set_debug(set);
1651
1652 /* check node hashes */
1653 if (set->used >= LYD_HT_MIN_ITEMS) {
1654 assert(set->ht);
1655 for (i = 0; i < set->used; ++i) {
1656 hnode.node = set->val.nodes[i].node;
1657 hnode.type = set->val.nodes[i].type;
1658
1659 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1660 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1661 hash = dict_hash_multi(hash, NULL, 0);
1662
1663 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1664 }
1665 }
1666
1667 return ret - 1;
1668}
1669
1670/**
1671 * @brief Remove duplicate entries in a sorted node set.
1672 *
1673 * @param[in] set Sorted set to check.
1674 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1675 */
1676static LY_ERR
1677set_sorted_dup_node_clean(struct lyxp_set *set)
1678{
1679 uint32_t i = 0;
1680 LY_ERR ret = LY_SUCCESS;
1681
1682 if (set->used > 1) {
1683 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001684 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1685 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001686 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001687 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001688 }
Michal Vasko57eab132019-09-24 11:46:26 +02001689 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001690 }
1691 }
1692
Michal Vasko2caefc12019-11-14 16:07:56 +01001693 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001694 return ret;
1695}
1696
1697#endif
1698
1699/**
1700 * @brief Merge 2 sorted sets into one.
1701 *
1702 * @param[in,out] trg Set to merge into. Duplicates are removed.
1703 * @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 +02001704 * @return LY_ERR
1705 */
1706static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001707set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001708{
1709 uint32_t i, j, k, count, dup_count;
1710 int cmp;
1711 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712
Michal Vaskod3678892020-05-21 10:06:58 +02001713 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001714 return LY_EINVAL;
1715 }
1716
Michal Vaskod3678892020-05-21 10:06:58 +02001717 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001718 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001719 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001721 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 return LY_SUCCESS;
1723 }
1724
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001725 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001726 for (root = trg->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001727 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001728
1729 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001730 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001731 return LY_EINT;
1732 }
1733
1734#ifndef NDEBUG
1735 LOGDBG(LY_LDGXPATH, "MERGE target");
1736 print_set_debug(trg);
1737 LOGDBG(LY_LDGXPATH, "MERGE source");
1738 print_set_debug(src);
1739#endif
1740
1741 /* make memory for the merge (duplicates are not detected yet, so space
1742 * will likely be wasted on them, too bad) */
1743 if (trg->size - trg->used < src->used) {
1744 trg->size = trg->used + src->used;
1745
1746 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1747 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1748 }
1749
1750 i = 0;
1751 j = 0;
1752 count = 0;
1753 dup_count = 0;
1754 do {
1755 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1756 if (!cmp) {
1757 if (!count) {
1758 /* duplicate, just skip it */
1759 ++i;
1760 ++j;
1761 } else {
1762 /* we are copying something already, so let's copy the duplicate too,
1763 * we are hoping that afterwards there are some more nodes to
1764 * copy and this way we can copy them all together */
1765 ++count;
1766 ++dup_count;
1767 ++i;
1768 ++j;
1769 }
1770 } else if (cmp < 0) {
1771 /* inserting src node into trg, just remember it for now */
1772 ++count;
1773 ++i;
1774
1775 /* insert the hash now */
1776 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1777 } else if (count) {
1778copy_nodes:
1779 /* time to actually copy the nodes, we have found the largest block of nodes */
1780 memmove(&trg->val.nodes[j + (count - dup_count)],
1781 &trg->val.nodes[j],
1782 (trg->used - j) * sizeof *trg->val.nodes);
1783 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1784
1785 trg->used += count - dup_count;
1786 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1787 j += count - dup_count;
1788
1789 count = 0;
1790 dup_count = 0;
1791 } else {
1792 ++j;
1793 }
1794 } while ((i < src->used) && (j < trg->used));
1795
1796 if ((i < src->used) || count) {
1797 /* insert all the hashes first */
1798 for (k = i; k < src->used; ++k) {
1799 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1800 }
1801
1802 /* loop ended, but we need to copy something at trg end */
1803 count += src->used - i;
1804 i = src->used;
1805 goto copy_nodes;
1806 }
1807
1808 /* we are inserting hashes before the actual node insert, which causes
1809 * situations when there were initially not enough items for a hash table,
1810 * but even after some were inserted, hash table was not created (during
1811 * insertion the number of items is not updated yet) */
1812 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1813 set_insert_node_hash(trg, NULL, 0);
1814 }
1815
1816#ifndef NDEBUG
1817 LOGDBG(LY_LDGXPATH, "MERGE result");
1818 print_set_debug(trg);
1819#endif
1820
Michal Vaskod3678892020-05-21 10:06:58 +02001821 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001822 return LY_SUCCESS;
1823}
1824
1825/*
1826 * (re)parse functions
1827 *
1828 * Parse functions parse the expression into
1829 * tokens (syntactic analysis).
1830 *
1831 * Reparse functions perform semantic analysis
1832 * (do not save the result, just a check) of
1833 * the expression and fill repeat indices.
1834 */
1835
Michal Vasko14676352020-05-29 11:35:55 +02001836LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001837lyxp_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 +02001838{
Michal Vasko004d3152020-06-11 19:59:22 +02001839 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001840 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001841 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1842 }
Michal Vasko14676352020-05-29 11:35:55 +02001843 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001844 }
1845
Michal Vasko004d3152020-06-11 19:59:22 +02001846 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001847 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001848 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
1849 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001850 }
Michal Vasko14676352020-05-29 11:35:55 +02001851 return LY_ENOT;
1852 }
1853
1854 return LY_SUCCESS;
1855}
1856
Michal Vasko004d3152020-06-11 19:59:22 +02001857LY_ERR
1858lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1859{
1860 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1861
1862 /* skip the token */
1863 ++(*tok_idx);
1864
1865 return LY_SUCCESS;
1866}
1867
Michal Vasko14676352020-05-29 11:35:55 +02001868/* just like lyxp_check_token() but tests for 2 tokens */
1869static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001870exp_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 +02001871 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001872{
Michal Vasko004d3152020-06-11 19:59:22 +02001873 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001874 if (ctx) {
1875 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1876 }
1877 return LY_EINCOMPLETE;
1878 }
1879
Michal Vasko004d3152020-06-11 19:59:22 +02001880 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001881 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001882 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
1883 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001884 }
1885 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001886 }
1887
1888 return LY_SUCCESS;
1889}
1890
1891/**
1892 * @brief Stack operation push on the repeat array.
1893 *
1894 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001895 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001896 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1897 */
1898static void
Michal Vasko004d3152020-06-11 19:59:22 +02001899exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001900{
1901 uint16_t i;
1902
Michal Vasko004d3152020-06-11 19:59:22 +02001903 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001904 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001905 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1906 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1907 exp->repeat[tok_idx][i] = repeat_op_idx;
1908 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001909 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001910 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1911 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1912 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001913 }
1914}
1915
1916/**
1917 * @brief Reparse Predicate. Logs directly on error.
1918 *
1919 * [7] Predicate ::= '[' Expr ']'
1920 *
1921 * @param[in] ctx Context for logging.
1922 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001923 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001924 * @return LY_ERR
1925 */
1926static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001927reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001928{
1929 LY_ERR rc;
1930
Michal Vasko004d3152020-06-11 19:59:22 +02001931 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001933 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001934
Michal Vasko004d3152020-06-11 19:59:22 +02001935 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936 LY_CHECK_RET(rc);
1937
Michal Vasko004d3152020-06-11 19:59:22 +02001938 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001939 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001940 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941
1942 return LY_SUCCESS;
1943}
1944
1945/**
1946 * @brief Reparse RelativeLocationPath. Logs directly on error.
1947 *
1948 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1949 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1950 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1951 *
1952 * @param[in] ctx Context for logging.
1953 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001954 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1956 */
1957static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001958reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959{
1960 LY_ERR rc;
1961
Michal Vasko004d3152020-06-11 19:59:22 +02001962 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001963 LY_CHECK_RET(rc);
1964
1965 goto step;
1966 do {
1967 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001968 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001969
Michal Vasko004d3152020-06-11 19:59:22 +02001970 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001971 LY_CHECK_RET(rc);
1972step:
1973 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001974 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001976 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977 break;
1978
1979 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001980 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981 break;
1982
1983 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001984 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985
Michal Vasko004d3152020-06-11 19:59:22 +02001986 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001988 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02001990 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991 return LY_EVALID;
1992 }
Radek Krejci0f969882020-08-21 16:56:47 +02001993 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001995 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001996 goto reparse_predicate;
1997 break;
1998
1999 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002000 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001
2002 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002003 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004 LY_CHECK_RET(rc);
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_PAR2);
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
2012reparse_predicate:
2013 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002014 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2015 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016 LY_CHECK_RET(rc);
2017 }
2018 break;
2019 default:
2020 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002021 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 return LY_EVALID;
2023 }
Michal Vasko004d3152020-06-11 19:59:22 +02002024 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025
2026 return LY_SUCCESS;
2027}
2028
2029/**
2030 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2031 *
2032 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2033 *
2034 * @param[in] ctx Context for logging.
2035 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002036 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 * @return LY_ERR
2038 */
2039static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002040reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041{
2042 LY_ERR rc;
2043
Michal Vasko004d3152020-06-11 19:59:22 +02002044 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 +02002045
2046 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002047 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002048 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002049 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050
Michal Vasko004d3152020-06-11 19:59:22 +02002051 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 return LY_SUCCESS;
2053 }
Michal Vasko004d3152020-06-11 19:59:22 +02002054 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 case LYXP_TOKEN_DOT:
2056 case LYXP_TOKEN_DDOT:
2057 case LYXP_TOKEN_AT:
2058 case LYXP_TOKEN_NAMETEST:
2059 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002060 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002062 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 default:
2064 break;
2065 }
2066
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002068 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002069 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070
Michal Vasko004d3152020-06-11 19:59:22 +02002071 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072 LY_CHECK_RET(rc);
2073 }
2074
2075 return LY_SUCCESS;
2076}
2077
2078/**
2079 * @brief Reparse FunctionCall. Logs directly on error.
2080 *
2081 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2082 *
2083 * @param[in] ctx Context for logging.
2084 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002085 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002086 * @return LY_ERR
2087 */
2088static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002089reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002091 int8_t min_arg_count = -1;
2092 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002093 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 LY_ERR rc;
2095
Michal Vasko004d3152020-06-11 19:59:22 +02002096 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002098 func_tok_idx = *tok_idx;
2099 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002101 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 min_arg_count = 1;
2103 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002104 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 min_arg_count = 1;
2106 max_arg_count = 1;
2107 }
2108 break;
2109 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002110 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 min_arg_count = 1;
2112 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002113 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 min_arg_count = 0;
2115 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002116 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 min_arg_count = 0;
2118 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002119 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 0;
2121 max_arg_count = 0;
2122 }
2123 break;
2124 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002125 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 min_arg_count = 1;
2127 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002128 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 0;
2130 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002131 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 min_arg_count = 1;
2133 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 1;
2136 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 1;
2139 max_arg_count = 1;
2140 }
2141 break;
2142 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002143 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002145 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002146 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 0;
2148 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002149 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 0;
2151 max_arg_count = 1;
2152 }
2153 break;
2154 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002155 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156 min_arg_count = 1;
2157 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002158 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 1;
2160 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 0;
2163 max_arg_count = 0;
2164 }
2165 break;
2166 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002167 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 2;
2169 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002170 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 0;
2172 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002173 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 2;
2175 max_arg_count = 2;
2176 }
2177 break;
2178 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002179 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 2;
2181 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002182 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 3;
2184 max_arg_count = 3;
2185 }
2186 break;
2187 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002188 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 min_arg_count = 0;
2190 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002191 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 1;
2193 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002194 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 2;
2196 max_arg_count = 2;
2197 }
2198 break;
2199 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002200 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002201 min_arg_count = 2;
2202 max_arg_count = 2;
2203 }
2204 break;
2205 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002206 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 2;
2208 max_arg_count = 2;
2209 }
2210 break;
2211 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002212 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 min_arg_count = 0;
2214 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002215 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 0;
2217 max_arg_count = 1;
2218 }
2219 break;
2220 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002221 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 0;
2223 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002224 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 min_arg_count = 2;
2226 max_arg_count = 2;
2227 }
2228 break;
2229 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002230 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 2;
2232 max_arg_count = 2;
2233 }
2234 break;
2235 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 min_arg_count = 2;
2238 max_arg_count = 2;
2239 }
2240 break;
2241 }
2242 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002243 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 +02002244 return LY_EINVAL;
2245 }
Michal Vasko004d3152020-06-11 19:59:22 +02002246 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247
2248 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002249 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002251 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252
2253 /* ( Expr ( ',' Expr )* )? */
2254 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002255 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002257 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002258 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002259 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 LY_CHECK_RET(rc);
2261 }
Michal Vasko004d3152020-06-11 19:59:22 +02002262 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2263 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264
2265 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002266 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267 LY_CHECK_RET(rc);
2268 }
2269
2270 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002271 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002273 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274
Radek Krejci857189e2020-09-01 13:26:36 +02002275 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002276 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 +02002277 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 return LY_EVALID;
2279 }
2280
2281 return LY_SUCCESS;
2282}
2283
2284/**
2285 * @brief Reparse PathExpr. Logs directly on error.
2286 *
2287 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2288 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2289 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2290 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2291 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2292 *
2293 * @param[in] ctx Context for logging.
2294 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002295 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 * @return LY_ERR
2297 */
2298static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002299reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300{
2301 LY_ERR rc;
2302
Michal Vasko004d3152020-06-11 19:59:22 +02002303 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002304 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 }
2306
Michal Vasko004d3152020-06-11 19:59:22 +02002307 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 case LYXP_TOKEN_PAR1:
2309 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002310 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311
Michal Vasko004d3152020-06-11 19:59:22 +02002312 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 LY_CHECK_RET(rc);
2314
Michal Vasko004d3152020-06-11 19:59:22 +02002315 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002317 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 goto predicate;
2319 break;
2320 case LYXP_TOKEN_DOT:
2321 case LYXP_TOKEN_DDOT:
2322 case LYXP_TOKEN_AT:
2323 case LYXP_TOKEN_NAMETEST:
2324 case LYXP_TOKEN_NODETYPE:
2325 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002326 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002327 LY_CHECK_RET(rc);
2328 break;
2329 case LYXP_TOKEN_FUNCNAME:
2330 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002331 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 LY_CHECK_RET(rc);
2333 goto predicate;
2334 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002335 case LYXP_TOKEN_OPER_PATH:
2336 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002338 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 LY_CHECK_RET(rc);
2340 break;
2341 case LYXP_TOKEN_LITERAL:
2342 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002343 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 goto predicate;
2345 break;
2346 case LYXP_TOKEN_NUMBER:
2347 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002348 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349 goto predicate;
2350 break;
2351 default:
2352 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002353 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 return LY_EVALID;
2355 }
2356
2357 return LY_SUCCESS;
2358
2359predicate:
2360 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002361 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2362 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
2364 }
2365
2366 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002367 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368
2369 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002370 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371
Michal Vasko004d3152020-06-11 19:59:22 +02002372 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
2374 }
2375
2376 return LY_SUCCESS;
2377}
2378
2379/**
2380 * @brief Reparse UnaryExpr. Logs directly on error.
2381 *
2382 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2383 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2384 *
2385 * @param[in] ctx Context for logging.
2386 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002387 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002388 * @return LY_ERR
2389 */
2390static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002391reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392{
2393 uint16_t prev_exp;
2394 LY_ERR rc;
2395
2396 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002397 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002398 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2399 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002400 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002401 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 }
2403
2404 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002405 prev_exp = *tok_idx;
2406 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002407 LY_CHECK_RET(rc);
2408
2409 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002410 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002412 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413
Michal Vasko004d3152020-06-11 19:59:22 +02002414 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 LY_CHECK_RET(rc);
2416 }
2417
2418 return LY_SUCCESS;
2419}
2420
2421/**
2422 * @brief Reparse AdditiveExpr. Logs directly on error.
2423 *
2424 * [15] AdditiveExpr ::= MultiplicativeExpr
2425 * | AdditiveExpr '+' MultiplicativeExpr
2426 * | AdditiveExpr '-' MultiplicativeExpr
2427 * [16] MultiplicativeExpr ::= UnaryExpr
2428 * | MultiplicativeExpr '*' UnaryExpr
2429 * | MultiplicativeExpr 'div' UnaryExpr
2430 * | MultiplicativeExpr 'mod' UnaryExpr
2431 *
2432 * @param[in] ctx Context for logging.
2433 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002434 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 * @return LY_ERR
2436 */
2437static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002438reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439{
2440 uint16_t prev_add_exp, prev_mul_exp;
2441 LY_ERR rc;
2442
Michal Vasko004d3152020-06-11 19:59:22 +02002443 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002444 goto reparse_multiplicative_expr;
2445
2446 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002447 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2448 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002450 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002451
2452reparse_multiplicative_expr:
2453 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002454 prev_mul_exp = *tok_idx;
2455 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002456 LY_CHECK_RET(rc);
2457
2458 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002459 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2460 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002462 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463
Michal Vasko004d3152020-06-11 19:59:22 +02002464 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 LY_CHECK_RET(rc);
2466 }
2467 }
2468
2469 return LY_SUCCESS;
2470}
2471
2472/**
2473 * @brief Reparse EqualityExpr. Logs directly on error.
2474 *
2475 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2476 * | EqualityExpr '!=' RelationalExpr
2477 * [14] RelationalExpr ::= AdditiveExpr
2478 * | RelationalExpr '<' AdditiveExpr
2479 * | RelationalExpr '>' AdditiveExpr
2480 * | RelationalExpr '<=' AdditiveExpr
2481 * | RelationalExpr '>=' AdditiveExpr
2482 *
2483 * @param[in] ctx Context for logging.
2484 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002485 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 * @return LY_ERR
2487 */
2488static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002489reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490{
2491 uint16_t prev_eq_exp, prev_rel_exp;
2492 LY_ERR rc;
2493
Michal Vasko004d3152020-06-11 19:59:22 +02002494 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002495 goto reparse_additive_expr;
2496
2497 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002498 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002500 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501
2502reparse_additive_expr:
2503 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002504 prev_rel_exp = *tok_idx;
2505 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506 LY_CHECK_RET(rc);
2507
2508 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002509 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002511 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512
Michal Vasko004d3152020-06-11 19:59:22 +02002513 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 LY_CHECK_RET(rc);
2515 }
2516 }
2517
2518 return LY_SUCCESS;
2519}
2520
2521/**
2522 * @brief Reparse OrExpr. Logs directly on error.
2523 *
2524 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2525 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2526 *
2527 * @param[in] ctx Context for logging.
2528 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002529 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002530 * @return LY_ERR
2531 */
2532static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002533reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002534{
2535 uint16_t prev_or_exp, prev_and_exp;
2536 LY_ERR rc;
2537
Michal Vasko004d3152020-06-11 19:59:22 +02002538 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002539 goto reparse_equality_expr;
2540
2541 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002542 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 +02002543 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002544 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002545
2546reparse_equality_expr:
2547 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002548 prev_and_exp = *tok_idx;
2549 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550 LY_CHECK_RET(rc);
2551
2552 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002553 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 +02002554 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002555 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556
Michal Vasko004d3152020-06-11 19:59:22 +02002557 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002558 LY_CHECK_RET(rc);
2559 }
2560 }
2561
2562 return LY_SUCCESS;
2563}
Radek Krejcib1646a92018-11-02 16:08:26 +01002564
2565/**
2566 * @brief Parse NCName.
2567 *
2568 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002569 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002570 */
Radek Krejcid4270262019-01-07 15:07:25 +01002571static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002572parse_ncname(const char *ncname)
2573{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002574 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002575 size_t size;
2576 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002577
2578 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2579 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2580 return len;
2581 }
2582
2583 do {
2584 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002585 if (!*ncname) {
2586 break;
2587 }
Radek Krejcid4270262019-01-07 15:07:25 +01002588 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002589 } while (is_xmlqnamechar(uc) && (uc != ':'));
2590
2591 return len;
2592}
2593
2594/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002595 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002596 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002597 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002598 * @param[in] exp Expression to use.
2599 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002600 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002601 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002603 */
2604static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002605exp_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 +01002606{
2607 uint32_t prev;
2608
2609 if (exp->used == exp->size) {
2610 prev = exp->size;
2611 exp->size += LYXP_EXPR_SIZE_STEP;
2612 if (prev > exp->size) {
2613 LOGINT(ctx);
2614 return LY_EINT;
2615 }
2616
2617 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2618 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2619 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2620 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2621 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2622 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2623 }
2624
2625 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002626 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002627 exp->tok_len[exp->used] = tok_len;
2628 ++exp->used;
2629 return LY_SUCCESS;
2630}
2631
2632void
Michal Vasko14676352020-05-29 11:35:55 +02002633lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002634{
2635 uint16_t i;
2636
2637 if (!expr) {
2638 return;
2639 }
2640
2641 lydict_remove(ctx, expr->expr);
2642 free(expr->tokens);
2643 free(expr->tok_pos);
2644 free(expr->tok_len);
2645 if (expr->repeat) {
2646 for (i = 0; i < expr->used; ++i) {
2647 free(expr->repeat[i]);
2648 }
2649 }
2650 free(expr->repeat);
2651 free(expr);
2652}
2653
Radek Krejcif03a9e22020-09-18 20:09:31 +02002654LY_ERR
2655lyxp_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 +01002656{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002657 LY_ERR ret = LY_SUCCESS;
2658 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002659 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002660 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002661 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002662 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002663
Radek Krejcif03a9e22020-09-18 20:09:31 +02002664 assert(expr_p);
2665
2666 if (!expr_str[0]) {
Michal Vasko004d3152020-06-11 19:59:22 +02002667 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002668 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002669 }
2670
2671 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002672 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002673 }
2674 if (expr_len > UINT16_MAX) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002675 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2676 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002677 }
2678
2679 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002680 expr = calloc(1, sizeof *expr);
2681 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2682 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2683 expr->used = 0;
2684 expr->size = LYXP_EXPR_SIZE_START;
2685 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2686 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002687
Radek Krejcif03a9e22020-09-18 20:09:31 +02002688 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2689 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002690
Radek Krejcif03a9e22020-09-18 20:09:31 +02002691 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2692 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002693
Michal Vasko004d3152020-06-11 19:59:22 +02002694 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002695 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002696
Radek Krejcif03a9e22020-09-18 20:09:31 +02002697 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002698 ++parsed;
2699 }
2700
2701 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002702 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002703
2704 /* '(' */
2705 tok_len = 1;
2706 tok_type = LYXP_TOKEN_PAR1;
2707
Radek Krejcif03a9e22020-09-18 20:09:31 +02002708 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002709 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002710 if (((expr->tok_len[expr->used - 1] == 4) &&
2711 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2712 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2713 ((expr->tok_len[expr->used - 1] == 7) &&
2714 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002715 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002716 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002717 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002718 }
2719 prev_function_check = 0;
2720 }
2721
Radek Krejcif03a9e22020-09-18 20:09:31 +02002722 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002723
2724 /* ')' */
2725 tok_len = 1;
2726 tok_type = LYXP_TOKEN_PAR2;
2727
Radek Krejcif03a9e22020-09-18 20:09:31 +02002728 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002729
2730 /* '[' */
2731 tok_len = 1;
2732 tok_type = LYXP_TOKEN_BRACK1;
2733
Radek Krejcif03a9e22020-09-18 20:09:31 +02002734 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002735
2736 /* ']' */
2737 tok_len = 1;
2738 tok_type = LYXP_TOKEN_BRACK2;
2739
Radek Krejcif03a9e22020-09-18 20:09:31 +02002740 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002741
2742 /* '..' */
2743 tok_len = 2;
2744 tok_type = LYXP_TOKEN_DDOT;
2745
Radek Krejcif03a9e22020-09-18 20:09:31 +02002746 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002747
2748 /* '.' */
2749 tok_len = 1;
2750 tok_type = LYXP_TOKEN_DOT;
2751
Radek Krejcif03a9e22020-09-18 20:09:31 +02002752 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002753
2754 /* '@' */
2755 tok_len = 1;
2756 tok_type = LYXP_TOKEN_AT;
2757
Radek Krejcif03a9e22020-09-18 20:09:31 +02002758 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002759
2760 /* ',' */
2761 tok_len = 1;
2762 tok_type = LYXP_TOKEN_COMMA;
2763
Radek Krejcif03a9e22020-09-18 20:09:31 +02002764 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002765
2766 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002767 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2768 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002769 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2770 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002771 ++tok_len;
2772 tok_type = LYXP_TOKEN_LITERAL;
2773
Radek Krejcif03a9e22020-09-18 20:09:31 +02002774 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002775
2776 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002777 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2778 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002779 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2780 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002781 ++tok_len;
2782 tok_type = LYXP_TOKEN_LITERAL;
2783
Radek Krejcif03a9e22020-09-18 20:09:31 +02002784 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002785
2786 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002787 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2788 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002789 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002790 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002791 }
2792 tok_type = LYXP_TOKEN_NUMBER;
2793
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002795
2796 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002797 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002798 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002799 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002800 } else {
2801 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002802 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002803 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002804
Radek Krejcif03a9e22020-09-18 20:09:31 +02002805 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002806
Michal Vasko3e48bf32020-06-01 08:39:07 +02002807 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002808 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002809 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2810
Radek Krejcif03a9e22020-09-18 20:09:31 +02002811 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002812
2813 /* Operator '<=', '>=' */
2814 tok_len = 2;
2815 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002816
Radek Krejcif03a9e22020-09-18 20:09:31 +02002817 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002818
2819 /* Operator '|' */
2820 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002821 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002822
Radek Krejcif03a9e22020-09-18 20:09:31 +02002823 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002824
2825 /* Operator '+', '-' */
2826 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002827 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002828
Radek Krejcif03a9e22020-09-18 20:09:31 +02002829 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002830
Michal Vasko3e48bf32020-06-01 08:39:07 +02002831 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002832 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002833 tok_type = LYXP_TOKEN_OPER_EQUAL;
2834
Radek Krejcif03a9e22020-09-18 20:09:31 +02002835 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002836
2837 /* Operator '<', '>' */
2838 tok_len = 1;
2839 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002840
Michal Vasko69730152020-10-09 16:30:07 +02002841 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2842 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2843 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2844 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2845 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2846 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2850 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2851 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2852 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002853
2854 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002855 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002856 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002857 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002860 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002862
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002864 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002865 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002866
Radek Krejcif03a9e22020-09-18 20:09:31 +02002867 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002868 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002869 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
2871 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002872 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 +02002873 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 +02002874 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002875 goto error;
2876 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
2878 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002879 goto error;
2880 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002881 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
2883 /* NameTest '*' */
2884 tok_len = 1;
2885 tok_type = LYXP_TOKEN_NAMETEST;
2886
2887 } else {
2888
2889 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002890 long int ncname_len = parse_ncname(&expr_str[parsed]);
2891 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002892 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2893 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002894 tok_len = ncname_len;
2895
Radek Krejcif03a9e22020-09-18 20:09:31 +02002896 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002897 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002898 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002899 ++tok_len;
2900 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002901 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2902 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002903 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2904 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002905 tok_len += ncname_len;
2906 }
2907 /* remove old flag to prevent ambiguities */
2908 prev_function_check = 0;
2909 tok_type = LYXP_TOKEN_NAMETEST;
2910 } else {
2911 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2912 prev_function_check = 1;
2913 tok_type = LYXP_TOKEN_NAMETEST;
2914 }
2915 }
2916
2917 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002918 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002919 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002920 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002921 ++parsed;
2922 }
2923
Radek Krejcif03a9e22020-09-18 20:09:31 +02002924 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002925
Michal Vasko004d3152020-06-11 19:59:22 +02002926 if (reparse) {
2927 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2929 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002930
Michal Vasko004d3152020-06-11 19:59:22 +02002931 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002932 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2933 if (expr->used > tok_idx) {
Michal Vasko004d3152020-06-11 19:59:22 +02002934 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 +02002935 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002936 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002937 goto error;
2938 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002939 }
2940
Radek Krejcif03a9e22020-09-18 20:09:31 +02002941 print_expr_struct_debug(expr);
2942 *expr_p = expr;
2943 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002944
2945error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 lyxp_expr_free(ctx, expr);
2947 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002948}
2949
Michal Vasko1734be92020-09-22 08:55:10 +02002950LY_ERR
2951lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002952{
Michal Vasko1734be92020-09-22 08:55:10 +02002953 LY_ERR ret = LY_SUCCESS;
2954 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002955 uint32_t i, j;
2956
Michal Vasko7f45cf22020-10-01 12:49:44 +02002957 if (!exp) {
2958 goto cleanup;
2959 }
2960
Michal Vasko004d3152020-06-11 19:59:22 +02002961 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002962 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002963
2964 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002965 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002966 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2967
2968 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002969 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002970 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2971
2972 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002973 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002974 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2975
2976 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002977 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002978 for (i = 0; i < exp->used; ++i) {
2979 if (!exp->repeat[i]) {
2980 dup->repeat[i] = NULL;
2981 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002982 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002983 /* the ending 0 as well */
2984 ++j;
2985
Michal Vasko99c71642020-07-03 13:33:36 +02002986 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002987 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002988 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2989 dup->repeat[i][j - 1] = 0;
2990 }
2991 }
2992
2993 dup->used = exp->used;
2994 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02002995 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002996
Michal Vasko1734be92020-09-22 08:55:10 +02002997cleanup:
2998 if (ret) {
2999 lyxp_expr_free(ctx, dup);
3000 } else {
3001 *dup_p = dup;
3002 }
3003 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003004}
3005
Michal Vasko03ff5a72019-09-11 13:49:33 +02003006/*
3007 * warn functions
3008 *
3009 * Warn functions check specific reasonable conditions for schema XPath
3010 * and print a warning if they are not satisfied.
3011 */
3012
3013/**
3014 * @brief Get the last-added schema node that is currently in the context.
3015 *
3016 * @param[in] set Set to search in.
3017 * @return Last-added schema context node, NULL if no node is in context.
3018 */
3019static struct lysc_node *
3020warn_get_scnode_in_ctx(struct lyxp_set *set)
3021{
3022 uint32_t i;
3023
3024 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3025 return NULL;
3026 }
3027
3028 i = set->used;
3029 do {
3030 --i;
3031 if (set->val.scnodes[i].in_ctx == 1) {
3032 /* if there are more, simply return the first found (last added) */
3033 return set->val.scnodes[i].scnode;
3034 }
3035 } while (i);
3036
3037 return NULL;
3038}
3039
3040/**
3041 * @brief Test whether a type is numeric - integer type or decimal64.
3042 *
3043 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003044 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003045 */
Radek Krejci857189e2020-09-01 13:26:36 +02003046static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003047warn_is_numeric_type(struct lysc_type *type)
3048{
3049 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003050 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003051 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003052
3053 switch (type->basetype) {
3054 case LY_TYPE_DEC64:
3055 case LY_TYPE_INT8:
3056 case LY_TYPE_UINT8:
3057 case LY_TYPE_INT16:
3058 case LY_TYPE_UINT16:
3059 case LY_TYPE_INT32:
3060 case LY_TYPE_UINT32:
3061 case LY_TYPE_INT64:
3062 case LY_TYPE_UINT64:
3063 return 1;
3064 case LY_TYPE_UNION:
3065 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003066 LY_ARRAY_FOR(uni->types, u) {
3067 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003068 if (ret) {
3069 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003070 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003071 }
3072 }
3073 /* did not find any suitable type */
3074 return 0;
3075 case LY_TYPE_LEAFREF:
3076 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3077 default:
3078 return 0;
3079 }
3080}
3081
3082/**
3083 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3084 *
3085 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003086 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003087 */
Radek Krejci857189e2020-09-01 13:26:36 +02003088static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003089warn_is_string_type(struct lysc_type *type)
3090{
3091 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003092 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003093 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003094
3095 switch (type->basetype) {
3096 case LY_TYPE_BITS:
3097 case LY_TYPE_ENUM:
3098 case LY_TYPE_IDENT:
3099 case LY_TYPE_INST:
3100 case LY_TYPE_STRING:
3101 return 1;
3102 case LY_TYPE_UNION:
3103 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003104 LY_ARRAY_FOR(uni->types, u) {
3105 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003106 if (ret) {
3107 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003108 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003109 }
3110 }
3111 /* did not find any suitable type */
3112 return 0;
3113 case LY_TYPE_LEAFREF:
3114 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3115 default:
3116 return 0;
3117 }
3118}
3119
3120/**
3121 * @brief Test whether a type is one specific type.
3122 *
3123 * @param[in] type Type to test.
3124 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003125 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003126 */
Radek Krejci857189e2020-09-01 13:26:36 +02003127static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003128warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3129{
3130 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003131 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003132 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003133
3134 if (type->basetype == base) {
3135 return 1;
3136 } else if (type->basetype == LY_TYPE_UNION) {
3137 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003138 LY_ARRAY_FOR(uni->types, u) {
3139 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003140 if (ret) {
3141 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003142 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003143 }
3144 }
3145 /* did not find any suitable type */
3146 return 0;
3147 } else if (type->basetype == LY_TYPE_LEAFREF) {
3148 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3149 }
3150
3151 return 0;
3152}
3153
3154/**
3155 * @brief Get next type of a (union) type.
3156 *
3157 * @param[in] type Base type.
3158 * @param[in] prev_type Previously returned type.
3159 * @return Next type or NULL.
3160 */
3161static struct lysc_type *
3162warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3163{
3164 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003165 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003166 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003167
3168 switch (type->basetype) {
3169 case LY_TYPE_UNION:
3170 uni = (struct lysc_type_union *)type;
3171 if (!prev_type) {
3172 return uni->types[0];
3173 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003174 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003175 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003176 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003177 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003178 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003179 found = 1;
3180 }
3181 }
3182 return NULL;
3183 default:
3184 if (prev_type) {
3185 assert(type == prev_type);
3186 return NULL;
3187 } else {
3188 return type;
3189 }
3190 }
3191}
3192
3193/**
3194 * @brief Test whether 2 types have a common type.
3195 *
3196 * @param[in] type1 First type.
3197 * @param[in] type2 Second type.
3198 * @return 1 if they do, 0 otherwise.
3199 */
3200static int
3201warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3202{
3203 struct lysc_type *t1, *rt1, *t2, *rt2;
3204
3205 t1 = NULL;
3206 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3207 if (t1->basetype == LY_TYPE_LEAFREF) {
3208 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3209 } else {
3210 rt1 = t1;
3211 }
3212
3213 t2 = NULL;
3214 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3215 if (t2->basetype == LY_TYPE_LEAFREF) {
3216 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3217 } else {
3218 rt2 = t2;
3219 }
3220
3221 if (rt2->basetype == rt1->basetype) {
3222 /* match found */
3223 return 1;
3224 }
3225 }
3226 }
3227
3228 return 0;
3229}
3230
3231/**
3232 * @brief Check both operands of comparison operators.
3233 *
3234 * @param[in] ctx Context for errors.
3235 * @param[in] set1 First operand set.
3236 * @param[in] set2 Second operand set.
3237 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3238 * @param[in] expr Start of the expression to print with the warning.
3239 * @param[in] tok_pos Token position.
3240 */
3241static void
Radek Krejci857189e2020-09-01 13:26:36 +02003242warn_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 +02003243{
3244 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003245 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003246
3247 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3248 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3249
3250 if (!node1 && !node2) {
3251 /* no node-sets involved, nothing to do */
3252 return;
3253 }
3254
3255 if (node1) {
3256 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3257 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3258 warning = 1;
3259 leaves = 0;
3260 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3261 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3262 warning = 1;
3263 }
3264 }
3265
3266 if (node2) {
3267 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3268 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3269 warning = 1;
3270 leaves = 0;
3271 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3272 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3273 warning = 1;
3274 }
3275 }
3276
3277 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003278 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3279 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3280 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3281 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003282 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3283 warning = 1;
3284 }
3285 }
3286
3287 if (warning) {
3288 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3289 }
3290}
3291
3292/**
3293 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3294 *
3295 * @param[in] exp Parsed XPath expression.
3296 * @param[in] set Set with the leaf/leaf-list.
3297 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3298 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3299 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3300 */
3301static void
Michal Vasko40308e72020-10-20 16:38:40 +02003302warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3303 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003304{
3305 struct lysc_node *scnode;
3306 struct lysc_type *type;
3307 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003308 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309 LY_ERR rc;
3310 struct ly_err_item *err = NULL;
3311
Michal Vasko69730152020-10-09 16:30:07 +02003312 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3313 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003314 /* check that the node can have the specified value */
3315 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3316 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3317 } else {
3318 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3319 }
3320 if (!value) {
3321 LOGMEM(set->ctx);
3322 return;
3323 }
3324
3325 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3326 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003327 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003328 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003329 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003330 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331 }
3332
3333 type = ((struct lysc_node_leaf *)scnode)->type;
3334 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003335 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003336 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003337 if (rc == LY_EINCOMPLETE) {
3338 rc = LY_SUCCESS;
3339 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003340
3341 if (err) {
3342 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3343 ly_err_free(err);
3344 } else if (rc != LY_SUCCESS) {
3345 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3346 }
3347 if (rc != LY_SUCCESS) {
3348 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003349 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003350 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003351 } else {
3352 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003353 }
3354 }
3355 free(value);
3356 }
3357}
3358
3359/*
3360 * XPath functions
3361 */
3362
3363/**
3364 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3365 * depending on whether the first node bit value from the second argument is set.
3366 *
3367 * @param[in] args Array of arguments.
3368 * @param[in] arg_count Count of elements in @p args.
3369 * @param[in,out] set Context and result set at the same time.
3370 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003371 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003372 */
3373static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003374xpath_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 +02003375{
3376 struct lyd_node_term *leaf;
3377 struct lysc_node_leaf *sleaf;
3378 struct lysc_type_bits *bits;
3379 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003380 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381
3382 if (options & LYXP_SCNODE_ALL) {
3383 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3384 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003385 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3386 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 +02003387 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3388 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003389 }
3390
3391 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3392 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3393 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 +02003394 } else if (!warn_is_string_type(sleaf->type)) {
3395 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003396 }
3397 }
3398 set_scnode_clear_ctx(set);
3399 return rc;
3400 }
3401
Michal Vaskod3678892020-05-21 10:06:58 +02003402 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003403 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3404 "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 return LY_EVALID;
3406 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003407 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003408 LY_CHECK_RET(rc);
3409
3410 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003411 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003413 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3414 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003415 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003416 LY_ARRAY_FOR(bits->bits, u) {
3417 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003418 set_fill_boolean(set, 1);
3419 break;
3420 }
3421 }
3422 }
3423 }
3424
3425 return LY_SUCCESS;
3426}
3427
3428/**
3429 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3430 * with the argument converted to boolean.
3431 *
3432 * @param[in] args Array of arguments.
3433 * @param[in] arg_count Count of elements in @p args.
3434 * @param[in,out] set Context and result set at the same time.
3435 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003436 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003437 */
3438static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003439xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003440{
3441 LY_ERR rc;
3442
3443 if (options & LYXP_SCNODE_ALL) {
3444 set_scnode_clear_ctx(set);
3445 return LY_SUCCESS;
3446 }
3447
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003448 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003449 LY_CHECK_RET(rc);
3450 set_fill_set(set, args[0]);
3451
3452 return LY_SUCCESS;
3453}
3454
3455/**
3456 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3457 * with the first argument rounded up to the nearest integer.
3458 *
3459 * @param[in] args Array of arguments.
3460 * @param[in] arg_count Count of elements in @p args.
3461 * @param[in,out] set Context and result set at the same time.
3462 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003463 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003464 */
3465static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003466xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003467{
3468 struct lysc_node_leaf *sleaf;
3469 LY_ERR rc = LY_SUCCESS;
3470
3471 if (options & LYXP_SCNODE_ALL) {
3472 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3473 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003474 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3475 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 +02003476 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3477 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003478 }
3479 set_scnode_clear_ctx(set);
3480 return rc;
3481 }
3482
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003483 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003484 LY_CHECK_RET(rc);
3485 if ((long long)args[0]->val.num != args[0]->val.num) {
3486 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3487 } else {
3488 set_fill_number(set, args[0]->val.num);
3489 }
3490
3491 return LY_SUCCESS;
3492}
3493
3494/**
3495 * @brief Execute the XPath concat(string, string, string*) function.
3496 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3497 *
3498 * @param[in] args Array of arguments.
3499 * @param[in] arg_count Count of elements in @p args.
3500 * @param[in,out] set Context and result set at the same time.
3501 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003502 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003503 */
3504static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003505xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003506{
3507 uint16_t i;
3508 char *str = NULL;
3509 size_t used = 1;
3510 LY_ERR rc = LY_SUCCESS;
3511 struct lysc_node_leaf *sleaf;
3512
3513 if (options & LYXP_SCNODE_ALL) {
3514 for (i = 0; i < arg_count; ++i) {
3515 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3516 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3517 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003518 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003519 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003520 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 +02003521 }
3522 }
3523 }
3524 set_scnode_clear_ctx(set);
3525 return rc;
3526 }
3527
3528 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003529 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003530 if (rc != LY_SUCCESS) {
3531 free(str);
3532 return rc;
3533 }
3534
3535 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3536 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3537 strcpy(str + used - 1, args[i]->val.str);
3538 used += strlen(args[i]->val.str);
3539 }
3540
3541 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003542 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003543 set->type = LYXP_SET_STRING;
3544 set->val.str = str;
3545
3546 return LY_SUCCESS;
3547}
3548
3549/**
3550 * @brief Execute the XPath contains(string, string) function.
3551 * Returns LYXP_SET_BOOLEAN whether the second argument can
3552 * be found in the first or not.
3553 *
3554 * @param[in] args Array of arguments.
3555 * @param[in] arg_count Count of elements in @p args.
3556 * @param[in,out] set Context and result set at the same time.
3557 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003558 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003559 */
3560static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003561xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003562{
3563 struct lysc_node_leaf *sleaf;
3564 LY_ERR rc = LY_SUCCESS;
3565
3566 if (options & LYXP_SCNODE_ALL) {
3567 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3568 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3569 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 +02003570 } else if (!warn_is_string_type(sleaf->type)) {
3571 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003572 }
3573 }
3574
3575 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3576 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3577 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 +02003578 } else if (!warn_is_string_type(sleaf->type)) {
3579 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003580 }
3581 }
3582 set_scnode_clear_ctx(set);
3583 return rc;
3584 }
3585
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003586 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003587 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003588 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003589 LY_CHECK_RET(rc);
3590
3591 if (strstr(args[0]->val.str, args[1]->val.str)) {
3592 set_fill_boolean(set, 1);
3593 } else {
3594 set_fill_boolean(set, 0);
3595 }
3596
3597 return LY_SUCCESS;
3598}
3599
3600/**
3601 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3602 * with the size of the node-set from the argument.
3603 *
3604 * @param[in] args Array of arguments.
3605 * @param[in] arg_count Count of elements in @p args.
3606 * @param[in,out] set Context and result set at the same time.
3607 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003608 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003609 */
3610static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003611xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003612{
3613 struct lysc_node *scnode = NULL;
3614 LY_ERR rc = LY_SUCCESS;
3615
3616 if (options & LYXP_SCNODE_ALL) {
3617 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3618 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 }
3620 set_scnode_clear_ctx(set);
3621 return rc;
3622 }
3623
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003625 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 +02003626 return LY_EVALID;
3627 }
3628
3629 set_fill_number(set, args[0]->used);
3630 return LY_SUCCESS;
3631}
3632
3633/**
3634 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3635 * with the context with the intial node.
3636 *
3637 * @param[in] args Array of arguments.
3638 * @param[in] arg_count Count of elements in @p args.
3639 * @param[in,out] set Context and result set at the same time.
3640 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003641 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 */
3643static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003644xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645{
3646 if (arg_count || args) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003647 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648 return LY_EVALID;
3649 }
3650
3651 if (options & LYXP_SCNODE_ALL) {
3652 set_scnode_clear_ctx(set);
3653
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003654 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003656 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657
3658 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003659 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660 }
3661
3662 return LY_SUCCESS;
3663}
3664
3665/**
3666 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3667 * leafref or instance-identifier target node(s).
3668 *
3669 * @param[in] args Array of arguments.
3670 * @param[in] arg_count Count of elements in @p args.
3671 * @param[in,out] set Context and result set at the same time.
3672 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003673 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674 */
3675static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003676xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677{
3678 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003679 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003680 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003681 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003682 struct ly_path *p;
3683 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003684 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003685 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 LY_ERR rc = LY_SUCCESS;
3687
3688 if (options & LYXP_SCNODE_ALL) {
3689 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3690 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3692 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 +02003693 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3694 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003695 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003696 }
3697 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003698 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003699 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003700 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003701
3702 /* it was already evaluated on schema, it must succeed */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003703 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003704 LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003705 assert(!rc);
3706
3707 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003708 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003709 ly_path_free(set->ctx, p);
3710
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003711 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003712 }
3713
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 return rc;
3715 }
3716
Michal Vaskod3678892020-05-21 10:06:58 +02003717 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003718 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 +02003719 return LY_EVALID;
3720 }
3721
Michal Vaskod3678892020-05-21 10:06:58 +02003722 lyxp_set_free_content(set);
3723 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003724 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3725 sleaf = (struct lysc_node_leaf *)leaf->schema;
3726 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3727 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3728 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003729 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
Michal Vasko69730152020-10-09 16:30:07 +02003730 &leaf->value, set->tree, &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003731 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003732 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003733 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003735 } else {
3736 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003737 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003738 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003739 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003740 return LY_EVALID;
3741 }
3742 }
Michal Vasko004d3152020-06-11 19:59:22 +02003743
3744 /* insert it */
3745 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746 }
3747 }
3748
3749 return LY_SUCCESS;
3750}
3751
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003752static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003753xpath_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 +02003754{
3755 uint16_t i;
3756 LY_ARRAY_COUNT_TYPE u;
3757 struct lyd_node_term *leaf;
3758 struct lysc_node_leaf *sleaf;
3759 struct lyd_meta *meta;
3760 struct lyd_value data = {0}, *val;
3761 struct ly_err_item *err = NULL;
3762 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003763 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003764
3765 if (options & LYXP_SCNODE_ALL) {
3766 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3767 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3768 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3769 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003770 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003771 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3772 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3773 }
3774
3775 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3776 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3777 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003778 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003779 } else if (!warn_is_string_type(sleaf->type)) {
3780 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3781 }
3782 }
3783 set_scnode_clear_ctx(set);
3784 return rc;
3785 }
3786
3787 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003788 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 +02003789 "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003790 return LY_EVALID;
3791 }
3792 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3793 LY_CHECK_RET(rc);
3794
3795 set_fill_boolean(set, 0);
3796 found = 0;
3797 for (i = 0; i < args[0]->used; ++i) {
3798 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3799 continue;
3800 }
3801
3802 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3803 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3804 sleaf = (struct lysc_node_leaf *)leaf->schema;
3805 val = &leaf->value;
3806 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3807 /* uninteresting */
3808 continue;
3809 }
3810
3811 /* store args[1] as ident */
3812 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vasko405cc9e2020-12-01 12:01:27 +01003813 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003814 } else {
3815 meta = args[0]->val.meta[i].meta;
3816 val = &meta->value;
3817 if (val->realtype->basetype != LY_TYPE_IDENT) {
3818 /* uninteresting */
3819 continue;
3820 }
3821
3822 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003823 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003824 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003825 }
3826
3827 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003828 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003829 ly_err_free(err);
3830 }
3831 LY_CHECK_RET(rc);
3832
3833 /* finally check the identity itself */
3834 if (self_match && (data.ident == val->ident)) {
3835 set_fill_boolean(set, 1);
3836 found = 1;
3837 }
3838 if (!found) {
3839 LY_ARRAY_FOR(data.ident->derived, u) {
3840 if (data.ident->derived[u] == val->ident) {
3841 set_fill_boolean(set, 1);
3842 found = 1;
3843 break;
3844 }
3845 }
3846 }
3847
3848 /* free temporary value */
3849 val->realtype->plugin->free(set->ctx, &data);
3850 if (found) {
3851 break;
3852 }
3853 }
3854
3855 return LY_SUCCESS;
3856}
3857
Michal Vasko03ff5a72019-09-11 13:49:33 +02003858/**
3859 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3860 * on whether the first argument nodes contain a node of an identity derived from the second
3861 * argument identity.
3862 *
3863 * @param[in] args Array of arguments.
3864 * @param[in] arg_count Count of elements in @p args.
3865 * @param[in,out] set Context and result set at the same time.
3866 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003867 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003868 */
3869static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003870xpath_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 +02003871{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003872 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003873}
3874
3875/**
3876 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3877 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3878 * the second argument identity.
3879 *
3880 * @param[in] args Array of arguments.
3881 * @param[in] arg_count Count of elements in @p args.
3882 * @param[in,out] set Context and result set at the same time.
3883 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003884 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003885 */
3886static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003887xpath_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 +02003888{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003889 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003890}
3891
3892/**
3893 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3894 * with the integer value of the first node's enum value, otherwise NaN.
3895 *
3896 * @param[in] args Array of arguments.
3897 * @param[in] arg_count Count of elements in @p args.
3898 * @param[in,out] set Context and result set at the same time.
3899 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003900 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003901 */
3902static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003903xpath_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 +02003904{
3905 struct lyd_node_term *leaf;
3906 struct lysc_node_leaf *sleaf;
3907 LY_ERR rc = LY_SUCCESS;
3908
3909 if (options & LYXP_SCNODE_ALL) {
3910 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3911 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003912 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3913 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 +02003914 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3915 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003916 }
3917 set_scnode_clear_ctx(set);
3918 return rc;
3919 }
3920
Michal Vaskod3678892020-05-21 10:06:58 +02003921 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003922 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 +02003923 return LY_EVALID;
3924 }
3925
3926 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003927 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003928 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3929 sleaf = (struct lysc_node_leaf *)leaf->schema;
3930 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3931 set_fill_number(set, leaf->value.enum_item->value);
3932 }
3933 }
3934
3935 return LY_SUCCESS;
3936}
3937
3938/**
3939 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3940 * with false value.
3941 *
3942 * @param[in] args Array of arguments.
3943 * @param[in] arg_count Count of elements in @p args.
3944 * @param[in,out] set Context and result set at the same time.
3945 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003946 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003947 */
3948static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003949xpath_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 +02003950{
3951 if (options & LYXP_SCNODE_ALL) {
3952 set_scnode_clear_ctx(set);
3953 return LY_SUCCESS;
3954 }
3955
3956 set_fill_boolean(set, 0);
3957 return LY_SUCCESS;
3958}
3959
3960/**
3961 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3962 * with the first argument floored (truncated).
3963 *
3964 * @param[in] args Array of arguments.
3965 * @param[in] arg_count Count of elements in @p args.
3966 * @param[in,out] set Context and result set at the same time.
3967 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003968 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003969 */
3970static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003971xpath_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 +02003972{
3973 LY_ERR rc;
3974
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003975 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003976 LY_CHECK_RET(rc);
3977 if (isfinite(args[0]->val.num)) {
3978 set_fill_number(set, (long long)args[0]->val.num);
3979 }
3980
3981 return LY_SUCCESS;
3982}
3983
3984/**
3985 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3986 * whether the language of the text matches the one from the argument.
3987 *
3988 * @param[in] args Array of arguments.
3989 * @param[in] arg_count Count of elements in @p args.
3990 * @param[in,out] set Context and result set at the same time.
3991 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003992 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003993 */
3994static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003995xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996{
3997 const struct lyd_node *node;
3998 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003999 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004000 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004001 LY_ERR rc = LY_SUCCESS;
4002
4003 if (options & LYXP_SCNODE_ALL) {
4004 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4005 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4006 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 +02004007 } else if (!warn_is_string_type(sleaf->type)) {
4008 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004009 }
4010 }
4011 set_scnode_clear_ctx(set);
4012 return rc;
4013 }
4014
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004015 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004016 LY_CHECK_RET(rc);
4017
Michal Vasko03ff5a72019-09-11 13:49:33 +02004018 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004019 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 +02004020 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004021 } else if (!set->used) {
4022 set_fill_boolean(set, 0);
4023 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 }
4025
4026 switch (set->val.nodes[0].type) {
4027 case LYXP_NODE_ELEM:
4028 case LYXP_NODE_TEXT:
4029 node = set->val.nodes[0].node;
4030 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004031 case LYXP_NODE_META:
4032 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004033 break;
4034 default:
4035 /* nothing to do with roots */
4036 set_fill_boolean(set, 0);
4037 return LY_SUCCESS;
4038 }
4039
Michal Vasko9f96a052020-03-10 09:41:45 +01004040 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004041 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004042 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004043 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004044 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 break;
4046 }
4047 }
4048
Michal Vasko9f96a052020-03-10 09:41:45 +01004049 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 break;
4051 }
4052 }
4053
4054 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004055 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004056 set_fill_boolean(set, 0);
4057 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004058 uint64_t i;
4059
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004060 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004061 for (i = 0; args[0]->val.str[i]; ++i) {
4062 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4063 set_fill_boolean(set, 0);
4064 break;
4065 }
4066 }
4067 if (!args[0]->val.str[i]) {
4068 if (!val[i] || (val[i] == '-')) {
4069 set_fill_boolean(set, 1);
4070 } else {
4071 set_fill_boolean(set, 0);
4072 }
4073 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 }
4075
4076 return LY_SUCCESS;
4077}
4078
4079/**
4080 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4081 * with the context size.
4082 *
4083 * @param[in] args Array of arguments.
4084 * @param[in] arg_count Count of elements in @p args.
4085 * @param[in,out] set Context and result set at the same time.
4086 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004087 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004088 */
4089static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004090xpath_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 +02004091{
4092 if (options & LYXP_SCNODE_ALL) {
4093 set_scnode_clear_ctx(set);
4094 return LY_SUCCESS;
4095 }
4096
Michal Vasko03ff5a72019-09-11 13:49:33 +02004097 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004098 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 +02004099 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004100 } else if (!set->used) {
4101 set_fill_number(set, 0);
4102 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004103 }
4104
4105 set_fill_number(set, set->ctx_size);
4106 return LY_SUCCESS;
4107}
4108
4109/**
4110 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4111 * with the node name without namespace from the argument or the context.
4112 *
4113 * @param[in] args Array of arguments.
4114 * @param[in] arg_count Count of elements in @p args.
4115 * @param[in,out] set Context and result set at the same time.
4116 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004117 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004118 */
4119static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004120xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121{
4122 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004123
Michal Vasko03ff5a72019-09-11 13:49:33 +02004124 /* suppress unused variable warning */
4125 (void)options;
4126
4127 if (options & LYXP_SCNODE_ALL) {
4128 set_scnode_clear_ctx(set);
4129 return LY_SUCCESS;
4130 }
4131
4132 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004133 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004134 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4135 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004136 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004137 } else if (!args[0]->used) {
4138 set_fill_string(set, "", 0);
4139 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004140 }
4141
4142 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004143 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004144
4145 item = &args[0]->val.nodes[0];
4146 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004147 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004148 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 +02004149 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004150 } else if (!set->used) {
4151 set_fill_string(set, "", 0);
4152 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153 }
4154
4155 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004156 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157
4158 item = &set->val.nodes[0];
4159 }
4160
4161 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004162 case LYXP_NODE_NONE:
4163 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004164 case LYXP_NODE_ROOT:
4165 case LYXP_NODE_ROOT_CONFIG:
4166 case LYXP_NODE_TEXT:
4167 set_fill_string(set, "", 0);
4168 break;
4169 case LYXP_NODE_ELEM:
4170 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4171 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004172 case LYXP_NODE_META:
4173 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 break;
4175 }
4176
4177 return LY_SUCCESS;
4178}
4179
4180/**
4181 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4182 * with the node name fully qualified (with namespace) from the argument or the context.
4183 *
4184 * @param[in] args Array of arguments.
4185 * @param[in] arg_count Count of elements in @p args.
4186 * @param[in,out] set Context and result set at the same time.
4187 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004188 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189 */
4190static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004191xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004192{
4193 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004194 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004195 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004196 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197
4198 if (options & LYXP_SCNODE_ALL) {
4199 set_scnode_clear_ctx(set);
4200 return LY_SUCCESS;
4201 }
4202
4203 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004205 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 +02004206 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004207 } else if (!args[0]->used) {
4208 set_fill_string(set, "", 0);
4209 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210 }
4211
4212 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004213 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214
4215 item = &args[0]->val.nodes[0];
4216 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004218 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 +02004219 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004220 } else if (!set->used) {
4221 set_fill_string(set, "", 0);
4222 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 }
4224
4225 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004226 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004227
4228 item = &set->val.nodes[0];
4229 }
4230
4231 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004232 case LYXP_NODE_NONE:
4233 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004234 case LYXP_NODE_ROOT:
4235 case LYXP_NODE_ROOT_CONFIG:
4236 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004237 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004238 break;
4239 case LYXP_NODE_ELEM:
4240 mod = item->node->schema->module;
4241 name = item->node->schema->name;
4242 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004243 case LYXP_NODE_META:
4244 mod = ((struct lyd_meta *)item->node)->annotation->module;
4245 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004246 break;
4247 }
4248
4249 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004250 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004251 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4252 set_fill_string(set, str, strlen(str));
4253 free(str);
4254 } else {
4255 set_fill_string(set, "", 0);
4256 }
4257
4258 return LY_SUCCESS;
4259}
4260
4261/**
4262 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4263 * with the namespace of the node from the argument or the context.
4264 *
4265 * @param[in] args Array of arguments.
4266 * @param[in] arg_count Count of elements in @p args.
4267 * @param[in,out] set Context and result set at the same time.
4268 * @param[in] options XPath options.
4269 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4270 */
4271static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004272xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273{
4274 struct lyxp_set_node *item;
4275 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004276
Michal Vasko03ff5a72019-09-11 13:49:33 +02004277 /* suppress unused variable warning */
4278 (void)options;
4279
4280 if (options & LYXP_SCNODE_ALL) {
4281 set_scnode_clear_ctx(set);
4282 return LY_SUCCESS;
4283 }
4284
4285 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004286 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004287 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 +02004288 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004289 return LY_EVALID;
4290 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004291 set_fill_string(set, "", 0);
4292 return LY_SUCCESS;
4293 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294
4295 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004296 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004297
4298 item = &args[0]->val.nodes[0];
4299 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004301 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 +02004302 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004303 } else if (!set->used) {
4304 set_fill_string(set, "", 0);
4305 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004306 }
4307
4308 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004309 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004310
4311 item = &set->val.nodes[0];
4312 }
4313
4314 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004315 case LYXP_NODE_NONE:
4316 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004317 case LYXP_NODE_ROOT:
4318 case LYXP_NODE_ROOT_CONFIG:
4319 case LYXP_NODE_TEXT:
4320 set_fill_string(set, "", 0);
4321 break;
4322 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004323 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004324 if (item->type == LYXP_NODE_ELEM) {
4325 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004326 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004328 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329 }
4330
4331 set_fill_string(set, mod->ns, strlen(mod->ns));
4332 break;
4333 }
4334
4335 return LY_SUCCESS;
4336}
4337
4338/**
4339 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4340 * with only nodes from the context. In practice it either leaves the context
4341 * as it is or returns an empty node set.
4342 *
4343 * @param[in] args Array of arguments.
4344 * @param[in] arg_count Count of elements in @p args.
4345 * @param[in,out] set Context and result set at the same time.
4346 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004347 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004348 */
4349static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004350xpath_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 +02004351{
4352 if (options & LYXP_SCNODE_ALL) {
4353 set_scnode_clear_ctx(set);
4354 return LY_SUCCESS;
4355 }
4356
4357 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004358 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359 }
4360 return LY_SUCCESS;
4361}
4362
4363/**
4364 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4365 * with normalized value (no leading, trailing, double white spaces) of the node
4366 * from the argument or the context.
4367 *
4368 * @param[in] args Array of arguments.
4369 * @param[in] arg_count Count of elements in @p args.
4370 * @param[in,out] set Context and result set at the same time.
4371 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004372 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004373 */
4374static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004375xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004376{
4377 uint16_t i, new_used;
4378 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004379 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004380 struct lysc_node_leaf *sleaf;
4381 LY_ERR rc = LY_SUCCESS;
4382
4383 if (options & LYXP_SCNODE_ALL) {
4384 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4385 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4386 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 +02004387 } else if (!warn_is_string_type(sleaf->type)) {
4388 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004389 }
4390 }
4391 set_scnode_clear_ctx(set);
4392 return rc;
4393 }
4394
4395 if (arg_count) {
4396 set_fill_set(set, args[0]);
4397 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004398 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004399 LY_CHECK_RET(rc);
4400
4401 /* is there any normalization necessary? */
4402 for (i = 0; set->val.str[i]; ++i) {
4403 if (is_xmlws(set->val.str[i])) {
4404 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4405 have_spaces = 1;
4406 break;
4407 }
4408 space_before = 1;
4409 } else {
4410 space_before = 0;
4411 }
4412 }
4413
4414 /* yep, there is */
4415 if (have_spaces) {
4416 /* it's enough, at least one character will go, makes space for ending '\0' */
4417 new = malloc(strlen(set->val.str) * sizeof(char));
4418 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4419 new_used = 0;
4420
4421 space_before = 0;
4422 for (i = 0; set->val.str[i]; ++i) {
4423 if (is_xmlws(set->val.str[i])) {
4424 if ((i == 0) || space_before) {
4425 space_before = 1;
4426 continue;
4427 } else {
4428 space_before = 1;
4429 }
4430 } else {
4431 space_before = 0;
4432 }
4433
4434 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4435 ++new_used;
4436 }
4437
4438 /* at worst there is one trailing space now */
4439 if (new_used && is_xmlws(new[new_used - 1])) {
4440 --new_used;
4441 }
4442
4443 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4444 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4445 new[new_used] = '\0';
4446
4447 free(set->val.str);
4448 set->val.str = new;
4449 }
4450
4451 return LY_SUCCESS;
4452}
4453
4454/**
4455 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4456 * with the argument converted to boolean and logically inverted.
4457 *
4458 * @param[in] args Array of arguments.
4459 * @param[in] arg_count Count of elements in @p args.
4460 * @param[in,out] set Context and result set at the same time.
4461 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004462 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004463 */
4464static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004465xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466{
4467 if (options & LYXP_SCNODE_ALL) {
4468 set_scnode_clear_ctx(set);
4469 return LY_SUCCESS;
4470 }
4471
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004472 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004473 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004474 set_fill_boolean(set, 0);
4475 } else {
4476 set_fill_boolean(set, 1);
4477 }
4478
4479 return LY_SUCCESS;
4480}
4481
4482/**
4483 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4484 * with the number representation of either the argument or the context.
4485 *
4486 * @param[in] args Array of arguments.
4487 * @param[in] arg_count Count of elements in @p args.
4488 * @param[in,out] set Context and result set at the same time.
4489 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004490 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004491 */
4492static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004493xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004494{
4495 LY_ERR rc;
4496
4497 if (options & LYXP_SCNODE_ALL) {
4498 set_scnode_clear_ctx(set);
4499 return LY_SUCCESS;
4500 }
4501
4502 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004503 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004504 LY_CHECK_RET(rc);
4505 set_fill_set(set, args[0]);
4506 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004507 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004508 LY_CHECK_RET(rc);
4509 }
4510
4511 return LY_SUCCESS;
4512}
4513
4514/**
4515 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4516 * with the context position.
4517 *
4518 * @param[in] args Array of arguments.
4519 * @param[in] arg_count Count of elements in @p args.
4520 * @param[in,out] set Context and result set at the same time.
4521 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004522 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004523 */
4524static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004525xpath_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 +02004526{
4527 if (options & LYXP_SCNODE_ALL) {
4528 set_scnode_clear_ctx(set);
4529 return LY_SUCCESS;
4530 }
4531
Michal Vasko03ff5a72019-09-11 13:49:33 +02004532 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004533 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 +02004534 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004535 } else if (!set->used) {
4536 set_fill_number(set, 0);
4537 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004538 }
4539
4540 set_fill_number(set, set->ctx_pos);
4541
4542 /* UNUSED in 'Release' build type */
4543 (void)options;
4544 return LY_SUCCESS;
4545}
4546
4547/**
4548 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4549 * depending on whether the second argument regex matches the first argument string. For details refer to
4550 * YANG 1.1 RFC section 10.2.1.
4551 *
4552 * @param[in] args Array of arguments.
4553 * @param[in] arg_count Count of elements in @p args.
4554 * @param[in,out] set Context and result set at the same time.
4555 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004556 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004557 */
4558static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004559xpath_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 +02004560{
4561 struct lysc_pattern **patterns = NULL, **pattern;
4562 struct lysc_node_leaf *sleaf;
4563 char *path;
4564 LY_ERR rc = LY_SUCCESS;
4565 struct ly_err_item *err;
4566
4567 if (options & LYXP_SCNODE_ALL) {
4568 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4569 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4570 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 +02004571 } else if (!warn_is_string_type(sleaf->type)) {
4572 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 }
4574 }
4575
4576 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4577 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4578 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 +02004579 } else if (!warn_is_string_type(sleaf->type)) {
4580 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 }
4582 }
4583 set_scnode_clear_ctx(set);
4584 return rc;
4585 }
4586
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004587 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004589 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590 LY_CHECK_RET(rc);
4591
4592 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4593 *pattern = malloc(sizeof **pattern);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004594 path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004595 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4596 free(path);
4597 if (rc != LY_SUCCESS) {
4598 LY_ARRAY_FREE(patterns);
4599 return rc;
4600 }
4601
4602 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4603 pcre2_code_free((*pattern)->code);
4604 free(*pattern);
4605 LY_ARRAY_FREE(patterns);
4606 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004607 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 ly_err_free(err);
4609 return rc;
4610 }
4611
4612 if (rc == LY_EVALID) {
4613 ly_err_free(err);
4614 set_fill_boolean(set, 0);
4615 } else {
4616 set_fill_boolean(set, 1);
4617 }
4618
4619 return LY_SUCCESS;
4620}
4621
4622/**
4623 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4624 * with the rounded first argument. For details refer to
4625 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4626 *
4627 * @param[in] args Array of arguments.
4628 * @param[in] arg_count Count of elements in @p args.
4629 * @param[in,out] set Context and result set at the same time.
4630 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004631 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004632 */
4633static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004634xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635{
4636 struct lysc_node_leaf *sleaf;
4637 LY_ERR rc = LY_SUCCESS;
4638
4639 if (options & LYXP_SCNODE_ALL) {
4640 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4641 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4643 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 +02004644 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4645 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004646 }
4647 set_scnode_clear_ctx(set);
4648 return rc;
4649 }
4650
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004651 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004652 LY_CHECK_RET(rc);
4653
4654 /* cover only the cases where floor can't be used */
4655 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4656 set_fill_number(set, -0.0f);
4657 } else {
4658 args[0]->val.num += 0.5;
4659 rc = xpath_floor(args, 1, args[0], options);
4660 LY_CHECK_RET(rc);
4661 set_fill_number(set, args[0]->val.num);
4662 }
4663
4664 return LY_SUCCESS;
4665}
4666
4667/**
4668 * @brief Execute the XPath starts-with(string, string) function.
4669 * Returns LYXP_SET_BOOLEAN whether the second argument is
4670 * the prefix of the first or not.
4671 *
4672 * @param[in] args Array of arguments.
4673 * @param[in] arg_count Count of elements in @p args.
4674 * @param[in,out] set Context and result set at the same time.
4675 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004676 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004677 */
4678static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004679xpath_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 +02004680{
4681 struct lysc_node_leaf *sleaf;
4682 LY_ERR rc = LY_SUCCESS;
4683
4684 if (options & LYXP_SCNODE_ALL) {
4685 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4686 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4687 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 +02004688 } else if (!warn_is_string_type(sleaf->type)) {
4689 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004690 }
4691 }
4692
4693 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4694 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4695 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 +02004696 } else if (!warn_is_string_type(sleaf->type)) {
4697 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004698 }
4699 }
4700 set_scnode_clear_ctx(set);
4701 return rc;
4702 }
4703
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004704 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004705 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004706 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004707 LY_CHECK_RET(rc);
4708
4709 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4710 set_fill_boolean(set, 0);
4711 } else {
4712 set_fill_boolean(set, 1);
4713 }
4714
4715 return LY_SUCCESS;
4716}
4717
4718/**
4719 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4720 * with the string representation of either the argument or the context.
4721 *
4722 * @param[in] args Array of arguments.
4723 * @param[in] arg_count Count of elements in @p args.
4724 * @param[in,out] set Context and result set at the same time.
4725 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004726 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004727 */
4728static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004729xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004730{
4731 LY_ERR rc;
4732
4733 if (options & LYXP_SCNODE_ALL) {
4734 set_scnode_clear_ctx(set);
4735 return LY_SUCCESS;
4736 }
4737
4738 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004739 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004740 LY_CHECK_RET(rc);
4741 set_fill_set(set, args[0]);
4742 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004743 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004744 LY_CHECK_RET(rc);
4745 }
4746
4747 return LY_SUCCESS;
4748}
4749
4750/**
4751 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4752 * with the length of the string in either the argument or the context.
4753 *
4754 * @param[in] args Array of arguments.
4755 * @param[in] arg_count Count of elements in @p args.
4756 * @param[in,out] set Context and result set at the same time.
4757 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004758 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004759 */
4760static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004761xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004762{
4763 struct lysc_node_leaf *sleaf;
4764 LY_ERR rc = LY_SUCCESS;
4765
4766 if (options & LYXP_SCNODE_ALL) {
4767 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4768 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4769 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 +02004770 } else if (!warn_is_string_type(sleaf->type)) {
4771 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772 }
4773 }
4774 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4775 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4776 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 +02004777 } else if (!warn_is_string_type(sleaf->type)) {
4778 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004779 }
4780 }
4781 set_scnode_clear_ctx(set);
4782 return rc;
4783 }
4784
4785 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004786 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 LY_CHECK_RET(rc);
4788 set_fill_number(set, strlen(args[0]->val.str));
4789 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004790 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004791 LY_CHECK_RET(rc);
4792 set_fill_number(set, strlen(set->val.str));
4793 }
4794
4795 return LY_SUCCESS;
4796}
4797
4798/**
4799 * @brief Execute the XPath substring(string, number, number?) function.
4800 * Returns LYXP_SET_STRING substring of the first argument starting
4801 * on the second argument index ending on the third argument index,
4802 * indexed from 1. For exact definition refer to
4803 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4804 *
4805 * @param[in] args Array of arguments.
4806 * @param[in] arg_count Count of elements in @p args.
4807 * @param[in,out] set Context and result set at the same time.
4808 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004809 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004810 */
4811static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004812xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004813{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004814 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815 uint16_t str_start, str_len, pos;
4816 struct lysc_node_leaf *sleaf;
4817 LY_ERR rc = LY_SUCCESS;
4818
4819 if (options & LYXP_SCNODE_ALL) {
4820 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4821 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4822 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 +02004823 } else if (!warn_is_string_type(sleaf->type)) {
4824 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 }
4826 }
4827
4828 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4829 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4830 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 +02004831 } else if (!warn_is_numeric_type(sleaf->type)) {
4832 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 }
4834 }
4835
Michal Vasko69730152020-10-09 16:30:07 +02004836 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4837 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4839 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 +02004840 } else if (!warn_is_numeric_type(sleaf->type)) {
4841 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004842 }
4843 }
4844 set_scnode_clear_ctx(set);
4845 return rc;
4846 }
4847
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004848 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004849 LY_CHECK_RET(rc);
4850
4851 /* start */
4852 if (xpath_round(&args[1], 1, args[1], options)) {
4853 return -1;
4854 }
4855 if (isfinite(args[1]->val.num)) {
4856 start = args[1]->val.num - 1;
4857 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004858 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004859 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004860 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 }
4862
4863 /* len */
4864 if (arg_count == 3) {
4865 rc = xpath_round(&args[2], 1, args[2], options);
4866 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004867 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004869 } else if (isfinite(args[2]->val.num)) {
4870 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004872 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004873 }
4874 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004875 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004876 }
4877
4878 /* find matching character positions */
4879 str_start = 0;
4880 str_len = 0;
4881 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4882 if (pos < start) {
4883 ++str_start;
4884 } else if (pos < start + len) {
4885 ++str_len;
4886 } else {
4887 break;
4888 }
4889 }
4890
4891 set_fill_string(set, args[0]->val.str + str_start, str_len);
4892 return LY_SUCCESS;
4893}
4894
4895/**
4896 * @brief Execute the XPath substring-after(string, string) function.
4897 * Returns LYXP_SET_STRING with the string succeeding the occurance
4898 * of the second argument in the first or an empty string.
4899 *
4900 * @param[in] args Array of arguments.
4901 * @param[in] arg_count Count of elements in @p args.
4902 * @param[in,out] set Context and result set at the same time.
4903 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004904 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 */
4906static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004907xpath_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 +02004908{
4909 char *ptr;
4910 struct lysc_node_leaf *sleaf;
4911 LY_ERR rc = LY_SUCCESS;
4912
4913 if (options & LYXP_SCNODE_ALL) {
4914 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4915 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4916 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 +02004917 } else if (!warn_is_string_type(sleaf->type)) {
4918 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004919 }
4920 }
4921
4922 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4923 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4924 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 +02004925 } else if (!warn_is_string_type(sleaf->type)) {
4926 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 }
4928 }
4929 set_scnode_clear_ctx(set);
4930 return rc;
4931 }
4932
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004933 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004934 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004935 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004936 LY_CHECK_RET(rc);
4937
4938 ptr = strstr(args[0]->val.str, args[1]->val.str);
4939 if (ptr) {
4940 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4941 } else {
4942 set_fill_string(set, "", 0);
4943 }
4944
4945 return LY_SUCCESS;
4946}
4947
4948/**
4949 * @brief Execute the XPath substring-before(string, string) function.
4950 * Returns LYXP_SET_STRING with the string preceding the occurance
4951 * of the second argument in the first or an empty string.
4952 *
4953 * @param[in] args Array of arguments.
4954 * @param[in] arg_count Count of elements in @p args.
4955 * @param[in,out] set Context and result set at the same time.
4956 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004957 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 */
4959static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004960xpath_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 +02004961{
4962 char *ptr;
4963 struct lysc_node_leaf *sleaf;
4964 LY_ERR rc = LY_SUCCESS;
4965
4966 if (options & LYXP_SCNODE_ALL) {
4967 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4968 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4969 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 +02004970 } else if (!warn_is_string_type(sleaf->type)) {
4971 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004972 }
4973 }
4974
4975 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4976 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4977 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 +02004978 } else if (!warn_is_string_type(sleaf->type)) {
4979 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 }
4981 }
4982 set_scnode_clear_ctx(set);
4983 return rc;
4984 }
4985
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004986 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004988 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989 LY_CHECK_RET(rc);
4990
4991 ptr = strstr(args[0]->val.str, args[1]->val.str);
4992 if (ptr) {
4993 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4994 } else {
4995 set_fill_string(set, "", 0);
4996 }
4997
4998 return LY_SUCCESS;
4999}
5000
5001/**
5002 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5003 * with the sum of all the nodes in the context.
5004 *
5005 * @param[in] args Array of arguments.
5006 * @param[in] arg_count Count of elements in @p args.
5007 * @param[in,out] set Context and result set at the same time.
5008 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005009 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005010 */
5011static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005012xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005013{
5014 long double num;
5015 char *str;
5016 uint16_t i;
5017 struct lyxp_set set_item;
5018 struct lysc_node_leaf *sleaf;
5019 LY_ERR rc = LY_SUCCESS;
5020
5021 if (options & LYXP_SCNODE_ALL) {
5022 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5023 for (i = 0; i < args[0]->used; ++i) {
5024 if (args[0]->val.scnodes[i].in_ctx == 1) {
5025 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5026 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5027 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005028 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 } else if (!warn_is_numeric_type(sleaf->type)) {
5030 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005031 }
5032 }
5033 }
5034 }
5035 set_scnode_clear_ctx(set);
5036 return rc;
5037 }
5038
5039 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040
5041 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005042 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 +02005043 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005044 } else if (!args[0]->used) {
5045 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005046 }
5047
Michal Vasko5c4e5892019-11-14 12:31:38 +01005048 set_init(&set_item, set);
5049
Michal Vasko03ff5a72019-09-11 13:49:33 +02005050 set_item.type = LYXP_SET_NODE_SET;
5051 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5052 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5053
5054 set_item.used = 1;
5055 set_item.size = 1;
5056
5057 for (i = 0; i < args[0]->used; ++i) {
5058 set_item.val.nodes[0] = args[0]->val.nodes[i];
5059
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005060 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005061 LY_CHECK_RET(rc);
5062 num = cast_string_to_number(str);
5063 free(str);
5064 set->val.num += num;
5065 }
5066
5067 free(set_item.val.nodes);
5068
5069 return LY_SUCCESS;
5070}
5071
5072/**
5073 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5074 * with the text content of the nodes in the context.
5075 *
5076 * @param[in] args Array of arguments.
5077 * @param[in] arg_count Count of elements in @p args.
5078 * @param[in,out] set Context and result set at the same time.
5079 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005080 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005081 */
5082static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005083xpath_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 +02005084{
5085 uint32_t i;
5086
5087 if (options & LYXP_SCNODE_ALL) {
5088 set_scnode_clear_ctx(set);
5089 return LY_SUCCESS;
5090 }
5091
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005093 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 +02005094 return LY_EVALID;
5095 }
5096
Michal Vaskod989ba02020-08-24 10:59:24 +02005097 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005098 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005099 case LYXP_NODE_NONE:
5100 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005101 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5103 set->val.nodes[i].type = LYXP_NODE_TEXT;
5104 ++i;
5105 break;
5106 }
Radek Krejci0f969882020-08-21 16:56:47 +02005107 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 case LYXP_NODE_ROOT:
5109 case LYXP_NODE_ROOT_CONFIG:
5110 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005111 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 set_remove_node(set, i);
5113 break;
5114 }
5115 }
5116
5117 return LY_SUCCESS;
5118}
5119
5120/**
5121 * @brief Execute the XPath translate(string, string, string) function.
5122 * Returns LYXP_SET_STRING with the first argument with the characters
5123 * from the second argument replaced by those on the corresponding
5124 * positions in the third argument.
5125 *
5126 * @param[in] args Array of arguments.
5127 * @param[in] arg_count Count of elements in @p args.
5128 * @param[in,out] set Context and result set at the same time.
5129 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005130 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 */
5132static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005133xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005134{
5135 uint16_t i, j, new_used;
5136 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005137 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005138 struct lysc_node_leaf *sleaf;
5139 LY_ERR rc = LY_SUCCESS;
5140
5141 if (options & LYXP_SCNODE_ALL) {
5142 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5143 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5144 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 +02005145 } else if (!warn_is_string_type(sleaf->type)) {
5146 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005147 }
5148 }
5149
5150 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5151 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5152 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 +02005153 } else if (!warn_is_string_type(sleaf->type)) {
5154 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 }
5156 }
5157
5158 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5159 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5160 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 +02005161 } else if (!warn_is_string_type(sleaf->type)) {
5162 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 }
5164 }
5165 set_scnode_clear_ctx(set);
5166 return rc;
5167 }
5168
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005169 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005170 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005171 rc = lyxp_set_cast(args[1], 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[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005174 LY_CHECK_RET(rc);
5175
5176 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5177 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5178 new_used = 0;
5179
5180 have_removed = 0;
5181 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005182 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005183
5184 for (j = 0; args[1]->val.str[j]; ++j) {
5185 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5186 /* removing this char */
5187 if (j >= strlen(args[2]->val.str)) {
5188 have_removed = 1;
5189 found = 1;
5190 break;
5191 }
5192 /* replacing this char */
5193 new[new_used] = args[2]->val.str[j];
5194 ++new_used;
5195 found = 1;
5196 break;
5197 }
5198 }
5199
5200 /* copying this char */
5201 if (!found) {
5202 new[new_used] = args[0]->val.str[i];
5203 ++new_used;
5204 }
5205 }
5206
5207 if (have_removed) {
5208 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5209 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5210 }
5211 new[new_used] = '\0';
5212
Michal Vaskod3678892020-05-21 10:06:58 +02005213 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005214 set->type = LYXP_SET_STRING;
5215 set->val.str = new;
5216
5217 return LY_SUCCESS;
5218}
5219
5220/**
5221 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5222 * with true value.
5223 *
5224 * @param[in] args Array of arguments.
5225 * @param[in] arg_count Count of elements in @p args.
5226 * @param[in,out] set Context and result set at the same time.
5227 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005228 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 */
5230static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005231xpath_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 +02005232{
5233 if (options & LYXP_SCNODE_ALL) {
5234 set_scnode_clear_ctx(set);
5235 return LY_SUCCESS;
5236 }
5237
5238 set_fill_boolean(set, 1);
5239 return LY_SUCCESS;
5240}
5241
5242/*
5243 * moveto functions
5244 *
5245 * They and only they actually change the context (set).
5246 */
5247
5248/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005249 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005250 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005251 * XPath @p set is expected to be a (sc)node set!
5252 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005253 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5254 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005255 * @param[in] set Set with general XPath context.
5256 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005257 * @param[out] moveto_mod Expected module of a matching node.
5258 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005259 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005260static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005261moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5262 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005263{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005264 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005265 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005266 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267
Michal Vasko2104e9f2020-03-06 08:23:25 +01005268 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5269
Michal Vasko6346ece2019-09-24 13:12:53 +02005270 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5271 /* specific module */
5272 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005273 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005274
Michal Vasko004d3152020-06-11 19:59:22 +02005275 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005276 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005277 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005278 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005279 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005280 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005281 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005282 return LY_EVALID;
5283 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005284
Michal Vasko6346ece2019-09-24 13:12:53 +02005285 *qname += pref_len + 1;
5286 *qname_len -= pref_len + 1;
5287 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5288 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005289 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005290 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005291 switch (set->format) {
5292 case LY_PREF_SCHEMA:
5293 case LY_PREF_SCHEMA_RESOLVED:
5294 /* current module */
5295 mod = set->cur_mod;
5296 break;
5297 case LY_PREF_JSON:
5298 /* inherit parent (context node) module */
5299 if (ctx_scnode) {
5300 mod = ctx_scnode->module;
5301 } else {
5302 mod = NULL;
5303 }
5304 break;
5305 case LY_PREF_XML:
5306 /* not defined */
5307 LOGINT_RET(set->ctx);
5308 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 }
5310
Michal Vasko6346ece2019-09-24 13:12:53 +02005311 *moveto_mod = mod;
5312 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005313}
5314
5315/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316 * @brief Move context @p set to the root. Handles absolute path.
5317 * Result is LYXP_SET_NODE_SET.
5318 *
5319 * @param[in,out] set Set to use.
5320 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005321 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005323static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005324moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005325{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005327 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005328 }
5329
5330 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005331 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005332 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005334 set->type = LYXP_SET_NODE_SET;
5335 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005336 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005338
5339 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340}
5341
5342/**
5343 * @brief Check @p node as a part of NameTest processing.
5344 *
5345 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005346 * @param[in] ctx_node Context node.
5347 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005348 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005349 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005350 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005351 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5352 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353 */
5354static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005355moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005356 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005357{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005358 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5359 switch (set->format) {
5360 case LY_PREF_SCHEMA:
5361 case LY_PREF_SCHEMA_RESOLVED:
5362 /* use current module */
5363 moveto_mod = set->cur_mod;
5364 break;
5365 case LY_PREF_JSON:
5366 /* inherit module of the context node, if any */
5367 if (ctx_node) {
5368 moveto_mod = ctx_node->schema->module;
5369 }
5370 break;
5371 case LY_PREF_XML:
5372 /* not defined */
5373 LOGINT(set->ctx);
5374 return LY_EINVAL;
5375 }
5376 }
5377
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378 /* module check */
5379 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005380 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 }
5382
Michal Vasko5c4e5892019-11-14 12:31:38 +01005383 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005384 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005386 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5387 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005388 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389 }
5390
5391 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005392 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005393 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 }
5395
Michal Vaskoa1424542019-11-14 16:08:52 +01005396 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005397 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005399 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400
5401 /* match */
5402 return LY_SUCCESS;
5403}
5404
5405/**
5406 * @brief Check @p node as a part of schema NameTest processing.
5407 *
5408 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005409 * @param[in] ctx_scnode Context node.
5410 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005411 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005412 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005413 * @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 +02005414 */
5415static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005416moveto_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 +02005417 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005418{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005419 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5420 switch (set->format) {
5421 case LY_PREF_SCHEMA:
5422 case LY_PREF_SCHEMA_RESOLVED:
5423 /* use current module */
5424 moveto_mod = set->cur_mod;
5425 break;
5426 case LY_PREF_JSON:
5427 /* inherit module of the context node, if any */
5428 if (ctx_scnode) {
5429 moveto_mod = ctx_scnode->module;
5430 }
5431 break;
5432 case LY_PREF_XML:
5433 /* not defined */
5434 LOGINT(set->ctx);
5435 return LY_EINVAL;
5436 }
5437 }
5438
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005440 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005441 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005442 }
5443
5444 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005445 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005446 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005447 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005448 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 }
5450
5451 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005452 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005453 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454 }
5455
5456 /* match */
5457 return LY_SUCCESS;
5458}
5459
5460/**
Michal Vaskod3678892020-05-21 10:06:58 +02005461 * @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 +02005462 *
5463 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005464 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005465 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005466 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005467 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5468 */
5469static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005470moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005471{
Michal Vaskof03ed032020-03-04 13:31:44 +01005472 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005473 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005474 LY_ERR rc;
5475
Michal Vaskod3678892020-05-21 10:06:58 +02005476 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005477 return LY_SUCCESS;
5478 }
5479
5480 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005481 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 +02005482 return LY_EVALID;
5483 }
5484
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005486 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005487
5488 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 +01005489 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005490
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005491 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005492 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005493 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005494 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005495 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005496 ctx_node = set->val.nodes[i].node;
5497 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005498 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005499
Michal Vaskod3678892020-05-21 10:06:58 +02005500 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005501 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005502 if (rc == LY_SUCCESS) {
5503 if (!replaced) {
5504 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5505 replaced = 1;
5506 } else {
5507 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005508 }
Michal Vaskod3678892020-05-21 10:06:58 +02005509 ++i;
5510 } else if (rc == LY_EINCOMPLETE) {
5511 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005512 }
5513 }
5514
5515 if (!replaced) {
5516 /* no match */
5517 set_remove_node(set, i);
5518 }
5519 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005520
5521 return LY_SUCCESS;
5522}
5523
5524/**
Michal Vaskod3678892020-05-21 10:06:58 +02005525 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5526 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005527 *
5528 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005529 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005530 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskocdad7122020-11-09 21:04:44 +01005531 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005532 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5533 */
5534static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005535moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5536 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005537{
Michal Vasko004d3152020-06-11 19:59:22 +02005538 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005539 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005540 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005541 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005542
Michal Vasko004d3152020-06-11 19:59:22 +02005543 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005544
5545 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005546 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005547 }
5548
5549 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005550 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 +02005551 ret = LY_EVALID;
5552 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005553 }
5554
5555 /* context check for all the nodes since we have the schema node */
5556 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5557 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005558 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005559 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5560 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005561 lyxp_set_free_content(set);
5562 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005563 }
5564
5565 /* create specific data instance if needed */
5566 if (scnode->nodetype == LYS_LIST) {
5567 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5568 } else if (scnode->nodetype == LYS_LEAFLIST) {
5569 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005570 }
5571
5572 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005573 siblings = NULL;
5574
5575 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5576 assert(!set->val.nodes[i].node);
5577
5578 /* search in all the trees */
5579 siblings = set->tree;
5580 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5581 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005582 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005583 }
5584
5585 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005586 if (inst) {
5587 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005588 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005589 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005590 }
5591
5592 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005593 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005594 ret = LY_EINCOMPLETE;
5595 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005596 }
5597
5598 if (sub) {
5599 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005600 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005601 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005602 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005603 /* no match */
5604 set_remove_node(set, i);
5605 }
5606 }
5607
Michal Vasko004d3152020-06-11 19:59:22 +02005608cleanup:
5609 lyd_free_tree(inst);
5610 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005611}
5612
5613/**
5614 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5615 *
5616 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005617 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005618 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005619 * @param[in] options XPath options.
5620 * @return LY_ERR
5621 */
5622static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005623moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005624{
Radek Krejci857189e2020-09-01 13:26:36 +02005625 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005626 uint32_t getnext_opts;
5627 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005628 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005629 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005630 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005631
Michal Vaskod3678892020-05-21 10:06:58 +02005632 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005633 return LY_SUCCESS;
5634 }
5635
5636 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005637 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 +02005638 return LY_EVALID;
5639 }
5640
Michal Vaskocafad9d2019-11-07 15:20:03 +01005641 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005642 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005643 if (options & LYXP_SCNODE_OUTPUT) {
5644 getnext_opts |= LYS_GETNEXT_OUTPUT;
5645 }
5646
Michal Vasko03ff5a72019-09-11 13:49:33 +02005647 orig_used = set->used;
5648 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005649 uint32_t idx;
5650
Michal Vasko03ff5a72019-09-11 13:49:33 +02005651 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005652 if (set->val.scnodes[i].in_ctx != -2) {
5653 continue;
5654 }
5655
5656 /* remember context node */
5657 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005658 } else {
5659 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005660 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005661
5662 start_parent = set->val.scnodes[i].scnode;
5663
5664 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 +02005665 /* 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 +02005666 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005668 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005669 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005670 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005671 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005672 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005673 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5674
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005676 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005677 set->val.scnodes[idx].in_ctx = 2;
5678 temp_ctx = 1;
5679 }
5680 }
5681 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682 }
5683
Michal Vasko519fd602020-05-26 12:17:39 +02005684 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5685 iter = NULL;
5686 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005687 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005688 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5689
5690 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005691 set->val.scnodes[idx].in_ctx = 2;
5692 temp_ctx = 1;
5693 }
5694 }
5695 }
5696 }
5697 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698
5699 /* correct temporary in_ctx values */
5700 if (temp_ctx) {
5701 for (i = 0; i < orig_used; ++i) {
5702 if (set->val.scnodes[i].in_ctx == 2) {
5703 set->val.scnodes[i].in_ctx = 1;
5704 }
5705 }
5706 }
5707
5708 return LY_SUCCESS;
5709}
5710
5711/**
Michal Vaskod3678892020-05-21 10:06:58 +02005712 * @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 +02005713 * Context position aware.
5714 *
5715 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005716 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005717 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005718 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5720 */
5721static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005722moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723{
5724 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005725 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005726 struct lyxp_set ret_set;
5727 LY_ERR rc;
5728
Michal Vaskod3678892020-05-21 10:06:58 +02005729 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005730 return LY_SUCCESS;
5731 }
5732
5733 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005734 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 +02005735 return LY_EVALID;
5736 }
5737
Michal Vasko9f96a052020-03-10 09:41:45 +01005738 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskocdad7122020-11-09 21:04:44 +01005739 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005740 LY_CHECK_RET(rc);
5741
Michal Vasko6346ece2019-09-24 13:12:53 +02005742 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743 set_init(&ret_set, set);
5744 for (i = 0; i < set->used; ++i) {
5745
5746 /* TREE DFS */
5747 start = set->val.nodes[i].node;
5748 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005749 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005750 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005751 /* add matching node into result set */
5752 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5753 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5754 /* the node is a duplicate, we'll process it later in the set */
5755 goto skip_children;
5756 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005757 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005758 return rc;
5759 } else if (rc == LY_EINVAL) {
5760 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005761 }
5762
5763 /* TREE DFS NEXT ELEM */
5764 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005765 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766 if (!next) {
5767skip_children:
5768 /* no children, so try siblings, but only if it's not the start,
5769 * that is considered to be the root and it's siblings are not traversed */
5770 if (elem != start) {
5771 next = elem->next;
5772 } else {
5773 break;
5774 }
5775 }
5776 while (!next) {
5777 /* no siblings, go back through the parents */
5778 if ((struct lyd_node *)elem->parent == start) {
5779 /* we are done, no next element to process */
5780 break;
5781 }
5782 /* parent is already processed, go to its sibling */
5783 elem = (struct lyd_node *)elem->parent;
5784 next = elem->next;
5785 }
5786 }
5787 }
5788
5789 /* make the temporary set the current one */
5790 ret_set.ctx_pos = set->ctx_pos;
5791 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005792 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793 memcpy(set, &ret_set, sizeof *set);
5794
5795 return LY_SUCCESS;
5796}
5797
5798/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005799 * @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 +02005800 *
5801 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005802 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005803 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005804 * @param[in] options XPath options.
5805 * @return LY_ERR
5806 */
5807static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005808moveto_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 +02005809{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005810 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005812 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005813
Michal Vaskod3678892020-05-21 10:06:58 +02005814 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 return LY_SUCCESS;
5816 }
5817
5818 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005819 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 +02005820 return LY_EVALID;
5821 }
5822
Michal Vasko03ff5a72019-09-11 13:49:33 +02005823 orig_used = set->used;
5824 for (i = 0; i < orig_used; ++i) {
5825 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005826 if (set->val.scnodes[i].in_ctx != -2) {
5827 continue;
5828 }
5829
5830 /* remember context node */
5831 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005832 } else {
5833 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005834 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005835
5836 /* TREE DFS */
5837 start = set->val.scnodes[i].scnode;
5838 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005839 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5840 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005841 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005842 }
5843
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005844 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005845 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005846 uint32_t idx;
5847
5848 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005849 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005850 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005851 /* we will process it later in the set */
5852 goto skip_children;
5853 }
5854 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005855 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005857 } else if (rc == LY_EINVAL) {
5858 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859 }
5860
5861next_iter:
5862 /* TREE DFS NEXT ELEM */
5863 /* select element for the next run - children first */
5864 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 if (!next) {
5866skip_children:
5867 /* no children, so try siblings, but only if it's not the start,
5868 * that is considered to be the root and it's siblings are not traversed */
5869 if (elem != start) {
5870 next = elem->next;
5871 } else {
5872 break;
5873 }
5874 }
5875 while (!next) {
5876 /* no siblings, go back through the parents */
5877 if (elem->parent == start) {
5878 /* we are done, no next element to process */
5879 break;
5880 }
5881 /* parent is already processed, go to its sibling */
5882 elem = elem->parent;
5883 next = elem->next;
5884 }
5885 }
5886 }
5887
5888 return LY_SUCCESS;
5889}
5890
5891/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005892 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005893 * Indirectly context position aware.
5894 *
5895 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005896 * @param[in] mod Matching metadata module, NULL for any.
5897 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005898 * @return LY_ERR
5899 */
5900static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005901moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902{
Michal Vasko9f96a052020-03-10 09:41:45 +01005903 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005904
Michal Vaskod3678892020-05-21 10:06:58 +02005905 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005906 return LY_SUCCESS;
5907 }
5908
5909 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005910 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 +02005911 return LY_EVALID;
5912 }
5913
Radek Krejci1deb5be2020-08-26 16:43:36 +02005914 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005915 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916
5917 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5918 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005919 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005920 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921
5922 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005923 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 continue;
5925 }
5926
Michal Vaskod3678892020-05-21 10:06:58 +02005927 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928 /* match */
5929 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005930 set->val.meta[i].meta = sub;
5931 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005932 /* pos does not change */
5933 replaced = 1;
5934 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005935 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 +02005936 }
5937 ++i;
5938 }
5939 }
5940 }
5941
5942 if (!replaced) {
5943 /* no match */
5944 set_remove_node(set, i);
5945 }
5946 }
5947
5948 return LY_SUCCESS;
5949}
5950
5951/**
5952 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005953 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005954 *
5955 * @param[in,out] set1 Set to use for the result.
5956 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005957 * @return LY_ERR
5958 */
5959static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005960moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005961{
5962 LY_ERR rc;
5963
Michal Vaskod3678892020-05-21 10:06:58 +02005964 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005965 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 +02005966 return LY_EVALID;
5967 }
5968
5969 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005970 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005971 return LY_SUCCESS;
5972 }
5973
Michal Vaskod3678892020-05-21 10:06:58 +02005974 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005975 memcpy(set1, set2, sizeof *set1);
5976 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005977 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 return LY_SUCCESS;
5979 }
5980
5981 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005982 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983
5984 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005985 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 LY_CHECK_RET(rc);
5987
5988 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005989 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990
5991 return LY_SUCCESS;
5992}
5993
5994/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005995 * @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 +02005996 * Context position aware.
5997 *
5998 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005999 * @param[in] mod Matching metadata module, NULL for any.
6000 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006001 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6003 */
6004static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006005moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006006{
Michal Vasko9f96a052020-03-10 09:41:45 +01006007 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008 struct lyxp_set *set_all_desc = NULL;
6009 LY_ERR rc;
6010
Michal Vaskod3678892020-05-21 10:06:58 +02006011 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006012 return LY_SUCCESS;
6013 }
6014
6015 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006016 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 +02006017 return LY_EVALID;
6018 }
6019
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6021 * but it likely won't be used much, so it's a waste of time */
6022 /* copy the context */
6023 set_all_desc = set_copy(set);
6024 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006025 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006026 if (rc != LY_SUCCESS) {
6027 lyxp_set_free(set_all_desc);
6028 return rc;
6029 }
6030 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006031 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 if (rc != LY_SUCCESS) {
6033 lyxp_set_free(set_all_desc);
6034 return rc;
6035 }
6036 lyxp_set_free(set_all_desc);
6037
Radek Krejci1deb5be2020-08-26 16:43:36 +02006038 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006039 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040
6041 /* only attributes of an elem can be in the result, skip all the rest,
6042 * we have all attributes qualified in lyd tree */
6043 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006044 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006046 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 continue;
6048 }
6049
Michal Vaskod3678892020-05-21 10:06:58 +02006050 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051 /* match */
6052 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006053 set->val.meta[i].meta = sub;
6054 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006055 /* pos does not change */
6056 replaced = 1;
6057 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006058 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 +02006059 }
6060 ++i;
6061 }
6062 }
6063 }
6064
6065 if (!replaced) {
6066 /* no match */
6067 set_remove_node(set, i);
6068 }
6069 }
6070
6071 return LY_SUCCESS;
6072}
6073
6074/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006075 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6076 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006077 *
6078 * @param[in] parent Current parent.
6079 * @param[in] parent_pos Position of @p parent.
6080 * @param[in] parent_type Node type of @p parent.
6081 * @param[in,out] to_set Set to use.
6082 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006083 * @param[in] options XPath options.
6084 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6085 */
6086static LY_ERR
6087moveto_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 +02006088 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006089{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006090 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091 LY_ERR rc;
6092
6093 switch (parent_type) {
6094 case LYXP_NODE_ROOT:
6095 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006096 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097
Michal Vasko61ac2f62020-05-25 12:39:51 +02006098 /* add all top-level nodes as elements */
6099 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100 break;
6101 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006102 /* add just the text node of this term element node */
6103 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006104 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6105 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6106 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006107 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006109
6110 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006111 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112 break;
6113 default:
6114 LOGINT_RET(parent->schema->module->ctx);
6115 }
6116
Michal Vasko61ac2f62020-05-25 12:39:51 +02006117 /* add all top-level nodes as elements */
6118 LY_LIST_FOR(first, iter) {
6119 /* context check */
6120 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6121 continue;
6122 }
6123
6124 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006125 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006126 return LY_EINCOMPLETE;
6127 }
6128
6129 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6130 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6131
6132 /* also add all the children of this node, recursively */
6133 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6134 LY_CHECK_RET(rc);
6135 }
6136 }
6137
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 return LY_SUCCESS;
6139}
6140
6141/**
6142 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6143 * (or LYXP_SET_EMPTY). Context position aware.
6144 *
6145 * @param[in,out] set Set to use.
6146 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6147 * @param[in] options XPath options.
6148 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6149 */
6150static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006151moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006152{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006153 struct lyxp_set ret_set;
6154 LY_ERR rc;
6155
Michal Vaskod3678892020-05-21 10:06:58 +02006156 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006157 return LY_SUCCESS;
6158 }
6159
6160 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006161 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 +02006162 return LY_EVALID;
6163 }
6164
6165 /* nothing to do */
6166 if (!all_desc) {
6167 return LY_SUCCESS;
6168 }
6169
Michal Vasko03ff5a72019-09-11 13:49:33 +02006170 /* add all the children, they get added recursively */
6171 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006172 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006173 /* copy the current node to tmp */
6174 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6175
6176 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006177 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 continue;
6179 }
6180
Michal Vasko03ff5a72019-09-11 13:49:33 +02006181 /* add all the children */
6182 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 +02006183 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006184 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006185 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006186 return rc;
6187 }
6188 }
6189
6190 /* use the temporary set as the current one */
6191 ret_set.ctx_pos = set->ctx_pos;
6192 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006193 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006194 memcpy(set, &ret_set, sizeof *set);
6195
6196 return LY_SUCCESS;
6197}
6198
6199/**
6200 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6201 * (or LYXP_SET_EMPTY).
6202 *
6203 * @param[in,out] set Set to use.
6204 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6205 * @param[in] options XPath options.
6206 * @return LY_ERR
6207 */
6208static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006209moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006210{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006211 uint32_t getnext_opts;
6212 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006213 const struct lysc_node *iter, *start_parent;
6214 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006215
Michal Vaskod3678892020-05-21 10:06:58 +02006216 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006217 return LY_SUCCESS;
6218 }
6219
6220 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006221 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 +02006222 return LY_EVALID;
6223 }
6224
6225 /* nothing to do */
6226 if (!all_desc) {
6227 return LY_SUCCESS;
6228 }
6229
Michal Vasko519fd602020-05-26 12:17:39 +02006230 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006231 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006232 if (options & LYXP_SCNODE_OUTPUT) {
6233 getnext_opts |= LYS_GETNEXT_OUTPUT;
6234 }
6235
6236 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006237 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006239 if (set->val.scnodes[i].in_ctx != -2) {
6240 continue;
6241 }
6242
Michal Vasko519fd602020-05-26 12:17:39 +02006243 /* remember context node */
6244 set->val.scnodes[i].in_ctx = -1;
6245 } else {
6246 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006247 }
6248
Michal Vasko519fd602020-05-26 12:17:39 +02006249 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006250
Michal Vasko519fd602020-05-26 12:17:39 +02006251 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6252 /* it can actually be in any module, it's all <running> */
6253 mod_idx = 0;
6254 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6255 iter = NULL;
6256 /* module may not be implemented */
6257 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6258 /* context check */
6259 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6260 continue;
6261 }
6262
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006263 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006264 /* throw away the insert index, we want to consider that node again, recursively */
6265 }
6266 }
6267
6268 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6269 iter = NULL;
6270 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006272 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273 continue;
6274 }
6275
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006276 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277 }
6278 }
6279 }
6280
6281 return LY_SUCCESS;
6282}
6283
6284/**
6285 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6286 * (or LYXP_SET_EMPTY). Context position aware.
6287 *
6288 * @param[in] set Set to use.
6289 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6290 * @param[in] options XPath options.
6291 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6292 */
6293static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006294moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006295{
6296 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006297 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006298 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006299
Michal Vaskod3678892020-05-21 10:06:58 +02006300 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006301 return LY_SUCCESS;
6302 }
6303
6304 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006305 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 +02006306 return LY_EVALID;
6307 }
6308
6309 if (all_desc) {
6310 /* <path>//.. == <path>//./.. */
6311 rc = moveto_self(set, 1, options);
6312 LY_CHECK_RET(rc);
6313 }
6314
Radek Krejci1deb5be2020-08-26 16:43:36 +02006315 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006316 node = set->val.nodes[i].node;
6317
6318 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6319 new_node = (struct lyd_node *)node->parent;
6320 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6321 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006322 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6323 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006324 if (!new_node) {
6325 LOGINT_RET(set->ctx);
6326 }
6327 } else {
6328 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006329 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006330 continue;
6331 }
6332
Michal Vaskoa1424542019-11-14 16:08:52 +01006333 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006334 if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && !(new_node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006335 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006336 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006337
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006338 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006339 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006340 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341
Michal Vasko03ff5a72019-09-11 13:49:33 +02006342 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006343 /* 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 +02006344 new_type = LYXP_NODE_ELEM;
6345 }
6346
Michal Vasko03ff5a72019-09-11 13:49:33 +02006347 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006348 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349 } else {
6350 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006351 }
6352 }
6353
Michal Vasko2caefc12019-11-14 16:07:56 +01006354 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006355 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006356
6357 return LY_SUCCESS;
6358}
6359
6360/**
6361 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6362 * (or LYXP_SET_EMPTY).
6363 *
6364 * @param[in] set Set to use.
6365 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6366 * @param[in] options XPath options.
6367 * @return LY_ERR
6368 */
6369static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006370moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006371{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006372 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006373 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006374 const struct lysc_node *node, *new_node;
6375 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376
Michal Vaskod3678892020-05-21 10:06:58 +02006377 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006378 return LY_SUCCESS;
6379 }
6380
6381 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006382 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 +02006383 return LY_EVALID;
6384 }
6385
6386 if (all_desc) {
6387 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006388 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389 }
6390
Michal Vasko03ff5a72019-09-11 13:49:33 +02006391 orig_used = set->used;
6392 for (i = 0; i < orig_used; ++i) {
6393 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006394 if (set->val.scnodes[i].in_ctx != -2) {
6395 continue;
6396 }
6397
6398 /* remember context node */
6399 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006400 } else {
6401 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006402 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006403
6404 node = set->val.scnodes[i].scnode;
6405
6406 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006407 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408 } else {
6409 /* root does not have a parent */
6410 continue;
6411 }
6412
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006413 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006414 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006415 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416
Michal Vasko03ff5a72019-09-11 13:49:33 +02006417 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006418 /* 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 +02006419 new_type = LYXP_NODE_ELEM;
6420 }
6421
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006422 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6423 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424 set->val.scnodes[idx].in_ctx = 2;
6425 temp_ctx = 1;
6426 }
6427 }
6428
6429 if (temp_ctx) {
6430 for (i = 0; i < orig_used; ++i) {
6431 if (set->val.scnodes[i].in_ctx == 2) {
6432 set->val.scnodes[i].in_ctx = 1;
6433 }
6434 }
6435 }
6436
6437 return LY_SUCCESS;
6438}
6439
6440/**
6441 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6442 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6443 *
6444 * @param[in,out] set1 Set to use for the result.
6445 * @param[in] set2 Set acting as the second operand for @p op.
6446 * @param[in] op Comparison operator to process.
6447 * @param[in] options XPath options.
6448 * @return LY_ERR
6449 */
6450static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006451moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006452{
6453 /*
6454 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6455 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6456 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6457 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6458 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6459 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6460 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6461 *
6462 * '=' or '!='
6463 * BOOLEAN + BOOLEAN
6464 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6465 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6466 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6467 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6468 * NUMBER + NUMBER
6469 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6470 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6471 * STRING + STRING
6472 *
6473 * '<=', '<', '>=', '>'
6474 * NUMBER + NUMBER
6475 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6476 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6477 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6478 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6479 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6480 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6481 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6482 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6483 */
6484 struct lyxp_set iter1, iter2;
6485 int result;
6486 int64_t i;
6487 LY_ERR rc;
6488
Michal Vasko004d3152020-06-11 19:59:22 +02006489 memset(&iter1, 0, sizeof iter1);
6490 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491
6492 /* iterative evaluation with node-sets */
6493 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6494 if (set1->type == LYXP_SET_NODE_SET) {
6495 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006496 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497 switch (set2->type) {
6498 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006499 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006500 break;
6501 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006502 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006503 break;
6504 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006505 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006506 break;
6507 }
6508 LY_CHECK_RET(rc);
6509
Michal Vasko4c7763f2020-07-27 17:40:37 +02006510 /* canonize set2 */
6511 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6512
6513 /* compare recursively */
6514 rc = moveto_op_comp(&iter1, &iter2, op, options);
6515 lyxp_set_free_content(&iter2);
6516 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006517
6518 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006519 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006520 set_fill_boolean(set1, 1);
6521 return LY_SUCCESS;
6522 }
6523 }
6524 } else {
6525 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006526 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006527 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006528 case LYXP_SET_NUMBER:
6529 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6530 break;
6531 case LYXP_SET_BOOLEAN:
6532 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6533 break;
6534 default:
6535 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6536 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006537 }
6538 LY_CHECK_RET(rc);
6539
Michal Vasko4c7763f2020-07-27 17:40:37 +02006540 /* canonize set1 */
6541 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 +02006542
Michal Vasko4c7763f2020-07-27 17:40:37 +02006543 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006544 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006545 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006546 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006547
6548 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006549 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006550 set_fill_boolean(set1, 1);
6551 return LY_SUCCESS;
6552 }
6553 }
6554 }
6555
6556 /* false for all nodes */
6557 set_fill_boolean(set1, 0);
6558 return LY_SUCCESS;
6559 }
6560
6561 /* first convert properly */
6562 if ((op[0] == '=') || (op[0] == '!')) {
6563 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006564 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6565 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006567 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006568 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006569 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006570 LY_CHECK_RET(rc);
6571 } /* else we have 2 strings */
6572 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006573 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006574 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006575 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006576 LY_CHECK_RET(rc);
6577 }
6578
6579 assert(set1->type == set2->type);
6580
6581 /* compute result */
6582 if (op[0] == '=') {
6583 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006584 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006585 } else if (set1->type == LYXP_SET_NUMBER) {
6586 result = (set1->val.num == set2->val.num);
6587 } else {
6588 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006589 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 }
6591 } else if (op[0] == '!') {
6592 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006593 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594 } else if (set1->type == LYXP_SET_NUMBER) {
6595 result = (set1->val.num != set2->val.num);
6596 } else {
6597 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006598 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006599 }
6600 } else {
6601 assert(set1->type == LYXP_SET_NUMBER);
6602 if (op[0] == '<') {
6603 if (op[1] == '=') {
6604 result = (set1->val.num <= set2->val.num);
6605 } else {
6606 result = (set1->val.num < set2->val.num);
6607 }
6608 } else {
6609 if (op[1] == '=') {
6610 result = (set1->val.num >= set2->val.num);
6611 } else {
6612 result = (set1->val.num > set2->val.num);
6613 }
6614 }
6615 }
6616
6617 /* assign result */
6618 if (result) {
6619 set_fill_boolean(set1, 1);
6620 } else {
6621 set_fill_boolean(set1, 0);
6622 }
6623
6624 return LY_SUCCESS;
6625}
6626
6627/**
6628 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6629 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6630 *
6631 * @param[in,out] set1 Set to use for the result.
6632 * @param[in] set2 Set acting as the second operand for @p op.
6633 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006634 * @return LY_ERR
6635 */
6636static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006637moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638{
6639 LY_ERR rc;
6640
6641 /* unary '-' */
6642 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006643 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006644 LY_CHECK_RET(rc);
6645 set1->val.num *= -1;
6646 lyxp_set_free(set2);
6647 return LY_SUCCESS;
6648 }
6649
6650 assert(set1 && set2);
6651
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006652 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006654 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 LY_CHECK_RET(rc);
6656
6657 switch (op[0]) {
6658 /* '+' */
6659 case '+':
6660 set1->val.num += set2->val.num;
6661 break;
6662
6663 /* '-' */
6664 case '-':
6665 set1->val.num -= set2->val.num;
6666 break;
6667
6668 /* '*' */
6669 case '*':
6670 set1->val.num *= set2->val.num;
6671 break;
6672
6673 /* 'div' */
6674 case 'd':
6675 set1->val.num /= set2->val.num;
6676 break;
6677
6678 /* 'mod' */
6679 case 'm':
6680 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6681 break;
6682
6683 default:
6684 LOGINT_RET(set1->ctx);
6685 }
6686
6687 return LY_SUCCESS;
6688}
6689
6690/*
6691 * eval functions
6692 *
6693 * They execute a parsed XPath expression on some data subtree.
6694 */
6695
6696/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 * @brief Evaluate Predicate. Logs directly on error.
6698 *
Michal Vaskod3678892020-05-21 10:06:58 +02006699 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700 *
6701 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006702 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6704 * @param[in] options XPath options.
6705 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6706 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6707 */
6708static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006709eval_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 +02006710{
6711 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006712 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006713 uint32_t orig_pos, orig_size;
6714 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006715 struct lyxp_set set2;
6716 struct lyd_node *orig_parent;
6717
6718 /* '[' */
6719 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006720 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006721 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006722
6723 if (!set) {
6724only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006725 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006726 LY_CHECK_RET(rc);
6727 } else if (set->type == LYXP_SET_NODE_SET) {
6728 /* 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 +01006729 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730
6731 /* empty set, nothing to evaluate */
6732 if (!set->used) {
6733 goto only_parse;
6734 }
6735
Michal Vasko004d3152020-06-11 19:59:22 +02006736 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737 orig_pos = 0;
6738 orig_size = set->used;
6739 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006740 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006741 set_init(&set2, set);
6742 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6743 /* remember the node context position for position() and context size for last(),
6744 * predicates should always be evaluated with respect to the child axis (since we do
6745 * not support explicit axes) so we assign positions based on their parents */
6746 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6747 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6748 orig_pos = 1;
6749 } else {
6750 ++orig_pos;
6751 }
6752
6753 set2.ctx_pos = orig_pos;
6754 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006755 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006756
Michal Vasko004d3152020-06-11 19:59:22 +02006757 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006758 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006759 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006760 return rc;
6761 }
6762
6763 /* number is a position */
6764 if (set2.type == LYXP_SET_NUMBER) {
6765 if ((long long)set2.val.num == orig_pos) {
6766 set2.val.num = 1;
6767 } else {
6768 set2.val.num = 0;
6769 }
6770 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006771 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772
6773 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006774 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006775 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776 }
6777 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006778 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779
6780 } else if (set->type == LYXP_SET_SCNODE_SET) {
6781 for (i = 0; i < set->used; ++i) {
6782 if (set->val.scnodes[i].in_ctx == 1) {
6783 /* there is a currently-valid node */
6784 break;
6785 }
6786 }
6787 /* empty set, nothing to evaluate */
6788 if (i == set->used) {
6789 goto only_parse;
6790 }
6791
Michal Vasko004d3152020-06-11 19:59:22 +02006792 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793
Michal Vasko03ff5a72019-09-11 13:49:33 +02006794 /* set special in_ctx to all the valid snodes */
6795 pred_in_ctx = set_scnode_new_in_ctx(set);
6796
6797 /* use the valid snodes one-by-one */
6798 for (i = 0; i < set->used; ++i) {
6799 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6800 continue;
6801 }
6802 set->val.scnodes[i].in_ctx = 1;
6803
Michal Vasko004d3152020-06-11 19:59:22 +02006804 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006805
Michal Vasko004d3152020-06-11 19:59:22 +02006806 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006807 LY_CHECK_RET(rc);
6808
6809 set->val.scnodes[i].in_ctx = pred_in_ctx;
6810 }
6811
6812 /* restore the state as it was before the predicate */
6813 for (i = 0; i < set->used; ++i) {
6814 if (set->val.scnodes[i].in_ctx == 1) {
6815 set->val.scnodes[i].in_ctx = 0;
6816 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6817 set->val.scnodes[i].in_ctx = 1;
6818 }
6819 }
6820
6821 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006822 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823 set_fill_set(&set2, set);
6824
Michal Vasko004d3152020-06-11 19:59:22 +02006825 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006826 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006827 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006828 return rc;
6829 }
6830
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006831 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006832 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006833 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834 }
Michal Vaskod3678892020-05-21 10:06:58 +02006835 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 }
6837
6838 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006839 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006841 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006842 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006843
6844 return LY_SUCCESS;
6845}
6846
6847/**
Michal Vaskod3678892020-05-21 10:06:58 +02006848 * @brief Evaluate Literal. Logs directly on error.
6849 *
6850 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006851 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006852 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6853 */
6854static void
Michal Vasko40308e72020-10-20 16:38:40 +02006855eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006856{
6857 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006858 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006859 set_fill_string(set, "", 0);
6860 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006861 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 +02006862 }
6863 }
6864 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006865 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006866 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006867}
6868
6869/**
Michal Vasko004d3152020-06-11 19:59:22 +02006870 * @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 +02006871 *
Michal Vasko004d3152020-06-11 19:59:22 +02006872 * @param[in] exp Full parsed XPath expression.
6873 * @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 +02006874 * @param[in] ctx_node Found schema node as the context for the predicate.
6875 * @param[in] cur_mod Current module for the expression.
6876 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006877 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006878 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006879 * @param[out] predicates Parsed predicates.
6880 * @param[out] pred_type Type of @p predicates.
6881 * @return LY_SUCCESS on success,
6882 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006883 */
6884static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006885eval_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 +02006886 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6887 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006888{
Michal Vasko004d3152020-06-11 19:59:22 +02006889 LY_ERR ret = LY_SUCCESS;
6890 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006891 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006892 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006893 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006894 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006895
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006896 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006897
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006898 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006899 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006900 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006901 return LY_EINVAL;
6902 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006903 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 +02006904 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006905
Michal Vasko004d3152020-06-11 19:59:22 +02006906 /* learn where the predicates end */
6907 e_idx = *tok_idx;
6908 while (key_count) {
6909 /* '[' */
6910 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6911 return LY_EINVAL;
6912 }
6913 ++e_idx;
6914
6915 /* ']' */
6916 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6917 ++e_idx;
6918 }
6919 ++e_idx;
6920
6921 /* another presumably key predicate parsed */
6922 --key_count;
6923 }
Michal Vasko004d3152020-06-11 19:59:22 +02006924 } else {
6925 /* learn just where this single predicate ends */
6926 e_idx = *tok_idx;
6927
Michal Vaskod3678892020-05-21 10:06:58 +02006928 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006929 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6930 return LY_EINVAL;
6931 }
6932 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006933
6934 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006935 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6936 ++e_idx;
6937 }
6938 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006939 }
6940
Michal Vasko004d3152020-06-11 19:59:22 +02006941 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6942 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6943
6944 /* turn logging off */
6945 prev_lo = ly_log_options(0);
6946
6947 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006948 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6949 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006950
6951 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006952 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6953 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006954 LY_CHECK_GOTO(ret, cleanup);
6955
6956 /* success, the predicate must include all the needed information for hash-based search */
6957 *tok_idx = e_idx;
6958
6959cleanup:
6960 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006961 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006962 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006963}
6964
6965/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006966 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6967 * data node(s) could be found using a single hash-based search.
6968 *
6969 * @param[in] node Next context node to check.
6970 * @param[in] name Expected node name.
6971 * @param[in] name_len Length of @p name.
6972 * @param[in] moveto_mod Expected node module.
6973 * @param[in] root_type XPath root type.
6974 * @param[in] no_prefix Whether the node name was unprefixed.
6975 * @param[in] format Prefix format.
6976 * @param[in,out] found Previously found node, is updated.
6977 * @return LY_SUCCESS on success,
6978 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6979 */
6980static LY_ERR
6981eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len,
6982 const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format,
6983 const struct lysc_node **found)
6984{
6985 const struct lysc_node *scnode;
6986 const struct lys_module *mod;
6987 uint32_t idx = 0;
6988
6989continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02006990 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006991 if (!node) {
6992 if (no_prefix && (format == LY_PREF_JSON)) {
6993 /* search all modules for a single match */
6994 while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) {
6995 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
6996 if (scnode) {
6997 /* we have found a match */
6998 break;
6999 }
7000 }
7001
Michal Vasko7d1d0912020-10-16 08:38:30 +02007002 if (!scnode) {
7003 /* all modules searched */
7004 idx = 0;
7005 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007006 } else {
7007 /* search in top-level */
7008 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7009 }
7010 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
7011 if (no_prefix && (format == LY_PREF_JSON)) {
7012 /* we must adjust the module to inherit the one from the context node */
7013 moveto_mod = node->schema->module;
7014 }
7015
7016 /* search in children, do not repeat the same search */
7017 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007018 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007019
7020 /* additional context check */
7021 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7022 scnode = NULL;
7023 }
7024
7025 if (scnode) {
7026 if (*found) {
7027 /* we found a schema node with the same name but at different level, give up, too complicated
7028 * (more hash-based searches would be required, not supported) */
7029 return LY_ENOT;
7030 } else {
7031 /* remember the found schema node and continue to make sure it can be used */
7032 *found = scnode;
7033 }
7034 }
7035
7036 if (idx) {
7037 /* continue searching all the following models */
7038 goto continue_search;
7039 }
7040
7041 return LY_SUCCESS;
7042}
7043
7044/**
Michal Vaskod3678892020-05-21 10:06:58 +02007045 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7046 *
7047 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7048 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7049 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7050 *
7051 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007052 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007053 * @param[in] attr_axis Whether to search attributes or standard nodes.
7054 * @param[in] all_desc Whether to search all the descendants or children only.
7055 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7056 * @param[in] options XPath options.
7057 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7058 */
7059static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007060eval_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 +02007061 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007062{
Michal Vaskod3678892020-05-21 10:06:58 +02007063 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007064 const char *ncname, *ncname_dict = NULL;
7065 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007066 const struct lys_module *moveto_mod = NULL;
7067 const struct lysc_node *scnode = NULL, *ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02007068 struct ly_path_predicate *predicates = NULL;
7069 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007070 LY_ERR rc = LY_SUCCESS;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007071 ly_bool no_prefix;
Michal Vaskod3678892020-05-21 10:06:58 +02007072
7073 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007074 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007075 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007076
7077 if (!set) {
7078 goto moveto;
7079 }
7080
Michal Vasko004d3152020-06-11 19:59:22 +02007081 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7082 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007083
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007084 /* get the first schema context node */
7085 if (set->used) {
7086 if (set->type == LYXP_SET_NODE_SET) {
7087 ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL;
7088 } else {
7089 ctx_scnode = set->val.scnodes[0].scnode;
7090 }
7091 } else {
7092 ctx_scnode = NULL;
7093 }
7094
7095 /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there
7096 * is no prefix for ::LY_PREF_JSON */
7097 rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007098 LY_CHECK_GOTO(rc, cleanup);
7099
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007100 if (ncname_len == exp->tok_len[*tok_idx - 1]) {
7101 /* remember that there is no prefix */
7102 no_prefix = 1;
7103 } else {
7104 no_prefix = 0;
7105 }
7106
Michal Vaskod3678892020-05-21 10:06:58 +02007107 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
7108 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007109 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007110 if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod,
7111 set->root_type, no_prefix, set->format, &scnode)) {
7112 /* check failed */
7113 scnode = NULL;
7114 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007115 }
7116 }
7117
Michal Vasko004d3152020-06-11 19:59:22 +02007118 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7119 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007120 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7121 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007122 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007123 scnode = NULL;
7124 }
7125 }
7126 }
7127
Michal Vasko004d3152020-06-11 19:59:22 +02007128 if (!scnode && moveto_mod) {
7129 /* we are not able to match based on a schema node and not all the modules match,
7130 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007131 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007132 }
7133
Michal Vasko97ea1632020-11-23 17:00:09 +01007134 if ((set->format == LY_PREF_JSON) && no_prefix) {
7135 /* do not set moveto_mod if there is no explicit prefix for JSON, it should be inherited from
7136 * the specific context node */
7137 moveto_mod = NULL;
7138 }
7139
Michal Vaskod3678892020-05-21 10:06:58 +02007140moveto:
7141 /* move to the attribute(s), data node(s), or schema node(s) */
7142 if (attr_axis) {
7143 if (set && (options & LYXP_SCNODE_ALL)) {
7144 set_scnode_clear_ctx(set);
7145 } else {
7146 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007147 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007148 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007149 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007150 }
7151 LY_CHECK_GOTO(rc, cleanup);
7152 }
7153 } else {
7154 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007155 int64_t i;
7156
Michal Vaskod3678892020-05-21 10:06:58 +02007157 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007158 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007159 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007160 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007161 }
7162 LY_CHECK_GOTO(rc, cleanup);
7163
7164 for (i = set->used - 1; i > -1; --i) {
7165 if (set->val.scnodes[i].in_ctx > 0) {
7166 break;
7167 }
7168 }
7169 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007170 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02007171 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02007172 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007173 free(path);
7174 }
7175 } else {
7176 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007177 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007178 } else {
7179 if (scnode) {
7180 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007181 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007182 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007183 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007184 }
7185 }
7186 LY_CHECK_GOTO(rc, cleanup);
7187 }
7188 }
7189
7190 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007191 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7192 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007193 LY_CHECK_RET(rc);
7194 }
7195
7196cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007197 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007198 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007199 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007200 }
Michal Vaskod3678892020-05-21 10:06:58 +02007201 return rc;
7202}
7203
7204/**
7205 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7206 *
7207 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7208 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7209 * [8] NodeType ::= 'text' | 'node'
7210 *
7211 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007212 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007213 * @param[in] attr_axis Whether to search attributes or standard nodes.
7214 * @param[in] all_desc Whether to search all the descendants or children only.
7215 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7216 * @param[in] options XPath options.
7217 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7218 */
7219static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007220eval_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 +02007221 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007222{
7223 LY_ERR rc;
7224
7225 /* TODO */
7226 (void)attr_axis;
7227 (void)all_desc;
7228
7229 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007230 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007231 if (set->type == LYXP_SET_SCNODE_SET) {
7232 set_scnode_clear_ctx(set);
7233 /* just for the debug message below */
7234 set = NULL;
7235 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007237 rc = xpath_node(NULL, 0, set, options);
7238 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007239 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007240 rc = xpath_text(NULL, 0, set, options);
7241 }
7242 LY_CHECK_RET(rc);
7243 }
7244 }
7245 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007246 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007247 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007248
7249 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007250 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007251 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007252 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007253 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007254
7255 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007256 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007257 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007258 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007259 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007260
7261 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007262 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7263 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007264 LY_CHECK_RET(rc);
7265 }
7266
7267 return LY_SUCCESS;
7268}
7269
7270/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007271 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7272 *
7273 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7274 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007275 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276 *
7277 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007278 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 * @param[in] all_desc Whether to search all the descendants or children only.
7280 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7281 * @param[in] options XPath options.
7282 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7283 */
7284static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007285eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7286 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007287{
Radek Krejci857189e2020-09-01 13:26:36 +02007288 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007289 LY_ERR rc;
7290
7291 goto step;
7292 do {
7293 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007294 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007295 all_desc = 0;
7296 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007297 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298 all_desc = 1;
7299 }
7300 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007301 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007302 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303
7304step:
Michal Vaskod3678892020-05-21 10:06:58 +02007305 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007306 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007307 attr_axis = 1;
7308
7309 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007310 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007311 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007312 } else {
7313 attr_axis = 0;
7314 }
7315
Michal Vasko03ff5a72019-09-11 13:49:33 +02007316 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007317 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007318 case LYXP_TOKEN_DOT:
7319 /* evaluate '.' */
7320 if (set && (options & LYXP_SCNODE_ALL)) {
7321 rc = moveto_scnode_self(set, all_desc, options);
7322 } else {
7323 rc = moveto_self(set, all_desc, options);
7324 }
7325 LY_CHECK_RET(rc);
7326 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007327 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007328 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007329 break;
7330
7331 case LYXP_TOKEN_DDOT:
7332 /* evaluate '..' */
7333 if (set && (options & LYXP_SCNODE_ALL)) {
7334 rc = moveto_scnode_parent(set, all_desc, options);
7335 } else {
7336 rc = moveto_parent(set, all_desc, options);
7337 }
7338 LY_CHECK_RET(rc);
7339 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007340 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007341 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 break;
7343
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007345 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007346 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007348 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007349
Michal Vaskod3678892020-05-21 10:06:58 +02007350 case LYXP_TOKEN_NODETYPE:
7351 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007352 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007353 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 break;
7355
7356 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007357 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 }
Michal Vasko004d3152020-06-11 19:59:22 +02007359 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007360
7361 return LY_SUCCESS;
7362}
7363
7364/**
7365 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7366 *
7367 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7368 *
7369 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007370 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007371 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7372 * @param[in] options XPath options.
7373 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7374 */
7375static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007376eval_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 +02007377{
Radek Krejci857189e2020-09-01 13:26:36 +02007378 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379
7380 if (set) {
7381 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007382 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 }
7384
7385 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007386 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 /* evaluate '/' - deferred */
7388 all_desc = 0;
7389 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007390 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007391 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007392
Michal Vasko004d3152020-06-11 19:59:22 +02007393 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394 return LY_SUCCESS;
7395 }
Michal Vasko004d3152020-06-11 19:59:22 +02007396 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 case LYXP_TOKEN_DOT:
7398 case LYXP_TOKEN_DDOT:
7399 case LYXP_TOKEN_AT:
7400 case LYXP_TOKEN_NAMETEST:
7401 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007402 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 break;
7404 default:
7405 break;
7406 }
7407
Michal Vasko03ff5a72019-09-11 13:49:33 +02007408 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007409 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7411 all_desc = 1;
7412 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007413 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007414 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415
Michal Vaskob0099a92020-08-31 14:55:23 +02007416 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417 }
7418
7419 return LY_SUCCESS;
7420}
7421
7422/**
7423 * @brief Evaluate FunctionCall. Logs directly on error.
7424 *
Michal Vaskod3678892020-05-21 10:06:58 +02007425 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 *
7427 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007428 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7430 * @param[in] options XPath options.
7431 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7432 */
7433static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007434eval_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 +02007435{
7436 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007437
Radek Krejci1deb5be2020-08-26 16:43:36 +02007438 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007439 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 struct lyxp_set **args = NULL, **args_aux;
7441
7442 if (set) {
7443 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007444 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007446 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007448 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449 xpath_func = &xpath_sum;
7450 }
7451 break;
7452 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007453 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007455 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007457 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007458 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007459 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007460 xpath_func = &xpath_true;
7461 }
7462 break;
7463 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007464 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007466 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007468 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007470 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007472 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 xpath_func = &xpath_deref;
7474 }
7475 break;
7476 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007477 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007478 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007479 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007480 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007481 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 xpath_func = &xpath_string;
7483 }
7484 break;
7485 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007486 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007488 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007490 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 xpath_func = &xpath_current;
7492 }
7493 break;
7494 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007495 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007497 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007499 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 xpath_func = &xpath_re_match;
7501 }
7502 break;
7503 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007504 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007505 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007506 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 xpath_func = &xpath_translate;
7508 }
7509 break;
7510 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007511 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007513 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007515 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 xpath_func = &xpath_bit_is_set;
7517 }
7518 break;
7519 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007520 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_starts_with;
7522 }
7523 break;
7524 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007525 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_derived_from;
7527 }
7528 break;
7529 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007530 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007532 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_string_length;
7534 }
7535 break;
7536 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007537 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007539 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540 xpath_func = &xpath_substring_after;
7541 }
7542 break;
7543 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007544 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_substring_before;
7546 }
7547 break;
7548 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007549 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 xpath_func = &xpath_derived_from_or_self;
7551 }
7552 break;
7553 }
7554
7555 if (!xpath_func) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007556 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 +02007557 return LY_EVALID;
7558 }
7559 }
7560
7561 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007562 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007563 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007564
7565 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007566 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007568 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007569 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570
7571 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007572 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007573 if (set) {
7574 args = malloc(sizeof *args);
7575 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7576 arg_count = 1;
7577 args[0] = set_copy(set);
7578 if (!args[0]) {
7579 rc = LY_EMEM;
7580 goto cleanup;
7581 }
7582
Michal Vasko004d3152020-06-11 19:59:22 +02007583 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 LY_CHECK_GOTO(rc, cleanup);
7585 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007586 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 LY_CHECK_GOTO(rc, cleanup);
7588 }
7589 }
Michal Vasko004d3152020-06-11 19:59:22 +02007590 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007592 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007593 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594
7595 if (set) {
7596 ++arg_count;
7597 args_aux = realloc(args, arg_count * sizeof *args);
7598 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7599 args = args_aux;
7600 args[arg_count - 1] = set_copy(set);
7601 if (!args[arg_count - 1]) {
7602 rc = LY_EMEM;
7603 goto cleanup;
7604 }
7605
Michal Vasko004d3152020-06-11 19:59:22 +02007606 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 LY_CHECK_GOTO(rc, cleanup);
7608 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007609 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610 LY_CHECK_GOTO(rc, cleanup);
7611 }
7612 }
7613
7614 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007615 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007617 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007618 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619
7620 if (set) {
7621 /* evaluate function */
7622 rc = xpath_func(args, arg_count, set, options);
7623
7624 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007625 /* merge all nodes from arg evaluations */
7626 for (i = 0; i < arg_count; ++i) {
7627 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007628 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 }
7630 }
7631 } else {
7632 rc = LY_SUCCESS;
7633 }
7634
7635cleanup:
7636 for (i = 0; i < arg_count; ++i) {
7637 lyxp_set_free(args[i]);
7638 }
7639 free(args);
7640
7641 return rc;
7642}
7643
7644/**
7645 * @brief Evaluate Number. Logs directly on error.
7646 *
7647 * @param[in] ctx Context for errors.
7648 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007649 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7651 * @return LY_ERR
7652 */
7653static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007654eval_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 +02007655{
7656 long double num;
7657 char *endptr;
7658
7659 if (set) {
7660 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007661 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007662 if (errno) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007663 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7664 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 +02007665 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007667 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007668 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7669 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 +02007670 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007671 return LY_EVALID;
7672 }
7673
7674 set_fill_number(set, num);
7675 }
7676
7677 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007678 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007679 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007680 return LY_SUCCESS;
7681}
7682
7683/**
7684 * @brief Evaluate PathExpr. Logs directly on error.
7685 *
Michal Vaskod3678892020-05-21 10:06:58 +02007686 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007687 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7688 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7689 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007690 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007691 *
7692 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007693 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007694 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7695 * @param[in] options XPath options.
7696 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7697 */
7698static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007699eval_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 +02007700{
Radek Krejci857189e2020-09-01 13:26:36 +02007701 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007702 LY_ERR rc;
7703
Michal Vasko004d3152020-06-11 19:59:22 +02007704 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007705 case LYXP_TOKEN_PAR1:
7706 /* '(' Expr ')' */
7707
7708 /* '(' */
7709 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007710 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007711 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007712
7713 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007714 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715 LY_CHECK_RET(rc);
7716
7717 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007718 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007719 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007720 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007721 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007722
7723 parent_pos_pred = 0;
7724 goto predicate;
7725
7726 case LYXP_TOKEN_DOT:
7727 case LYXP_TOKEN_DDOT:
7728 case LYXP_TOKEN_AT:
7729 case LYXP_TOKEN_NAMETEST:
7730 case LYXP_TOKEN_NODETYPE:
7731 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007732 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007733 LY_CHECK_RET(rc);
7734 break;
7735
7736 case LYXP_TOKEN_FUNCNAME:
7737 /* FunctionCall */
7738 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007739 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007740 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007741 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007742 }
7743 LY_CHECK_RET(rc);
7744
7745 parent_pos_pred = 1;
7746 goto predicate;
7747
Michal Vasko3e48bf32020-06-01 08:39:07 +02007748 case LYXP_TOKEN_OPER_PATH:
7749 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007751 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752 LY_CHECK_RET(rc);
7753 break;
7754
7755 case LYXP_TOKEN_LITERAL:
7756 /* Literal */
7757 if (!set || (options & LYXP_SCNODE_ALL)) {
7758 if (set) {
7759 set_scnode_clear_ctx(set);
7760 }
Michal Vasko004d3152020-06-11 19:59:22 +02007761 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007762 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007763 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007764 }
7765
7766 parent_pos_pred = 1;
7767 goto predicate;
7768
7769 case LYXP_TOKEN_NUMBER:
7770 /* Number */
7771 if (!set || (options & LYXP_SCNODE_ALL)) {
7772 if (set) {
7773 set_scnode_clear_ctx(set);
7774 }
Michal Vasko004d3152020-06-11 19:59:22 +02007775 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007776 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007777 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007778 }
7779 LY_CHECK_RET(rc);
7780
7781 parent_pos_pred = 1;
7782 goto predicate;
7783
7784 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007785 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 +02007786 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007787 return LY_EVALID;
7788 }
7789
7790 return LY_SUCCESS;
7791
7792predicate:
7793 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007794 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7795 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007796 LY_CHECK_RET(rc);
7797 }
7798
7799 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007800 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801
7802 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007803 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007804 all_desc = 0;
7805 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007806 all_desc = 1;
7807 }
7808
7809 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007810 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007811 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812
Michal Vasko004d3152020-06-11 19:59:22 +02007813 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007814 LY_CHECK_RET(rc);
7815 }
7816
7817 return LY_SUCCESS;
7818}
7819
7820/**
7821 * @brief Evaluate UnionExpr. Logs directly on error.
7822 *
Michal Vaskod3678892020-05-21 10:06:58 +02007823 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007824 *
7825 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007826 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827 * @param[in] repeat How many times this expression is repeated.
7828 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7829 * @param[in] options XPath options.
7830 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7831 */
7832static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007833eval_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 +02007834{
7835 LY_ERR rc = LY_SUCCESS;
7836 struct lyxp_set orig_set, set2;
7837 uint16_t i;
7838
7839 assert(repeat);
7840
7841 set_init(&orig_set, set);
7842 set_init(&set2, set);
7843
7844 set_fill_set(&orig_set, set);
7845
Michal Vasko004d3152020-06-11 19:59:22 +02007846 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 LY_CHECK_GOTO(rc, cleanup);
7848
7849 /* ('|' PathExpr)* */
7850 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007851 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007852 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007853 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007854 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007855
7856 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007857 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007858 LY_CHECK_GOTO(rc, cleanup);
7859 continue;
7860 }
7861
7862 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007863 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864 LY_CHECK_GOTO(rc, cleanup);
7865
7866 /* eval */
7867 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007868 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007869 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007870 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007871 LY_CHECK_GOTO(rc, cleanup);
7872 }
7873 }
7874
7875cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007876 lyxp_set_free_content(&orig_set);
7877 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007878 return rc;
7879}
7880
7881/**
7882 * @brief Evaluate UnaryExpr. Logs directly on error.
7883 *
Michal Vaskod3678892020-05-21 10:06:58 +02007884 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007885 *
7886 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007887 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007888 * @param[in] repeat How many times this expression is repeated.
7889 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7890 * @param[in] options XPath options.
7891 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7892 */
7893static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007894eval_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 +02007895{
7896 LY_ERR rc;
7897 uint16_t this_op, i;
7898
7899 assert(repeat);
7900
7901 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007902 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007903 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007904 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 +02007905
7906 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007907 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007908 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007909 }
7910
Michal Vasko004d3152020-06-11 19:59:22 +02007911 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007912 LY_CHECK_RET(rc);
7913
7914 if (set && (repeat % 2)) {
7915 if (options & LYXP_SCNODE_ALL) {
7916 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7917 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007918 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007919 LY_CHECK_RET(rc);
7920 }
7921 }
7922
7923 return LY_SUCCESS;
7924}
7925
7926/**
7927 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7928 *
Michal Vaskod3678892020-05-21 10:06:58 +02007929 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930 * | MultiplicativeExpr '*' UnaryExpr
7931 * | MultiplicativeExpr 'div' UnaryExpr
7932 * | MultiplicativeExpr 'mod' UnaryExpr
7933 *
7934 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007935 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007936 * @param[in] repeat How many times this expression is repeated.
7937 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7938 * @param[in] options XPath options.
7939 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7940 */
7941static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007942eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7943 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007944{
7945 LY_ERR rc;
7946 uint16_t this_op;
7947 struct lyxp_set orig_set, set2;
7948 uint16_t i;
7949
7950 assert(repeat);
7951
7952 set_init(&orig_set, set);
7953 set_init(&set2, set);
7954
7955 set_fill_set(&orig_set, set);
7956
Michal Vasko004d3152020-06-11 19:59:22 +02007957 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007958 LY_CHECK_GOTO(rc, cleanup);
7959
7960 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7961 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007962 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963
Michal Vasko004d3152020-06-11 19:59:22 +02007964 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007966 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007967 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007968
7969 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007970 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 LY_CHECK_GOTO(rc, cleanup);
7972 continue;
7973 }
7974
7975 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007976 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007977 LY_CHECK_GOTO(rc, cleanup);
7978
7979 /* eval */
7980 if (options & LYXP_SCNODE_ALL) {
7981 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007982 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983 set_scnode_clear_ctx(set);
7984 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007985 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007986 LY_CHECK_GOTO(rc, cleanup);
7987 }
7988 }
7989
7990cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007991 lyxp_set_free_content(&orig_set);
7992 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 return rc;
7994}
7995
7996/**
7997 * @brief Evaluate AdditiveExpr. Logs directly on error.
7998 *
Michal Vaskod3678892020-05-21 10:06:58 +02007999 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008000 * | AdditiveExpr '+' MultiplicativeExpr
8001 * | AdditiveExpr '-' MultiplicativeExpr
8002 *
8003 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008004 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008005 * @param[in] repeat How many times this expression is repeated.
8006 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8007 * @param[in] options XPath options.
8008 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8009 */
8010static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008011eval_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 +02008012{
8013 LY_ERR rc;
8014 uint16_t this_op;
8015 struct lyxp_set orig_set, set2;
8016 uint16_t i;
8017
8018 assert(repeat);
8019
8020 set_init(&orig_set, set);
8021 set_init(&set2, set);
8022
8023 set_fill_set(&orig_set, set);
8024
Michal Vasko004d3152020-06-11 19:59:22 +02008025 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026 LY_CHECK_GOTO(rc, cleanup);
8027
8028 /* ('+' / '-' MultiplicativeExpr)* */
8029 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008030 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008031
Michal Vasko004d3152020-06-11 19:59:22 +02008032 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008033 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008034 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008035 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008036
8037 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008038 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008039 LY_CHECK_GOTO(rc, cleanup);
8040 continue;
8041 }
8042
8043 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008044 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045 LY_CHECK_GOTO(rc, cleanup);
8046
8047 /* eval */
8048 if (options & LYXP_SCNODE_ALL) {
8049 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008050 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008051 set_scnode_clear_ctx(set);
8052 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008053 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 LY_CHECK_GOTO(rc, cleanup);
8055 }
8056 }
8057
8058cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008059 lyxp_set_free_content(&orig_set);
8060 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 return rc;
8062}
8063
8064/**
8065 * @brief Evaluate RelationalExpr. Logs directly on error.
8066 *
Michal Vaskod3678892020-05-21 10:06:58 +02008067 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 * | RelationalExpr '<' AdditiveExpr
8069 * | RelationalExpr '>' AdditiveExpr
8070 * | RelationalExpr '<=' AdditiveExpr
8071 * | RelationalExpr '>=' AdditiveExpr
8072 *
8073 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008074 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008075 * @param[in] repeat How many times this expression is repeated.
8076 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8077 * @param[in] options XPath options.
8078 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8079 */
8080static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008081eval_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 +02008082{
8083 LY_ERR rc;
8084 uint16_t this_op;
8085 struct lyxp_set orig_set, set2;
8086 uint16_t i;
8087
8088 assert(repeat);
8089
8090 set_init(&orig_set, set);
8091 set_init(&set2, set);
8092
8093 set_fill_set(&orig_set, set);
8094
Michal Vasko004d3152020-06-11 19:59:22 +02008095 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096 LY_CHECK_GOTO(rc, cleanup);
8097
8098 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8099 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008100 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101
Michal Vasko004d3152020-06-11 19:59:22 +02008102 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008103 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008104 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008105 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106
8107 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008108 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109 LY_CHECK_GOTO(rc, cleanup);
8110 continue;
8111 }
8112
8113 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008114 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 LY_CHECK_GOTO(rc, cleanup);
8116
8117 /* eval */
8118 if (options & LYXP_SCNODE_ALL) {
8119 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008120 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008121 set_scnode_clear_ctx(set);
8122 } else {
8123 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8124 LY_CHECK_GOTO(rc, cleanup);
8125 }
8126 }
8127
8128cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008129 lyxp_set_free_content(&orig_set);
8130 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008131 return rc;
8132}
8133
8134/**
8135 * @brief Evaluate EqualityExpr. Logs directly on error.
8136 *
Michal Vaskod3678892020-05-21 10:06:58 +02008137 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008138 * | EqualityExpr '!=' RelationalExpr
8139 *
8140 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008141 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008142 * @param[in] repeat How many times this expression is repeated.
8143 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8144 * @param[in] options XPath options.
8145 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8146 */
8147static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008148eval_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 +02008149{
8150 LY_ERR rc;
8151 uint16_t this_op;
8152 struct lyxp_set orig_set, set2;
8153 uint16_t i;
8154
8155 assert(repeat);
8156
8157 set_init(&orig_set, set);
8158 set_init(&set2, set);
8159
8160 set_fill_set(&orig_set, set);
8161
Michal Vasko004d3152020-06-11 19:59:22 +02008162 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008163 LY_CHECK_GOTO(rc, cleanup);
8164
8165 /* ('=' / '!=' RelationalExpr)* */
8166 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008167 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008168
Michal Vasko004d3152020-06-11 19:59:22 +02008169 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008170 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008171 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008172 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008173
8174 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008175 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176 LY_CHECK_GOTO(rc, cleanup);
8177 continue;
8178 }
8179
8180 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008181 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008182 LY_CHECK_GOTO(rc, cleanup);
8183
8184 /* eval */
8185 if (options & LYXP_SCNODE_ALL) {
8186 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008187 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8188 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008189 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 set_scnode_clear_ctx(set);
8191 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8193 LY_CHECK_GOTO(rc, cleanup);
8194 }
8195 }
8196
8197cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008198 lyxp_set_free_content(&orig_set);
8199 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008200 return rc;
8201}
8202
8203/**
8204 * @brief Evaluate AndExpr. Logs directly on error.
8205 *
Michal Vaskod3678892020-05-21 10:06:58 +02008206 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008207 *
8208 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008209 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008210 * @param[in] repeat How many times this expression is repeated.
8211 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8212 * @param[in] options XPath options.
8213 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8214 */
8215static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008216eval_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 +02008217{
8218 LY_ERR rc;
8219 struct lyxp_set orig_set, set2;
8220 uint16_t i;
8221
8222 assert(repeat);
8223
8224 set_init(&orig_set, set);
8225 set_init(&set2, set);
8226
8227 set_fill_set(&orig_set, set);
8228
Michal Vasko004d3152020-06-11 19:59:22 +02008229 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 LY_CHECK_GOTO(rc, cleanup);
8231
8232 /* cast to boolean, we know that will be the final result */
8233 if (set && (options & LYXP_SCNODE_ALL)) {
8234 set_scnode_clear_ctx(set);
8235 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008236 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237 }
8238
8239 /* ('and' EqualityExpr)* */
8240 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008241 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8242 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008243 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008244 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245
8246 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008247 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8248 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 LY_CHECK_GOTO(rc, cleanup);
8250 continue;
8251 }
8252
8253 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008254 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 LY_CHECK_GOTO(rc, cleanup);
8256
8257 /* eval - just get boolean value actually */
8258 if (set->type == LYXP_SET_SCNODE_SET) {
8259 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008260 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008262 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 set_fill_set(set, &set2);
8264 }
8265 }
8266
8267cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008268 lyxp_set_free_content(&orig_set);
8269 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 return rc;
8271}
8272
8273/**
8274 * @brief Evaluate OrExpr. Logs directly on error.
8275 *
Michal Vaskod3678892020-05-21 10:06:58 +02008276 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 *
8278 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008279 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008280 * @param[in] repeat How many times this expression is repeated.
8281 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8282 * @param[in] options XPath options.
8283 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8284 */
8285static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008286eval_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 +02008287{
8288 LY_ERR rc;
8289 struct lyxp_set orig_set, set2;
8290 uint16_t i;
8291
8292 assert(repeat);
8293
8294 set_init(&orig_set, set);
8295 set_init(&set2, set);
8296
8297 set_fill_set(&orig_set, set);
8298
Michal Vasko004d3152020-06-11 19:59:22 +02008299 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008300 LY_CHECK_GOTO(rc, cleanup);
8301
8302 /* cast to boolean, we know that will be the final result */
8303 if (set && (options & LYXP_SCNODE_ALL)) {
8304 set_scnode_clear_ctx(set);
8305 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008306 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008307 }
8308
8309 /* ('or' AndExpr)* */
8310 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008311 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8312 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008313 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008314 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315
8316 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008317 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8318 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008319 LY_CHECK_GOTO(rc, cleanup);
8320 continue;
8321 }
8322
8323 set_fill_set(&set2, &orig_set);
8324 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8325 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008326 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327 LY_CHECK_GOTO(rc, cleanup);
8328
8329 /* eval - just get boolean value actually */
8330 if (set->type == LYXP_SET_SCNODE_SET) {
8331 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008332 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008334 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 set_fill_set(set, &set2);
8336 }
8337 }
8338
8339cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008340 lyxp_set_free_content(&orig_set);
8341 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342 return rc;
8343}
8344
8345/**
Michal Vasko004d3152020-06-11 19:59:22 +02008346 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008347 *
8348 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008349 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 * @param[in] etype Expression type to evaluate.
8351 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8352 * @param[in] options XPath options.
8353 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8354 */
8355static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008356eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8357 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358{
8359 uint16_t i, count;
8360 enum lyxp_expr_type next_etype;
8361 LY_ERR rc;
8362
8363 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008364 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 next_etype = LYXP_EXPR_NONE;
8366 } else {
8367 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008368 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369
8370 /* select one-priority lower because etype expression called us */
8371 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008372 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008374 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 } else {
8376 next_etype = LYXP_EXPR_NONE;
8377 }
8378 }
8379
8380 /* decide what expression are we parsing based on the repeat */
8381 switch (next_etype) {
8382 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008383 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 break;
8385 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008386 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008387 break;
8388 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008389 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 break;
8391 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008392 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008393 break;
8394 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008395 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008396 break;
8397 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008398 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 break;
8400 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008401 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008402 break;
8403 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008404 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008405 break;
8406 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008407 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 break;
8409 default:
8410 LOGINT_RET(set->ctx);
8411 }
8412
8413 return rc;
8414}
8415
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008416/**
8417 * @brief Get root type.
8418 *
8419 * @param[in] ctx_node Context node.
8420 * @param[in] ctx_scnode Schema context node.
8421 * @param[in] options XPath options.
8422 * @return Root type.
8423 */
8424static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008425lyxp_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 +01008426{
Michal Vasko6b26e742020-07-17 15:02:10 +02008427 const struct lysc_node *op;
8428
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008429 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008430 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008431 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008432
8433 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008434 /* general root that can access everything */
8435 return LYXP_NODE_ROOT;
8436 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8437 /* root context node can access only config data (because we said so, it is unspecified) */
8438 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008439 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008440 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008441 }
8442
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008443 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008444 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008445 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008446
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008447 if (op || !(options & LYXP_SCHEMA)) {
8448 /* general root that can access everything */
8449 return LYXP_NODE_ROOT;
8450 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008451 /* root context node can access only config data (because we said so, it is unspecified) */
8452 return LYXP_NODE_ROOT_CONFIG;
8453 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008454 return LYXP_NODE_ROOT;
8455}
8456
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008458lyxp_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 +02008459 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 +02008460{
Michal Vasko004d3152020-06-11 19:59:22 +02008461 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 LY_ERR rc;
8463
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008464 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8465 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8466 LOGARG(NULL, "Current module must be set if schema format is used.");
8467 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008468 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469
8470 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008472 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008473 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8474 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8475
8476 set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL)));
8477 set->cur_node = ctx_node;
8478 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008479 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8480 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008481 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008482 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008483 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008484 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008485
8486 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008487 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008489 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490 }
8491
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 return rc;
8493}
8494
8495#if 0
8496
8497/* full xml printing of set elements, not used currently */
8498
8499void
8500lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8501{
8502 uint32_t i;
8503 char *str_num;
8504 struct lyout out;
8505
8506 memset(&out, 0, sizeof out);
8507
8508 out.type = LYOUT_STREAM;
8509 out.method.f = f;
8510
8511 switch (set->type) {
8512 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008513 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008514 break;
8515 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008516 ly_print_(&out, "Boolean XPath set:\n");
8517 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008518 break;
8519 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008520 ly_print_(&out, "String XPath set:\n");
8521 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 break;
8523 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008524 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008525
8526 if (isnan(set->value.num)) {
8527 str_num = strdup("NaN");
8528 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8529 str_num = strdup("0");
8530 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8531 str_num = strdup("Infinity");
8532 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8533 str_num = strdup("-Infinity");
8534 } else if ((long long)set->value.num == set->value.num) {
8535 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8536 str_num = NULL;
8537 }
8538 } else {
8539 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8540 str_num = NULL;
8541 }
8542 }
8543 if (!str_num) {
8544 LOGMEM;
8545 return;
8546 }
Michal Vasko5233e962020-08-14 14:26:20 +02008547 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548 free(str_num);
8549 break;
8550 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008551 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552
8553 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008554 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 switch (set->node_type[i]) {
8556 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008557 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558 break;
8559 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008560 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 break;
8562 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008563 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 break;
8565 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008566 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 break;
8568 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008569 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 break;
8571 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008572 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008573 break;
8574 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008575 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008576 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008577 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578 break;
8579 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008580 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 +02008581 break;
8582 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008583 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 +02008584 break;
8585 }
8586 }
8587 break;
8588 }
8589}
8590
8591#endif
8592
8593LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008594lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008595{
8596 long double num;
8597 char *str;
8598 LY_ERR rc;
8599
8600 if (!set || (set->type == target)) {
8601 return LY_SUCCESS;
8602 }
8603
8604 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008605 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606
8607 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008608 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 return LY_EINVAL;
8610 }
8611
8612 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008613 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 switch (set->type) {
8615 case LYXP_SET_NUMBER:
8616 if (isnan(set->val.num)) {
8617 set->val.str = strdup("NaN");
8618 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8619 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8620 set->val.str = strdup("0");
8621 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8622 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8623 set->val.str = strdup("Infinity");
8624 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8625 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8626 set->val.str = strdup("-Infinity");
8627 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8628 } else if ((long long)set->val.num == set->val.num) {
8629 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8630 LOGMEM_RET(set->ctx);
8631 }
8632 set->val.str = str;
8633 } else {
8634 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8635 LOGMEM_RET(set->ctx);
8636 }
8637 set->val.str = str;
8638 }
8639 break;
8640 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008641 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 set->val.str = strdup("true");
8643 } else {
8644 set->val.str = strdup("false");
8645 }
8646 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8647 break;
8648 case LYXP_SET_NODE_SET:
8649 assert(set->used);
8650
8651 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008652 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008653
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008654 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008655 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008656 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008657 set->val.str = str;
8658 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008659 default:
8660 LOGINT_RET(set->ctx);
8661 }
8662 set->type = LYXP_SET_STRING;
8663 }
8664
8665 /* to NUMBER */
8666 if (target == LYXP_SET_NUMBER) {
8667 switch (set->type) {
8668 case LYXP_SET_STRING:
8669 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008670 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008671 set->val.num = num;
8672 break;
8673 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008674 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675 set->val.num = 1;
8676 } else {
8677 set->val.num = 0;
8678 }
8679 break;
8680 default:
8681 LOGINT_RET(set->ctx);
8682 }
8683 set->type = LYXP_SET_NUMBER;
8684 }
8685
8686 /* to BOOLEAN */
8687 if (target == LYXP_SET_BOOLEAN) {
8688 switch (set->type) {
8689 case LYXP_SET_NUMBER:
8690 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008691 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008692 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008693 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008694 }
8695 break;
8696 case LYXP_SET_STRING:
8697 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008698 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008699 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008700 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008701 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008702 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008703 }
8704 break;
8705 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008706 if (set->used) {
8707 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008708 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008709 } else {
8710 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008711 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008712 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008713 break;
8714 default:
8715 LOGINT_RET(set->ctx);
8716 }
8717 set->type = LYXP_SET_BOOLEAN;
8718 }
8719
Michal Vasko03ff5a72019-09-11 13:49:33 +02008720 return LY_SUCCESS;
8721}
8722
8723LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008724lyxp_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 +02008725 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008726{
Michal Vasko004d3152020-06-11 19:59:22 +02008727 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008728
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008729 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8730 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8731 LOGARG(NULL, "Current module must be set if schema format is used.");
8732 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008733 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008734
8735 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736 memset(set, 0, sizeof *set);
8737 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008738 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8739 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 +01008740 set->val.scnodes[0].in_ctx = -2;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008741
8742 set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL);
8743 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008744 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008745 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8746 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008747 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008749 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008750
8751 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008752 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008753}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008754
8755API const char *
8756lyxp_get_expr(const struct lyxp_expr *path)
8757{
8758 if (!path) {
8759 return NULL;
8760 }
8761
8762 return path->expr;
8763}