blob: c1f043e616a4968ff163ec34c2002056f817c6c8 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
24#include <limits.h>
25#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "hash_table.h"
Radek Krejci7931b192020-06-25 17:05:03 +020036#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020037#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020038#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "printer.h"
40#include "printer_data.h"
41#include "tree.h"
42#include "tree_data_internal.h"
43#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vasko004d3152020-06-11 19:59:22 +020046static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
47static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020048
49/**
50 * @brief Print the type of an XPath \p set.
51 *
52 * @param[in] set Set to use.
53 * @return Set type string.
54 */
55static const char *
56print_set_type(struct lyxp_set *set)
57{
58 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020059 case LYXP_SET_NODE_SET:
60 return "node set";
61 case LYXP_SET_SCNODE_SET:
62 return "schema node set";
63 case LYXP_SET_BOOLEAN:
64 return "boolean";
65 case LYXP_SET_NUMBER:
66 return "number";
67 case LYXP_SET_STRING:
68 return "string";
69 }
70
71 return NULL;
72}
73
Michal Vasko24cddf82020-06-01 08:17:01 +020074const char *
75lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020076{
77 switch (tok) {
78 case LYXP_TOKEN_PAR1:
79 return "(";
80 case LYXP_TOKEN_PAR2:
81 return ")";
82 case LYXP_TOKEN_BRACK1:
83 return "[";
84 case LYXP_TOKEN_BRACK2:
85 return "]";
86 case LYXP_TOKEN_DOT:
87 return ".";
88 case LYXP_TOKEN_DDOT:
89 return "..";
90 case LYXP_TOKEN_AT:
91 return "@";
92 case LYXP_TOKEN_COMMA:
93 return ",";
94 case LYXP_TOKEN_NAMETEST:
95 return "NameTest";
96 case LYXP_TOKEN_NODETYPE:
97 return "NodeType";
98 case LYXP_TOKEN_FUNCNAME:
99 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200100 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200101 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_EQUAL:
103 return "Operator(Equal)";
104 case LYXP_TOKEN_OPER_NEQUAL:
105 return "Operator(Non-equal)";
106 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200107 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200108 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200115 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116 case LYXP_TOKEN_LITERAL:
117 return "Literal";
118 case LYXP_TOKEN_NUMBER:
119 return "Number";
120 default:
121 LOGINT(NULL);
122 return "";
123 }
124}
125
126/**
127 * @brief Print the whole expression \p exp to debug output.
128 *
129 * @param[in] exp Expression to use.
130 */
131static void
132print_expr_struct_debug(struct lyxp_expr *exp)
133{
134 uint16_t i, j;
135 char tmp[128];
136
137 if (!exp || (ly_log_level < LY_LLDBG)) {
138 return;
139 }
140
141 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
142 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200143 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko03ff5a72019-09-11 13:49:33 +0200144 &exp->expr[exp->tok_pos[i]]);
145 if (exp->repeat[i]) {
146 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
147 for (j = 1; exp->repeat[i][j]; ++j) {
148 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
149 }
150 strcat(tmp, ")");
151 }
152 LOGDBG(LY_LDGXPATH, tmp);
153 }
154}
155
156#ifndef NDEBUG
157
158/**
159 * @brief Print XPath set content to debug output.
160 *
161 * @param[in] set Set to print.
162 */
163static void
164print_set_debug(struct lyxp_set *set)
165{
166 uint32_t i;
167 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200168 struct lyxp_set_node *item;
169 struct lyxp_set_scnode *sitem;
170
171 if (ly_log_level < LY_LLDBG) {
172 return;
173 }
174
175 switch (set->type) {
176 case LYXP_SET_NODE_SET:
177 LOGDBG(LY_LDGXPATH, "set NODE SET:");
178 for (i = 0; i < set->used; ++i) {
179 item = &set->val.nodes[i];
180
181 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100182 case LYXP_NODE_NONE:
183 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
184 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200185 case LYXP_NODE_ROOT:
186 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
187 break;
188 case LYXP_NODE_ROOT_CONFIG:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
190 break;
191 case LYXP_NODE_ELEM:
192 if ((item->node->schema->nodetype == LYS_LIST)
193 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vaskob7be7a82020-08-20 09:09:04 +0200195 item->node->schema->name, LYD_CANON_VALUE(lyd_node_children(item->node, 0)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200196 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vaskob7be7a82020-08-20 09:09:04 +0200198 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200199 } else {
200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
201 }
202 break;
203 case LYXP_NODE_TEXT:
204 if (item->node->schema->nodetype & LYS_ANYDATA) {
205 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
206 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
207 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200208 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 +0200209 }
210 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100211 case LYXP_NODE_META:
212 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
213 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200214 break;
215 }
216 }
217 break;
218
219 case LYXP_SET_SCNODE_SET:
220 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
221 for (i = 0; i < set->used; ++i) {
222 sitem = &set->val.scnodes[i];
223
224 switch (sitem->type) {
225 case LYXP_NODE_ROOT:
226 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
227 break;
228 case LYXP_NODE_ROOT_CONFIG:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ELEM:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
233 break;
234 default:
235 LOGINT(NULL);
236 break;
237 }
238 }
239 break;
240
Michal Vasko03ff5a72019-09-11 13:49:33 +0200241 case LYXP_SET_BOOLEAN:
242 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200243 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 break;
245
246 case LYXP_SET_STRING:
247 LOGDBG(LY_LDGXPATH, "set STRING");
248 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
249 break;
250
251 case LYXP_SET_NUMBER:
252 LOGDBG(LY_LDGXPATH, "set NUMBER");
253
254 if (isnan(set->val.num)) {
255 str = strdup("NaN");
256 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
257 str = strdup("0");
258 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
259 str = strdup("Infinity");
260 } else if (isinf(set->val.num) && signbit(set->val.num)) {
261 str = strdup("-Infinity");
262 } else if ((long long)set->val.num == set->val.num) {
263 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
264 str = NULL;
265 }
266 } else {
267 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
268 str = NULL;
269 }
270 }
271 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
272
273 LOGDBG(LY_LDGXPATH, "\t%s", str);
274 free(str);
275 }
276}
277
278#endif
279
280/**
281 * @brief Realloc the string \p str.
282 *
283 * @param[in] ctx libyang context for logging.
284 * @param[in] needed How much free space is required.
285 * @param[in,out] str Pointer to the string to use.
286 * @param[in,out] used Used bytes in \p str.
287 * @param[in,out] size Allocated bytes in \p str.
288 * @return LY_ERR
289 */
290static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100291cast_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 +0200292{
293 if (*size - *used < needed) {
294 do {
295 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
296 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
297 return LY_EINVAL;
298 }
299 *size += LYXP_STRING_CAST_SIZE_STEP;
300 } while (*size - *used < needed);
301 *str = ly_realloc(*str, *size * sizeof(char));
302 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
303 }
304
305 return LY_SUCCESS;
306}
307
308/**
309 * @brief Cast nodes recursively to one string @p str.
310 *
311 * @param[in] node Node to cast.
312 * @param[in] fake_cont Whether to put the data into a "fake" container.
313 * @param[in] root_type Type of the XPath root.
314 * @param[in] indent Current indent.
315 * @param[in,out] str Resulting string.
316 * @param[in,out] used Used bytes in @p str.
317 * @param[in,out] size Allocated bytes in @p str.
318 * @return LY_ERR
319 */
320static LY_ERR
321cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
322 uint16_t *used, uint16_t *size)
323{
Radek Krejci7f769d72020-07-11 23:13:56 +0200324 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200325 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200327 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 struct lyd_node_any *any;
329 LY_ERR rc;
330
331 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
332 return LY_SUCCESS;
333 }
334
335 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200336 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200337 LY_CHECK_RET(rc);
338 strcpy(*str + (*used - 1), "\n");
339 ++(*used);
340
341 ++indent;
342 }
343
344 switch (node->schema->nodetype) {
345 case LYS_CONTAINER:
346 case LYS_LIST:
347 case LYS_RPC:
348 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200349 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200350 LY_CHECK_RET(rc);
351 strcpy(*str + (*used - 1), "\n");
352 ++(*used);
353
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200354 for (child = lyd_node_children(node, 0); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200355 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
356 LY_CHECK_RET(rc);
357 }
358
359 break;
360
361 case LYS_LEAF:
362 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200363 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200364
365 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200366 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 +0200367 memset(*str + (*used - 1), ' ', indent * 2);
368 *used += indent * 2;
369
370 /* print value */
371 if (*used == 1) {
372 sprintf(*str + (*used - 1), "%s", value_str);
373 *used += strlen(value_str);
374 } else {
375 sprintf(*str + (*used - 1), "%s\n", value_str);
376 *used += strlen(value_str) + 1;
377 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200378
379 break;
380
381 case LYS_ANYXML:
382 case LYS_ANYDATA:
383 any = (struct lyd_node_any *)node;
384 if (!(void *)any->value.tree) {
385 /* no content */
386 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200387 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200388 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200389 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100390
Michal Vasko60ea6352020-06-29 13:39:39 +0200391 if (any->value_type == LYD_ANYDATA_LYB) {
392 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200393 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 +0200394 /* successfully parsed */
395 free(any->value.mem);
396 any->value.tree = tree;
397 any->value_type = LYD_ANYDATA_DATATREE;
398 }
Radek Krejci7931b192020-06-25 17:05:03 +0200399 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200400 }
401
Michal Vasko03ff5a72019-09-11 13:49:33 +0200402 switch (any->value_type) {
403 case LYD_ANYDATA_STRING:
404 case LYD_ANYDATA_XML:
405 case LYD_ANYDATA_JSON:
406 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200407 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200408 break;
409 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200410 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200411 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200412 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100413 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200414 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200416 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 }
419 }
420
421 line = strtok_r(buf, "\n", &ptr);
422 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200423 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200424 if (rc != LY_SUCCESS) {
425 free(buf);
426 return rc;
427 }
428 memset(*str + (*used - 1), ' ', indent * 2);
429 *used += indent * 2;
430
431 strcpy(*str + (*used - 1), line);
432 *used += strlen(line);
433
434 strcpy(*str + (*used - 1), "\n");
435 *used += 1;
436 } while ((line = strtok_r(NULL, "\n", &ptr)));
437
438 free(buf);
439 break;
440
441 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200442 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200443 }
444
445 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 LY_CHECK_RET(rc);
448 strcpy(*str + (*used - 1), "\n");
449 ++(*used);
450
451 --indent;
452 }
453
454 return LY_SUCCESS;
455}
456
457/**
458 * @brief Cast an element into a string.
459 *
460 * @param[in] node Node to cast.
461 * @param[in] fake_cont Whether to put the data into a "fake" container.
462 * @param[in] root_type Type of the XPath root.
463 * @param[out] str Element cast to dynamically-allocated string.
464 * @return LY_ERR
465 */
466static LY_ERR
467cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
468{
469 uint16_t used, size;
470 LY_ERR rc;
471
472 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200473 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200474 (*str)[0] = '\0';
475 used = 1;
476 size = LYXP_STRING_CAST_SIZE_START;
477
478 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
479 if (rc != LY_SUCCESS) {
480 free(*str);
481 return rc;
482 }
483
484 if (size > used) {
485 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200486 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200487 }
488 return LY_SUCCESS;
489}
490
491/**
492 * @brief Cast a LYXP_SET_NODE_SET set into a string.
493 * Context position aware.
494 *
495 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200496 * @param[out] str Cast dynamically-allocated string.
497 * @return LY_ERR
498 */
499static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100500cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200501{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200502 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100503 case LYXP_NODE_NONE:
504 /* invalid */
505 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 case LYXP_NODE_ROOT:
507 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100508 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509 case LYXP_NODE_ELEM:
510 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100511 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100512 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200513 *str = strdup(set->val.meta[0].meta->value.canonical);
514 if (!*str) {
515 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200516 }
517 return LY_SUCCESS;
518 }
519
520 LOGINT_RET(set->ctx);
521}
522
523/**
524 * @brief Cast a string into an XPath number.
525 *
526 * @param[in] str String to use.
527 * @return Cast number.
528 */
529static long double
530cast_string_to_number(const char *str)
531{
532 long double num;
533 char *ptr;
534
535 errno = 0;
536 num = strtold(str, &ptr);
537 if (errno || *ptr) {
538 num = NAN;
539 }
540 return num;
541}
542
543/**
544 * @brief Callback for checking value equality.
545 *
546 * @param[in] val1_p First value.
547 * @param[in] val2_p Second value.
548 * @param[in] mod Whether hash table is being modified.
549 * @param[in] cb_data Callback data.
550 * @return 0 if not equal, non-zero if equal.
551 */
552static int
553set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
554{
555 struct lyxp_set_hash_node *val1, *val2;
556
557 val1 = (struct lyxp_set_hash_node *)val1_p;
558 val2 = (struct lyxp_set_hash_node *)val2_p;
559
560 if ((val1->node == val2->node) && (val1->type == val2->type)) {
561 return 1;
562 }
563
564 return 0;
565}
566
567/**
568 * @brief Insert node and its hash into set.
569 *
570 * @param[in] set et to insert to.
571 * @param[in] node Node with hash.
572 * @param[in] type Node type.
573 */
574static void
575set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
576{
577 int r;
578 uint32_t i, hash;
579 struct lyxp_set_hash_node hnode;
580
581 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
582 /* create hash table and add all the nodes */
583 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
584 for (i = 0; i < set->used; ++i) {
585 hnode.node = set->val.nodes[i].node;
586 hnode.type = set->val.nodes[i].type;
587
588 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
589 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
590 hash = dict_hash_multi(hash, NULL, 0);
591
592 r = lyht_insert(set->ht, &hnode, hash, NULL);
593 assert(!r);
594 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200595
Michal Vasko9d6befd2019-12-11 14:56:56 +0100596 if (hnode.node == node) {
597 /* it was just added, do not add it twice */
598 node = NULL;
599 }
600 }
601 }
602
603 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200604 /* add the new node into hash table */
605 hnode.node = node;
606 hnode.type = type;
607
608 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
609 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
610 hash = dict_hash_multi(hash, NULL, 0);
611
612 r = lyht_insert(set->ht, &hnode, hash, NULL);
613 assert(!r);
614 (void)r;
615 }
616}
617
618/**
619 * @brief Remove node and its hash from set.
620 *
621 * @param[in] set Set to remove from.
622 * @param[in] node Node to remove.
623 * @param[in] type Node type.
624 */
625static void
626set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
627{
628 int r;
629 struct lyxp_set_hash_node hnode;
630 uint32_t hash;
631
632 if (set->ht) {
633 hnode.node = node;
634 hnode.type = type;
635
636 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
637 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
638 hash = dict_hash_multi(hash, NULL, 0);
639
640 r = lyht_remove(set->ht, &hnode, hash);
641 assert(!r);
642 (void)r;
643
644 if (!set->ht->used) {
645 lyht_free(set->ht);
646 set->ht = NULL;
647 }
648 }
649}
650
651/**
652 * @brief Check whether node is in set based on its hash.
653 *
654 * @param[in] set Set to search in.
655 * @param[in] node Node to search for.
656 * @param[in] type Node type.
657 * @param[in] skip_idx Index in @p set to skip.
658 * @return LY_ERR
659 */
660static LY_ERR
661set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
662{
663 struct lyxp_set_hash_node hnode, *match_p;
664 uint32_t hash;
665
666 hnode.node = node;
667 hnode.type = type;
668
669 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
670 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
671 hash = dict_hash_multi(hash, NULL, 0);
672
673 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
674 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
675 /* we found it on the index that should be skipped, find another */
676 hnode = *match_p;
677 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
678 /* none other found */
679 return LY_SUCCESS;
680 }
681 }
682
683 return LY_EEXIST;
684 }
685
686 /* not found */
687 return LY_SUCCESS;
688}
689
Michal Vaskod3678892020-05-21 10:06:58 +0200690void
691lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200692{
693 if (!set) {
694 return;
695 }
696
697 if (set->type == LYXP_SET_NODE_SET) {
698 free(set->val.nodes);
699 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200700 } else if (set->type == LYXP_SET_SCNODE_SET) {
701 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200702 lyht_free(set->ht);
703 } else {
704 if (set->type == LYXP_SET_STRING) {
705 free(set->val.str);
706 }
707 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200708 }
Michal Vaskod3678892020-05-21 10:06:58 +0200709
710 set->val.nodes = NULL;
711 set->used = 0;
712 set->size = 0;
713 set->ht = NULL;
714 set->ctx_pos = 0;
715 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200716}
717
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100718/**
719 * @brief Free dynamically-allocated set.
720 *
721 * @param[in] set Set to free.
722 */
723static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200724lyxp_set_free(struct lyxp_set *set)
725{
726 if (!set) {
727 return;
728 }
729
Michal Vaskod3678892020-05-21 10:06:58 +0200730 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200731 free(set);
732}
733
734/**
735 * @brief Initialize set context.
736 *
737 * @param[in] new Set to initialize.
738 * @param[in] set Arbitrary initialized set.
739 */
740static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200741set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200742{
743 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200744 if (set) {
745 new->ctx = set->ctx;
746 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100747 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200748 new->context_op = set->context_op;
Michal Vasko02a77382019-09-12 11:47:35 +0200749 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100750 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200751 new->format = set->format;
752 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200753}
754
755/**
756 * @brief Create a deep copy of a set.
757 *
758 * @param[in] set Set to copy.
759 * @return Copy of @p set.
760 */
761static struct lyxp_set *
762set_copy(struct lyxp_set *set)
763{
764 struct lyxp_set *ret;
765 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100766 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767
768 if (!set) {
769 return NULL;
770 }
771
772 ret = malloc(sizeof *ret);
773 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
774 set_init(ret, set);
775
776 if (set->type == LYXP_SET_SCNODE_SET) {
777 ret->type = set->type;
778
779 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100780 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
781 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100782 /* coverity seems to think scnodes can be NULL */
783 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200784 lyxp_set_free(ret);
785 return NULL;
786 }
Michal Vaskoba716542019-12-16 10:01:58 +0100787 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200788 }
789 }
790 } else if (set->type == LYXP_SET_NODE_SET) {
791 ret->type = set->type;
792 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
793 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
794 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
795
796 ret->used = ret->size = set->used;
797 ret->ctx_pos = set->ctx_pos;
798 ret->ctx_size = set->ctx_size;
799 ret->ht = lyht_dup(set->ht);
800 } else {
801 memcpy(ret, set, sizeof *ret);
802 if (set->type == LYXP_SET_STRING) {
803 ret->val.str = strdup(set->val.str);
804 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
805 }
806 }
807
808 return ret;
809}
810
811/**
812 * @brief Fill XPath set with a string. Any current data are disposed of.
813 *
814 * @param[in] set Set to fill.
815 * @param[in] string String to fill into \p set.
816 * @param[in] str_len Length of \p string. 0 is a valid value!
817 */
818static void
819set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
820{
Michal Vaskod3678892020-05-21 10:06:58 +0200821 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822
823 set->type = LYXP_SET_STRING;
824 if ((str_len == 0) && (string[0] != '\0')) {
825 string = "";
826 }
827 set->val.str = strndup(string, str_len);
828}
829
830/**
831 * @brief Fill XPath set with a number. Any current data are disposed of.
832 *
833 * @param[in] set Set to fill.
834 * @param[in] number Number to fill into \p set.
835 */
836static void
837set_fill_number(struct lyxp_set *set, long double number)
838{
Michal Vaskod3678892020-05-21 10:06:58 +0200839 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200840
841 set->type = LYXP_SET_NUMBER;
842 set->val.num = number;
843}
844
845/**
846 * @brief Fill XPath set with a boolean. Any current data are disposed of.
847 *
848 * @param[in] set Set to fill.
849 * @param[in] boolean Boolean to fill into \p set.
850 */
851static void
852set_fill_boolean(struct lyxp_set *set, int boolean)
853{
Michal Vaskod3678892020-05-21 10:06:58 +0200854 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200855
856 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200857 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200858}
859
860/**
861 * @brief Fill XPath set with the value from another set (deep assign).
862 * Any current data are disposed of.
863 *
864 * @param[in] trg Set to fill.
865 * @param[in] src Source set to copy into \p trg.
866 */
867static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200868set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200869{
870 if (!trg || !src) {
871 return;
872 }
873
874 if (trg->type == LYXP_SET_NODE_SET) {
875 free(trg->val.nodes);
876 } else if (trg->type == LYXP_SET_STRING) {
877 free(trg->val.str);
878 }
879 set_init(trg, src);
880
881 if (src->type == LYXP_SET_SCNODE_SET) {
882 trg->type = LYXP_SET_SCNODE_SET;
883 trg->used = src->used;
884 trg->size = src->used;
885
886 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
887 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
888 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
889 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200890 set_fill_boolean(trg, src->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891 } else if (src->type == LYXP_SET_NUMBER) {
892 set_fill_number(trg, src->val.num);
893 } else if (src->type == LYXP_SET_STRING) {
894 set_fill_string(trg, src->val.str, strlen(src->val.str));
895 } else {
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901
Michal Vaskod3678892020-05-21 10:06:58 +0200902 assert(src->type == LYXP_SET_NODE_SET);
903
904 trg->type = LYXP_SET_NODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907 trg->ctx_pos = src->ctx_pos;
908 trg->ctx_size = src->ctx_size;
909
910 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
911 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
912 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
913 if (src->ht) {
914 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200916 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200917 }
918 }
919}
920
921/**
922 * @brief Clear context of all schema nodes.
923 *
924 * @param[in] set Set to clear.
925 */
926static void
927set_scnode_clear_ctx(struct lyxp_set *set)
928{
929 uint32_t i;
930
931 for (i = 0; i < set->used; ++i) {
932 if (set->val.scnodes[i].in_ctx == 1) {
933 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100934 } else if (set->val.scnodes[i].in_ctx == -2) {
935 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200936 }
937 }
938}
939
940/**
941 * @brief Remove a node from a set. Removing last node changes
942 * set into LYXP_SET_EMPTY. Context position aware.
943 *
944 * @param[in] set Set to use.
945 * @param[in] idx Index from @p set of the node to be removed.
946 */
947static void
948set_remove_node(struct lyxp_set *set, uint32_t idx)
949{
950 assert(set && (set->type == LYXP_SET_NODE_SET));
951 assert(idx < set->used);
952
953 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
954
955 --set->used;
956 if (set->used) {
957 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
958 (set->used - idx) * sizeof *set->val.nodes);
959 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200960 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200961 }
962}
963
964/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100965 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200966 *
967 * @param[in] set Set to use.
968 * @param[in] idx Index from @p set of the node to be removed.
969 */
970static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100971set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200972{
973 assert(set && (set->type == LYXP_SET_NODE_SET));
974 assert(idx < set->used);
975
Michal Vasko2caefc12019-11-14 16:07:56 +0100976 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
977 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
978 }
979 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200980}
981
982/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100983 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200984 * set into LYXP_SET_EMPTY. Context position aware.
985 *
986 * @param[in] set Set to consolidate.
987 */
988static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100989set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200990{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200991 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +0200992 int32_t start;
993
Michal Vaskod3678892020-05-21 10:06:58 +0200994 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +0200995
996 orig_used = set->used;
997 set->used = 0;
998 for (i = 0; i < orig_used;) {
999 start = -1;
1000 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001001 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001002 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001003 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001004 end = i;
1005 ++i;
1006 break;
1007 }
1008
1009 ++i;
1010 if (i == orig_used) {
1011 end = i;
1012 }
1013 } while (i < orig_used);
1014
1015 if (start > -1) {
1016 /* move the whole chunk of valid nodes together */
1017 if (set->used != (unsigned)start) {
1018 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1019 }
1020 set->used += end - start;
1021 }
1022 }
Michal Vasko57eab132019-09-24 11:46:26 +02001023}
1024
1025/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001026 * @brief Check for duplicates in a node set.
1027 *
1028 * @param[in] set Set to check.
1029 * @param[in] node Node to look for in @p set.
1030 * @param[in] node_type Type of @p node.
1031 * @param[in] skip_idx Index from @p set to skip.
1032 * @return LY_ERR
1033 */
1034static LY_ERR
1035set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1036{
1037 uint32_t i;
1038
Michal Vasko2caefc12019-11-14 16:07:56 +01001039 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001040 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1041 }
1042
1043 for (i = 0; i < set->used; ++i) {
1044 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1045 continue;
1046 }
1047
1048 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1049 return LY_EEXIST;
1050 }
1051 }
1052
1053 return LY_SUCCESS;
1054}
1055
Michal Vaskoecd62de2019-11-13 12:35:11 +01001056int
1057lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001058{
1059 uint32_t i;
1060
1061 for (i = 0; i < set->used; ++i) {
1062 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1063 continue;
1064 }
1065
1066 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1067 return i;
1068 }
1069 }
1070
1071 return -1;
1072}
1073
Michal Vaskoecd62de2019-11-13 12:35:11 +01001074void
1075lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001076{
1077 uint32_t orig_used, i, j;
1078
Michal Vaskod3678892020-05-21 10:06:58 +02001079 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001080
Michal Vaskod3678892020-05-21 10:06:58 +02001081 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001082 return;
1083 }
1084
Michal Vaskod3678892020-05-21 10:06:58 +02001085 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001086 memcpy(set1, set2, sizeof *set1);
1087 return;
1088 }
1089
1090 if (set1->used + set2->used > set1->size) {
1091 set1->size = set1->used + set2->used;
1092 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1093 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1094 }
1095
1096 orig_used = set1->used;
1097
1098 for (i = 0; i < set2->used; ++i) {
1099 for (j = 0; j < orig_used; ++j) {
1100 /* detect duplicities */
1101 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1102 break;
1103 }
1104 }
1105
1106 if (j == orig_used) {
1107 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1108 ++set1->used;
1109 }
1110 }
1111
Michal Vaskod3678892020-05-21 10:06:58 +02001112 lyxp_set_free_content(set2);
1113 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114}
1115
1116/**
1117 * @brief Insert a node into a set. Context position aware.
1118 *
1119 * @param[in] set Set to use.
1120 * @param[in] node Node to insert to @p set.
1121 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1122 * @param[in] node_type Node type of @p node.
1123 * @param[in] idx Index in @p set to insert into.
1124 */
1125static void
1126set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1127{
Michal Vaskod3678892020-05-21 10:06:58 +02001128 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001129
Michal Vaskod3678892020-05-21 10:06:58 +02001130 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001131 /* first item */
1132 if (idx) {
1133 /* no real harm done, but it is a bug */
1134 LOGINT(set->ctx);
1135 idx = 0;
1136 }
1137 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1138 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1139 set->type = LYXP_SET_NODE_SET;
1140 set->used = 0;
1141 set->size = LYXP_SET_SIZE_START;
1142 set->ctx_pos = 1;
1143 set->ctx_size = 1;
1144 set->ht = NULL;
1145 } else {
1146 /* not an empty set */
1147 if (set->used == set->size) {
1148
1149 /* set is full */
1150 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1151 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1152 set->size += LYXP_SET_SIZE_STEP;
1153 }
1154
1155 if (idx > set->used) {
1156 LOGINT(set->ctx);
1157 idx = set->used;
1158 }
1159
1160 /* make space for the new node */
1161 if (idx < set->used) {
1162 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1163 }
1164 }
1165
1166 /* finally assign the value */
1167 set->val.nodes[idx].node = (struct lyd_node *)node;
1168 set->val.nodes[idx].type = node_type;
1169 set->val.nodes[idx].pos = pos;
1170 ++set->used;
1171
Michal Vasko2caefc12019-11-14 16:07:56 +01001172 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1173 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1174 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001175}
1176
Michal Vaskoecd62de2019-11-13 12:35:11 +01001177int
1178lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001179{
1180 int ret;
1181
1182 assert(set->type == LYXP_SET_SCNODE_SET);
1183
Michal Vaskoecd62de2019-11-13 12:35:11 +01001184 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001185 if (ret > -1) {
1186 set->val.scnodes[ret].in_ctx = 1;
1187 } else {
1188 if (set->used == set->size) {
1189 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1190 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1191 set->size += LYXP_SET_SIZE_STEP;
1192 }
1193
1194 ret = set->used;
1195 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1196 set->val.scnodes[ret].type = node_type;
1197 set->val.scnodes[ret].in_ctx = 1;
1198 ++set->used;
1199 }
1200
1201 return ret;
1202}
1203
1204/**
1205 * @brief Replace a node in a set with another. Context position aware.
1206 *
1207 * @param[in] set Set to use.
1208 * @param[in] node Node to insert to @p set.
1209 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1210 * @param[in] node_type Node type of @p node.
1211 * @param[in] idx Index in @p set of the node to replace.
1212 */
1213static void
1214set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1215{
1216 assert(set && (idx < set->used));
1217
Michal Vasko2caefc12019-11-14 16:07:56 +01001218 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1219 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1220 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001221 set->val.nodes[idx].node = (struct lyd_node *)node;
1222 set->val.nodes[idx].type = node_type;
1223 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001224 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1225 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1226 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001227}
1228
1229/**
1230 * @brief Set all nodes with ctx 1 to a new unique context value.
1231 *
1232 * @param[in] set Set to modify.
1233 * @return New context value.
1234 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001235static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001236set_scnode_new_in_ctx(struct lyxp_set *set)
1237{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001238 uint32_t i;
1239 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001240
1241 assert(set->type == LYXP_SET_SCNODE_SET);
1242
1243 ret_ctx = 3;
1244retry:
1245 for (i = 0; i < set->used; ++i) {
1246 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1247 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1248 goto retry;
1249 }
1250 }
1251 for (i = 0; i < set->used; ++i) {
1252 if (set->val.scnodes[i].in_ctx == 1) {
1253 set->val.scnodes[i].in_ctx = ret_ctx;
1254 }
1255 }
1256
1257 return ret_ctx;
1258}
1259
1260/**
1261 * @brief Get unique @p node position in the data.
1262 *
1263 * @param[in] node Node to find.
1264 * @param[in] node_type Node type of @p node.
1265 * @param[in] root Root node.
1266 * @param[in] root_type Type of the XPath @p root node.
1267 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1268 * be used to increase efficiency and start the DFS from this node.
1269 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1270 * @return Node position.
1271 */
1272static uint32_t
1273get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1274 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1275{
Michal Vasko56daf732020-08-10 10:57:18 +02001276 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001277 uint32_t pos = 1;
1278
1279 assert(prev && prev_pos && !root->prev->next);
1280
1281 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1282 return 0;
1283 }
1284
1285 if (*prev) {
1286 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001287 pos = *prev_pos;
Radek Krejci1e008d22020-08-17 11:37:37 +02001288 for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289 goto dfs_search;
1290 }
1291
1292 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
Michal Vasko56daf732020-08-10 10:57:18 +02001293 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001294dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001295 if (*prev && !elem) {
1296 /* resume previous DFS */
1297 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1298 LYD_TREE_DFS_continue = 0;
1299 }
1300
Michal Vasko03ff5a72019-09-11 13:49:33 +02001301 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001302 /* skip */
1303 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001304 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001305 if (elem == node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001306 break;
1307 }
Michal Vasko56daf732020-08-10 10:57:18 +02001308 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001309 }
Michal Vasko56daf732020-08-10 10:57:18 +02001310
1311 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312 }
1313
1314 /* node found */
1315 if (elem) {
1316 break;
1317 }
1318 }
1319
1320 if (!elem) {
1321 if (!(*prev)) {
1322 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001323 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324 return 0;
1325 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001326 /* start the search again from the beginning */
1327 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001328
Michal Vasko56daf732020-08-10 10:57:18 +02001329 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330 pos = 1;
1331 goto dfs_search;
1332 }
1333 }
1334
1335 /* remember the last found node for next time */
1336 *prev = node;
1337 *prev_pos = pos;
1338
1339 return pos;
1340}
1341
1342/**
1343 * @brief Assign (fill) missing node positions.
1344 *
1345 * @param[in] set Set to fill positions in.
1346 * @param[in] root Context root node.
1347 * @param[in] root_type Context root type.
1348 * @return LY_ERR
1349 */
1350static LY_ERR
1351set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1352{
1353 const struct lyd_node *prev = NULL, *tmp_node;
1354 uint32_t i, tmp_pos = 0;
1355
1356 for (i = 0; i < set->used; ++i) {
1357 if (!set->val.nodes[i].pos) {
1358 tmp_node = NULL;
1359 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001360 case LYXP_NODE_META:
1361 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001362 if (!tmp_node) {
1363 LOGINT_RET(root->schema->module->ctx);
1364 }
1365 /* fallthrough */
1366 case LYXP_NODE_ELEM:
1367 case LYXP_NODE_TEXT:
1368 if (!tmp_node) {
1369 tmp_node = set->val.nodes[i].node;
1370 }
1371 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1372 break;
1373 default:
1374 /* all roots have position 0 */
1375 break;
1376 }
1377 }
1378 }
1379
1380 return LY_SUCCESS;
1381}
1382
1383/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001384 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001385 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001386 * @param[in] meta Metadata to use.
1387 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001388 */
1389static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001390get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001391{
1392 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001393 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001394
Michal Vasko9f96a052020-03-10 09:41:45 +01001395 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001396 ++pos;
1397 }
1398
Michal Vasko9f96a052020-03-10 09:41:45 +01001399 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400 return pos;
1401}
1402
1403/**
1404 * @brief Compare 2 nodes in respect to XPath document order.
1405 *
1406 * @param[in] item1 1st node.
1407 * @param[in] item2 2nd node.
1408 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1409 */
1410static int
1411set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1412{
Michal Vasko9f96a052020-03-10 09:41:45 +01001413 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001414
1415 if (item1->pos < item2->pos) {
1416 return -1;
1417 }
1418
1419 if (item1->pos > item2->pos) {
1420 return 1;
1421 }
1422
1423 /* node positions are equal, the fun case */
1424
1425 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1426 /* special case since text nodes are actually saved as their parents */
1427 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1428 if (item1->type == LYXP_NODE_ELEM) {
1429 assert(item2->type == LYXP_NODE_TEXT);
1430 return -1;
1431 } else {
1432 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1433 return 1;
1434 }
1435 }
1436
Michal Vasko9f96a052020-03-10 09:41:45 +01001437 /* we need meta positions now */
1438 if (item1->type == LYXP_NODE_META) {
1439 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001441 if (item2->type == LYXP_NODE_META) {
1442 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443 }
1444
Michal Vasko9f96a052020-03-10 09:41:45 +01001445 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001446 /* check for duplicates */
1447 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001448 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001449 return 0;
1450 }
1451
Michal Vasko9f96a052020-03-10 09:41:45 +01001452 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001453 /* elem is always first, 2nd node is after it */
1454 if (item1->type == LYXP_NODE_ELEM) {
1455 assert(item2->type != LYXP_NODE_ELEM);
1456 return -1;
1457 }
1458
Michal Vasko9f96a052020-03-10 09:41:45 +01001459 /* 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 +02001460 /* 2nd is before 1st */
1461 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001462 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1463 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1464 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1465 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001466 return 1;
1467 }
1468
Michal Vasko9f96a052020-03-10 09:41:45 +01001469 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001470 /* 2nd is after 1st */
1471 return -1;
1472}
1473
1474/**
1475 * @brief Set cast for comparisons.
1476 *
1477 * @param[in] trg Target set to cast source into.
1478 * @param[in] src Source set.
1479 * @param[in] type Target set type.
1480 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001481 * @return LY_ERR
1482 */
1483static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001484set_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 +02001485{
1486 assert(src->type == LYXP_SET_NODE_SET);
1487
1488 set_init(trg, src);
1489
1490 /* insert node into target set */
1491 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1492
1493 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001494 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001495}
1496
Michal Vasko4c7763f2020-07-27 17:40:37 +02001497/**
1498 * @brief Set content canonization for comparisons.
1499 *
1500 * @param[in] trg Target set to put the canononized source into.
1501 * @param[in] src Source set.
1502 * @param[in] xp_node Source XPath node/meta to use for canonization.
1503 * @return LY_ERR
1504 */
1505static LY_ERR
1506set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1507{
1508 struct lysc_type *type = NULL;
1509 struct lyd_value val;
1510 struct ly_err_item *err = NULL;
1511 char *str, *ptr;
1512 int dynamic;
1513 LY_ERR rc;
1514
1515 /* is there anything to canonize even? */
1516 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1517 /* do we have a type to use for canonization? */
1518 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1519 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1520 } else if (xp_node->type == LYXP_NODE_META) {
1521 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1522 }
1523 }
1524 if (!type) {
1525 goto fill;
1526 }
1527
1528 if (src->type == LYXP_SET_NUMBER) {
1529 /* canonize number */
1530 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1531 LOGMEM(src->ctx);
1532 return LY_EMEM;
1533 }
1534 } else {
1535 /* canonize string */
1536 str = strdup(src->val.str);
1537 }
1538
1539 /* ignore errors, the value may not satisfy schema constraints */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001540 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC,
1541 LY_PREF_JSON, NULL, NULL, NULL, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001542 ly_err_free(err);
1543 if (rc) {
1544 /* invalid value */
1545 free(str);
1546 goto fill;
1547 }
1548
1549 /* storing successful, now print the canonical value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001550 str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001551
1552 /* use the canonized value */
1553 set_init(trg, src);
1554 trg->type = src->type;
1555 if (src->type == LYXP_SET_NUMBER) {
1556 trg->val.num = strtold(str, &ptr);
1557 if (dynamic) {
1558 free(str);
1559 }
1560 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1561 } else {
1562 trg->val.str = (dynamic ? str : strdup(str));
1563 }
1564 type->plugin->free(src->ctx, &val);
1565 return LY_SUCCESS;
1566
1567fill:
1568 /* no canonization needed/possible */
1569 set_fill_set(trg, src);
1570 return LY_SUCCESS;
1571}
1572
Michal Vasko03ff5a72019-09-11 13:49:33 +02001573#ifndef NDEBUG
1574
1575/**
1576 * @brief Bubble sort @p set into XPath document order.
1577 * Context position aware. Unused in the 'Release' build target.
1578 *
1579 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001580 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1581 */
1582static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001583set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001584{
1585 uint32_t i, j;
1586 int ret = 0, cmp, inverted, change;
1587 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588 struct lyxp_set_node item;
1589 struct lyxp_set_hash_node hnode;
1590 uint64_t hash;
1591
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001592 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001593 return 0;
1594 }
1595
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001596 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001597 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
1598 for (; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599
1600 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001601 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001602 return -1;
1603 }
1604
1605 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1606 print_set_debug(set);
1607
1608 for (i = 0; i < set->used; ++i) {
1609 inverted = 0;
1610 change = 0;
1611
1612 for (j = 1; j < set->used - i; ++j) {
1613 /* compare node positions */
1614 if (inverted) {
1615 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1616 } else {
1617 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1618 }
1619
1620 /* swap if needed */
1621 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1622 change = 1;
1623
1624 item = set->val.nodes[j - 1];
1625 set->val.nodes[j - 1] = set->val.nodes[j];
1626 set->val.nodes[j] = item;
1627 } else {
1628 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1629 inverted = !inverted;
1630 }
1631 }
1632
1633 ++ret;
1634
1635 if (!change) {
1636 break;
1637 }
1638 }
1639
1640 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1641 print_set_debug(set);
1642
1643 /* check node hashes */
1644 if (set->used >= LYD_HT_MIN_ITEMS) {
1645 assert(set->ht);
1646 for (i = 0; i < set->used; ++i) {
1647 hnode.node = set->val.nodes[i].node;
1648 hnode.type = set->val.nodes[i].type;
1649
1650 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1651 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1652 hash = dict_hash_multi(hash, NULL, 0);
1653
1654 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1655 }
1656 }
1657
1658 return ret - 1;
1659}
1660
1661/**
1662 * @brief Remove duplicate entries in a sorted node set.
1663 *
1664 * @param[in] set Sorted set to check.
1665 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1666 */
1667static LY_ERR
1668set_sorted_dup_node_clean(struct lyxp_set *set)
1669{
1670 uint32_t i = 0;
1671 LY_ERR ret = LY_SUCCESS;
1672
1673 if (set->used > 1) {
1674 while (i < set->used - 1) {
1675 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1676 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001677 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001678 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001679 }
Michal Vasko57eab132019-09-24 11:46:26 +02001680 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001681 }
1682 }
1683
Michal Vasko2caefc12019-11-14 16:07:56 +01001684 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001685 return ret;
1686}
1687
1688#endif
1689
1690/**
1691 * @brief Merge 2 sorted sets into one.
1692 *
1693 * @param[in,out] trg Set to merge into. Duplicates are removed.
1694 * @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 +02001695 * @return LY_ERR
1696 */
1697static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001698set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001699{
1700 uint32_t i, j, k, count, dup_count;
1701 int cmp;
1702 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001703
Michal Vaskod3678892020-05-21 10:06:58 +02001704 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001705 return LY_EINVAL;
1706 }
1707
Michal Vaskod3678892020-05-21 10:06:58 +02001708 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001709 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001710 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001712 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001713 return LY_SUCCESS;
1714 }
1715
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001716 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001717 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
1718 for (; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719
1720 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001721 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 return LY_EINT;
1723 }
1724
1725#ifndef NDEBUG
1726 LOGDBG(LY_LDGXPATH, "MERGE target");
1727 print_set_debug(trg);
1728 LOGDBG(LY_LDGXPATH, "MERGE source");
1729 print_set_debug(src);
1730#endif
1731
1732 /* make memory for the merge (duplicates are not detected yet, so space
1733 * will likely be wasted on them, too bad) */
1734 if (trg->size - trg->used < src->used) {
1735 trg->size = trg->used + src->used;
1736
1737 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1738 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1739 }
1740
1741 i = 0;
1742 j = 0;
1743 count = 0;
1744 dup_count = 0;
1745 do {
1746 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1747 if (!cmp) {
1748 if (!count) {
1749 /* duplicate, just skip it */
1750 ++i;
1751 ++j;
1752 } else {
1753 /* we are copying something already, so let's copy the duplicate too,
1754 * we are hoping that afterwards there are some more nodes to
1755 * copy and this way we can copy them all together */
1756 ++count;
1757 ++dup_count;
1758 ++i;
1759 ++j;
1760 }
1761 } else if (cmp < 0) {
1762 /* inserting src node into trg, just remember it for now */
1763 ++count;
1764 ++i;
1765
1766 /* insert the hash now */
1767 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1768 } else if (count) {
1769copy_nodes:
1770 /* time to actually copy the nodes, we have found the largest block of nodes */
1771 memmove(&trg->val.nodes[j + (count - dup_count)],
1772 &trg->val.nodes[j],
1773 (trg->used - j) * sizeof *trg->val.nodes);
1774 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1775
1776 trg->used += count - dup_count;
1777 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1778 j += count - dup_count;
1779
1780 count = 0;
1781 dup_count = 0;
1782 } else {
1783 ++j;
1784 }
1785 } while ((i < src->used) && (j < trg->used));
1786
1787 if ((i < src->used) || count) {
1788 /* insert all the hashes first */
1789 for (k = i; k < src->used; ++k) {
1790 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1791 }
1792
1793 /* loop ended, but we need to copy something at trg end */
1794 count += src->used - i;
1795 i = src->used;
1796 goto copy_nodes;
1797 }
1798
1799 /* we are inserting hashes before the actual node insert, which causes
1800 * situations when there were initially not enough items for a hash table,
1801 * but even after some were inserted, hash table was not created (during
1802 * insertion the number of items is not updated yet) */
1803 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1804 set_insert_node_hash(trg, NULL, 0);
1805 }
1806
1807#ifndef NDEBUG
1808 LOGDBG(LY_LDGXPATH, "MERGE result");
1809 print_set_debug(trg);
1810#endif
1811
Michal Vaskod3678892020-05-21 10:06:58 +02001812 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001813 return LY_SUCCESS;
1814}
1815
1816/*
1817 * (re)parse functions
1818 *
1819 * Parse functions parse the expression into
1820 * tokens (syntactic analysis).
1821 *
1822 * Reparse functions perform semantic analysis
1823 * (do not save the result, just a check) of
1824 * the expression and fill repeat indices.
1825 */
1826
Michal Vasko14676352020-05-29 11:35:55 +02001827LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001828lyxp_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 +02001829{
Michal Vasko004d3152020-06-11 19:59:22 +02001830 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001831 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001832 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1833 }
Michal Vasko14676352020-05-29 11:35:55 +02001834 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001835 }
1836
Michal Vasko004d3152020-06-11 19:59:22 +02001837 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001838 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001839 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001840 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001841 }
Michal Vasko14676352020-05-29 11:35:55 +02001842 return LY_ENOT;
1843 }
1844
1845 return LY_SUCCESS;
1846}
1847
Michal Vasko004d3152020-06-11 19:59:22 +02001848LY_ERR
1849lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1850{
1851 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1852
1853 /* skip the token */
1854 ++(*tok_idx);
1855
1856 return LY_SUCCESS;
1857}
1858
Michal Vasko14676352020-05-29 11:35:55 +02001859/* just like lyxp_check_token() but tests for 2 tokens */
1860static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001861exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Michal Vasko14676352020-05-29 11:35:55 +02001862 enum lyxp_token want_tok2)
1863{
Michal Vasko004d3152020-06-11 19:59:22 +02001864 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001865 if (ctx) {
1866 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1867 }
1868 return LY_EINCOMPLETE;
1869 }
1870
Michal Vasko004d3152020-06-11 19:59:22 +02001871 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001872 if (ctx) {
1873 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001874 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001875 }
1876 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001877 }
1878
1879 return LY_SUCCESS;
1880}
1881
1882/**
1883 * @brief Stack operation push on the repeat array.
1884 *
1885 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001886 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001887 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1888 */
1889static void
Michal Vasko004d3152020-06-11 19:59:22 +02001890exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001891{
1892 uint16_t i;
1893
Michal Vasko004d3152020-06-11 19:59:22 +02001894 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001895 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001896 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1897 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1898 exp->repeat[tok_idx][i] = repeat_op_idx;
1899 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001900 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001901 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1902 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1903 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001904 }
1905}
1906
1907/**
1908 * @brief Reparse Predicate. Logs directly on error.
1909 *
1910 * [7] Predicate ::= '[' Expr ']'
1911 *
1912 * @param[in] ctx Context for logging.
1913 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001914 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001915 * @return LY_ERR
1916 */
1917static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001918reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001919{
1920 LY_ERR rc;
1921
Michal Vasko004d3152020-06-11 19:59:22 +02001922 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001923 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001924 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925
Michal Vasko004d3152020-06-11 19:59:22 +02001926 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001927 LY_CHECK_RET(rc);
1928
Michal Vasko004d3152020-06-11 19:59:22 +02001929 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001931 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932
1933 return LY_SUCCESS;
1934}
1935
1936/**
1937 * @brief Reparse RelativeLocationPath. Logs directly on error.
1938 *
1939 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1940 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1941 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1942 *
1943 * @param[in] ctx Context for logging.
1944 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001945 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1947 */
1948static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001949reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001950{
1951 LY_ERR rc;
1952
Michal Vasko004d3152020-06-11 19:59:22 +02001953 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954 LY_CHECK_RET(rc);
1955
1956 goto step;
1957 do {
1958 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001959 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001960
Michal Vasko004d3152020-06-11 19:59:22 +02001961 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 LY_CHECK_RET(rc);
1963step:
1964 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001965 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001966 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001967 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968 break;
1969
1970 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001971 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972 break;
1973
1974 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001975 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976
Michal Vasko004d3152020-06-11 19:59:22 +02001977 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001979 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001981 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 return LY_EVALID;
1983 }
1984 /* fall through */
1985 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001986 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 goto reparse_predicate;
1988 break;
1989
1990 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02001991 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992
1993 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02001994 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001996 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997
1998 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02001999 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002001 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002
2003reparse_predicate:
2004 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002005 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2006 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007 LY_CHECK_RET(rc);
2008 }
2009 break;
2010 default:
2011 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002012 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 return LY_EVALID;
2014 }
Michal Vasko004d3152020-06-11 19:59:22 +02002015 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016
2017 return LY_SUCCESS;
2018}
2019
2020/**
2021 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2022 *
2023 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2024 *
2025 * @param[in] ctx Context for logging.
2026 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002027 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 * @return LY_ERR
2029 */
2030static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002031reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032{
2033 LY_ERR rc;
2034
Michal Vasko004d3152020-06-11 19:59:22 +02002035 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 +02002036
2037 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002038 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002040 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041
Michal Vasko004d3152020-06-11 19:59:22 +02002042 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 return LY_SUCCESS;
2044 }
Michal Vasko004d3152020-06-11 19:59:22 +02002045 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046 case LYXP_TOKEN_DOT:
2047 case LYXP_TOKEN_DDOT:
2048 case LYXP_TOKEN_AT:
2049 case LYXP_TOKEN_NAMETEST:
2050 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002051 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 LY_CHECK_RET(rc);
2053 /* fall through */
2054 default:
2055 break;
2056 }
2057
2058 /* '//' RelativeLocationPath */
2059 } else {
2060 /* '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002061 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002062
Michal Vasko004d3152020-06-11 19:59:22 +02002063 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064 LY_CHECK_RET(rc);
2065 }
2066
2067 return LY_SUCCESS;
2068}
2069
2070/**
2071 * @brief Reparse FunctionCall. Logs directly on error.
2072 *
2073 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2074 *
2075 * @param[in] ctx Context for logging.
2076 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002077 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 * @return LY_ERR
2079 */
2080static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002081reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002082{
2083 int min_arg_count = -1, max_arg_count, arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002084 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 LY_ERR rc;
2086
Michal Vasko004d3152020-06-11 19:59:22 +02002087 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002089 func_tok_idx = *tok_idx;
2090 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002092 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093 min_arg_count = 1;
2094 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002095 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 min_arg_count = 1;
2097 max_arg_count = 1;
2098 }
2099 break;
2100 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002101 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
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]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 min_arg_count = 0;
2106 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002107 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 min_arg_count = 0;
2109 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002110 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 min_arg_count = 0;
2112 max_arg_count = 0;
2113 }
2114 break;
2115 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002116 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 min_arg_count = 1;
2118 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002119 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 0;
2121 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002122 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 min_arg_count = 1;
2124 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002125 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 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]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 1;
2130 max_arg_count = 1;
2131 }
2132 break;
2133 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002134 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002136 max_arg_count = INT_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 0;
2139 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 0;
2142 max_arg_count = 1;
2143 }
2144 break;
2145 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002146 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 1;
2148 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002149 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 1;
2151 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002152 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 0;
2154 max_arg_count = 0;
2155 }
2156 break;
2157 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 2;
2160 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 0;
2163 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002164 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 2;
2166 max_arg_count = 2;
2167 }
2168 break;
2169 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002170 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 2;
2172 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002173 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 3;
2175 max_arg_count = 3;
2176 }
2177 break;
2178 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002179 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 0;
2181 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002182 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 1;
2184 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002185 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 2;
2187 max_arg_count = 2;
2188 }
2189 break;
2190 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002191 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
2193 max_arg_count = 2;
2194 }
2195 break;
2196 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002197 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 2;
2199 max_arg_count = 2;
2200 }
2201 break;
2202 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 0;
2205 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002206 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 0;
2208 max_arg_count = 1;
2209 }
2210 break;
2211 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002212 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
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]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 2;
2217 max_arg_count = 2;
2218 }
2219 break;
2220 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002221 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 2;
2223 max_arg_count = 2;
2224 }
2225 break;
2226 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002227 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 2;
2229 max_arg_count = 2;
2230 }
2231 break;
2232 }
2233 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002234 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 +02002235 return LY_EINVAL;
2236 }
Michal Vasko004d3152020-06-11 19:59:22 +02002237 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238
2239 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002240 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002242 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243
2244 /* ( Expr ( ',' Expr )* )? */
2245 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002246 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002248 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002250 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002251 LY_CHECK_RET(rc);
2252 }
Michal Vasko004d3152020-06-11 19:59:22 +02002253 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2254 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255
2256 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002257 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002258 LY_CHECK_RET(rc);
2259 }
2260
2261 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002262 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002264 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265
2266 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002267 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
2268 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 return LY_EVALID;
2270 }
2271
2272 return LY_SUCCESS;
2273}
2274
2275/**
2276 * @brief Reparse PathExpr. Logs directly on error.
2277 *
2278 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2279 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2280 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2281 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2282 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2283 *
2284 * @param[in] ctx Context for logging.
2285 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002286 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287 * @return LY_ERR
2288 */
2289static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002290reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002291{
2292 LY_ERR rc;
2293
Michal Vasko004d3152020-06-11 19:59:22 +02002294 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002295 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 }
2297
Michal Vasko004d3152020-06-11 19:59:22 +02002298 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002299 case LYXP_TOKEN_PAR1:
2300 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002301 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302
Michal Vasko004d3152020-06-11 19:59:22 +02002303 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 LY_CHECK_RET(rc);
2305
Michal Vasko004d3152020-06-11 19:59:22 +02002306 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002308 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002309 goto predicate;
2310 break;
2311 case LYXP_TOKEN_DOT:
2312 case LYXP_TOKEN_DDOT:
2313 case LYXP_TOKEN_AT:
2314 case LYXP_TOKEN_NAMETEST:
2315 case LYXP_TOKEN_NODETYPE:
2316 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002317 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 LY_CHECK_RET(rc);
2319 break;
2320 case LYXP_TOKEN_FUNCNAME:
2321 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002322 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002323 LY_CHECK_RET(rc);
2324 goto predicate;
2325 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002326 case LYXP_TOKEN_OPER_PATH:
2327 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002329 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 LY_CHECK_RET(rc);
2331 break;
2332 case LYXP_TOKEN_LITERAL:
2333 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002334 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 goto predicate;
2336 break;
2337 case LYXP_TOKEN_NUMBER:
2338 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002339 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002340 goto predicate;
2341 break;
2342 default:
2343 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002344 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345 return LY_EVALID;
2346 }
2347
2348 return LY_SUCCESS;
2349
2350predicate:
2351 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002352 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2353 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 LY_CHECK_RET(rc);
2355 }
2356
2357 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002358 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359
2360 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002361 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362
Michal Vasko004d3152020-06-11 19:59:22 +02002363 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 LY_CHECK_RET(rc);
2365 }
2366
2367 return LY_SUCCESS;
2368}
2369
2370/**
2371 * @brief Reparse UnaryExpr. Logs directly on error.
2372 *
2373 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2374 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2375 *
2376 * @param[in] ctx Context for logging.
2377 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002378 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379 * @return LY_ERR
2380 */
2381static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002382reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002383{
2384 uint16_t prev_exp;
2385 LY_ERR rc;
2386
2387 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002388 prev_exp = *tok_idx;
2389 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2390 && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002392 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393 }
2394
2395 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002396 prev_exp = *tok_idx;
2397 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002398 LY_CHECK_RET(rc);
2399
2400 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002401 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002403 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404
Michal Vasko004d3152020-06-11 19:59:22 +02002405 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 LY_CHECK_RET(rc);
2407 }
2408
2409 return LY_SUCCESS;
2410}
2411
2412/**
2413 * @brief Reparse AdditiveExpr. Logs directly on error.
2414 *
2415 * [15] AdditiveExpr ::= MultiplicativeExpr
2416 * | AdditiveExpr '+' MultiplicativeExpr
2417 * | AdditiveExpr '-' MultiplicativeExpr
2418 * [16] MultiplicativeExpr ::= UnaryExpr
2419 * | MultiplicativeExpr '*' UnaryExpr
2420 * | MultiplicativeExpr 'div' UnaryExpr
2421 * | MultiplicativeExpr 'mod' UnaryExpr
2422 *
2423 * @param[in] ctx Context for logging.
2424 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002425 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426 * @return LY_ERR
2427 */
2428static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002429reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430{
2431 uint16_t prev_add_exp, prev_mul_exp;
2432 LY_ERR rc;
2433
Michal Vasko004d3152020-06-11 19:59:22 +02002434 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 goto reparse_multiplicative_expr;
2436
2437 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002438 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2439 && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002441 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002442
2443reparse_multiplicative_expr:
2444 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002445 prev_mul_exp = *tok_idx;
2446 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447 LY_CHECK_RET(rc);
2448
2449 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002450 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2451 && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002453 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454
Michal Vasko004d3152020-06-11 19:59:22 +02002455 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002456 LY_CHECK_RET(rc);
2457 }
2458 }
2459
2460 return LY_SUCCESS;
2461}
2462
2463/**
2464 * @brief Reparse EqualityExpr. Logs directly on error.
2465 *
2466 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2467 * | EqualityExpr '!=' RelationalExpr
2468 * [14] RelationalExpr ::= AdditiveExpr
2469 * | RelationalExpr '<' AdditiveExpr
2470 * | RelationalExpr '>' AdditiveExpr
2471 * | RelationalExpr '<=' AdditiveExpr
2472 * | RelationalExpr '>=' AdditiveExpr
2473 *
2474 * @param[in] ctx Context for logging.
2475 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002476 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002477 * @return LY_ERR
2478 */
2479static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002480reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481{
2482 uint16_t prev_eq_exp, prev_rel_exp;
2483 LY_ERR rc;
2484
Michal Vasko004d3152020-06-11 19:59:22 +02002485 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 goto reparse_additive_expr;
2487
2488 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002489 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002491 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002492
2493reparse_additive_expr:
2494 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002495 prev_rel_exp = *tok_idx;
2496 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002497 LY_CHECK_RET(rc);
2498
2499 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002500 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002502 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503
Michal Vasko004d3152020-06-11 19:59:22 +02002504 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 LY_CHECK_RET(rc);
2506 }
2507 }
2508
2509 return LY_SUCCESS;
2510}
2511
2512/**
2513 * @brief Reparse OrExpr. Logs directly on error.
2514 *
2515 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2516 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2517 *
2518 * @param[in] ctx Context for logging.
2519 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002520 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 * @return LY_ERR
2522 */
2523static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002524reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525{
2526 uint16_t prev_or_exp, prev_and_exp;
2527 LY_ERR rc;
2528
Michal Vasko004d3152020-06-11 19:59:22 +02002529 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002530 goto reparse_equality_expr;
2531
2532 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002533 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 +02002534 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002535 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536
2537reparse_equality_expr:
2538 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002539 prev_and_exp = *tok_idx;
2540 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002541 LY_CHECK_RET(rc);
2542
2543 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002544 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 +02002545 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002546 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547
Michal Vasko004d3152020-06-11 19:59:22 +02002548 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 LY_CHECK_RET(rc);
2550 }
2551 }
2552
2553 return LY_SUCCESS;
2554}
Radek Krejcib1646a92018-11-02 16:08:26 +01002555
2556/**
2557 * @brief Parse NCName.
2558 *
2559 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002561 */
Radek Krejcid4270262019-01-07 15:07:25 +01002562static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002563parse_ncname(const char *ncname)
2564{
2565 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002566 size_t size;
2567 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002568
2569 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2570 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2571 return len;
2572 }
2573
2574 do {
2575 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002576 if (!*ncname) {
2577 break;
2578 }
Radek Krejcid4270262019-01-07 15:07:25 +01002579 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002580 } while (is_xmlqnamechar(uc) && (uc != ':'));
2581
2582 return len;
2583}
2584
2585/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002586 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002587 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002588 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002589 * @param[in] exp Expression to use.
2590 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002591 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002592 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002593 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002594 */
2595static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002596exp_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 +01002597{
2598 uint32_t prev;
2599
2600 if (exp->used == exp->size) {
2601 prev = exp->size;
2602 exp->size += LYXP_EXPR_SIZE_STEP;
2603 if (prev > exp->size) {
2604 LOGINT(ctx);
2605 return LY_EINT;
2606 }
2607
2608 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2609 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2610 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2611 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2612 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2613 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2614 }
2615
2616 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002618 exp->tok_len[exp->used] = tok_len;
2619 ++exp->used;
2620 return LY_SUCCESS;
2621}
2622
2623void
Michal Vasko14676352020-05-29 11:35:55 +02002624lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002625{
2626 uint16_t i;
2627
2628 if (!expr) {
2629 return;
2630 }
2631
2632 lydict_remove(ctx, expr->expr);
2633 free(expr->tokens);
2634 free(expr->tok_pos);
2635 free(expr->tok_len);
2636 if (expr->repeat) {
2637 for (i = 0; i < expr->used; ++i) {
2638 free(expr->repeat[i]);
2639 }
2640 }
2641 free(expr->repeat);
2642 free(expr);
2643}
2644
2645struct lyxp_expr *
Michal Vasko004d3152020-06-11 19:59:22 +02002646lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse)
Radek Krejcib1646a92018-11-02 16:08:26 +01002647{
2648 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002649 size_t parsed = 0, tok_len;
2650 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002651 enum lyxp_token tok_type;
2652 int prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002653 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002654
Michal Vasko004d3152020-06-11 19:59:22 +02002655 if (!expr[0]) {
2656 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
2657 return NULL;
2658 }
2659
2660 if (!expr_len) {
2661 expr_len = strlen(expr);
2662 }
2663 if (expr_len > UINT16_MAX) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002664 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2665 return NULL;
2666 }
2667
2668 /* init lyxp_expr structure */
2669 ret = calloc(1, sizeof *ret);
2670 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
Michal Vasko004d3152020-06-11 19:59:22 +02002671 ret->expr = lydict_insert(ctx, expr, expr_len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002672 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2673 ret->used = 0;
2674 ret->size = LYXP_EXPR_SIZE_START;
2675 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2676 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2677
2678 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2679 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2680
2681 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2682 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2683
Michal Vasko004d3152020-06-11 19:59:22 +02002684 /* make expr 0-terminated */
2685 expr = ret->expr;
2686
Radek Krejcib1646a92018-11-02 16:08:26 +01002687 while (is_xmlws(expr[parsed])) {
2688 ++parsed;
2689 }
2690
2691 do {
2692 if (expr[parsed] == '(') {
2693
2694 /* '(' */
2695 tok_len = 1;
2696 tok_type = LYXP_TOKEN_PAR1;
2697
2698 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2699 /* it is a NodeType/FunctionName after all */
2700 if (((ret->tok_len[ret->used - 1] == 4)
2701 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2702 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2703 ((ret->tok_len[ret->used - 1] == 7)
2704 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2705 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2706 } else {
2707 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2708 }
2709 prev_function_check = 0;
2710 }
2711
2712 } else if (expr[parsed] == ')') {
2713
2714 /* ')' */
2715 tok_len = 1;
2716 tok_type = LYXP_TOKEN_PAR2;
2717
2718 } else if (expr[parsed] == '[') {
2719
2720 /* '[' */
2721 tok_len = 1;
2722 tok_type = LYXP_TOKEN_BRACK1;
2723
2724 } else if (expr[parsed] == ']') {
2725
2726 /* ']' */
2727 tok_len = 1;
2728 tok_type = LYXP_TOKEN_BRACK2;
2729
2730 } else if (!strncmp(&expr[parsed], "..", 2)) {
2731
2732 /* '..' */
2733 tok_len = 2;
2734 tok_type = LYXP_TOKEN_DDOT;
2735
2736 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2737
2738 /* '.' */
2739 tok_len = 1;
2740 tok_type = LYXP_TOKEN_DOT;
2741
2742 } else if (expr[parsed] == '@') {
2743
2744 /* '@' */
2745 tok_len = 1;
2746 tok_type = LYXP_TOKEN_AT;
2747
2748 } else if (expr[parsed] == ',') {
2749
2750 /* ',' */
2751 tok_len = 1;
2752 tok_type = LYXP_TOKEN_COMMA;
2753
2754 } else if (expr[parsed] == '\'') {
2755
2756 /* Literal with ' */
Radek Krejci1e008d22020-08-17 11:37:37 +02002757 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002758 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2759 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2760 ++tok_len;
2761 tok_type = LYXP_TOKEN_LITERAL;
2762
2763 } else if (expr[parsed] == '\"') {
2764
2765 /* Literal with " */
Radek Krejci1e008d22020-08-17 11:37:37 +02002766 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002767 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2768 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2769 ++tok_len;
2770 tok_type = LYXP_TOKEN_LITERAL;
2771
2772 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2773
2774 /* Number */
Radek Krejci1e008d22020-08-17 11:37:37 +02002775 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002776 if (expr[parsed + tok_len] == '.') {
2777 ++tok_len;
Radek Krejci1e008d22020-08-17 11:37:37 +02002778 for (; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002779 }
2780 tok_type = LYXP_TOKEN_NUMBER;
2781
2782 } else if (expr[parsed] == '/') {
2783
2784 /* Operator '/', '//' */
2785 if (!strncmp(&expr[parsed], "//", 2)) {
2786 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002787 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002788 } else {
2789 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002790 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002791 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002792
Michal Vasko3e48bf32020-06-01 08:39:07 +02002793 } else if (!strncmp(&expr[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002794
Michal Vasko3e48bf32020-06-01 08:39:07 +02002795 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002796 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002797 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2798
2799 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2800
2801 /* Operator '<=', '>=' */
2802 tok_len = 2;
2803 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002804
2805 } else if (expr[parsed] == '|') {
2806
2807 /* Operator '|' */
2808 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002809 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002810
2811 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2812
2813 /* Operator '+', '-' */
2814 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002815 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002816
Michal Vasko3e48bf32020-06-01 08:39:07 +02002817 } else if (expr[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002818
Michal Vasko3e48bf32020-06-01 08:39:07 +02002819 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002820 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002821 tok_type = LYXP_TOKEN_OPER_EQUAL;
2822
2823 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2824
2825 /* Operator '<', '>' */
2826 tok_len = 1;
2827 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002828
2829 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2830 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2831 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2832 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002833 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2834 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2835 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2836 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2837 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2838 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2839 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2840 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002841
2842 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2843 if (expr[parsed] == '*') {
2844 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002845 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002846
2847 } else if (!strncmp(&expr[parsed], "or", 2)) {
2848 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002849 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002850
2851 } else if (!strncmp(&expr[parsed], "and", 3)) {
2852 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002853 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002854
2855 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2856 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002857 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
2859 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002860 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2861 expr[parsed], expr[parsed], ret->tok_len[ret->used - 1], &ret->expr[ret->tok_pos[ret->used - 1]]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002862 goto error;
2863 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002864 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002865 goto error;
2866 }
2867 } else if (expr[parsed] == '*') {
2868
2869 /* NameTest '*' */
2870 tok_len = 1;
2871 tok_type = LYXP_TOKEN_NAMETEST;
2872
2873 } else {
2874
2875 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2876 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002877 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002878 tok_len = ncname_len;
2879
2880 if (expr[parsed + tok_len] == ':') {
2881 ++tok_len;
2882 if (expr[parsed + tok_len] == '*') {
2883 ++tok_len;
2884 } else {
2885 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002886 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002887 tok_len += ncname_len;
2888 }
2889 /* remove old flag to prevent ambiguities */
2890 prev_function_check = 0;
2891 tok_type = LYXP_TOKEN_NAMETEST;
2892 } else {
2893 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2894 prev_function_check = 1;
2895 tok_type = LYXP_TOKEN_NAMETEST;
2896 }
2897 }
2898
2899 /* store the token, move on to the next one */
2900 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2901 parsed += tok_len;
2902 while (is_xmlws(expr[parsed])) {
2903 ++parsed;
2904 }
2905
2906 } while (expr[parsed]);
2907
Michal Vasko004d3152020-06-11 19:59:22 +02002908 if (reparse) {
2909 /* prealloc repeat */
2910 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2911 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002912
Michal Vasko004d3152020-06-11 19:59:22 +02002913 /* fill repeat */
2914 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error);
2915 if (ret->used > tok_idx) {
2916 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2917 &ret->expr[ret->tok_pos[tok_idx]]);
2918 goto error;
2919 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002920 }
2921
2922 print_expr_struct_debug(ret);
2923
Radek Krejcib1646a92018-11-02 16:08:26 +01002924 return ret;
2925
2926error:
2927 lyxp_expr_free(ctx, ret);
2928 return NULL;
2929}
2930
Michal Vasko004d3152020-06-11 19:59:22 +02002931struct lyxp_expr *
2932lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp)
2933{
2934 struct lyxp_expr *dup;
2935 uint32_t i, j;
2936
2937 dup = calloc(1, sizeof *dup);
2938 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error);
2939
2940 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2941 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error);
2942 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2943
2944 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2945 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error);
2946 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2947
2948 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
2949 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error);
2950 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2951
2952 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
2953 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error);
2954 for (i = 0; i < exp->used; ++i) {
2955 if (!exp->repeat[i]) {
2956 dup->repeat[i] = NULL;
2957 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002958 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002959 /* the ending 0 as well */
2960 ++j;
2961
Michal Vasko99c71642020-07-03 13:33:36 +02002962 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko004d3152020-06-11 19:59:22 +02002963 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error);
2964 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2965 dup->repeat[i][j - 1] = 0;
2966 }
2967 }
2968
2969 dup->used = exp->used;
2970 dup->size = exp->used;
2971 dup->expr = lydict_insert(ctx, exp->expr, 0);
2972
2973 return dup;
2974
2975error:
2976 lyxp_expr_free(ctx, dup);
2977 return NULL;
2978}
2979
Michal Vasko03ff5a72019-09-11 13:49:33 +02002980/*
2981 * warn functions
2982 *
2983 * Warn functions check specific reasonable conditions for schema XPath
2984 * and print a warning if they are not satisfied.
2985 */
2986
2987/**
2988 * @brief Get the last-added schema node that is currently in the context.
2989 *
2990 * @param[in] set Set to search in.
2991 * @return Last-added schema context node, NULL if no node is in context.
2992 */
2993static struct lysc_node *
2994warn_get_scnode_in_ctx(struct lyxp_set *set)
2995{
2996 uint32_t i;
2997
2998 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2999 return NULL;
3000 }
3001
3002 i = set->used;
3003 do {
3004 --i;
3005 if (set->val.scnodes[i].in_ctx == 1) {
3006 /* if there are more, simply return the first found (last added) */
3007 return set->val.scnodes[i].scnode;
3008 }
3009 } while (i);
3010
3011 return NULL;
3012}
3013
3014/**
3015 * @brief Test whether a type is numeric - integer type or decimal64.
3016 *
3017 * @param[in] type Type to test.
3018 * @return 1 if numeric, 0 otherwise.
3019 */
3020static int
3021warn_is_numeric_type(struct lysc_type *type)
3022{
3023 struct lysc_type_union *uni;
3024 int ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003025 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003026
3027 switch (type->basetype) {
3028 case LY_TYPE_DEC64:
3029 case LY_TYPE_INT8:
3030 case LY_TYPE_UINT8:
3031 case LY_TYPE_INT16:
3032 case LY_TYPE_UINT16:
3033 case LY_TYPE_INT32:
3034 case LY_TYPE_UINT32:
3035 case LY_TYPE_INT64:
3036 case LY_TYPE_UINT64:
3037 return 1;
3038 case LY_TYPE_UNION:
3039 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003040 LY_ARRAY_FOR(uni->types, u) {
3041 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003042 if (ret) {
3043 /* found a suitable type */
3044 return 1;
3045 }
3046 }
3047 /* did not find any suitable type */
3048 return 0;
3049 case LY_TYPE_LEAFREF:
3050 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3051 default:
3052 return 0;
3053 }
3054}
3055
3056/**
3057 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3058 *
3059 * @param[in] type Type to test.
3060 * @return 1 if string, 0 otherwise.
3061 */
3062static int
3063warn_is_string_type(struct lysc_type *type)
3064{
3065 struct lysc_type_union *uni;
3066 int ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003067 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003068
3069 switch (type->basetype) {
3070 case LY_TYPE_BITS:
3071 case LY_TYPE_ENUM:
3072 case LY_TYPE_IDENT:
3073 case LY_TYPE_INST:
3074 case LY_TYPE_STRING:
3075 return 1;
3076 case LY_TYPE_UNION:
3077 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003078 LY_ARRAY_FOR(uni->types, u) {
3079 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003080 if (ret) {
3081 /* found a suitable type */
3082 return 1;
3083 }
3084 }
3085 /* did not find any suitable type */
3086 return 0;
3087 case LY_TYPE_LEAFREF:
3088 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3089 default:
3090 return 0;
3091 }
3092}
3093
3094/**
3095 * @brief Test whether a type is one specific type.
3096 *
3097 * @param[in] type Type to test.
3098 * @param[in] base Expected type.
3099 * @return 1 if it is, 0 otherwise.
3100 */
3101static int
3102warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3103{
3104 struct lysc_type_union *uni;
3105 int ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003106 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003107
3108 if (type->basetype == base) {
3109 return 1;
3110 } else if (type->basetype == LY_TYPE_UNION) {
3111 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003112 LY_ARRAY_FOR(uni->types, u) {
3113 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003114 if (ret) {
3115 /* found a suitable type */
3116 return 1;
3117 }
3118 }
3119 /* did not find any suitable type */
3120 return 0;
3121 } else if (type->basetype == LY_TYPE_LEAFREF) {
3122 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3123 }
3124
3125 return 0;
3126}
3127
3128/**
3129 * @brief Get next type of a (union) type.
3130 *
3131 * @param[in] type Base type.
3132 * @param[in] prev_type Previously returned type.
3133 * @return Next type or NULL.
3134 */
3135static struct lysc_type *
3136warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3137{
3138 struct lysc_type_union *uni;
3139 int found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003140 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003141
3142 switch (type->basetype) {
3143 case LY_TYPE_UNION:
3144 uni = (struct lysc_type_union *)type;
3145 if (!prev_type) {
3146 return uni->types[0];
3147 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003148 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003149 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003150 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003151 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003152 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003153 found = 1;
3154 }
3155 }
3156 return NULL;
3157 default:
3158 if (prev_type) {
3159 assert(type == prev_type);
3160 return NULL;
3161 } else {
3162 return type;
3163 }
3164 }
3165}
3166
3167/**
3168 * @brief Test whether 2 types have a common type.
3169 *
3170 * @param[in] type1 First type.
3171 * @param[in] type2 Second type.
3172 * @return 1 if they do, 0 otherwise.
3173 */
3174static int
3175warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3176{
3177 struct lysc_type *t1, *rt1, *t2, *rt2;
3178
3179 t1 = NULL;
3180 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3181 if (t1->basetype == LY_TYPE_LEAFREF) {
3182 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3183 } else {
3184 rt1 = t1;
3185 }
3186
3187 t2 = NULL;
3188 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3189 if (t2->basetype == LY_TYPE_LEAFREF) {
3190 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3191 } else {
3192 rt2 = t2;
3193 }
3194
3195 if (rt2->basetype == rt1->basetype) {
3196 /* match found */
3197 return 1;
3198 }
3199 }
3200 }
3201
3202 return 0;
3203}
3204
3205/**
3206 * @brief Check both operands of comparison operators.
3207 *
3208 * @param[in] ctx Context for errors.
3209 * @param[in] set1 First operand set.
3210 * @param[in] set2 Second operand set.
3211 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3212 * @param[in] expr Start of the expression to print with the warning.
3213 * @param[in] tok_pos Token position.
3214 */
3215static void
3216warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3217{
3218 struct lysc_node_leaf *node1, *node2;
3219 int leaves = 1, warning = 0;
3220
3221 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3222 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3223
3224 if (!node1 && !node2) {
3225 /* no node-sets involved, nothing to do */
3226 return;
3227 }
3228
3229 if (node1) {
3230 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3231 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3232 warning = 1;
3233 leaves = 0;
3234 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3235 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3236 warning = 1;
3237 }
3238 }
3239
3240 if (node2) {
3241 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3242 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3243 warning = 1;
3244 leaves = 0;
3245 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3246 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3247 warning = 1;
3248 }
3249 }
3250
3251 if (node1 && node2 && leaves && !numbers_only) {
3252 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3253 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3254 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3255 && !warn_is_equal_type(node1->type, node2->type))) {
3256 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3257 warning = 1;
3258 }
3259 }
3260
3261 if (warning) {
3262 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3263 }
3264}
3265
3266/**
3267 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3268 *
3269 * @param[in] exp Parsed XPath expression.
3270 * @param[in] set Set with the leaf/leaf-list.
3271 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3272 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3273 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3274 */
3275static void
3276warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3277{
3278 struct lysc_node *scnode;
3279 struct lysc_type *type;
3280 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003281 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003282 LY_ERR rc;
3283 struct ly_err_item *err = NULL;
3284
3285 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3286 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3287 /* check that the node can have the specified value */
3288 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3289 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3290 } else {
3291 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3292 }
3293 if (!value) {
3294 LOGMEM(set->ctx);
3295 return;
3296 }
3297
3298 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3299 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3300 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3301 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3302 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3303 exp->expr + exp->tok_pos[equal_exp]);
3304 }
3305
3306 type = ((struct lysc_node_leaf *)scnode)->type;
3307 if (type->basetype != LY_TYPE_IDENT) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003308 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA,
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003309 (void *)set->local_mod, NULL, NULL, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003310
3311 if (err) {
3312 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3313 ly_err_free(err);
3314 } else if (rc != LY_SUCCESS) {
3315 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3316 }
3317 if (rc != LY_SUCCESS) {
3318 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3319 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3320 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003321 } else {
3322 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003323 }
3324 }
3325 free(value);
3326 }
3327}
3328
3329/*
3330 * XPath functions
3331 */
3332
3333/**
3334 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3335 * depending on whether the first node bit value from the second argument is set.
3336 *
3337 * @param[in] args Array of arguments.
3338 * @param[in] arg_count Count of elements in @p args.
3339 * @param[in,out] set Context and result set at the same time.
3340 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003341 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003342 */
3343static LY_ERR
3344xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3345{
3346 struct lyd_node_term *leaf;
3347 struct lysc_node_leaf *sleaf;
3348 struct lysc_type_bits *bits;
3349 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003350 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003351
3352 if (options & LYXP_SCNODE_ALL) {
3353 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3354 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003355 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3356 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 +02003357 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3358 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003359 }
3360
3361 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3362 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3363 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 +02003364 } else if (!warn_is_string_type(sleaf->type)) {
3365 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003366 }
3367 }
3368 set_scnode_clear_ctx(set);
3369 return rc;
3370 }
3371
Michal Vaskod3678892020-05-21 10:06:58 +02003372 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003373 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
3374 return LY_EVALID;
3375 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003376 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003377 LY_CHECK_RET(rc);
3378
3379 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003380 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3382 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3383 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3384 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003385 LY_ARRAY_FOR(bits->bits, u) {
3386 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387 set_fill_boolean(set, 1);
3388 break;
3389 }
3390 }
3391 }
3392 }
3393
3394 return LY_SUCCESS;
3395}
3396
3397/**
3398 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3399 * with the argument converted to boolean.
3400 *
3401 * @param[in] args Array of arguments.
3402 * @param[in] arg_count Count of elements in @p args.
3403 * @param[in,out] set Context and result set at the same time.
3404 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003405 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003406 */
3407static LY_ERR
3408xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3409{
3410 LY_ERR rc;
3411
3412 if (options & LYXP_SCNODE_ALL) {
3413 set_scnode_clear_ctx(set);
3414 return LY_SUCCESS;
3415 }
3416
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003417 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003418 LY_CHECK_RET(rc);
3419 set_fill_set(set, args[0]);
3420
3421 return LY_SUCCESS;
3422}
3423
3424/**
3425 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3426 * with the first argument rounded up to the nearest integer.
3427 *
3428 * @param[in] args Array of arguments.
3429 * @param[in] arg_count Count of elements in @p args.
3430 * @param[in,out] set Context and result set at the same time.
3431 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003432 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003433 */
3434static LY_ERR
3435xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3436{
3437 struct lysc_node_leaf *sleaf;
3438 LY_ERR rc = LY_SUCCESS;
3439
3440 if (options & LYXP_SCNODE_ALL) {
3441 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3442 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3444 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 +02003445 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3446 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003447 }
3448 set_scnode_clear_ctx(set);
3449 return rc;
3450 }
3451
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003452 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003453 LY_CHECK_RET(rc);
3454 if ((long long)args[0]->val.num != args[0]->val.num) {
3455 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3456 } else {
3457 set_fill_number(set, args[0]->val.num);
3458 }
3459
3460 return LY_SUCCESS;
3461}
3462
3463/**
3464 * @brief Execute the XPath concat(string, string, string*) function.
3465 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3466 *
3467 * @param[in] args Array of arguments.
3468 * @param[in] arg_count Count of elements in @p args.
3469 * @param[in,out] set Context and result set at the same time.
3470 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003471 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003472 */
3473static LY_ERR
3474xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3475{
3476 uint16_t i;
3477 char *str = NULL;
3478 size_t used = 1;
3479 LY_ERR rc = LY_SUCCESS;
3480 struct lysc_node_leaf *sleaf;
3481
3482 if (options & LYXP_SCNODE_ALL) {
3483 for (i = 0; i < arg_count; ++i) {
3484 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3485 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3486 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3487 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003488 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003489 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 +02003490 }
3491 }
3492 }
3493 set_scnode_clear_ctx(set);
3494 return rc;
3495 }
3496
3497 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003498 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003499 if (rc != LY_SUCCESS) {
3500 free(str);
3501 return rc;
3502 }
3503
3504 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3505 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3506 strcpy(str + used - 1, args[i]->val.str);
3507 used += strlen(args[i]->val.str);
3508 }
3509
3510 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003511 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003512 set->type = LYXP_SET_STRING;
3513 set->val.str = str;
3514
3515 return LY_SUCCESS;
3516}
3517
3518/**
3519 * @brief Execute the XPath contains(string, string) function.
3520 * Returns LYXP_SET_BOOLEAN whether the second argument can
3521 * be found in the first or not.
3522 *
3523 * @param[in] args Array of arguments.
3524 * @param[in] arg_count Count of elements in @p args.
3525 * @param[in,out] set Context and result set at the same time.
3526 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003527 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528 */
3529static LY_ERR
3530xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3531{
3532 struct lysc_node_leaf *sleaf;
3533 LY_ERR rc = LY_SUCCESS;
3534
3535 if (options & LYXP_SCNODE_ALL) {
3536 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3537 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3538 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 +02003539 } else if (!warn_is_string_type(sleaf->type)) {
3540 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003541 }
3542 }
3543
3544 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3545 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3546 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 +02003547 } else if (!warn_is_string_type(sleaf->type)) {
3548 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003549 }
3550 }
3551 set_scnode_clear_ctx(set);
3552 return rc;
3553 }
3554
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003555 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003556 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003557 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003558 LY_CHECK_RET(rc);
3559
3560 if (strstr(args[0]->val.str, args[1]->val.str)) {
3561 set_fill_boolean(set, 1);
3562 } else {
3563 set_fill_boolean(set, 0);
3564 }
3565
3566 return LY_SUCCESS;
3567}
3568
3569/**
3570 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3571 * with the size of the node-set from the argument.
3572 *
3573 * @param[in] args Array of arguments.
3574 * @param[in] arg_count Count of elements in @p args.
3575 * @param[in,out] set Context and result set at the same time.
3576 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003577 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003578 */
3579static LY_ERR
3580xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3581{
3582 struct lysc_node *scnode = NULL;
3583 LY_ERR rc = LY_SUCCESS;
3584
3585 if (options & LYXP_SCNODE_ALL) {
3586 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3587 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 }
3589 set_scnode_clear_ctx(set);
3590 return rc;
3591 }
3592
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 if (args[0]->type != LYXP_SET_NODE_SET) {
3594 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3595 return LY_EVALID;
3596 }
3597
3598 set_fill_number(set, args[0]->used);
3599 return LY_SUCCESS;
3600}
3601
3602/**
3603 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3604 * with the context with the intial node.
3605 *
3606 * @param[in] args Array of arguments.
3607 * @param[in] arg_count Count of elements in @p args.
3608 * @param[in,out] set Context and result set at the same time.
3609 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003610 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611 */
3612static LY_ERR
3613xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3614{
3615 if (arg_count || args) {
3616 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3617 return LY_EVALID;
3618 }
3619
3620 if (options & LYXP_SCNODE_ALL) {
3621 set_scnode_clear_ctx(set);
3622
Michal Vaskoecd62de2019-11-13 12:35:11 +01003623 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003625 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626
3627 /* position is filled later */
3628 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3629 }
3630
3631 return LY_SUCCESS;
3632}
3633
3634/**
3635 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3636 * leafref or instance-identifier target node(s).
3637 *
3638 * @param[in] args Array of arguments.
3639 * @param[in] arg_count Count of elements in @p args.
3640 * @param[in,out] set Context and result set at the same time.
3641 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003642 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003643 */
3644static LY_ERR
3645xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3646{
3647 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003648 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003649 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003650 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003651 struct ly_path *p;
3652 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003654 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 LY_ERR rc = LY_SUCCESS;
3656
3657 if (options & LYXP_SCNODE_ALL) {
3658 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3659 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3661 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 +02003662 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3663 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3664 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003665 }
3666 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003667 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003668 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003669 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003670
3671 /* it was already evaluated on schema, it must succeed */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003672 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3673 oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003674 assert(!rc);
3675
3676 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003677 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003678 ly_path_free(set->ctx, p);
3679
3680 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003681 }
3682
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 return rc;
3684 }
3685
Michal Vaskod3678892020-05-21 10:06:58 +02003686 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3688 return LY_EVALID;
3689 }
3690
Michal Vaskod3678892020-05-21 10:06:58 +02003691 lyxp_set_free_content(set);
3692 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003693 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3694 sleaf = (struct lysc_node_leaf *)leaf->schema;
3695 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3696 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3697 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003698 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3699 &leaf->value, set->tree, &node, &errmsg)) {
3700 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003701 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003702 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003703 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704 } else {
3705 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003706 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003707 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vaskob7be7a82020-08-20 09:09:04 +02003708 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 return LY_EVALID;
3710 }
3711 }
Michal Vasko004d3152020-06-11 19:59:22 +02003712
3713 /* insert it */
3714 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715 }
3716 }
3717
3718 return LY_SUCCESS;
3719}
3720
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003721static LY_ERR
3722xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, int options, int self_match, const char *func)
3723{
3724 uint16_t i;
3725 LY_ARRAY_COUNT_TYPE u;
3726 struct lyd_node_term *leaf;
3727 struct lysc_node_leaf *sleaf;
3728 struct lyd_meta *meta;
3729 struct lyd_value data = {0}, *val;
3730 struct ly_err_item *err = NULL;
3731 LY_ERR rc = LY_SUCCESS;
3732 int found;
3733
3734 if (options & LYXP_SCNODE_ALL) {
3735 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3736 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3737 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3738 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3739 sleaf->name);
3740 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3741 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3742 }
3743
3744 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3745 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3746 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3747 sleaf->name);
3748 } else if (!warn_is_string_type(sleaf->type)) {
3749 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3750 }
3751 }
3752 set_scnode_clear_ctx(set);
3753 return rc;
3754 }
3755
3756 if (args[0]->type != LYXP_SET_NODE_SET) {
3757 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3758 "derived-from(-or-self)(node-set, string)");
3759 return LY_EVALID;
3760 }
3761 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3762 LY_CHECK_RET(rc);
3763
3764 set_fill_boolean(set, 0);
3765 found = 0;
3766 for (i = 0; i < args[0]->used; ++i) {
3767 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3768 continue;
3769 }
3770
3771 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3772 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3773 sleaf = (struct lysc_node_leaf *)leaf->schema;
3774 val = &leaf->value;
3775 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3776 /* uninteresting */
3777 continue;
3778 }
3779
3780 /* store args[1] as ident */
3781 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003782 0, set->format, (void *)set->local_mod,
3783 (struct lyd_node *)leaf, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003784 } else {
3785 meta = args[0]->val.meta[i].meta;
3786 val = &meta->value;
3787 if (val->realtype->basetype != LY_TYPE_IDENT) {
3788 /* uninteresting */
3789 continue;
3790 }
3791
3792 /* store args[1] as ident */
3793 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003794 0, set->format, (void *)meta->annotation->module,
3795 meta->parent, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003796 }
3797
3798 if (err) {
3799 ly_err_print(err);
3800 ly_err_free(err);
3801 }
3802 LY_CHECK_RET(rc);
3803
3804 /* finally check the identity itself */
3805 if (self_match && (data.ident == val->ident)) {
3806 set_fill_boolean(set, 1);
3807 found = 1;
3808 }
3809 if (!found) {
3810 LY_ARRAY_FOR(data.ident->derived, u) {
3811 if (data.ident->derived[u] == val->ident) {
3812 set_fill_boolean(set, 1);
3813 found = 1;
3814 break;
3815 }
3816 }
3817 }
3818
3819 /* free temporary value */
3820 val->realtype->plugin->free(set->ctx, &data);
3821 if (found) {
3822 break;
3823 }
3824 }
3825
3826 return LY_SUCCESS;
3827}
3828
Michal Vasko03ff5a72019-09-11 13:49:33 +02003829/**
3830 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3831 * on whether the first argument nodes contain a node of an identity derived from the second
3832 * argument identity.
3833 *
3834 * @param[in] args Array of arguments.
3835 * @param[in] arg_count Count of elements in @p args.
3836 * @param[in,out] set Context and result set at the same time.
3837 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003838 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003839 */
3840static LY_ERR
3841xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3842{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003843 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003844}
3845
3846/**
3847 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3848 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3849 * the second argument identity.
3850 *
3851 * @param[in] args Array of arguments.
3852 * @param[in] arg_count Count of elements in @p args.
3853 * @param[in,out] set Context and result set at the same time.
3854 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003855 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003856 */
3857static LY_ERR
3858xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3859{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003860 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003861}
3862
3863/**
3864 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3865 * with the integer value of the first node's enum value, otherwise NaN.
3866 *
3867 * @param[in] args Array of arguments.
3868 * @param[in] arg_count Count of elements in @p args.
3869 * @param[in,out] set Context and result set at the same time.
3870 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003871 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003872 */
3873static LY_ERR
3874xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3875{
3876 struct lyd_node_term *leaf;
3877 struct lysc_node_leaf *sleaf;
3878 LY_ERR rc = LY_SUCCESS;
3879
3880 if (options & LYXP_SCNODE_ALL) {
3881 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3882 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003883 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3884 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 +02003885 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3886 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003887 }
3888 set_scnode_clear_ctx(set);
3889 return rc;
3890 }
3891
Michal Vaskod3678892020-05-21 10:06:58 +02003892 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3894 return LY_EVALID;
3895 }
3896
3897 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003898 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003899 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3900 sleaf = (struct lysc_node_leaf *)leaf->schema;
3901 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3902 set_fill_number(set, leaf->value.enum_item->value);
3903 }
3904 }
3905
3906 return LY_SUCCESS;
3907}
3908
3909/**
3910 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3911 * with false value.
3912 *
3913 * @param[in] args Array of arguments.
3914 * @param[in] arg_count Count of elements in @p args.
3915 * @param[in,out] set Context and result set at the same time.
3916 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003917 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003918 */
3919static LY_ERR
3920xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3921{
3922 if (options & LYXP_SCNODE_ALL) {
3923 set_scnode_clear_ctx(set);
3924 return LY_SUCCESS;
3925 }
3926
3927 set_fill_boolean(set, 0);
3928 return LY_SUCCESS;
3929}
3930
3931/**
3932 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3933 * with the first argument floored (truncated).
3934 *
3935 * @param[in] args Array of arguments.
3936 * @param[in] arg_count Count of elements in @p args.
3937 * @param[in,out] set Context and result set at the same time.
3938 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003939 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003940 */
3941static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003942xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003943{
3944 LY_ERR rc;
3945
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003946 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003947 LY_CHECK_RET(rc);
3948 if (isfinite(args[0]->val.num)) {
3949 set_fill_number(set, (long long)args[0]->val.num);
3950 }
3951
3952 return LY_SUCCESS;
3953}
3954
3955/**
3956 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3957 * whether the language of the text matches the one from the argument.
3958 *
3959 * @param[in] args Array of arguments.
3960 * @param[in] arg_count Count of elements in @p args.
3961 * @param[in,out] set Context and result set at the same time.
3962 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003963 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003964 */
3965static LY_ERR
3966xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3967{
3968 const struct lyd_node *node;
3969 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003970 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003971 const char *val;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003972 int i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003973 LY_ERR rc = LY_SUCCESS;
3974
3975 if (options & LYXP_SCNODE_ALL) {
3976 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3977 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3978 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 +02003979 } else if (!warn_is_string_type(sleaf->type)) {
3980 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003981 }
3982 }
3983 set_scnode_clear_ctx(set);
3984 return rc;
3985 }
3986
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003987 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003988 LY_CHECK_RET(rc);
3989
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 if (set->type != LYXP_SET_NODE_SET) {
3991 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3992 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003993 } else if (!set->used) {
3994 set_fill_boolean(set, 0);
3995 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996 }
3997
3998 switch (set->val.nodes[0].type) {
3999 case LYXP_NODE_ELEM:
4000 case LYXP_NODE_TEXT:
4001 node = set->val.nodes[0].node;
4002 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004003 case LYXP_NODE_META:
4004 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004005 break;
4006 default:
4007 /* nothing to do with roots */
4008 set_fill_boolean(set, 0);
4009 return LY_SUCCESS;
4010 }
4011
Michal Vasko9f96a052020-03-10 09:41:45 +01004012 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004014 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004016 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 break;
4018 }
4019 }
4020
Michal Vasko9f96a052020-03-10 09:41:45 +01004021 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004022 break;
4023 }
4024 }
4025
4026 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004027 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 set_fill_boolean(set, 0);
4029 } else {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004030 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004031 for (i = 0; args[0]->val.str[i]; ++i) {
4032 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4033 set_fill_boolean(set, 0);
4034 break;
4035 }
4036 }
4037 if (!args[0]->val.str[i]) {
4038 if (!val[i] || (val[i] == '-')) {
4039 set_fill_boolean(set, 1);
4040 } else {
4041 set_fill_boolean(set, 0);
4042 }
4043 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044 }
4045
4046 return LY_SUCCESS;
4047}
4048
4049/**
4050 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4051 * with the context size.
4052 *
4053 * @param[in] args Array of arguments.
4054 * @param[in] arg_count Count of elements in @p args.
4055 * @param[in,out] set Context and result set at the same time.
4056 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004057 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 */
4059static LY_ERR
4060xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4061{
4062 if (options & LYXP_SCNODE_ALL) {
4063 set_scnode_clear_ctx(set);
4064 return LY_SUCCESS;
4065 }
4066
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 if (set->type != LYXP_SET_NODE_SET) {
4068 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4069 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004070 } else if (!set->used) {
4071 set_fill_number(set, 0);
4072 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004073 }
4074
4075 set_fill_number(set, set->ctx_size);
4076 return LY_SUCCESS;
4077}
4078
4079/**
4080 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4081 * with the node name without namespace from the argument or the context.
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
4090xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4091{
4092 struct lyxp_set_node *item;
4093 /* suppress unused variable warning */
4094 (void)options;
4095
4096 if (options & LYXP_SCNODE_ALL) {
4097 set_scnode_clear_ctx(set);
4098 return LY_SUCCESS;
4099 }
4100
4101 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 if (args[0]->type != LYXP_SET_NODE_SET) {
4103 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4104 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004105 } else if (!args[0]->used) {
4106 set_fill_string(set, "", 0);
4107 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 }
4109
4110 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004111 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004112
4113 item = &args[0]->val.nodes[0];
4114 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004115 if (set->type != LYXP_SET_NODE_SET) {
4116 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4117 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004118 } else if (!set->used) {
4119 set_fill_string(set, "", 0);
4120 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121 }
4122
4123 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004124 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125
4126 item = &set->val.nodes[0];
4127 }
4128
4129 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004130 case LYXP_NODE_NONE:
4131 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 case LYXP_NODE_ROOT:
4133 case LYXP_NODE_ROOT_CONFIG:
4134 case LYXP_NODE_TEXT:
4135 set_fill_string(set, "", 0);
4136 break;
4137 case LYXP_NODE_ELEM:
4138 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4139 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004140 case LYXP_NODE_META:
4141 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004142 break;
4143 }
4144
4145 return LY_SUCCESS;
4146}
4147
4148/**
4149 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4150 * with the node name fully qualified (with namespace) from the argument or the context.
4151 *
4152 * @param[in] args Array of arguments.
4153 * @param[in] arg_count Count of elements in @p args.
4154 * @param[in,out] set Context and result set at the same time.
4155 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004156 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157 */
4158static LY_ERR
4159xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4160{
4161 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004162 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004164 const char *name = NULL;
4165 int rc = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166
4167 if (options & LYXP_SCNODE_ALL) {
4168 set_scnode_clear_ctx(set);
4169 return LY_SUCCESS;
4170 }
4171
4172 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004173 if (args[0]->type != LYXP_SET_NODE_SET) {
4174 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4175 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004176 } else if (!args[0]->used) {
4177 set_fill_string(set, "", 0);
4178 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004179 }
4180
4181 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004182 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183
4184 item = &args[0]->val.nodes[0];
4185 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186 if (set->type != LYXP_SET_NODE_SET) {
4187 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4188 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004189 } else if (!set->used) {
4190 set_fill_string(set, "", 0);
4191 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004192 }
4193
4194 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004195 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196
4197 item = &set->val.nodes[0];
4198 }
4199
4200 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004201 case LYXP_NODE_NONE:
4202 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203 case LYXP_NODE_ROOT:
4204 case LYXP_NODE_ROOT_CONFIG:
4205 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004206 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207 break;
4208 case LYXP_NODE_ELEM:
4209 mod = item->node->schema->module;
4210 name = item->node->schema->name;
4211 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004212 case LYXP_NODE_META:
4213 mod = ((struct lyd_meta *)item->node)->annotation->module;
4214 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 break;
4216 }
4217
4218 if (mod && name) {
4219 switch (set->format) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004220 case LY_PREF_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4222 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004223 case LY_PREF_JSON:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 rc = asprintf(&str, "%s:%s", mod->name, name);
4225 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004226 case LY_PREF_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004227 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 }
4229 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4230 set_fill_string(set, str, strlen(str));
4231 free(str);
4232 } else {
4233 set_fill_string(set, "", 0);
4234 }
4235
4236 return LY_SUCCESS;
4237}
4238
4239/**
4240 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4241 * with the namespace of the node from the argument or the context.
4242 *
4243 * @param[in] args Array of arguments.
4244 * @param[in] arg_count Count of elements in @p args.
4245 * @param[in,out] set Context and result set at the same time.
4246 * @param[in] options XPath options.
4247 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4248 */
4249static LY_ERR
4250xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4251{
4252 struct lyxp_set_node *item;
4253 struct lys_module *mod;
4254 /* suppress unused variable warning */
4255 (void)options;
4256
4257 if (options & LYXP_SCNODE_ALL) {
4258 set_scnode_clear_ctx(set);
4259 return LY_SUCCESS;
4260 }
4261
4262 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004263 if (args[0]->type != LYXP_SET_NODE_SET) {
4264 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4265 "namespace-uri(node-set?)");
4266 return LY_EVALID;
4267 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004268 set_fill_string(set, "", 0);
4269 return LY_SUCCESS;
4270 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004271
4272 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004273 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004274
4275 item = &args[0]->val.nodes[0];
4276 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004277 if (set->type != LYXP_SET_NODE_SET) {
4278 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4279 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004280 } else if (!set->used) {
4281 set_fill_string(set, "", 0);
4282 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283 }
4284
4285 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004286 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287
4288 item = &set->val.nodes[0];
4289 }
4290
4291 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004292 case LYXP_NODE_NONE:
4293 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 case LYXP_NODE_ROOT:
4295 case LYXP_NODE_ROOT_CONFIG:
4296 case LYXP_NODE_TEXT:
4297 set_fill_string(set, "", 0);
4298 break;
4299 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004300 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004301 if (item->type == LYXP_NODE_ELEM) {
4302 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004303 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004304 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004305 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004306 }
4307
4308 set_fill_string(set, mod->ns, strlen(mod->ns));
4309 break;
4310 }
4311
4312 return LY_SUCCESS;
4313}
4314
4315/**
4316 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4317 * with only nodes from the context. In practice it either leaves the context
4318 * as it is or returns an empty node set.
4319 *
4320 * @param[in] args Array of arguments.
4321 * @param[in] arg_count Count of elements in @p args.
4322 * @param[in,out] set Context and result set at the same time.
4323 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004324 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004325 */
4326static LY_ERR
4327xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4328{
4329 if (options & LYXP_SCNODE_ALL) {
4330 set_scnode_clear_ctx(set);
4331 return LY_SUCCESS;
4332 }
4333
4334 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004335 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004336 }
4337 return LY_SUCCESS;
4338}
4339
4340/**
4341 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4342 * with normalized value (no leading, trailing, double white spaces) of the node
4343 * from the argument or the context.
4344 *
4345 * @param[in] args Array of arguments.
4346 * @param[in] arg_count Count of elements in @p args.
4347 * @param[in,out] set Context and result set at the same time.
4348 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004349 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004350 */
4351static LY_ERR
4352xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4353{
4354 uint16_t i, new_used;
4355 char *new;
4356 int have_spaces = 0, space_before = 0;
4357 struct lysc_node_leaf *sleaf;
4358 LY_ERR rc = LY_SUCCESS;
4359
4360 if (options & LYXP_SCNODE_ALL) {
4361 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4362 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4363 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 +02004364 } else if (!warn_is_string_type(sleaf->type)) {
4365 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 }
4367 }
4368 set_scnode_clear_ctx(set);
4369 return rc;
4370 }
4371
4372 if (arg_count) {
4373 set_fill_set(set, args[0]);
4374 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004375 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004376 LY_CHECK_RET(rc);
4377
4378 /* is there any normalization necessary? */
4379 for (i = 0; set->val.str[i]; ++i) {
4380 if (is_xmlws(set->val.str[i])) {
4381 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4382 have_spaces = 1;
4383 break;
4384 }
4385 space_before = 1;
4386 } else {
4387 space_before = 0;
4388 }
4389 }
4390
4391 /* yep, there is */
4392 if (have_spaces) {
4393 /* it's enough, at least one character will go, makes space for ending '\0' */
4394 new = malloc(strlen(set->val.str) * sizeof(char));
4395 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4396 new_used = 0;
4397
4398 space_before = 0;
4399 for (i = 0; set->val.str[i]; ++i) {
4400 if (is_xmlws(set->val.str[i])) {
4401 if ((i == 0) || space_before) {
4402 space_before = 1;
4403 continue;
4404 } else {
4405 space_before = 1;
4406 }
4407 } else {
4408 space_before = 0;
4409 }
4410
4411 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4412 ++new_used;
4413 }
4414
4415 /* at worst there is one trailing space now */
4416 if (new_used && is_xmlws(new[new_used - 1])) {
4417 --new_used;
4418 }
4419
4420 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4421 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4422 new[new_used] = '\0';
4423
4424 free(set->val.str);
4425 set->val.str = new;
4426 }
4427
4428 return LY_SUCCESS;
4429}
4430
4431/**
4432 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4433 * with the argument converted to boolean and logically inverted.
4434 *
4435 * @param[in] args Array of arguments.
4436 * @param[in] arg_count Count of elements in @p args.
4437 * @param[in,out] set Context and result set at the same time.
4438 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004439 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004440 */
4441static LY_ERR
4442xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4443{
4444 if (options & LYXP_SCNODE_ALL) {
4445 set_scnode_clear_ctx(set);
4446 return LY_SUCCESS;
4447 }
4448
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004449 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004450 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451 set_fill_boolean(set, 0);
4452 } else {
4453 set_fill_boolean(set, 1);
4454 }
4455
4456 return LY_SUCCESS;
4457}
4458
4459/**
4460 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4461 * with the number representation of either the argument or the context.
4462 *
4463 * @param[in] args Array of arguments.
4464 * @param[in] arg_count Count of elements in @p args.
4465 * @param[in,out] set Context and result set at the same time.
4466 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004467 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468 */
4469static LY_ERR
4470xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4471{
4472 LY_ERR rc;
4473
4474 if (options & LYXP_SCNODE_ALL) {
4475 set_scnode_clear_ctx(set);
4476 return LY_SUCCESS;
4477 }
4478
4479 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004480 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004481 LY_CHECK_RET(rc);
4482 set_fill_set(set, args[0]);
4483 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004484 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004485 LY_CHECK_RET(rc);
4486 }
4487
4488 return LY_SUCCESS;
4489}
4490
4491/**
4492 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4493 * with the context position.
4494 *
4495 * @param[in] args Array of arguments.
4496 * @param[in] arg_count Count of elements in @p args.
4497 * @param[in,out] set Context and result set at the same time.
4498 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004499 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004500 */
4501static LY_ERR
4502xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4503{
4504 if (options & LYXP_SCNODE_ALL) {
4505 set_scnode_clear_ctx(set);
4506 return LY_SUCCESS;
4507 }
4508
Michal Vasko03ff5a72019-09-11 13:49:33 +02004509 if (set->type != LYXP_SET_NODE_SET) {
4510 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4511 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004512 } else if (!set->used) {
4513 set_fill_number(set, 0);
4514 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004515 }
4516
4517 set_fill_number(set, set->ctx_pos);
4518
4519 /* UNUSED in 'Release' build type */
4520 (void)options;
4521 return LY_SUCCESS;
4522}
4523
4524/**
4525 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4526 * depending on whether the second argument regex matches the first argument string. For details refer to
4527 * YANG 1.1 RFC section 10.2.1.
4528 *
4529 * @param[in] args Array of arguments.
4530 * @param[in] arg_count Count of elements in @p args.
4531 * @param[in,out] set Context and result set at the same time.
4532 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004533 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534 */
4535static LY_ERR
4536xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4537{
4538 struct lysc_pattern **patterns = NULL, **pattern;
4539 struct lysc_node_leaf *sleaf;
4540 char *path;
4541 LY_ERR rc = LY_SUCCESS;
4542 struct ly_err_item *err;
4543
4544 if (options & LYXP_SCNODE_ALL) {
4545 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4546 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4547 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 +02004548 } else if (!warn_is_string_type(sleaf->type)) {
4549 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004550 }
4551 }
4552
4553 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4554 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4555 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 +02004556 } else if (!warn_is_string_type(sleaf->type)) {
4557 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004558 }
4559 }
4560 set_scnode_clear_ctx(set);
4561 return rc;
4562 }
4563
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004564 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004566 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004567 LY_CHECK_RET(rc);
4568
4569 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4570 *pattern = malloc(sizeof **pattern);
4571 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4572 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4573 free(path);
4574 if (rc != LY_SUCCESS) {
4575 LY_ARRAY_FREE(patterns);
4576 return rc;
4577 }
4578
4579 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4580 pcre2_code_free((*pattern)->code);
4581 free(*pattern);
4582 LY_ARRAY_FREE(patterns);
4583 if (rc && (rc != LY_EVALID)) {
4584 ly_err_print(err);
4585 ly_err_free(err);
4586 return rc;
4587 }
4588
4589 if (rc == LY_EVALID) {
4590 ly_err_free(err);
4591 set_fill_boolean(set, 0);
4592 } else {
4593 set_fill_boolean(set, 1);
4594 }
4595
4596 return LY_SUCCESS;
4597}
4598
4599/**
4600 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4601 * with the rounded first argument. For details refer to
4602 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4603 *
4604 * @param[in] args Array of arguments.
4605 * @param[in] arg_count Count of elements in @p args.
4606 * @param[in,out] set Context and result set at the same time.
4607 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004608 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 */
4610static LY_ERR
4611xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4612{
4613 struct lysc_node_leaf *sleaf;
4614 LY_ERR rc = LY_SUCCESS;
4615
4616 if (options & LYXP_SCNODE_ALL) {
4617 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4618 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004619 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4620 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 +02004621 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4622 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004623 }
4624 set_scnode_clear_ctx(set);
4625 return rc;
4626 }
4627
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004628 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004629 LY_CHECK_RET(rc);
4630
4631 /* cover only the cases where floor can't be used */
4632 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4633 set_fill_number(set, -0.0f);
4634 } else {
4635 args[0]->val.num += 0.5;
4636 rc = xpath_floor(args, 1, args[0], options);
4637 LY_CHECK_RET(rc);
4638 set_fill_number(set, args[0]->val.num);
4639 }
4640
4641 return LY_SUCCESS;
4642}
4643
4644/**
4645 * @brief Execute the XPath starts-with(string, string) function.
4646 * Returns LYXP_SET_BOOLEAN whether the second argument is
4647 * the prefix of the first or not.
4648 *
4649 * @param[in] args Array of arguments.
4650 * @param[in] arg_count Count of elements in @p args.
4651 * @param[in,out] set Context and result set at the same time.
4652 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004653 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 */
4655static LY_ERR
4656xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4657{
4658 struct lysc_node_leaf *sleaf;
4659 LY_ERR rc = LY_SUCCESS;
4660
4661 if (options & LYXP_SCNODE_ALL) {
4662 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4663 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4664 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 +02004665 } else if (!warn_is_string_type(sleaf->type)) {
4666 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004667 }
4668 }
4669
4670 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4671 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4672 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 +02004673 } else if (!warn_is_string_type(sleaf->type)) {
4674 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675 }
4676 }
4677 set_scnode_clear_ctx(set);
4678 return rc;
4679 }
4680
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004681 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004682 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004683 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 LY_CHECK_RET(rc);
4685
4686 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4687 set_fill_boolean(set, 0);
4688 } else {
4689 set_fill_boolean(set, 1);
4690 }
4691
4692 return LY_SUCCESS;
4693}
4694
4695/**
4696 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4697 * with the string representation of either the argument or the context.
4698 *
4699 * @param[in] args Array of arguments.
4700 * @param[in] arg_count Count of elements in @p args.
4701 * @param[in,out] set Context and result set at the same time.
4702 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004703 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004704 */
4705static LY_ERR
4706xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4707{
4708 LY_ERR rc;
4709
4710 if (options & LYXP_SCNODE_ALL) {
4711 set_scnode_clear_ctx(set);
4712 return LY_SUCCESS;
4713 }
4714
4715 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004716 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717 LY_CHECK_RET(rc);
4718 set_fill_set(set, args[0]);
4719 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004720 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004721 LY_CHECK_RET(rc);
4722 }
4723
4724 return LY_SUCCESS;
4725}
4726
4727/**
4728 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4729 * with the length of the string in either the argument or the context.
4730 *
4731 * @param[in] args Array of arguments.
4732 * @param[in] arg_count Count of elements in @p args.
4733 * @param[in,out] set Context and result set at the same time.
4734 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004735 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004736 */
4737static LY_ERR
4738xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4739{
4740 struct lysc_node_leaf *sleaf;
4741 LY_ERR rc = LY_SUCCESS;
4742
4743 if (options & LYXP_SCNODE_ALL) {
4744 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4745 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4746 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 +02004747 } else if (!warn_is_string_type(sleaf->type)) {
4748 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004749 }
4750 }
4751 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4752 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4753 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 +02004754 } else if (!warn_is_string_type(sleaf->type)) {
4755 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756 }
4757 }
4758 set_scnode_clear_ctx(set);
4759 return rc;
4760 }
4761
4762 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004763 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004764 LY_CHECK_RET(rc);
4765 set_fill_number(set, strlen(args[0]->val.str));
4766 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004767 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004768 LY_CHECK_RET(rc);
4769 set_fill_number(set, strlen(set->val.str));
4770 }
4771
4772 return LY_SUCCESS;
4773}
4774
4775/**
4776 * @brief Execute the XPath substring(string, number, number?) function.
4777 * Returns LYXP_SET_STRING substring of the first argument starting
4778 * on the second argument index ending on the third argument index,
4779 * indexed from 1. For exact definition refer to
4780 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4781 *
4782 * @param[in] args Array of arguments.
4783 * @param[in] arg_count Count of elements in @p args.
4784 * @param[in,out] set Context and result set at the same time.
4785 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004786 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004787 */
4788static LY_ERR
4789xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4790{
4791 int start, len;
4792 uint16_t str_start, str_len, pos;
4793 struct lysc_node_leaf *sleaf;
4794 LY_ERR rc = LY_SUCCESS;
4795
4796 if (options & LYXP_SCNODE_ALL) {
4797 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4798 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4799 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 +02004800 } else if (!warn_is_string_type(sleaf->type)) {
4801 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 }
4803 }
4804
4805 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4806 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4807 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 +02004808 } else if (!warn_is_numeric_type(sleaf->type)) {
4809 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004810 }
4811 }
4812
4813 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4814 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4815 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4816 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 +02004817 } else if (!warn_is_numeric_type(sleaf->type)) {
4818 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004819 }
4820 }
4821 set_scnode_clear_ctx(set);
4822 return rc;
4823 }
4824
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004825 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 LY_CHECK_RET(rc);
4827
4828 /* start */
4829 if (xpath_round(&args[1], 1, args[1], options)) {
4830 return -1;
4831 }
4832 if (isfinite(args[1]->val.num)) {
4833 start = args[1]->val.num - 1;
4834 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4835 start = INT_MIN;
4836 } else {
4837 start = INT_MAX;
4838 }
4839
4840 /* len */
4841 if (arg_count == 3) {
4842 rc = xpath_round(&args[2], 1, args[2], options);
4843 LY_CHECK_RET(rc);
4844 if (isfinite(args[2]->val.num)) {
4845 len = args[2]->val.num;
4846 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4847 len = 0;
4848 } else {
4849 len = INT_MAX;
4850 }
4851 } else {
4852 len = INT_MAX;
4853 }
4854
4855 /* find matching character positions */
4856 str_start = 0;
4857 str_len = 0;
4858 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4859 if (pos < start) {
4860 ++str_start;
4861 } else if (pos < start + len) {
4862 ++str_len;
4863 } else {
4864 break;
4865 }
4866 }
4867
4868 set_fill_string(set, args[0]->val.str + str_start, str_len);
4869 return LY_SUCCESS;
4870}
4871
4872/**
4873 * @brief Execute the XPath substring-after(string, string) function.
4874 * Returns LYXP_SET_STRING with the string succeeding the occurance
4875 * of the second argument in the first or an empty string.
4876 *
4877 * @param[in] args Array of arguments.
4878 * @param[in] arg_count Count of elements in @p args.
4879 * @param[in,out] set Context and result set at the same time.
4880 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004881 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004882 */
4883static LY_ERR
4884xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4885{
4886 char *ptr;
4887 struct lysc_node_leaf *sleaf;
4888 LY_ERR rc = LY_SUCCESS;
4889
4890 if (options & LYXP_SCNODE_ALL) {
4891 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4892 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4893 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 +02004894 } else if (!warn_is_string_type(sleaf->type)) {
4895 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004896 }
4897 }
4898
4899 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4900 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4901 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 +02004902 } else if (!warn_is_string_type(sleaf->type)) {
4903 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004904 }
4905 }
4906 set_scnode_clear_ctx(set);
4907 return rc;
4908 }
4909
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004910 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004911 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004912 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004913 LY_CHECK_RET(rc);
4914
4915 ptr = strstr(args[0]->val.str, args[1]->val.str);
4916 if (ptr) {
4917 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4918 } else {
4919 set_fill_string(set, "", 0);
4920 }
4921
4922 return LY_SUCCESS;
4923}
4924
4925/**
4926 * @brief Execute the XPath substring-before(string, string) function.
4927 * Returns LYXP_SET_STRING with the string preceding the occurance
4928 * of the second argument in the first or an empty string.
4929 *
4930 * @param[in] args Array of arguments.
4931 * @param[in] arg_count Count of elements in @p args.
4932 * @param[in,out] set Context and result set at the same time.
4933 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004934 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004935 */
4936static LY_ERR
4937xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4938{
4939 char *ptr;
4940 struct lysc_node_leaf *sleaf;
4941 LY_ERR rc = LY_SUCCESS;
4942
4943 if (options & LYXP_SCNODE_ALL) {
4944 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4945 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4946 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 +02004947 } else if (!warn_is_string_type(sleaf->type)) {
4948 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004949 }
4950 }
4951
4952 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4953 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4954 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 +02004955 } else if (!warn_is_string_type(sleaf->type)) {
4956 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004957 }
4958 }
4959 set_scnode_clear_ctx(set);
4960 return rc;
4961 }
4962
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004963 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004964 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004965 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004966 LY_CHECK_RET(rc);
4967
4968 ptr = strstr(args[0]->val.str, args[1]->val.str);
4969 if (ptr) {
4970 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4971 } else {
4972 set_fill_string(set, "", 0);
4973 }
4974
4975 return LY_SUCCESS;
4976}
4977
4978/**
4979 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4980 * with the sum of all the nodes in the context.
4981 *
4982 * @param[in] args Array of arguments.
4983 * @param[in] arg_count Count of elements in @p args.
4984 * @param[in,out] set Context and result set at the same time.
4985 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004986 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004987 */
4988static LY_ERR
4989xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4990{
4991 long double num;
4992 char *str;
4993 uint16_t i;
4994 struct lyxp_set set_item;
4995 struct lysc_node_leaf *sleaf;
4996 LY_ERR rc = LY_SUCCESS;
4997
4998 if (options & LYXP_SCNODE_ALL) {
4999 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5000 for (i = 0; i < args[0]->used; ++i) {
5001 if (args[0]->val.scnodes[i].in_ctx == 1) {
5002 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5003 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5004 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5005 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005006 } else if (!warn_is_numeric_type(sleaf->type)) {
5007 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005008 }
5009 }
5010 }
5011 }
5012 set_scnode_clear_ctx(set);
5013 return rc;
5014 }
5015
5016 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005017
5018 if (args[0]->type != LYXP_SET_NODE_SET) {
5019 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5020 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005021 } else if (!args[0]->used) {
5022 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005023 }
5024
Michal Vasko5c4e5892019-11-14 12:31:38 +01005025 set_init(&set_item, set);
5026
Michal Vasko03ff5a72019-09-11 13:49:33 +02005027 set_item.type = LYXP_SET_NODE_SET;
5028 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5029 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5030
5031 set_item.used = 1;
5032 set_item.size = 1;
5033
5034 for (i = 0; i < args[0]->used; ++i) {
5035 set_item.val.nodes[0] = args[0]->val.nodes[i];
5036
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005037 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005038 LY_CHECK_RET(rc);
5039 num = cast_string_to_number(str);
5040 free(str);
5041 set->val.num += num;
5042 }
5043
5044 free(set_item.val.nodes);
5045
5046 return LY_SUCCESS;
5047}
5048
5049/**
5050 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5051 * with the text content of the nodes in the context.
5052 *
5053 * @param[in] args Array of arguments.
5054 * @param[in] arg_count Count of elements in @p args.
5055 * @param[in,out] set Context and result set at the same time.
5056 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005057 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005058 */
5059static LY_ERR
5060xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5061{
5062 uint32_t i;
5063
5064 if (options & LYXP_SCNODE_ALL) {
5065 set_scnode_clear_ctx(set);
5066 return LY_SUCCESS;
5067 }
5068
Michal Vasko03ff5a72019-09-11 13:49:33 +02005069 if (set->type != LYXP_SET_NODE_SET) {
5070 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5071 return LY_EVALID;
5072 }
5073
5074 for (i = 0; i < set->used;) {
5075 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005076 case LYXP_NODE_NONE:
5077 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005078 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5080 set->val.nodes[i].type = LYXP_NODE_TEXT;
5081 ++i;
5082 break;
5083 }
5084 /* fall through */
5085 case LYXP_NODE_ROOT:
5086 case LYXP_NODE_ROOT_CONFIG:
5087 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005088 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005089 set_remove_node(set, i);
5090 break;
5091 }
5092 }
5093
5094 return LY_SUCCESS;
5095}
5096
5097/**
5098 * @brief Execute the XPath translate(string, string, string) function.
5099 * Returns LYXP_SET_STRING with the first argument with the characters
5100 * from the second argument replaced by those on the corresponding
5101 * positions in the third argument.
5102 *
5103 * @param[in] args Array of arguments.
5104 * @param[in] arg_count Count of elements in @p args.
5105 * @param[in,out] set Context and result set at the same time.
5106 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005107 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005108 */
5109static LY_ERR
5110xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5111{
5112 uint16_t i, j, new_used;
5113 char *new;
5114 int found, have_removed;
5115 struct lysc_node_leaf *sleaf;
5116 LY_ERR rc = LY_SUCCESS;
5117
5118 if (options & LYXP_SCNODE_ALL) {
5119 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5120 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5121 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 +02005122 } else if (!warn_is_string_type(sleaf->type)) {
5123 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 }
5125 }
5126
5127 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5128 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5129 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 +02005130 } else if (!warn_is_string_type(sleaf->type)) {
5131 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005132 }
5133 }
5134
5135 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5136 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5137 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 +02005138 } else if (!warn_is_string_type(sleaf->type)) {
5139 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 }
5141 }
5142 set_scnode_clear_ctx(set);
5143 return rc;
5144 }
5145
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005146 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005147 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005148 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005149 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005150 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005151 LY_CHECK_RET(rc);
5152
5153 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5154 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5155 new_used = 0;
5156
5157 have_removed = 0;
5158 for (i = 0; args[0]->val.str[i]; ++i) {
5159 found = 0;
5160
5161 for (j = 0; args[1]->val.str[j]; ++j) {
5162 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5163 /* removing this char */
5164 if (j >= strlen(args[2]->val.str)) {
5165 have_removed = 1;
5166 found = 1;
5167 break;
5168 }
5169 /* replacing this char */
5170 new[new_used] = args[2]->val.str[j];
5171 ++new_used;
5172 found = 1;
5173 break;
5174 }
5175 }
5176
5177 /* copying this char */
5178 if (!found) {
5179 new[new_used] = args[0]->val.str[i];
5180 ++new_used;
5181 }
5182 }
5183
5184 if (have_removed) {
5185 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5186 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5187 }
5188 new[new_used] = '\0';
5189
Michal Vaskod3678892020-05-21 10:06:58 +02005190 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005191 set->type = LYXP_SET_STRING;
5192 set->val.str = new;
5193
5194 return LY_SUCCESS;
5195}
5196
5197/**
5198 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5199 * with true value.
5200 *
5201 * @param[in] args Array of arguments.
5202 * @param[in] arg_count Count of elements in @p args.
5203 * @param[in,out] set Context and result set at the same time.
5204 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005205 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005206 */
5207static LY_ERR
5208xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5209{
5210 if (options & LYXP_SCNODE_ALL) {
5211 set_scnode_clear_ctx(set);
5212 return LY_SUCCESS;
5213 }
5214
5215 set_fill_boolean(set, 1);
5216 return LY_SUCCESS;
5217}
5218
5219/*
5220 * moveto functions
5221 *
5222 * They and only they actually change the context (set).
5223 */
5224
5225/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005226 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005227 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005228 * XPath @p set is expected to be a (sc)node set!
5229 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005230 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5231 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5232 * @param[in] set Set with XPath context.
5233 * @param[out] moveto_mod Expected module of a matching node.
5234 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005235 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005236static LY_ERR
5237moveto_resolve_model(const char **qname, uint16_t *qname_len, struct lyxp_set *set, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005238{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005239 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005240 const char *ptr;
5241 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242
Michal Vasko2104e9f2020-03-06 08:23:25 +01005243 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5244
Michal Vasko6346ece2019-09-24 13:12:53 +02005245 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5246 /* specific module */
5247 pref_len = ptr - *qname;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005248 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005249
Michal Vasko004d3152020-06-11 19:59:22 +02005250 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005251 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005252 if (set->type == LYXP_SET_SCNODE_SET) {
5253 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5254 } else {
5255 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5256 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005257 return LY_EVALID;
5258 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005259
Michal Vasko6346ece2019-09-24 13:12:53 +02005260 *qname += pref_len + 1;
5261 *qname_len -= pref_len + 1;
5262 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5263 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005264 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005265 } else if (set->type == LYXP_SET_SCNODE_SET) {
5266 /* current node module */
5267 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005268 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005269 /* current node module */
5270 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 }
5272
Michal Vasko6346ece2019-09-24 13:12:53 +02005273 *moveto_mod = mod;
5274 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005275}
5276
5277/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005278 * @brief Move context @p set to the root. Handles absolute path.
5279 * Result is LYXP_SET_NODE_SET.
5280 *
5281 * @param[in,out] set Set to use.
5282 * @param[in] options Xpath options.
5283 */
5284static void
5285moveto_root(struct lyxp_set *set, int options)
5286{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005287 if (!set) {
5288 return;
5289 }
5290
5291 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005292 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005293 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005294 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005295 set->type = LYXP_SET_NODE_SET;
5296 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005297 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005298 }
5299}
5300
5301/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005302 * @brief Check whether a node has some unresolved "when".
5303 *
5304 * @param[in] node Node to check.
5305 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5306 */
5307static LY_ERR
5308moveto_when_check(const struct lyd_node *node)
5309{
5310 const struct lysc_node *schema;
5311
5312 if (!node) {
5313 return LY_SUCCESS;
5314 }
5315
5316 schema = node->schema;
5317 do {
5318 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5319 return LY_EINCOMPLETE;
5320 }
5321 schema = schema->parent;
5322 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5323
5324 return LY_SUCCESS;
5325}
5326
5327/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005328 * @brief Check @p node as a part of NameTest processing.
5329 *
5330 * @param[in] node Node to check.
5331 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005332 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005333 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5334 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005335 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5336 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337 */
5338static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005339moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op,
5340 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341{
5342 /* module check */
5343 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005344 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345 }
5346
Michal Vasko5c4e5892019-11-14 12:31:38 +01005347 /* context check */
5348 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005349 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005350 } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) {
5351 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005352 }
5353
5354 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005355 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005356 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005357 }
5358
Michal Vaskoa1424542019-11-14 16:08:52 +01005359 /* when check */
5360 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005361 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005362 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005363
5364 /* match */
5365 return LY_SUCCESS;
5366}
5367
5368/**
5369 * @brief Check @p node as a part of schema NameTest processing.
5370 *
5371 * @param[in] node Schema node to check.
5372 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005373 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005374 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5375 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005376 * @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 +02005377 */
5378static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005379moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op,
5380 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005382 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005383 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005384 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385 }
5386
5387 /* context check */
5388 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5389 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005390 } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) {
5391 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 }
5393
5394 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005395 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005396 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 }
5398
5399 /* match */
5400 return LY_SUCCESS;
5401}
5402
5403/**
Michal Vaskod3678892020-05-21 10:06:58 +02005404 * @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 +02005405 *
5406 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005407 * @param[in] mod Matching node module, NULL for any.
5408 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5410 */
5411static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005412moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005413{
Michal Vaskof03ed032020-03-04 13:31:44 +01005414 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005415 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005416 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 LY_ERR rc;
5418
Michal Vaskod3678892020-05-21 10:06:58 +02005419 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005420 return LY_SUCCESS;
5421 }
5422
5423 if (set->type != LYXP_SET_NODE_SET) {
5424 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5425 return LY_EVALID;
5426 }
5427
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428 for (i = 0; i < set->used; ) {
5429 replaced = 0;
5430
5431 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 +01005432 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005433
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005434 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005435 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005436 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005437 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005438 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005439 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005440
Michal Vaskod3678892020-05-21 10:06:58 +02005441 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005442 rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005443 if (rc == LY_SUCCESS) {
5444 if (!replaced) {
5445 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5446 replaced = 1;
5447 } else {
5448 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 }
Michal Vaskod3678892020-05-21 10:06:58 +02005450 ++i;
5451 } else if (rc == LY_EINCOMPLETE) {
5452 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005453 }
5454 }
5455
5456 if (!replaced) {
5457 /* no match */
5458 set_remove_node(set, i);
5459 }
5460 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461
5462 return LY_SUCCESS;
5463}
5464
5465/**
Michal Vaskod3678892020-05-21 10:06:58 +02005466 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5467 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468 *
5469 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005470 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005471 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskod3678892020-05-21 10:06:58 +02005472 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5473 */
5474static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005475moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005476{
Michal Vasko004d3152020-06-11 19:59:22 +02005477 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005478 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005479 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005480 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005481
Michal Vasko004d3152020-06-11 19:59:22 +02005482 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005483
5484 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005485 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005486 }
5487
5488 if (set->type != LYXP_SET_NODE_SET) {
5489 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005490 ret = LY_EVALID;
5491 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005492 }
5493
5494 /* context check for all the nodes since we have the schema node */
5495 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5496 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005497 goto cleanup;
Michal Vasko6b26e742020-07-17 15:02:10 +02005498 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
5499 && (scnode != set->context_op)) {
5500 lyxp_set_free_content(set);
5501 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005502 }
5503
5504 /* create specific data instance if needed */
5505 if (scnode->nodetype == LYS_LIST) {
5506 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5507 } else if (scnode->nodetype == LYS_LEAFLIST) {
5508 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005509 }
5510
5511 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005512 siblings = NULL;
5513
5514 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5515 assert(!set->val.nodes[i].node);
5516
5517 /* search in all the trees */
5518 siblings = set->tree;
5519 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5520 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005521 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005522 }
5523
5524 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005525 if (inst) {
5526 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005527 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005528 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005529 }
5530
5531 /* when check */
5532 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005533 ret = LY_EINCOMPLETE;
5534 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005535 }
5536
5537 if (sub) {
5538 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005539 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005540 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005541 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005542 /* no match */
5543 set_remove_node(set, i);
5544 }
5545 }
5546
Michal Vasko004d3152020-06-11 19:59:22 +02005547cleanup:
5548 lyd_free_tree(inst);
5549 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005550}
5551
5552/**
5553 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5554 *
5555 * @param[in,out] set Set to use.
5556 * @param[in] mod Matching node module, NULL for any.
5557 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005558 * @param[in] options XPath options.
5559 * @return LY_ERR
5560 */
5561static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005562moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005563{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005564 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005565 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005566 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005567
Michal Vaskod3678892020-05-21 10:06:58 +02005568 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005569 return LY_SUCCESS;
5570 }
5571
5572 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005573 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005574 return LY_EVALID;
5575 }
5576
Michal Vaskocafad9d2019-11-07 15:20:03 +01005577 /* getnext opts */
5578 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5579 if (options & LYXP_SCNODE_OUTPUT) {
5580 getnext_opts |= LYS_GETNEXT_OUTPUT;
5581 }
5582
Michal Vasko03ff5a72019-09-11 13:49:33 +02005583 orig_used = set->used;
5584 for (i = 0; i < orig_used; ++i) {
5585 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005586 if (set->val.scnodes[i].in_ctx != -2) {
5587 continue;
5588 }
5589
5590 /* remember context node */
5591 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005592 } else {
5593 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005595
5596 start_parent = set->val.scnodes[i].scnode;
5597
5598 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vaskod3678892020-05-21 10:06:58 +02005599 /* it can actually be in any module, it's all <running>, but we know it's mod (if set),
Michal Vasko03ff5a72019-09-11 13:49:33 +02005600 * so use it directly (root node itself is useless in this case) */
5601 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005602 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005603 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005604 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005605 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005606 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, mod)) {
Michal Vasko519fd602020-05-26 12:17:39 +02005607 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005608 /* we need to prevent these nodes from being considered in this moveto */
5609 if ((idx < orig_used) && (idx > i)) {
5610 set->val.scnodes[idx].in_ctx = 2;
5611 temp_ctx = 1;
5612 }
5613 }
5614 }
5615
5616 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005617 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005618 break;
5619 }
5620 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005621 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005622 }
5623
Michal Vasko519fd602020-05-26 12:17:39 +02005624 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5625 iter = NULL;
5626 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005627 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005628 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005629 if ((idx < orig_used) && (idx > i)) {
5630 set->val.scnodes[idx].in_ctx = 2;
5631 temp_ctx = 1;
5632 }
5633 }
5634 }
5635 }
5636 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005637
5638 /* correct temporary in_ctx values */
5639 if (temp_ctx) {
5640 for (i = 0; i < orig_used; ++i) {
5641 if (set->val.scnodes[i].in_ctx == 2) {
5642 set->val.scnodes[i].in_ctx = 1;
5643 }
5644 }
5645 }
5646
5647 return LY_SUCCESS;
5648}
5649
5650/**
Michal Vaskod3678892020-05-21 10:06:58 +02005651 * @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 +02005652 * Context position aware.
5653 *
5654 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005655 * @param[in] mod Matching node module, NULL for any.
5656 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005657 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5658 */
5659static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005660moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005661{
5662 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005663 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005664 struct lyxp_set ret_set;
5665 LY_ERR rc;
5666
Michal Vaskod3678892020-05-21 10:06:58 +02005667 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005668 return LY_SUCCESS;
5669 }
5670
5671 if (set->type != LYXP_SET_NODE_SET) {
5672 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5673 return LY_EVALID;
5674 }
5675
Michal Vasko9f96a052020-03-10 09:41:45 +01005676 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskod3678892020-05-21 10:06:58 +02005677 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678 LY_CHECK_RET(rc);
5679
Michal Vasko6346ece2019-09-24 13:12:53 +02005680 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005681 set_init(&ret_set, set);
5682 for (i = 0; i < set->used; ++i) {
5683
5684 /* TREE DFS */
5685 start = set->val.nodes[i].node;
5686 for (elem = next = start; elem; elem = next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005687 rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005688 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689 /* add matching node into result set */
5690 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5691 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5692 /* the node is a duplicate, we'll process it later in the set */
5693 goto skip_children;
5694 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005695 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005696 return rc;
5697 } else if (rc == LY_EINVAL) {
5698 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 }
5700
5701 /* TREE DFS NEXT ELEM */
5702 /* select element for the next run - children first */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005703 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704 if (!next) {
5705skip_children:
5706 /* no children, so try siblings, but only if it's not the start,
5707 * that is considered to be the root and it's siblings are not traversed */
5708 if (elem != start) {
5709 next = elem->next;
5710 } else {
5711 break;
5712 }
5713 }
5714 while (!next) {
5715 /* no siblings, go back through the parents */
5716 if ((struct lyd_node *)elem->parent == start) {
5717 /* we are done, no next element to process */
5718 break;
5719 }
5720 /* parent is already processed, go to its sibling */
5721 elem = (struct lyd_node *)elem->parent;
5722 next = elem->next;
5723 }
5724 }
5725 }
5726
5727 /* make the temporary set the current one */
5728 ret_set.ctx_pos = set->ctx_pos;
5729 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005730 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005731 memcpy(set, &ret_set, sizeof *set);
5732
5733 return LY_SUCCESS;
5734}
5735
5736/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005737 * @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 +02005738 *
5739 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005740 * @param[in] mod Matching node module, NULL for any.
5741 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005742 * @param[in] options XPath options.
5743 * @return LY_ERR
5744 */
5745static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005746moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747{
Michal Vasko6346ece2019-09-24 13:12:53 +02005748 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005750 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005751
Michal Vaskod3678892020-05-21 10:06:58 +02005752 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005753 return LY_SUCCESS;
5754 }
5755
5756 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005757 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 return LY_EVALID;
5759 }
5760
Michal Vasko03ff5a72019-09-11 13:49:33 +02005761 orig_used = set->used;
5762 for (i = 0; i < orig_used; ++i) {
5763 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005764 if (set->val.scnodes[i].in_ctx != -2) {
5765 continue;
5766 }
5767
5768 /* remember context node */
5769 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005770 } else {
5771 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005773
5774 /* TREE DFS */
5775 start = set->val.scnodes[i].scnode;
5776 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005777 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5778 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780 }
5781
Michal Vasko6b26e742020-07-17 15:02:10 +02005782 rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005783 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005784 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005785 set->val.scnodes[idx].in_ctx = 1;
5786 if (idx > i) {
5787 /* we will process it later in the set */
5788 goto skip_children;
5789 }
5790 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005791 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005792 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005793 } else if (rc == LY_EINVAL) {
5794 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005795 }
5796
5797next_iter:
5798 /* TREE DFS NEXT ELEM */
5799 /* select element for the next run - children first */
5800 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 if (!next) {
5802skip_children:
5803 /* no children, so try siblings, but only if it's not the start,
5804 * that is considered to be the root and it's siblings are not traversed */
5805 if (elem != start) {
5806 next = elem->next;
5807 } else {
5808 break;
5809 }
5810 }
5811 while (!next) {
5812 /* no siblings, go back through the parents */
5813 if (elem->parent == start) {
5814 /* we are done, no next element to process */
5815 break;
5816 }
5817 /* parent is already processed, go to its sibling */
5818 elem = elem->parent;
5819 next = elem->next;
5820 }
5821 }
5822 }
5823
5824 return LY_SUCCESS;
5825}
5826
5827/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005828 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829 * Indirectly context position aware.
5830 *
5831 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005832 * @param[in] mod Matching metadata module, NULL for any.
5833 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005834 * @return LY_ERR
5835 */
5836static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005837moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838{
5839 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005840 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005841 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005842
Michal Vaskod3678892020-05-21 10:06:58 +02005843 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005844 return LY_SUCCESS;
5845 }
5846
5847 if (set->type != LYXP_SET_NODE_SET) {
5848 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5849 return LY_EVALID;
5850 }
5851
Michal Vasko03ff5a72019-09-11 13:49:33 +02005852 for (i = 0; i < set->used; ) {
5853 replaced = 0;
5854
5855 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5856 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005857 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005858 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859
5860 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005861 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005862 continue;
5863 }
5864
Michal Vaskod3678892020-05-21 10:06:58 +02005865 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 /* match */
5867 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005868 set->val.meta[i].meta = sub;
5869 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005870 /* pos does not change */
5871 replaced = 1;
5872 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005873 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 +02005874 }
5875 ++i;
5876 }
5877 }
5878 }
5879
5880 if (!replaced) {
5881 /* no match */
5882 set_remove_node(set, i);
5883 }
5884 }
5885
5886 return LY_SUCCESS;
5887}
5888
5889/**
5890 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005891 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005892 *
5893 * @param[in,out] set1 Set to use for the result.
5894 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 * @return LY_ERR
5896 */
5897static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005898moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899{
5900 LY_ERR rc;
5901
Michal Vaskod3678892020-05-21 10:06:58 +02005902 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5904 return LY_EVALID;
5905 }
5906
5907 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005908 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005909 return LY_SUCCESS;
5910 }
5911
Michal Vaskod3678892020-05-21 10:06:58 +02005912 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 memcpy(set1, set2, sizeof *set1);
5914 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005915 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916 return LY_SUCCESS;
5917 }
5918
5919 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005920 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921
5922 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005923 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 LY_CHECK_RET(rc);
5925
5926 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005927 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928
5929 return LY_SUCCESS;
5930}
5931
5932/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005933 * @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 +02005934 * Context position aware.
5935 *
5936 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005937 * @param[in] mod Matching metadata module, NULL for any.
5938 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005939 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5940 */
5941static int
Michal Vaskod3678892020-05-21 10:06:58 +02005942moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943{
5944 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005945 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005946 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005947 struct lyxp_set *set_all_desc = NULL;
5948 LY_ERR rc;
5949
Michal Vaskod3678892020-05-21 10:06:58 +02005950 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005951 return LY_SUCCESS;
5952 }
5953
5954 if (set->type != LYXP_SET_NODE_SET) {
5955 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5956 return LY_EVALID;
5957 }
5958
Michal Vasko03ff5a72019-09-11 13:49:33 +02005959 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5960 * but it likely won't be used much, so it's a waste of time */
5961 /* copy the context */
5962 set_all_desc = set_copy(set);
5963 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005964 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 if (rc != LY_SUCCESS) {
5966 lyxp_set_free(set_all_desc);
5967 return rc;
5968 }
5969 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005970 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005971 if (rc != LY_SUCCESS) {
5972 lyxp_set_free(set_all_desc);
5973 return rc;
5974 }
5975 lyxp_set_free(set_all_desc);
5976
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977 for (i = 0; i < set->used; ) {
5978 replaced = 0;
5979
5980 /* only attributes of an elem can be in the result, skip all the rest,
5981 * we have all attributes qualified in lyd tree */
5982 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005983 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005985 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 continue;
5987 }
5988
Michal Vaskod3678892020-05-21 10:06:58 +02005989 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990 /* match */
5991 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005992 set->val.meta[i].meta = sub;
5993 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994 /* pos does not change */
5995 replaced = 1;
5996 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005997 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 +02005998 }
5999 ++i;
6000 }
6001 }
6002 }
6003
6004 if (!replaced) {
6005 /* no match */
6006 set_remove_node(set, i);
6007 }
6008 }
6009
6010 return LY_SUCCESS;
6011}
6012
6013/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006014 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6015 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 *
6017 * @param[in] parent Current parent.
6018 * @param[in] parent_pos Position of @p parent.
6019 * @param[in] parent_type Node type of @p parent.
6020 * @param[in,out] to_set Set to use.
6021 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022 * @param[in] options XPath options.
6023 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6024 */
6025static LY_ERR
6026moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type,
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006027 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006029 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030 LY_ERR rc;
6031
6032 switch (parent_type) {
6033 case LYXP_NODE_ROOT:
6034 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006035 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006036
Michal Vasko61ac2f62020-05-25 12:39:51 +02006037 /* add all top-level nodes as elements */
6038 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006039 break;
6040 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006041 /* add just the text node of this term element node */
6042 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6044 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6045 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006046 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006048
6049 /* add all the children of this node */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02006050 first = lyd_node_children(parent, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051 break;
6052 default:
6053 LOGINT_RET(parent->schema->module->ctx);
6054 }
6055
Michal Vasko61ac2f62020-05-25 12:39:51 +02006056 /* add all top-level nodes as elements */
6057 LY_LIST_FOR(first, iter) {
6058 /* context check */
6059 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6060 continue;
6061 }
6062
6063 /* when check */
6064 if (moveto_when_check(iter)) {
6065 return LY_EINCOMPLETE;
6066 }
6067
6068 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6069 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6070
6071 /* also add all the children of this node, recursively */
6072 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6073 LY_CHECK_RET(rc);
6074 }
6075 }
6076
Michal Vasko03ff5a72019-09-11 13:49:33 +02006077 return LY_SUCCESS;
6078}
6079
6080/**
6081 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6082 * (or LYXP_SET_EMPTY). Context position aware.
6083 *
6084 * @param[in,out] set Set to use.
6085 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6086 * @param[in] options XPath options.
6087 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6088 */
6089static LY_ERR
6090moveto_self(struct lyxp_set *set, int all_desc, int options)
6091{
6092 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093 struct lyxp_set ret_set;
6094 LY_ERR rc;
6095
Michal Vaskod3678892020-05-21 10:06:58 +02006096 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097 return LY_SUCCESS;
6098 }
6099
6100 if (set->type != LYXP_SET_NODE_SET) {
6101 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6102 return LY_EVALID;
6103 }
6104
6105 /* nothing to do */
6106 if (!all_desc) {
6107 return LY_SUCCESS;
6108 }
6109
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110 /* add all the children, they get added recursively */
6111 set_init(&ret_set, set);
6112 for (i = 0; i < set->used; ++i) {
6113 /* copy the current node to tmp */
6114 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6115
6116 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006117 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006118 continue;
6119 }
6120
Michal Vasko03ff5a72019-09-11 13:49:33 +02006121 /* add all the children */
6122 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006123 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006124 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006125 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006126 return rc;
6127 }
6128 }
6129
6130 /* use the temporary set as the current one */
6131 ret_set.ctx_pos = set->ctx_pos;
6132 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006133 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006134 memcpy(set, &ret_set, sizeof *set);
6135
6136 return LY_SUCCESS;
6137}
6138
6139/**
6140 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6141 * (or LYXP_SET_EMPTY).
6142 *
6143 * @param[in,out] set Set to use.
6144 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6145 * @param[in] options XPath options.
6146 * @return LY_ERR
6147 */
6148static LY_ERR
6149moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6150{
Michal Vasko519fd602020-05-26 12:17:39 +02006151 int getnext_opts;
6152 uint32_t i, mod_idx;
6153 const struct lysc_node *iter, *start_parent;
6154 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155
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_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006161 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, 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 Vasko519fd602020-05-26 12:17:39 +02006170 /* getnext opts */
6171 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6172 if (options & LYXP_SCNODE_OUTPUT) {
6173 getnext_opts |= LYS_GETNEXT_OUTPUT;
6174 }
6175
6176 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006177 for (i = 0; i < set->used; ++i) {
6178 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006179 if (set->val.scnodes[i].in_ctx != -2) {
6180 continue;
6181 }
6182
Michal Vasko519fd602020-05-26 12:17:39 +02006183 /* remember context node */
6184 set->val.scnodes[i].in_ctx = -1;
6185 } else {
6186 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006187 }
6188
Michal Vasko519fd602020-05-26 12:17:39 +02006189 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006190
Michal Vasko519fd602020-05-26 12:17:39 +02006191 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6192 /* it can actually be in any module, it's all <running> */
6193 mod_idx = 0;
6194 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6195 iter = NULL;
6196 /* module may not be implemented */
6197 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6198 /* context check */
6199 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6200 continue;
6201 }
6202
6203 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6204 /* throw away the insert index, we want to consider that node again, recursively */
6205 }
6206 }
6207
6208 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6209 iter = NULL;
6210 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006211 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006212 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006213 continue;
6214 }
6215
Michal Vasko519fd602020-05-26 12:17:39 +02006216 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006217 }
6218 }
6219 }
6220
6221 return LY_SUCCESS;
6222}
6223
6224/**
6225 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6226 * (or LYXP_SET_EMPTY). Context position aware.
6227 *
6228 * @param[in] set Set to use.
6229 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6230 * @param[in] options XPath options.
6231 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6232 */
6233static LY_ERR
6234moveto_parent(struct lyxp_set *set, int all_desc, int options)
6235{
6236 LY_ERR rc;
6237 uint32_t i;
6238 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006239 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006240
Michal Vaskod3678892020-05-21 10:06:58 +02006241 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006242 return LY_SUCCESS;
6243 }
6244
6245 if (set->type != LYXP_SET_NODE_SET) {
6246 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6247 return LY_EVALID;
6248 }
6249
6250 if (all_desc) {
6251 /* <path>//.. == <path>//./.. */
6252 rc = moveto_self(set, 1, options);
6253 LY_CHECK_RET(rc);
6254 }
6255
Michal Vasko57eab132019-09-24 11:46:26 +02006256 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006257 node = set->val.nodes[i].node;
6258
6259 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6260 new_node = (struct lyd_node *)node->parent;
6261 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6262 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006263 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6264 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006265 if (!new_node) {
6266 LOGINT_RET(set->ctx);
6267 }
6268 } else {
6269 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006270 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271 continue;
6272 }
6273
Michal Vaskoa1424542019-11-14 16:08:52 +01006274 /* when check */
6275 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006276 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006277 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006278
6279 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006280 if (!new_node) {
6281 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006282
6283 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6284 } else {
6285 new_type = LYXP_NODE_ELEM;
6286 }
6287
Michal Vasko03ff5a72019-09-11 13:49:33 +02006288 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006289 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006290 } else {
6291 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 }
6293 }
6294
Michal Vasko2caefc12019-11-14 16:07:56 +01006295 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006296 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006297
6298 return LY_SUCCESS;
6299}
6300
6301/**
6302 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6303 * (or LYXP_SET_EMPTY).
6304 *
6305 * @param[in] set Set to use.
6306 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6307 * @param[in] options XPath options.
6308 * @return LY_ERR
6309 */
6310static LY_ERR
6311moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6312{
6313 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006314 const struct lysc_node *node, *new_node;
6315 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006316 LY_ERR rc;
6317
Michal Vaskod3678892020-05-21 10:06:58 +02006318 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006319 return LY_SUCCESS;
6320 }
6321
6322 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006323 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006324 return LY_EVALID;
6325 }
6326
6327 if (all_desc) {
6328 /* <path>//.. == <path>//./.. */
6329 rc = moveto_scnode_self(set, 1, options);
6330 LY_CHECK_RET(rc);
6331 }
6332
Michal Vasko03ff5a72019-09-11 13:49:33 +02006333 orig_used = set->used;
6334 for (i = 0; i < orig_used; ++i) {
6335 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006336 if (set->val.scnodes[i].in_ctx != -2) {
6337 continue;
6338 }
6339
6340 /* remember context node */
6341 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006342 } else {
6343 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006344 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345
6346 node = set->val.scnodes[i].scnode;
6347
6348 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006349 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 } else {
6351 /* root does not have a parent */
6352 continue;
6353 }
6354
Michal Vasko03ff5a72019-09-11 13:49:33 +02006355 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006356 if (!new_node) {
6357 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006358
6359 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6360 } else {
6361 new_type = LYXP_NODE_ELEM;
6362 }
6363
Michal Vaskoecd62de2019-11-13 12:35:11 +01006364 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 if ((idx < orig_used) && (idx > i)) {
6366 set->val.scnodes[idx].in_ctx = 2;
6367 temp_ctx = 1;
6368 }
6369 }
6370
6371 if (temp_ctx) {
6372 for (i = 0; i < orig_used; ++i) {
6373 if (set->val.scnodes[i].in_ctx == 2) {
6374 set->val.scnodes[i].in_ctx = 1;
6375 }
6376 }
6377 }
6378
6379 return LY_SUCCESS;
6380}
6381
6382/**
6383 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6384 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6385 *
6386 * @param[in,out] set1 Set to use for the result.
6387 * @param[in] set2 Set acting as the second operand for @p op.
6388 * @param[in] op Comparison operator to process.
6389 * @param[in] options XPath options.
6390 * @return LY_ERR
6391 */
6392static LY_ERR
6393moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6394{
6395 /*
6396 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6397 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6398 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6399 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6400 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6401 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6402 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6403 *
6404 * '=' or '!='
6405 * BOOLEAN + BOOLEAN
6406 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6407 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6408 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6409 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6410 * NUMBER + NUMBER
6411 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6412 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6413 * STRING + STRING
6414 *
6415 * '<=', '<', '>=', '>'
6416 * NUMBER + NUMBER
6417 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6418 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6419 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6420 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6421 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6422 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6423 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6424 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6425 */
6426 struct lyxp_set iter1, iter2;
6427 int result;
6428 int64_t i;
6429 LY_ERR rc;
6430
Michal Vasko004d3152020-06-11 19:59:22 +02006431 memset(&iter1, 0, sizeof iter1);
6432 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433
6434 /* iterative evaluation with node-sets */
6435 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6436 if (set1->type == LYXP_SET_NODE_SET) {
6437 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006438 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006439 switch (set2->type) {
6440 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006441 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006442 break;
6443 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006444 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006445 break;
6446 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006447 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006448 break;
6449 }
6450 LY_CHECK_RET(rc);
6451
Michal Vasko4c7763f2020-07-27 17:40:37 +02006452 /* canonize set2 */
6453 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6454
6455 /* compare recursively */
6456 rc = moveto_op_comp(&iter1, &iter2, op, options);
6457 lyxp_set_free_content(&iter2);
6458 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006459
6460 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006461 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006462 set_fill_boolean(set1, 1);
6463 return LY_SUCCESS;
6464 }
6465 }
6466 } else {
6467 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006468 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006469 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006470 case LYXP_SET_NUMBER:
6471 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6472 break;
6473 case LYXP_SET_BOOLEAN:
6474 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6475 break;
6476 default:
6477 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6478 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006479 }
6480 LY_CHECK_RET(rc);
6481
Michal Vasko4c7763f2020-07-27 17:40:37 +02006482 /* canonize set1 */
6483 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 +02006484
Michal Vasko4c7763f2020-07-27 17:40:37 +02006485 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006486 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006487 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006488 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006489
6490 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006491 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006492 set_fill_boolean(set1, 1);
6493 return LY_SUCCESS;
6494 }
6495 }
6496 }
6497
6498 /* false for all nodes */
6499 set_fill_boolean(set1, 0);
6500 return LY_SUCCESS;
6501 }
6502
6503 /* first convert properly */
6504 if ((op[0] == '=') || (op[0] == '!')) {
6505 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006506 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6507 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006508 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006509 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006510 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006511 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006512 LY_CHECK_RET(rc);
6513 } /* else we have 2 strings */
6514 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006515 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006517 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006518 LY_CHECK_RET(rc);
6519 }
6520
6521 assert(set1->type == set2->type);
6522
6523 /* compute result */
6524 if (op[0] == '=') {
6525 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006526 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006527 } else if (set1->type == LYXP_SET_NUMBER) {
6528 result = (set1->val.num == set2->val.num);
6529 } else {
6530 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006531 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006532 }
6533 } else if (op[0] == '!') {
6534 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006535 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006536 } else if (set1->type == LYXP_SET_NUMBER) {
6537 result = (set1->val.num != set2->val.num);
6538 } else {
6539 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006540 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541 }
6542 } else {
6543 assert(set1->type == LYXP_SET_NUMBER);
6544 if (op[0] == '<') {
6545 if (op[1] == '=') {
6546 result = (set1->val.num <= set2->val.num);
6547 } else {
6548 result = (set1->val.num < set2->val.num);
6549 }
6550 } else {
6551 if (op[1] == '=') {
6552 result = (set1->val.num >= set2->val.num);
6553 } else {
6554 result = (set1->val.num > set2->val.num);
6555 }
6556 }
6557 }
6558
6559 /* assign result */
6560 if (result) {
6561 set_fill_boolean(set1, 1);
6562 } else {
6563 set_fill_boolean(set1, 0);
6564 }
6565
6566 return LY_SUCCESS;
6567}
6568
6569/**
6570 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6571 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6572 *
6573 * @param[in,out] set1 Set to use for the result.
6574 * @param[in] set2 Set acting as the second operand for @p op.
6575 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006576 * @return LY_ERR
6577 */
6578static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006579moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580{
6581 LY_ERR rc;
6582
6583 /* unary '-' */
6584 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006585 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586 LY_CHECK_RET(rc);
6587 set1->val.num *= -1;
6588 lyxp_set_free(set2);
6589 return LY_SUCCESS;
6590 }
6591
6592 assert(set1 && set2);
6593
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006594 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006596 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006597 LY_CHECK_RET(rc);
6598
6599 switch (op[0]) {
6600 /* '+' */
6601 case '+':
6602 set1->val.num += set2->val.num;
6603 break;
6604
6605 /* '-' */
6606 case '-':
6607 set1->val.num -= set2->val.num;
6608 break;
6609
6610 /* '*' */
6611 case '*':
6612 set1->val.num *= set2->val.num;
6613 break;
6614
6615 /* 'div' */
6616 case 'd':
6617 set1->val.num /= set2->val.num;
6618 break;
6619
6620 /* 'mod' */
6621 case 'm':
6622 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6623 break;
6624
6625 default:
6626 LOGINT_RET(set1->ctx);
6627 }
6628
6629 return LY_SUCCESS;
6630}
6631
6632/*
6633 * eval functions
6634 *
6635 * They execute a parsed XPath expression on some data subtree.
6636 */
6637
6638/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006639 * @brief Evaluate Predicate. Logs directly on error.
6640 *
Michal Vaskod3678892020-05-21 10:06:58 +02006641 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006642 *
6643 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006644 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006645 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6646 * @param[in] options XPath options.
6647 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6648 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6649 */
6650static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006651eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options, int parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652{
6653 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006654 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006655 uint32_t orig_pos, orig_size;
6656 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 struct lyxp_set set2;
6658 struct lyd_node *orig_parent;
6659
6660 /* '[' */
6661 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006662 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6663 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006664
6665 if (!set) {
6666only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006667 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006668 LY_CHECK_RET(rc);
6669 } else if (set->type == LYXP_SET_NODE_SET) {
6670 /* 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 +01006671 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006672
6673 /* empty set, nothing to evaluate */
6674 if (!set->used) {
6675 goto only_parse;
6676 }
6677
Michal Vasko004d3152020-06-11 19:59:22 +02006678 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006679 orig_pos = 0;
6680 orig_size = set->used;
6681 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006682 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006683 set_init(&set2, set);
6684 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6685 /* remember the node context position for position() and context size for last(),
6686 * predicates should always be evaluated with respect to the child axis (since we do
6687 * not support explicit axes) so we assign positions based on their parents */
6688 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6689 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6690 orig_pos = 1;
6691 } else {
6692 ++orig_pos;
6693 }
6694
6695 set2.ctx_pos = orig_pos;
6696 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006697 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006698
Michal Vasko004d3152020-06-11 19:59:22 +02006699 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006701 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006702 return rc;
6703 }
6704
6705 /* number is a position */
6706 if (set2.type == LYXP_SET_NUMBER) {
6707 if ((long long)set2.val.num == orig_pos) {
6708 set2.val.num = 1;
6709 } else {
6710 set2.val.num = 0;
6711 }
6712 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006713 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006714
6715 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006716 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006717 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718 }
6719 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006720 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721
6722 } else if (set->type == LYXP_SET_SCNODE_SET) {
6723 for (i = 0; i < set->used; ++i) {
6724 if (set->val.scnodes[i].in_ctx == 1) {
6725 /* there is a currently-valid node */
6726 break;
6727 }
6728 }
6729 /* empty set, nothing to evaluate */
6730 if (i == set->used) {
6731 goto only_parse;
6732 }
6733
Michal Vasko004d3152020-06-11 19:59:22 +02006734 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006735
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736 /* set special in_ctx to all the valid snodes */
6737 pred_in_ctx = set_scnode_new_in_ctx(set);
6738
6739 /* use the valid snodes one-by-one */
6740 for (i = 0; i < set->used; ++i) {
6741 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6742 continue;
6743 }
6744 set->val.scnodes[i].in_ctx = 1;
6745
Michal Vasko004d3152020-06-11 19:59:22 +02006746 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747
Michal Vasko004d3152020-06-11 19:59:22 +02006748 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006749 LY_CHECK_RET(rc);
6750
6751 set->val.scnodes[i].in_ctx = pred_in_ctx;
6752 }
6753
6754 /* restore the state as it was before the predicate */
6755 for (i = 0; i < set->used; ++i) {
6756 if (set->val.scnodes[i].in_ctx == 1) {
6757 set->val.scnodes[i].in_ctx = 0;
6758 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6759 set->val.scnodes[i].in_ctx = 1;
6760 }
6761 }
6762
6763 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006764 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006765 set_fill_set(&set2, set);
6766
Michal Vasko004d3152020-06-11 19:59:22 +02006767 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006768 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006769 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006770 return rc;
6771 }
6772
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006773 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006774 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006775 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776 }
Michal Vaskod3678892020-05-21 10:06:58 +02006777 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006778 }
6779
6780 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006781 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006782 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006783 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6784 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006785
6786 return LY_SUCCESS;
6787}
6788
6789/**
Michal Vaskod3678892020-05-21 10:06:58 +02006790 * @brief Evaluate Literal. Logs directly on error.
6791 *
6792 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006793 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006794 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6795 */
6796static void
Michal Vasko004d3152020-06-11 19:59:22 +02006797eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006798{
6799 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006800 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006801 set_fill_string(set, "", 0);
6802 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006803 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 +02006804 }
6805 }
6806 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006807 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6808 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006809}
6810
6811/**
Michal Vasko004d3152020-06-11 19:59:22 +02006812 * @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 +02006813 *
Michal Vasko004d3152020-06-11 19:59:22 +02006814 * @param[in] exp Full parsed XPath expression.
6815 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6816 * @param[in] scnode Found schema node as context for the predicate.
6817 * @param[in] format Format of any prefixes in key names/values.
6818 * @param[out] predicates Parsed predicates.
6819 * @param[out] pred_type Type of @p predicates.
6820 * @return LY_SUCCESS on success,
6821 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006822 */
6823static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006824eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode,
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006825 LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates,
6826 enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006827{
Michal Vasko004d3152020-06-11 19:59:22 +02006828 LY_ERR ret = LY_SUCCESS;
6829 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006830 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006831 size_t pred_len;
6832 int prev_lo;
6833 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006834
Michal Vasko004d3152020-06-11 19:59:22 +02006835 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006836
Michal Vasko004d3152020-06-11 19:59:22 +02006837 if (scnode->nodetype == LYS_LIST) {
6838 /* get key count */
6839 if (scnode->flags & LYS_KEYLESS) {
6840 return LY_EINVAL;
6841 }
Radek Krejci1e008d22020-08-17 11:37:37 +02006842 for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006843 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006844
Michal Vasko004d3152020-06-11 19:59:22 +02006845 /* learn where the predicates end */
6846 e_idx = *tok_idx;
6847 while (key_count) {
6848 /* '[' */
6849 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6850 return LY_EINVAL;
6851 }
6852 ++e_idx;
6853
6854 /* ']' */
6855 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6856 ++e_idx;
6857 }
6858 ++e_idx;
6859
6860 /* another presumably key predicate parsed */
6861 --key_count;
6862 }
Michal Vasko004d3152020-06-11 19:59:22 +02006863 } else {
6864 /* learn just where this single predicate ends */
6865 e_idx = *tok_idx;
6866
Michal Vaskod3678892020-05-21 10:06:58 +02006867 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006868 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6869 return LY_EINVAL;
6870 }
6871 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006872
6873 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006874 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6875 ++e_idx;
6876 }
6877 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006878 }
6879
Michal Vasko004d3152020-06-11 19:59:22 +02006880 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6881 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6882
6883 /* turn logging off */
6884 prev_lo = ly_log_options(0);
6885
6886 /* parse the predicate(s) */
Michal Vasko6b26e742020-07-17 15:02:10 +02006887 LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, scnode, exp->expr + exp->tok_pos[*tok_idx], pred_len,
Michal Vasko004d3152020-06-11 19:59:22 +02006888 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6889
6890 /* compile */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006891 ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx,
6892 format, scnode->module, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006893 LY_CHECK_GOTO(ret, cleanup);
6894
6895 /* success, the predicate must include all the needed information for hash-based search */
6896 *tok_idx = e_idx;
6897
6898cleanup:
6899 ly_log_options(prev_lo);
6900 lyxp_expr_free(scnode->module->ctx, exp2);
6901 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006902}
6903
6904/**
6905 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6906 *
6907 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6908 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6909 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6910 *
6911 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006912 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006913 * @param[in] attr_axis Whether to search attributes or standard nodes.
6914 * @param[in] all_desc Whether to search all the descendants or children only.
6915 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6916 * @param[in] options XPath options.
6917 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6918 */
6919static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006920eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc, struct lyxp_set *set,
Michal Vaskod3678892020-05-21 10:06:58 +02006921 int options)
6922{
6923 int i;
6924 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006925 const char *ncname, *ncname_dict = NULL;
6926 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006927 const struct lys_module *moveto_mod;
6928 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006929 struct ly_path_predicate *predicates = NULL;
6930 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006931 LY_ERR rc = LY_SUCCESS;
6932
6933 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006934 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6935 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006936
6937 if (!set) {
6938 goto moveto;
6939 }
6940
Michal Vasko004d3152020-06-11 19:59:22 +02006941 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6942 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006943
6944 /* parse (and skip) module name */
6945 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6946 LY_CHECK_GOTO(rc, cleanup);
6947
6948 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6949 /* find the matching schema node in some parent in the context */
6950 for (i = 0; i < (signed)set->used; ++i) {
6951 if (set->val.nodes[i].type == set->root_type) {
6952 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6953 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6954 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6955 /* do not repeat the same search */
6956 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
Radek Krejcib1247842020-07-02 16:22:38 +02006957 } else {
6958 tmp = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006959 }
6960
6961 /* additional context check */
6962 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6963 tmp = NULL;
6964 }
6965
6966 if (tmp) {
6967 if (scnode) {
6968 /* we found a schema node with the same name but at different level, give up, too complicated */
6969 scnode = NULL;
6970 break;
6971 } else {
6972 /* remember the found schema node and continue to make sure it can be used */
6973 scnode = tmp;
6974 }
Michal Vaskod3678892020-05-21 10:06:58 +02006975 }
6976 }
6977
Michal Vasko004d3152020-06-11 19:59:22 +02006978 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
6979 /* try to create the predicates */
6980 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
6981 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02006982 scnode = NULL;
6983 }
6984 }
6985 }
6986
Michal Vasko004d3152020-06-11 19:59:22 +02006987 if (!scnode && moveto_mod) {
6988 /* we are not able to match based on a schema node and not all the modules match,
6989 * use dictionary for efficient comparison */
6990 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02006991 }
6992
6993moveto:
6994 /* move to the attribute(s), data node(s), or schema node(s) */
6995 if (attr_axis) {
6996 if (set && (options & LYXP_SCNODE_ALL)) {
6997 set_scnode_clear_ctx(set);
6998 } else {
6999 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007000 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007001 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007002 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007003 }
7004 LY_CHECK_GOTO(rc, cleanup);
7005 }
7006 } else {
7007 if (set && (options & LYXP_SCNODE_ALL)) {
7008 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007009 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007010 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007011 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007012 }
7013 LY_CHECK_GOTO(rc, cleanup);
7014
7015 for (i = set->used - 1; i > -1; --i) {
7016 if (set->val.scnodes[i].in_ctx > 0) {
7017 break;
7018 }
7019 }
7020 if (i == -1) {
7021 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7022 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko6b26e742020-07-17 15:02:10 +02007023 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007024 free(path);
7025 }
7026 } else {
7027 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007028 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007029 } else {
7030 if (scnode) {
7031 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007032 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007033 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007034 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007035 }
7036 }
7037 LY_CHECK_GOTO(rc, cleanup);
7038 }
7039 }
7040
7041 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007042 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7043 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007044 LY_CHECK_RET(rc);
7045 }
7046
7047cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007048 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007049 lydict_remove(set->ctx, ncname_dict);
7050 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007051 }
Michal Vaskod3678892020-05-21 10:06:58 +02007052 return rc;
7053}
7054
7055/**
7056 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7057 *
7058 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7059 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7060 * [8] NodeType ::= 'text' | 'node'
7061 *
7062 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007063 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007064 * @param[in] attr_axis Whether to search attributes or standard nodes.
7065 * @param[in] all_desc Whether to search all the descendants or children only.
7066 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7067 * @param[in] options XPath options.
7068 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7069 */
7070static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007071eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc,
Michal Vaskod3678892020-05-21 10:06:58 +02007072 struct lyxp_set *set, int options)
7073{
7074 LY_ERR rc;
7075
7076 /* TODO */
7077 (void)attr_axis;
7078 (void)all_desc;
7079
7080 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007081 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007082 if (set->type == LYXP_SET_SCNODE_SET) {
7083 set_scnode_clear_ctx(set);
7084 /* just for the debug message below */
7085 set = NULL;
7086 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007087 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007088 rc = xpath_node(NULL, 0, set, options);
7089 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007090 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007091 rc = xpath_text(NULL, 0, set, options);
7092 }
7093 LY_CHECK_RET(rc);
7094 }
7095 }
7096 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007097 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7098 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007099
7100 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007101 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007102 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007103 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7104 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007105
7106 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007107 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007108 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007109 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7110 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007111
7112 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007113 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7114 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007115 LY_CHECK_RET(rc);
7116 }
7117
7118 return LY_SUCCESS;
7119}
7120
7121/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007122 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7123 *
7124 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7125 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007126 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007127 *
7128 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007129 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007130 * @param[in] all_desc Whether to search all the descendants or children only.
7131 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7132 * @param[in] options XPath options.
7133 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7134 */
7135static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007136eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, int all_desc, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007137{
7138 int attr_axis;
7139 LY_ERR rc;
7140
7141 goto step;
7142 do {
7143 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007144 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007145 all_desc = 0;
7146 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007147 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007148 all_desc = 1;
7149 }
7150 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007151 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7152 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007153
7154step:
Michal Vaskod3678892020-05-21 10:06:58 +02007155 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007156 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007157 attr_axis = 1;
7158
7159 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007160 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7161 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007162 } else {
7163 attr_axis = 0;
7164 }
7165
Michal Vasko03ff5a72019-09-11 13:49:33 +02007166 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007167 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007168 case LYXP_TOKEN_DOT:
7169 /* evaluate '.' */
7170 if (set && (options & LYXP_SCNODE_ALL)) {
7171 rc = moveto_scnode_self(set, all_desc, options);
7172 } else {
7173 rc = moveto_self(set, all_desc, options);
7174 }
7175 LY_CHECK_RET(rc);
7176 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007177 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7178 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007179 break;
7180
7181 case LYXP_TOKEN_DDOT:
7182 /* evaluate '..' */
7183 if (set && (options & LYXP_SCNODE_ALL)) {
7184 rc = moveto_scnode_parent(set, all_desc, options);
7185 } else {
7186 rc = moveto_parent(set, all_desc, options);
7187 }
7188 LY_CHECK_RET(rc);
7189 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007190 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7191 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007192 break;
7193
Michal Vasko03ff5a72019-09-11 13:49:33 +02007194 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007195 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007196 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007197 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007198 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007199
Michal Vaskod3678892020-05-21 10:06:58 +02007200 case LYXP_TOKEN_NODETYPE:
7201 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007202 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007203 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007204 break;
7205
7206 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007207 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007208 }
Michal Vasko004d3152020-06-11 19:59:22 +02007209 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007210
7211 return LY_SUCCESS;
7212}
7213
7214/**
7215 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7216 *
7217 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7218 *
7219 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007220 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007221 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7222 * @param[in] options XPath options.
7223 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7224 */
7225static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007226eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007227{
7228 int all_desc;
7229 LY_ERR rc;
7230
7231 if (set) {
7232 /* no matter what tokens follow, we need to be at the root */
7233 moveto_root(set, options);
7234 }
7235
7236 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007237 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007238 /* evaluate '/' - deferred */
7239 all_desc = 0;
7240 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007241 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7242 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007243
Michal Vasko004d3152020-06-11 19:59:22 +02007244 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007245 return LY_SUCCESS;
7246 }
Michal Vasko004d3152020-06-11 19:59:22 +02007247 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007248 case LYXP_TOKEN_DOT:
7249 case LYXP_TOKEN_DDOT:
7250 case LYXP_TOKEN_AT:
7251 case LYXP_TOKEN_NAMETEST:
7252 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007253 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007254 LY_CHECK_RET(rc);
7255 break;
7256 default:
7257 break;
7258 }
7259
7260 /* '//' RelativeLocationPath */
7261 } else {
7262 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7263 all_desc = 1;
7264 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007265 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7266 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007267
Michal Vasko004d3152020-06-11 19:59:22 +02007268 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007269 LY_CHECK_RET(rc);
7270 }
7271
7272 return LY_SUCCESS;
7273}
7274
7275/**
7276 * @brief Evaluate FunctionCall. Logs directly on error.
7277 *
Michal Vaskod3678892020-05-21 10:06:58 +02007278 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 *
7280 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007281 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007282 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7283 * @param[in] options XPath options.
7284 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7285 */
7286static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007287eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007288{
7289 LY_ERR rc;
7290 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007291 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007292 struct lyxp_set **args = NULL, **args_aux;
7293
7294 if (set) {
7295 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007296 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007297 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007298 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007299 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007300 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007301 xpath_func = &xpath_sum;
7302 }
7303 break;
7304 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007305 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007306 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007307 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007308 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007309 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007310 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007311 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007312 xpath_func = &xpath_true;
7313 }
7314 break;
7315 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007316 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007317 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007318 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007320 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007321 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007322 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007323 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007324 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007325 xpath_func = &xpath_deref;
7326 }
7327 break;
7328 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007329 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007331 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007333 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 xpath_func = &xpath_string;
7335 }
7336 break;
7337 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007338 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007340 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007342 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 xpath_func = &xpath_current;
7344 }
7345 break;
7346 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007347 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007349 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007351 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_re_match;
7353 }
7354 break;
7355 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007356 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007358 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 xpath_func = &xpath_translate;
7360 }
7361 break;
7362 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007363 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007365 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007367 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 xpath_func = &xpath_bit_is_set;
7369 }
7370 break;
7371 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007372 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007373 xpath_func = &xpath_starts_with;
7374 }
7375 break;
7376 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007377 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007378 xpath_func = &xpath_derived_from;
7379 }
7380 break;
7381 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007382 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007384 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 xpath_func = &xpath_string_length;
7386 }
7387 break;
7388 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007389 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007390 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007391 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007392 xpath_func = &xpath_substring_after;
7393 }
7394 break;
7395 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007396 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 xpath_func = &xpath_substring_before;
7398 }
7399 break;
7400 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007401 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 xpath_func = &xpath_derived_from_or_self;
7403 }
7404 break;
7405 }
7406
7407 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007408 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409 return LY_EVALID;
7410 }
7411 }
7412
7413 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007414 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7415 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416
7417 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007418 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007420 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7421 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007422
7423 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007424 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 if (set) {
7426 args = malloc(sizeof *args);
7427 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7428 arg_count = 1;
7429 args[0] = set_copy(set);
7430 if (!args[0]) {
7431 rc = LY_EMEM;
7432 goto cleanup;
7433 }
7434
Michal Vasko004d3152020-06-11 19:59:22 +02007435 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436 LY_CHECK_GOTO(rc, cleanup);
7437 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007438 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439 LY_CHECK_GOTO(rc, cleanup);
7440 }
7441 }
Michal Vasko004d3152020-06-11 19:59:22 +02007442 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007444 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7445 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446
7447 if (set) {
7448 ++arg_count;
7449 args_aux = realloc(args, arg_count * sizeof *args);
7450 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7451 args = args_aux;
7452 args[arg_count - 1] = set_copy(set);
7453 if (!args[arg_count - 1]) {
7454 rc = LY_EMEM;
7455 goto cleanup;
7456 }
7457
Michal Vasko004d3152020-06-11 19:59:22 +02007458 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459 LY_CHECK_GOTO(rc, cleanup);
7460 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007461 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 LY_CHECK_GOTO(rc, cleanup);
7463 }
7464 }
7465
7466 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007467 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007469 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7470 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471
7472 if (set) {
7473 /* evaluate function */
7474 rc = xpath_func(args, arg_count, set, options);
7475
7476 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 /* merge all nodes from arg evaluations */
7478 for (i = 0; i < arg_count; ++i) {
7479 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007480 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 }
7482 }
7483 } else {
7484 rc = LY_SUCCESS;
7485 }
7486
7487cleanup:
7488 for (i = 0; i < arg_count; ++i) {
7489 lyxp_set_free(args[i]);
7490 }
7491 free(args);
7492
7493 return rc;
7494}
7495
7496/**
7497 * @brief Evaluate Number. Logs directly on error.
7498 *
7499 * @param[in] ctx Context for errors.
7500 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007501 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7503 * @return LY_ERR
7504 */
7505static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007506eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507{
7508 long double num;
7509 char *endptr;
7510
7511 if (set) {
7512 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007513 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007515 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko004d3152020-06-11 19:59:22 +02007517 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007519 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7520 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko004d3152020-06-11 19:59:22 +02007522 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 return LY_EVALID;
7524 }
7525
7526 set_fill_number(set, num);
7527 }
7528
7529 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007530 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7531 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532 return LY_SUCCESS;
7533}
7534
7535/**
7536 * @brief Evaluate PathExpr. Logs directly on error.
7537 *
Michal Vaskod3678892020-05-21 10:06:58 +02007538 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7540 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7541 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007542 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 *
7544 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007545 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7547 * @param[in] options XPath options.
7548 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7549 */
7550static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007551eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552{
7553 int all_desc, parent_pos_pred;
7554 LY_ERR rc;
7555
Michal Vasko004d3152020-06-11 19:59:22 +02007556 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007557 case LYXP_TOKEN_PAR1:
7558 /* '(' Expr ')' */
7559
7560 /* '(' */
7561 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007562 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7563 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007564
7565 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007566 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 LY_CHECK_RET(rc);
7568
7569 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007570 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007571 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007572 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7573 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574
7575 parent_pos_pred = 0;
7576 goto predicate;
7577
7578 case LYXP_TOKEN_DOT:
7579 case LYXP_TOKEN_DDOT:
7580 case LYXP_TOKEN_AT:
7581 case LYXP_TOKEN_NAMETEST:
7582 case LYXP_TOKEN_NODETYPE:
7583 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007584 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 LY_CHECK_RET(rc);
7586 break;
7587
7588 case LYXP_TOKEN_FUNCNAME:
7589 /* FunctionCall */
7590 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007591 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007592 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007593 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 }
7595 LY_CHECK_RET(rc);
7596
7597 parent_pos_pred = 1;
7598 goto predicate;
7599
Michal Vasko3e48bf32020-06-01 08:39:07 +02007600 case LYXP_TOKEN_OPER_PATH:
7601 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007603 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 LY_CHECK_RET(rc);
7605 break;
7606
7607 case LYXP_TOKEN_LITERAL:
7608 /* Literal */
7609 if (!set || (options & LYXP_SCNODE_ALL)) {
7610 if (set) {
7611 set_scnode_clear_ctx(set);
7612 }
Michal Vasko004d3152020-06-11 19:59:22 +02007613 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007615 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 }
7617
7618 parent_pos_pred = 1;
7619 goto predicate;
7620
7621 case LYXP_TOKEN_NUMBER:
7622 /* Number */
7623 if (!set || (options & LYXP_SCNODE_ALL)) {
7624 if (set) {
7625 set_scnode_clear_ctx(set);
7626 }
Michal Vasko004d3152020-06-11 19:59:22 +02007627 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007628 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007629 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007630 }
7631 LY_CHECK_RET(rc);
7632
7633 parent_pos_pred = 1;
7634 goto predicate;
7635
7636 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007637 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7638 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007639 return LY_EVALID;
7640 }
7641
7642 return LY_SUCCESS;
7643
7644predicate:
7645 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007646 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7647 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007648 LY_CHECK_RET(rc);
7649 }
7650
7651 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007652 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007653
7654 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007655 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007656 all_desc = 0;
7657 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007658 all_desc = 1;
7659 }
7660
7661 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007662 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7663 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007664
Michal Vasko004d3152020-06-11 19:59:22 +02007665 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666 LY_CHECK_RET(rc);
7667 }
7668
7669 return LY_SUCCESS;
7670}
7671
7672/**
7673 * @brief Evaluate UnionExpr. Logs directly on error.
7674 *
Michal Vaskod3678892020-05-21 10:06:58 +02007675 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007676 *
7677 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007678 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 * @param[in] repeat How many times this expression is repeated.
7680 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7681 * @param[in] options XPath options.
7682 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7683 */
7684static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007685eval_union_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007686{
7687 LY_ERR rc = LY_SUCCESS;
7688 struct lyxp_set orig_set, set2;
7689 uint16_t i;
7690
7691 assert(repeat);
7692
7693 set_init(&orig_set, set);
7694 set_init(&set2, set);
7695
7696 set_fill_set(&orig_set, set);
7697
Michal Vasko004d3152020-06-11 19:59:22 +02007698 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007699 LY_CHECK_GOTO(rc, cleanup);
7700
7701 /* ('|' PathExpr)* */
7702 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007703 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007704 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007705 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7706 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707
7708 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007709 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 LY_CHECK_GOTO(rc, cleanup);
7711 continue;
7712 }
7713
7714 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007715 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007716 LY_CHECK_GOTO(rc, cleanup);
7717
7718 /* eval */
7719 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007720 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007722 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 LY_CHECK_GOTO(rc, cleanup);
7724 }
7725 }
7726
7727cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007728 lyxp_set_free_content(&orig_set);
7729 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 return rc;
7731}
7732
7733/**
7734 * @brief Evaluate UnaryExpr. Logs directly on error.
7735 *
Michal Vaskod3678892020-05-21 10:06:58 +02007736 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007737 *
7738 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007739 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007740 * @param[in] repeat How many times this expression is repeated.
7741 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7742 * @param[in] options XPath options.
7743 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7744 */
7745static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007746eval_unary_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007747{
7748 LY_ERR rc;
7749 uint16_t this_op, i;
7750
7751 assert(repeat);
7752
7753 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007754 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007755 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007756 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 +02007757
7758 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007759 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7760 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007761 }
7762
Michal Vasko004d3152020-06-11 19:59:22 +02007763 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007764 LY_CHECK_RET(rc);
7765
7766 if (set && (repeat % 2)) {
7767 if (options & LYXP_SCNODE_ALL) {
7768 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7769 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007770 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007771 LY_CHECK_RET(rc);
7772 }
7773 }
7774
7775 return LY_SUCCESS;
7776}
7777
7778/**
7779 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7780 *
Michal Vaskod3678892020-05-21 10:06:58 +02007781 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007782 * | MultiplicativeExpr '*' UnaryExpr
7783 * | MultiplicativeExpr 'div' UnaryExpr
7784 * | MultiplicativeExpr 'mod' UnaryExpr
7785 *
7786 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007787 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007788 * @param[in] repeat How many times this expression is repeated.
7789 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7790 * @param[in] options XPath options.
7791 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7792 */
7793static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007794eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795{
7796 LY_ERR rc;
7797 uint16_t this_op;
7798 struct lyxp_set orig_set, set2;
7799 uint16_t i;
7800
7801 assert(repeat);
7802
7803 set_init(&orig_set, set);
7804 set_init(&set2, set);
7805
7806 set_fill_set(&orig_set, set);
7807
Michal Vasko004d3152020-06-11 19:59:22 +02007808 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809 LY_CHECK_GOTO(rc, cleanup);
7810
7811 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7812 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007813 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007814
Michal Vasko004d3152020-06-11 19:59:22 +02007815 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007816 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007817 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7818 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007819
7820 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007821 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 LY_CHECK_GOTO(rc, cleanup);
7823 continue;
7824 }
7825
7826 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007827 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007828 LY_CHECK_GOTO(rc, cleanup);
7829
7830 /* eval */
7831 if (options & LYXP_SCNODE_ALL) {
7832 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007833 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007834 set_scnode_clear_ctx(set);
7835 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007836 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007837 LY_CHECK_GOTO(rc, cleanup);
7838 }
7839 }
7840
7841cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007842 lyxp_set_free_content(&orig_set);
7843 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007844 return rc;
7845}
7846
7847/**
7848 * @brief Evaluate AdditiveExpr. Logs directly on error.
7849 *
Michal Vaskod3678892020-05-21 10:06:58 +02007850 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007851 * | AdditiveExpr '+' MultiplicativeExpr
7852 * | AdditiveExpr '-' MultiplicativeExpr
7853 *
7854 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007855 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007856 * @param[in] repeat How many times this expression is repeated.
7857 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7858 * @param[in] options XPath options.
7859 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7860 */
7861static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007862eval_additive_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863{
7864 LY_ERR rc;
7865 uint16_t this_op;
7866 struct lyxp_set orig_set, set2;
7867 uint16_t i;
7868
7869 assert(repeat);
7870
7871 set_init(&orig_set, set);
7872 set_init(&set2, set);
7873
7874 set_fill_set(&orig_set, set);
7875
Michal Vasko004d3152020-06-11 19:59:22 +02007876 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007877 LY_CHECK_GOTO(rc, cleanup);
7878
7879 /* ('+' / '-' MultiplicativeExpr)* */
7880 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007881 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007882
Michal Vasko004d3152020-06-11 19:59:22 +02007883 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007884 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007885 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7886 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887
7888 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007889 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007890 LY_CHECK_GOTO(rc, cleanup);
7891 continue;
7892 }
7893
7894 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007895 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007896 LY_CHECK_GOTO(rc, cleanup);
7897
7898 /* eval */
7899 if (options & LYXP_SCNODE_ALL) {
7900 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007901 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902 set_scnode_clear_ctx(set);
7903 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007904 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007905 LY_CHECK_GOTO(rc, cleanup);
7906 }
7907 }
7908
7909cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007910 lyxp_set_free_content(&orig_set);
7911 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007912 return rc;
7913}
7914
7915/**
7916 * @brief Evaluate RelationalExpr. Logs directly on error.
7917 *
Michal Vaskod3678892020-05-21 10:06:58 +02007918 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007919 * | RelationalExpr '<' AdditiveExpr
7920 * | RelationalExpr '>' AdditiveExpr
7921 * | RelationalExpr '<=' AdditiveExpr
7922 * | RelationalExpr '>=' AdditiveExpr
7923 *
7924 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007925 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007926 * @param[in] repeat How many times this expression is repeated.
7927 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7928 * @param[in] options XPath options.
7929 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7930 */
7931static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007932eval_relational_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007933{
7934 LY_ERR rc;
7935 uint16_t this_op;
7936 struct lyxp_set orig_set, set2;
7937 uint16_t i;
7938
7939 assert(repeat);
7940
7941 set_init(&orig_set, set);
7942 set_init(&set2, set);
7943
7944 set_fill_set(&orig_set, set);
7945
Michal Vasko004d3152020-06-11 19:59:22 +02007946 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007947 LY_CHECK_GOTO(rc, cleanup);
7948
7949 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7950 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007951 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952
Michal Vasko004d3152020-06-11 19:59:22 +02007953 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007954 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007955 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7956 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007957
7958 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007959 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 LY_CHECK_GOTO(rc, cleanup);
7961 continue;
7962 }
7963
7964 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007965 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 LY_CHECK_GOTO(rc, cleanup);
7967
7968 /* eval */
7969 if (options & LYXP_SCNODE_ALL) {
7970 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007971 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007972 set_scnode_clear_ctx(set);
7973 } else {
7974 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7975 LY_CHECK_GOTO(rc, cleanup);
7976 }
7977 }
7978
7979cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007980 lyxp_set_free_content(&orig_set);
7981 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007982 return rc;
7983}
7984
7985/**
7986 * @brief Evaluate EqualityExpr. Logs directly on error.
7987 *
Michal Vaskod3678892020-05-21 10:06:58 +02007988 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007989 * | EqualityExpr '!=' RelationalExpr
7990 *
7991 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007992 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 * @param[in] repeat How many times this expression is repeated.
7994 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7995 * @param[in] options XPath options.
7996 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7997 */
7998static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007999eval_equality_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008000{
8001 LY_ERR rc;
8002 uint16_t this_op;
8003 struct lyxp_set orig_set, set2;
8004 uint16_t i;
8005
8006 assert(repeat);
8007
8008 set_init(&orig_set, set);
8009 set_init(&set2, set);
8010
8011 set_fill_set(&orig_set, set);
8012
Michal Vasko004d3152020-06-11 19:59:22 +02008013 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008014 LY_CHECK_GOTO(rc, cleanup);
8015
8016 /* ('=' / '!=' RelationalExpr)* */
8017 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008018 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019
Michal Vasko004d3152020-06-11 19:59:22 +02008020 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008022 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8023 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024
8025 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008026 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008027 LY_CHECK_GOTO(rc, cleanup);
8028 continue;
8029 }
8030
8031 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008032 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008033 LY_CHECK_GOTO(rc, cleanup);
8034
8035 /* eval */
8036 if (options & LYXP_SCNODE_ALL) {
8037 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008038 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8039 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008040 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 set_scnode_clear_ctx(set);
8042 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008043 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8044 LY_CHECK_GOTO(rc, cleanup);
8045 }
8046 }
8047
8048cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008049 lyxp_set_free_content(&orig_set);
8050 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008051 return rc;
8052}
8053
8054/**
8055 * @brief Evaluate AndExpr. Logs directly on error.
8056 *
Michal Vaskod3678892020-05-21 10:06:58 +02008057 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008058 *
8059 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008060 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 * @param[in] repeat How many times this expression is repeated.
8062 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8063 * @param[in] options XPath options.
8064 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8065 */
8066static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008067eval_and_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068{
8069 LY_ERR rc;
8070 struct lyxp_set orig_set, set2;
8071 uint16_t i;
8072
8073 assert(repeat);
8074
8075 set_init(&orig_set, set);
8076 set_init(&set2, set);
8077
8078 set_fill_set(&orig_set, set);
8079
Michal Vasko004d3152020-06-11 19:59:22 +02008080 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008081 LY_CHECK_GOTO(rc, cleanup);
8082
8083 /* cast to boolean, we know that will be the final result */
8084 if (set && (options & LYXP_SCNODE_ALL)) {
8085 set_scnode_clear_ctx(set);
8086 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008087 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008088 }
8089
8090 /* ('and' EqualityExpr)* */
8091 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008092 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8093 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8094 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8095 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096
8097 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008098 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8099 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008100 LY_CHECK_GOTO(rc, cleanup);
8101 continue;
8102 }
8103
8104 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008105 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106 LY_CHECK_GOTO(rc, cleanup);
8107
8108 /* eval - just get boolean value actually */
8109 if (set->type == LYXP_SET_SCNODE_SET) {
8110 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008111 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008112 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008113 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114 set_fill_set(set, &set2);
8115 }
8116 }
8117
8118cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008119 lyxp_set_free_content(&orig_set);
8120 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008121 return rc;
8122}
8123
8124/**
8125 * @brief Evaluate OrExpr. Logs directly on error.
8126 *
Michal Vaskod3678892020-05-21 10:06:58 +02008127 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008128 *
8129 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008130 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008131 * @param[in] repeat How many times this expression is repeated.
8132 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8133 * @param[in] options XPath options.
8134 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8135 */
8136static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008137eval_or_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008138{
8139 LY_ERR rc;
8140 struct lyxp_set orig_set, set2;
8141 uint16_t i;
8142
8143 assert(repeat);
8144
8145 set_init(&orig_set, set);
8146 set_init(&set2, set);
8147
8148 set_fill_set(&orig_set, set);
8149
Michal Vasko004d3152020-06-11 19:59:22 +02008150 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008151 LY_CHECK_GOTO(rc, cleanup);
8152
8153 /* cast to boolean, we know that will be the final result */
8154 if (set && (options & LYXP_SCNODE_ALL)) {
8155 set_scnode_clear_ctx(set);
8156 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008157 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158 }
8159
8160 /* ('or' AndExpr)* */
8161 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008162 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8163 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8164 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8165 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008166
8167 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008168 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8169 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008170 LY_CHECK_GOTO(rc, cleanup);
8171 continue;
8172 }
8173
8174 set_fill_set(&set2, &orig_set);
8175 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8176 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008177 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178 LY_CHECK_GOTO(rc, cleanup);
8179
8180 /* eval - just get boolean value actually */
8181 if (set->type == LYXP_SET_SCNODE_SET) {
8182 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008183 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008185 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008186 set_fill_set(set, &set2);
8187 }
8188 }
8189
8190cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008191 lyxp_set_free_content(&orig_set);
8192 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008193 return rc;
8194}
8195
8196/**
Michal Vasko004d3152020-06-11 19:59:22 +02008197 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 *
8199 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008200 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008201 * @param[in] etype Expression type to evaluate.
8202 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8203 * @param[in] options XPath options.
8204 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8205 */
8206static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008207eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008208{
8209 uint16_t i, count;
8210 enum lyxp_expr_type next_etype;
8211 LY_ERR rc;
8212
8213 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008214 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 next_etype = LYXP_EXPR_NONE;
8216 } else {
8217 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008218 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008219
8220 /* select one-priority lower because etype expression called us */
8221 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008222 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008223 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008224 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 } else {
8226 next_etype = LYXP_EXPR_NONE;
8227 }
8228 }
8229
8230 /* decide what expression are we parsing based on the repeat */
8231 switch (next_etype) {
8232 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008233 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008234 break;
8235 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008236 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237 break;
8238 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008239 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008240 break;
8241 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008242 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 break;
8244 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008245 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 break;
8247 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008248 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 break;
8250 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008251 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 break;
8253 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008254 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 break;
8256 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008257 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258 break;
8259 default:
8260 LOGINT_RET(set->ctx);
8261 }
8262
8263 return rc;
8264}
8265
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008266/**
8267 * @brief Get root type.
8268 *
8269 * @param[in] ctx_node Context node.
8270 * @param[in] ctx_scnode Schema context node.
8271 * @param[in] options XPath options.
8272 * @return Root type.
8273 */
8274static enum lyxp_node_type
8275lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8276{
Michal Vasko6b26e742020-07-17 15:02:10 +02008277 const struct lysc_node *op;
8278
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008279 if (options & LYXP_SCNODE_ALL) {
Radek Krejci1e008d22020-08-17 11:37:37 +02008280 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008281
8282 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008283 /* general root that can access everything */
8284 return LYXP_NODE_ROOT;
8285 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8286 /* root context node can access only config data (because we said so, it is unspecified) */
8287 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008288 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008289 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008290 }
8291
Michal Vasko6b26e742020-07-17 15:02:10 +02008292 op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci1e008d22020-08-17 11:37:37 +02008293 for (; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008294
8295 if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008296 /* root context node can access only config data (because we said so, it is unspecified) */
8297 return LYXP_NODE_ROOT_CONFIG;
8298 }
8299
8300 return LYXP_NODE_ROOT;
8301}
8302
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008304lyxp_eval(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node,
Michal Vaskof03ed032020-03-04 13:31:44 +01008305 enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008306{
Michal Vasko004d3152020-06-11 19:59:22 +02008307 uint16_t tok_idx = 0;
8308 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 LY_ERR rc;
8310
Michal Vasko004d3152020-06-11 19:59:22 +02008311 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8312
8313 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8314 /* we always need some context node because it is used for resolving unqualified names */
8315 real_ctx_node = NULL;
8316 } else {
8317 real_ctx_node = ctx_node;
8318 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008319
8320 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008321 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008322 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008323 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008324 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008325 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008326 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008327 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008328 for (set->context_op = ctx_node->schema;
8329 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
Radek Krejci1e008d22020-08-17 11:37:37 +02008330 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008331 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008332 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 set->format = format;
8334
8335 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008336 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008337 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008338 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008339 }
8340
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341 return rc;
8342}
8343
8344#if 0
8345
8346/* full xml printing of set elements, not used currently */
8347
8348void
8349lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8350{
8351 uint32_t i;
8352 char *str_num;
8353 struct lyout out;
8354
8355 memset(&out, 0, sizeof out);
8356
8357 out.type = LYOUT_STREAM;
8358 out.method.f = f;
8359
8360 switch (set->type) {
8361 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008362 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 break;
8364 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008365 ly_print_(&out, "Boolean XPath set:\n");
8366 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 break;
8368 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008369 ly_print_(&out, "String XPath set:\n");
8370 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008371 break;
8372 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008373 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008374
8375 if (isnan(set->value.num)) {
8376 str_num = strdup("NaN");
8377 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8378 str_num = strdup("0");
8379 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8380 str_num = strdup("Infinity");
8381 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8382 str_num = strdup("-Infinity");
8383 } else if ((long long)set->value.num == set->value.num) {
8384 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8385 str_num = NULL;
8386 }
8387 } else {
8388 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8389 str_num = NULL;
8390 }
8391 }
8392 if (!str_num) {
8393 LOGMEM;
8394 return;
8395 }
Michal Vasko5233e962020-08-14 14:26:20 +02008396 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397 free(str_num);
8398 break;
8399 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008400 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401
8402 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008403 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 switch (set->node_type[i]) {
8405 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008406 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008407 break;
8408 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008409 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 break;
8411 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008412 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 break;
8414 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008415 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008416 break;
8417 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008418 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008419 break;
8420 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008421 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008422 break;
8423 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008424 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008426 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 break;
8428 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008429 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 +02008430 break;
8431 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008432 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 +02008433 break;
8434 }
8435 }
8436 break;
8437 }
8438}
8439
8440#endif
8441
8442LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008443lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444{
8445 long double num;
8446 char *str;
8447 LY_ERR rc;
8448
8449 if (!set || (set->type == target)) {
8450 return LY_SUCCESS;
8451 }
8452
8453 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008454 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455
8456 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008457 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008458 return LY_EINVAL;
8459 }
8460
8461 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008462 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008463 switch (set->type) {
8464 case LYXP_SET_NUMBER:
8465 if (isnan(set->val.num)) {
8466 set->val.str = strdup("NaN");
8467 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8468 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8469 set->val.str = strdup("0");
8470 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8471 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8472 set->val.str = strdup("Infinity");
8473 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8474 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8475 set->val.str = strdup("-Infinity");
8476 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8477 } else if ((long long)set->val.num == set->val.num) {
8478 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8479 LOGMEM_RET(set->ctx);
8480 }
8481 set->val.str = str;
8482 } else {
8483 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8484 LOGMEM_RET(set->ctx);
8485 }
8486 set->val.str = str;
8487 }
8488 break;
8489 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008490 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008491 set->val.str = strdup("true");
8492 } else {
8493 set->val.str = strdup("false");
8494 }
8495 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8496 break;
8497 case LYXP_SET_NODE_SET:
8498 assert(set->used);
8499
8500 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008501 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008502
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008503 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008505 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 set->val.str = str;
8507 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 default:
8509 LOGINT_RET(set->ctx);
8510 }
8511 set->type = LYXP_SET_STRING;
8512 }
8513
8514 /* to NUMBER */
8515 if (target == LYXP_SET_NUMBER) {
8516 switch (set->type) {
8517 case LYXP_SET_STRING:
8518 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008519 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008520 set->val.num = num;
8521 break;
8522 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008523 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 set->val.num = 1;
8525 } else {
8526 set->val.num = 0;
8527 }
8528 break;
8529 default:
8530 LOGINT_RET(set->ctx);
8531 }
8532 set->type = LYXP_SET_NUMBER;
8533 }
8534
8535 /* to BOOLEAN */
8536 if (target == LYXP_SET_BOOLEAN) {
8537 switch (set->type) {
8538 case LYXP_SET_NUMBER:
8539 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008540 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008542 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 }
8544 break;
8545 case LYXP_SET_STRING:
8546 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008547 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008548 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008549 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008550 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008551 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 }
8553 break;
8554 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008555 if (set->used) {
8556 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008557 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008558 } else {
8559 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008560 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008561 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 break;
8563 default:
8564 LOGINT_RET(set->ctx);
8565 }
8566 set->type = LYXP_SET_BOOLEAN;
8567 }
8568
Michal Vasko03ff5a72019-09-11 13:49:33 +02008569 return LY_SUCCESS;
8570}
8571
8572LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008573lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod,
8574 const struct lysc_node *ctx_scnode, enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575{
8576 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008577 const struct lysc_node *real_ctx_scnode;
8578 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579
Michal Vasko004d3152020-06-11 19:59:22 +02008580 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008581
8582 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008583 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8584 /* we always need some context node because it is used for resolving unqualified names */
8585 real_ctx_scnode = NULL;
8586 } else {
8587 real_ctx_scnode = ctx_scnode;
8588 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589
8590 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008591 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008592 memset(set, 0, sizeof *set);
8593 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008594 lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008595 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008596 set->ctx = ctx;
8597 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008598 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008599 for (set->context_op = ctx_scnode;
8600 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
Radek Krejci1e008d22020-08-17 11:37:37 +02008601 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008602 set->local_mod = local_mod;
8603 set->format = format;
8604
8605 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008606 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008607}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008608
8609API const char *
8610lyxp_get_expr(const struct lyxp_expr *path)
8611{
8612 if (!path) {
8613 return NULL;
8614 }
8615
8616 return path->expr;
8617}