blob: fbcda203417ce61ddf5dff94036a44de3a4d0604 [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;
168 int dynamic;
169 struct lyxp_set_node *item;
170 struct lyxp_set_scnode *sitem;
171
172 if (ly_log_level < LY_LLDBG) {
173 return;
174 }
175
176 switch (set->type) {
177 case LYXP_SET_NODE_SET:
178 LOGDBG(LY_LDGXPATH, "set NODE SET:");
179 for (i = 0; i < set->used; ++i) {
180 item = &set->val.nodes[i];
181
182 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100183 case LYXP_NODE_NONE:
184 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
185 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200186 case LYXP_NODE_ROOT:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
188 break;
189 case LYXP_NODE_ROOT_CONFIG:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ELEM:
193 if ((item->node->schema->nodetype == LYS_LIST)
194 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
195 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
196 item->node->schema->name,
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200197 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node, 0), &dynamic)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200198 if (dynamic) {
199 free(str);
200 }
201 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
202 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
203 item->node->schema->name,
204 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
205 if (dynamic) {
206 free(str);
207 }
208 } else {
209 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
210 }
211 break;
212 case LYXP_NODE_TEXT:
213 if (item->node->schema->nodetype & LYS_ANYDATA) {
214 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
215 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
216 } else {
217 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
218 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
219 if (dynamic) {
220 free(str);
221 }
222 }
223 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100224 case LYXP_NODE_META:
225 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
226 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200227 break;
228 }
229 }
230 break;
231
232 case LYXP_SET_SCNODE_SET:
233 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
234 for (i = 0; i < set->used; ++i) {
235 sitem = &set->val.scnodes[i];
236
237 switch (sitem->type) {
238 case LYXP_NODE_ROOT:
239 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
240 break;
241 case LYXP_NODE_ROOT_CONFIG:
242 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
243 break;
244 case LYXP_NODE_ELEM:
245 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
246 break;
247 default:
248 LOGINT(NULL);
249 break;
250 }
251 }
252 break;
253
Michal Vasko03ff5a72019-09-11 13:49:33 +0200254 case LYXP_SET_BOOLEAN:
255 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200256 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200257 break;
258
259 case LYXP_SET_STRING:
260 LOGDBG(LY_LDGXPATH, "set STRING");
261 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
262 break;
263
264 case LYXP_SET_NUMBER:
265 LOGDBG(LY_LDGXPATH, "set NUMBER");
266
267 if (isnan(set->val.num)) {
268 str = strdup("NaN");
269 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
270 str = strdup("0");
271 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
272 str = strdup("Infinity");
273 } else if (isinf(set->val.num) && signbit(set->val.num)) {
274 str = strdup("-Infinity");
275 } else if ((long long)set->val.num == set->val.num) {
276 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
277 str = NULL;
278 }
279 } else {
280 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
281 str = NULL;
282 }
283 }
284 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
285
286 LOGDBG(LY_LDGXPATH, "\t%s", str);
287 free(str);
288 }
289}
290
291#endif
292
293/**
294 * @brief Realloc the string \p str.
295 *
296 * @param[in] ctx libyang context for logging.
297 * @param[in] needed How much free space is required.
298 * @param[in,out] str Pointer to the string to use.
299 * @param[in,out] used Used bytes in \p str.
300 * @param[in,out] size Allocated bytes in \p str.
301 * @return LY_ERR
302 */
303static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100304cast_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 +0200305{
306 if (*size - *used < needed) {
307 do {
308 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
309 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
310 return LY_EINVAL;
311 }
312 *size += LYXP_STRING_CAST_SIZE_STEP;
313 } while (*size - *used < needed);
314 *str = ly_realloc(*str, *size * sizeof(char));
315 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
316 }
317
318 return LY_SUCCESS;
319}
320
321/**
322 * @brief Cast nodes recursively to one string @p str.
323 *
324 * @param[in] node Node to cast.
325 * @param[in] fake_cont Whether to put the data into a "fake" container.
326 * @param[in] root_type Type of the XPath root.
327 * @param[in] indent Current indent.
328 * @param[in,out] str Resulting string.
329 * @param[in,out] used Used bytes in @p str.
330 * @param[in,out] size Allocated bytes in @p str.
331 * @return LY_ERR
332 */
333static LY_ERR
334cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
335 uint16_t *used, uint16_t *size)
336{
Radek Krejci7f769d72020-07-11 23:13:56 +0200337 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200338 const char *value_str;
339 int dynamic;
340 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200341 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200342 struct lyd_node_any *any;
343 LY_ERR rc;
344
345 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
346 return LY_SUCCESS;
347 }
348
349 if (fake_cont) {
350 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
351 LY_CHECK_RET(rc);
352 strcpy(*str + (*used - 1), "\n");
353 ++(*used);
354
355 ++indent;
356 }
357
358 switch (node->schema->nodetype) {
359 case LYS_CONTAINER:
360 case LYS_LIST:
361 case LYS_RPC:
362 case LYS_NOTIF:
363 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
364 LY_CHECK_RET(rc);
365 strcpy(*str + (*used - 1), "\n");
366 ++(*used);
367
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200368 for (child = lyd_node_children(node, 0); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200369 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
370 LY_CHECK_RET(rc);
371 }
372
373 break;
374
375 case LYS_LEAF:
376 case LYS_LEAFLIST:
377 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
378
379 /* print indent */
380 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
381 if (rc != LY_SUCCESS) {
382 if (dynamic) {
383 free((char *)value_str);
384 }
385 return rc;
386 }
387 memset(*str + (*used - 1), ' ', indent * 2);
388 *used += indent * 2;
389
390 /* print value */
391 if (*used == 1) {
392 sprintf(*str + (*used - 1), "%s", value_str);
393 *used += strlen(value_str);
394 } else {
395 sprintf(*str + (*used - 1), "%s\n", value_str);
396 *used += strlen(value_str) + 1;
397 }
398 if (dynamic) {
399 free((char *)value_str);
400 }
401
402 break;
403
404 case LYS_ANYXML:
405 case LYS_ANYDATA:
406 any = (struct lyd_node_any *)node;
407 if (!(void *)any->value.tree) {
408 /* no content */
409 buf = strdup("");
410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
411 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200412 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100413
Michal Vasko60ea6352020-06-29 13:39:39 +0200414 if (any->value_type == LYD_ANYDATA_LYB) {
415 /* try to parse it into a data tree */
Radek Krejci7931b192020-06-25 17:05:03 +0200416 if (lyd_parse_data_mem((struct ly_ctx *)LYD_NODE_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 /* successfully parsed */
418 free(any->value.mem);
419 any->value.tree = tree;
420 any->value_type = LYD_ANYDATA_DATATREE;
421 }
Radek Krejci7931b192020-06-25 17:05:03 +0200422 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200423 }
424
Michal Vasko03ff5a72019-09-11 13:49:33 +0200425 switch (any->value_type) {
426 case LYD_ANYDATA_STRING:
427 case LYD_ANYDATA_XML:
428 case LYD_ANYDATA_JSON:
429 buf = strdup(any->value.json);
430 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
431 break;
432 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200433 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200434 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200435 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100436 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200437 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200438 case LYD_ANYDATA_LYB:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200439 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200440 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200441 }
442 }
443
444 line = strtok_r(buf, "\n", &ptr);
445 do {
446 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
447 if (rc != LY_SUCCESS) {
448 free(buf);
449 return rc;
450 }
451 memset(*str + (*used - 1), ' ', indent * 2);
452 *used += indent * 2;
453
454 strcpy(*str + (*used - 1), line);
455 *used += strlen(line);
456
457 strcpy(*str + (*used - 1), "\n");
458 *used += 1;
459 } while ((line = strtok_r(NULL, "\n", &ptr)));
460
461 free(buf);
462 break;
463
464 default:
465 LOGINT_RET(LYD_NODE_CTX(node));
466 }
467
468 if (fake_cont) {
469 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
470 LY_CHECK_RET(rc);
471 strcpy(*str + (*used - 1), "\n");
472 ++(*used);
473
474 --indent;
475 }
476
477 return LY_SUCCESS;
478}
479
480/**
481 * @brief Cast an element into a string.
482 *
483 * @param[in] node Node to cast.
484 * @param[in] fake_cont Whether to put the data into a "fake" container.
485 * @param[in] root_type Type of the XPath root.
486 * @param[out] str Element cast to dynamically-allocated string.
487 * @return LY_ERR
488 */
489static LY_ERR
490cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
491{
492 uint16_t used, size;
493 LY_ERR rc;
494
495 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
496 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
497 (*str)[0] = '\0';
498 used = 1;
499 size = LYXP_STRING_CAST_SIZE_START;
500
501 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
502 if (rc != LY_SUCCESS) {
503 free(*str);
504 return rc;
505 }
506
507 if (size > used) {
508 *str = ly_realloc(*str, used * sizeof(char));
509 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
510 }
511 return LY_SUCCESS;
512}
513
514/**
515 * @brief Cast a LYXP_SET_NODE_SET set into a string.
516 * Context position aware.
517 *
518 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200519 * @param[out] str Cast dynamically-allocated string.
520 * @return LY_ERR
521 */
522static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100523cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200524{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200525 int dynamic;
526
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100528 case LYXP_NODE_NONE:
529 /* invalid */
530 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200531 case LYXP_NODE_ROOT:
532 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100533 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200534 case LYXP_NODE_ELEM:
535 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100536 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100537 case LYXP_NODE_META:
538 *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200539 if (!dynamic) {
540 *str = strdup(*str);
541 if (!*str) {
542 LOGMEM_RET(set->ctx);
543 }
544 }
545 return LY_SUCCESS;
546 }
547
548 LOGINT_RET(set->ctx);
549}
550
551/**
552 * @brief Cast a string into an XPath number.
553 *
554 * @param[in] str String to use.
555 * @return Cast number.
556 */
557static long double
558cast_string_to_number(const char *str)
559{
560 long double num;
561 char *ptr;
562
563 errno = 0;
564 num = strtold(str, &ptr);
565 if (errno || *ptr) {
566 num = NAN;
567 }
568 return num;
569}
570
571/**
572 * @brief Callback for checking value equality.
573 *
574 * @param[in] val1_p First value.
575 * @param[in] val2_p Second value.
576 * @param[in] mod Whether hash table is being modified.
577 * @param[in] cb_data Callback data.
578 * @return 0 if not equal, non-zero if equal.
579 */
580static int
581set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
582{
583 struct lyxp_set_hash_node *val1, *val2;
584
585 val1 = (struct lyxp_set_hash_node *)val1_p;
586 val2 = (struct lyxp_set_hash_node *)val2_p;
587
588 if ((val1->node == val2->node) && (val1->type == val2->type)) {
589 return 1;
590 }
591
592 return 0;
593}
594
595/**
596 * @brief Insert node and its hash into set.
597 *
598 * @param[in] set et to insert to.
599 * @param[in] node Node with hash.
600 * @param[in] type Node type.
601 */
602static void
603set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
604{
605 int r;
606 uint32_t i, hash;
607 struct lyxp_set_hash_node hnode;
608
609 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
610 /* create hash table and add all the nodes */
611 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
612 for (i = 0; i < set->used; ++i) {
613 hnode.node = set->val.nodes[i].node;
614 hnode.type = set->val.nodes[i].type;
615
616 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
617 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
618 hash = dict_hash_multi(hash, NULL, 0);
619
620 r = lyht_insert(set->ht, &hnode, hash, NULL);
621 assert(!r);
622 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200623
Michal Vasko9d6befd2019-12-11 14:56:56 +0100624 if (hnode.node == node) {
625 /* it was just added, do not add it twice */
626 node = NULL;
627 }
628 }
629 }
630
631 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200632 /* add the new node into hash table */
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_insert(set->ht, &hnode, hash, NULL);
641 assert(!r);
642 (void)r;
643 }
644}
645
646/**
647 * @brief Remove node and its hash from set.
648 *
649 * @param[in] set Set to remove from.
650 * @param[in] node Node to remove.
651 * @param[in] type Node type.
652 */
653static void
654set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
655{
656 int r;
657 struct lyxp_set_hash_node hnode;
658 uint32_t hash;
659
660 if (set->ht) {
661 hnode.node = node;
662 hnode.type = type;
663
664 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
665 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
666 hash = dict_hash_multi(hash, NULL, 0);
667
668 r = lyht_remove(set->ht, &hnode, hash);
669 assert(!r);
670 (void)r;
671
672 if (!set->ht->used) {
673 lyht_free(set->ht);
674 set->ht = NULL;
675 }
676 }
677}
678
679/**
680 * @brief Check whether node is in set based on its hash.
681 *
682 * @param[in] set Set to search in.
683 * @param[in] node Node to search for.
684 * @param[in] type Node type.
685 * @param[in] skip_idx Index in @p set to skip.
686 * @return LY_ERR
687 */
688static LY_ERR
689set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
690{
691 struct lyxp_set_hash_node hnode, *match_p;
692 uint32_t hash;
693
694 hnode.node = node;
695 hnode.type = type;
696
697 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
698 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
699 hash = dict_hash_multi(hash, NULL, 0);
700
701 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
702 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
703 /* we found it on the index that should be skipped, find another */
704 hnode = *match_p;
705 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
706 /* none other found */
707 return LY_SUCCESS;
708 }
709 }
710
711 return LY_EEXIST;
712 }
713
714 /* not found */
715 return LY_SUCCESS;
716}
717
Michal Vaskod3678892020-05-21 10:06:58 +0200718void
719lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200720{
721 if (!set) {
722 return;
723 }
724
725 if (set->type == LYXP_SET_NODE_SET) {
726 free(set->val.nodes);
727 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200728 } else if (set->type == LYXP_SET_SCNODE_SET) {
729 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200730 lyht_free(set->ht);
731 } else {
732 if (set->type == LYXP_SET_STRING) {
733 free(set->val.str);
734 }
735 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200736 }
Michal Vaskod3678892020-05-21 10:06:58 +0200737
738 set->val.nodes = NULL;
739 set->used = 0;
740 set->size = 0;
741 set->ht = NULL;
742 set->ctx_pos = 0;
743 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744}
745
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100746/**
747 * @brief Free dynamically-allocated set.
748 *
749 * @param[in] set Set to free.
750 */
751static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200752lyxp_set_free(struct lyxp_set *set)
753{
754 if (!set) {
755 return;
756 }
757
Michal Vaskod3678892020-05-21 10:06:58 +0200758 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200759 free(set);
760}
761
762/**
763 * @brief Initialize set context.
764 *
765 * @param[in] new Set to initialize.
766 * @param[in] set Arbitrary initialized set.
767 */
768static void
769set_init(struct lyxp_set *new, struct lyxp_set *set)
770{
771 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200772 if (set) {
773 new->ctx = set->ctx;
774 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100775 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200776 new->context_op = set->context_op;
Michal Vasko02a77382019-09-12 11:47:35 +0200777 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100778 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200779 new->format = set->format;
780 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200781}
782
783/**
784 * @brief Create a deep copy of a set.
785 *
786 * @param[in] set Set to copy.
787 * @return Copy of @p set.
788 */
789static struct lyxp_set *
790set_copy(struct lyxp_set *set)
791{
792 struct lyxp_set *ret;
793 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100794 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200795
796 if (!set) {
797 return NULL;
798 }
799
800 ret = malloc(sizeof *ret);
801 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
802 set_init(ret, set);
803
804 if (set->type == LYXP_SET_SCNODE_SET) {
805 ret->type = set->type;
806
807 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100808 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
809 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100810 /* coverity seems to think scnodes can be NULL */
811 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200812 lyxp_set_free(ret);
813 return NULL;
814 }
Michal Vaskoba716542019-12-16 10:01:58 +0100815 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200816 }
817 }
818 } else if (set->type == LYXP_SET_NODE_SET) {
819 ret->type = set->type;
820 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
821 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
822 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
823
824 ret->used = ret->size = set->used;
825 ret->ctx_pos = set->ctx_pos;
826 ret->ctx_size = set->ctx_size;
827 ret->ht = lyht_dup(set->ht);
828 } else {
829 memcpy(ret, set, sizeof *ret);
830 if (set->type == LYXP_SET_STRING) {
831 ret->val.str = strdup(set->val.str);
832 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
833 }
834 }
835
836 return ret;
837}
838
839/**
840 * @brief Fill XPath set with a string. Any current data are disposed of.
841 *
842 * @param[in] set Set to fill.
843 * @param[in] string String to fill into \p set.
844 * @param[in] str_len Length of \p string. 0 is a valid value!
845 */
846static void
847set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
848{
Michal Vaskod3678892020-05-21 10:06:58 +0200849 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200850
851 set->type = LYXP_SET_STRING;
852 if ((str_len == 0) && (string[0] != '\0')) {
853 string = "";
854 }
855 set->val.str = strndup(string, str_len);
856}
857
858/**
859 * @brief Fill XPath set with a number. Any current data are disposed of.
860 *
861 * @param[in] set Set to fill.
862 * @param[in] number Number to fill into \p set.
863 */
864static void
865set_fill_number(struct lyxp_set *set, long double number)
866{
Michal Vaskod3678892020-05-21 10:06:58 +0200867 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200868
869 set->type = LYXP_SET_NUMBER;
870 set->val.num = number;
871}
872
873/**
874 * @brief Fill XPath set with a boolean. Any current data are disposed of.
875 *
876 * @param[in] set Set to fill.
877 * @param[in] boolean Boolean to fill into \p set.
878 */
879static void
880set_fill_boolean(struct lyxp_set *set, int boolean)
881{
Michal Vaskod3678892020-05-21 10:06:58 +0200882 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200883
884 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200885 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200886}
887
888/**
889 * @brief Fill XPath set with the value from another set (deep assign).
890 * Any current data are disposed of.
891 *
892 * @param[in] trg Set to fill.
893 * @param[in] src Source set to copy into \p trg.
894 */
895static void
896set_fill_set(struct lyxp_set *trg, struct lyxp_set *src)
897{
898 if (!trg || !src) {
899 return;
900 }
901
902 if (trg->type == LYXP_SET_NODE_SET) {
903 free(trg->val.nodes);
904 } else if (trg->type == LYXP_SET_STRING) {
905 free(trg->val.str);
906 }
907 set_init(trg, src);
908
909 if (src->type == LYXP_SET_SCNODE_SET) {
910 trg->type = LYXP_SET_SCNODE_SET;
911 trg->used = src->used;
912 trg->size = src->used;
913
914 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
915 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
916 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
917 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200918 set_fill_boolean(trg, src->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200919 } else if (src->type == LYXP_SET_NUMBER) {
920 set_fill_number(trg, src->val.num);
921 } else if (src->type == LYXP_SET_STRING) {
922 set_fill_string(trg, src->val.str, strlen(src->val.str));
923 } else {
924 if (trg->type == LYXP_SET_NODE_SET) {
925 free(trg->val.nodes);
926 } else if (trg->type == LYXP_SET_STRING) {
927 free(trg->val.str);
928 }
929
Michal Vaskod3678892020-05-21 10:06:58 +0200930 assert(src->type == LYXP_SET_NODE_SET);
931
932 trg->type = LYXP_SET_NODE_SET;
933 trg->used = src->used;
934 trg->size = src->used;
935 trg->ctx_pos = src->ctx_pos;
936 trg->ctx_size = src->ctx_size;
937
938 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
939 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
940 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
941 if (src->ht) {
942 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200943 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200944 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 }
946 }
947}
948
949/**
950 * @brief Clear context of all schema nodes.
951 *
952 * @param[in] set Set to clear.
953 */
954static void
955set_scnode_clear_ctx(struct lyxp_set *set)
956{
957 uint32_t i;
958
959 for (i = 0; i < set->used; ++i) {
960 if (set->val.scnodes[i].in_ctx == 1) {
961 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100962 } else if (set->val.scnodes[i].in_ctx == -2) {
963 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200964 }
965 }
966}
967
968/**
969 * @brief Remove a node from a set. Removing last node changes
970 * set into LYXP_SET_EMPTY. Context position aware.
971 *
972 * @param[in] set Set to use.
973 * @param[in] idx Index from @p set of the node to be removed.
974 */
975static void
976set_remove_node(struct lyxp_set *set, uint32_t idx)
977{
978 assert(set && (set->type == LYXP_SET_NODE_SET));
979 assert(idx < set->used);
980
981 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
982
983 --set->used;
984 if (set->used) {
985 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
986 (set->used - idx) * sizeof *set->val.nodes);
987 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200988 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200989 }
990}
991
992/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100993 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200994 *
995 * @param[in] set Set to use.
996 * @param[in] idx Index from @p set of the node to be removed.
997 */
998static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100999set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001000{
1001 assert(set && (set->type == LYXP_SET_NODE_SET));
1002 assert(idx < set->used);
1003
Michal Vasko2caefc12019-11-14 16:07:56 +01001004 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1005 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1006 }
1007 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001008}
1009
1010/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001011 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001012 * set into LYXP_SET_EMPTY. Context position aware.
1013 *
1014 * @param[in] set Set to consolidate.
1015 */
1016static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001017set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001018{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02001019 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +02001020 int32_t start;
1021
Michal Vaskod3678892020-05-21 10:06:58 +02001022 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001023
1024 orig_used = set->used;
1025 set->used = 0;
1026 for (i = 0; i < orig_used;) {
1027 start = -1;
1028 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001029 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001030 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001031 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001032 end = i;
1033 ++i;
1034 break;
1035 }
1036
1037 ++i;
1038 if (i == orig_used) {
1039 end = i;
1040 }
1041 } while (i < orig_used);
1042
1043 if (start > -1) {
1044 /* move the whole chunk of valid nodes together */
1045 if (set->used != (unsigned)start) {
1046 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1047 }
1048 set->used += end - start;
1049 }
1050 }
Michal Vasko57eab132019-09-24 11:46:26 +02001051}
1052
1053/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001054 * @brief Check for duplicates in a node set.
1055 *
1056 * @param[in] set Set to check.
1057 * @param[in] node Node to look for in @p set.
1058 * @param[in] node_type Type of @p node.
1059 * @param[in] skip_idx Index from @p set to skip.
1060 * @return LY_ERR
1061 */
1062static LY_ERR
1063set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1064{
1065 uint32_t i;
1066
Michal Vasko2caefc12019-11-14 16:07:56 +01001067 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001068 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1069 }
1070
1071 for (i = 0; i < set->used; ++i) {
1072 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1073 continue;
1074 }
1075
1076 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1077 return LY_EEXIST;
1078 }
1079 }
1080
1081 return LY_SUCCESS;
1082}
1083
Michal Vaskoecd62de2019-11-13 12:35:11 +01001084int
1085lyxp_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 +02001086{
1087 uint32_t i;
1088
1089 for (i = 0; i < set->used; ++i) {
1090 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1091 continue;
1092 }
1093
1094 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1095 return i;
1096 }
1097 }
1098
1099 return -1;
1100}
1101
Michal Vaskoecd62de2019-11-13 12:35:11 +01001102void
1103lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001104{
1105 uint32_t orig_used, i, j;
1106
Michal Vaskod3678892020-05-21 10:06:58 +02001107 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001108
Michal Vaskod3678892020-05-21 10:06:58 +02001109 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110 return;
1111 }
1112
Michal Vaskod3678892020-05-21 10:06:58 +02001113 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114 memcpy(set1, set2, sizeof *set1);
1115 return;
1116 }
1117
1118 if (set1->used + set2->used > set1->size) {
1119 set1->size = set1->used + set2->used;
1120 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1121 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1122 }
1123
1124 orig_used = set1->used;
1125
1126 for (i = 0; i < set2->used; ++i) {
1127 for (j = 0; j < orig_used; ++j) {
1128 /* detect duplicities */
1129 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1130 break;
1131 }
1132 }
1133
1134 if (j == orig_used) {
1135 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1136 ++set1->used;
1137 }
1138 }
1139
Michal Vaskod3678892020-05-21 10:06:58 +02001140 lyxp_set_free_content(set2);
1141 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001142}
1143
1144/**
1145 * @brief Insert a node into a set. Context position aware.
1146 *
1147 * @param[in] set Set to use.
1148 * @param[in] node Node to insert to @p set.
1149 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1150 * @param[in] node_type Node type of @p node.
1151 * @param[in] idx Index in @p set to insert into.
1152 */
1153static void
1154set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1155{
Michal Vaskod3678892020-05-21 10:06:58 +02001156 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001157
Michal Vaskod3678892020-05-21 10:06:58 +02001158 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001159 /* first item */
1160 if (idx) {
1161 /* no real harm done, but it is a bug */
1162 LOGINT(set->ctx);
1163 idx = 0;
1164 }
1165 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1166 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1167 set->type = LYXP_SET_NODE_SET;
1168 set->used = 0;
1169 set->size = LYXP_SET_SIZE_START;
1170 set->ctx_pos = 1;
1171 set->ctx_size = 1;
1172 set->ht = NULL;
1173 } else {
1174 /* not an empty set */
1175 if (set->used == set->size) {
1176
1177 /* set is full */
1178 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1179 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1180 set->size += LYXP_SET_SIZE_STEP;
1181 }
1182
1183 if (idx > set->used) {
1184 LOGINT(set->ctx);
1185 idx = set->used;
1186 }
1187
1188 /* make space for the new node */
1189 if (idx < set->used) {
1190 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1191 }
1192 }
1193
1194 /* finally assign the value */
1195 set->val.nodes[idx].node = (struct lyd_node *)node;
1196 set->val.nodes[idx].type = node_type;
1197 set->val.nodes[idx].pos = pos;
1198 ++set->used;
1199
Michal Vasko2caefc12019-11-14 16:07:56 +01001200 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1201 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1202 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001203}
1204
Michal Vaskoecd62de2019-11-13 12:35:11 +01001205int
1206lyxp_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 +02001207{
1208 int ret;
1209
1210 assert(set->type == LYXP_SET_SCNODE_SET);
1211
Michal Vaskoecd62de2019-11-13 12:35:11 +01001212 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001213 if (ret > -1) {
1214 set->val.scnodes[ret].in_ctx = 1;
1215 } else {
1216 if (set->used == set->size) {
1217 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1218 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1219 set->size += LYXP_SET_SIZE_STEP;
1220 }
1221
1222 ret = set->used;
1223 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1224 set->val.scnodes[ret].type = node_type;
1225 set->val.scnodes[ret].in_ctx = 1;
1226 ++set->used;
1227 }
1228
1229 return ret;
1230}
1231
1232/**
1233 * @brief Replace a node in a set with another. Context position aware.
1234 *
1235 * @param[in] set Set to use.
1236 * @param[in] node Node to insert to @p set.
1237 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1238 * @param[in] node_type Node type of @p node.
1239 * @param[in] idx Index in @p set of the node to replace.
1240 */
1241static void
1242set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1243{
1244 assert(set && (idx < set->used));
1245
Michal Vasko2caefc12019-11-14 16:07:56 +01001246 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1247 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1248 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001249 set->val.nodes[idx].node = (struct lyd_node *)node;
1250 set->val.nodes[idx].type = node_type;
1251 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001252 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1253 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1254 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001255}
1256
1257/**
1258 * @brief Set all nodes with ctx 1 to a new unique context value.
1259 *
1260 * @param[in] set Set to modify.
1261 * @return New context value.
1262 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001263static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001264set_scnode_new_in_ctx(struct lyxp_set *set)
1265{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001266 uint32_t i;
1267 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001268
1269 assert(set->type == LYXP_SET_SCNODE_SET);
1270
1271 ret_ctx = 3;
1272retry:
1273 for (i = 0; i < set->used; ++i) {
1274 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1275 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1276 goto retry;
1277 }
1278 }
1279 for (i = 0; i < set->used; ++i) {
1280 if (set->val.scnodes[i].in_ctx == 1) {
1281 set->val.scnodes[i].in_ctx = ret_ctx;
1282 }
1283 }
1284
1285 return ret_ctx;
1286}
1287
1288/**
1289 * @brief Get unique @p node position in the data.
1290 *
1291 * @param[in] node Node to find.
1292 * @param[in] node_type Node type of @p node.
1293 * @param[in] root Root node.
1294 * @param[in] root_type Type of the XPath @p root node.
1295 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1296 * be used to increase efficiency and start the DFS from this node.
1297 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1298 * @return Node position.
1299 */
1300static uint32_t
1301get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1302 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1303{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02001304 const struct lyd_node *next, *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001305 uint32_t pos = 1;
1306
1307 assert(prev && prev_pos && !root->prev->next);
1308
1309 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1310 return 0;
1311 }
1312
1313 if (*prev) {
1314 /* start from the previous element instead from the root */
1315 elem = next = *prev;
1316 pos = *prev_pos;
1317 for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent);
1318 goto dfs_search;
1319 }
1320
1321 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
1322 /* TREE DFS */
1323 LYD_TREE_DFS_BEGIN(top_sibling, next, elem) {
1324dfs_search:
1325 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
1326 goto skip_children;
1327 }
1328
1329 if (elem == node) {
1330 break;
1331 }
1332 ++pos;
1333
1334 /* TREE DFS END */
1335 /* select element for the next run - children first,
1336 * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */
1337 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1338 next = NULL;
1339 } else {
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001340 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001341 }
1342 if (!next) {
1343skip_children:
1344 /* no children */
1345 if (elem == top_sibling) {
1346 /* we are done, root has no children */
1347 elem = NULL;
1348 break;
1349 }
1350 /* try siblings */
1351 next = elem->next;
1352 }
1353 while (!next) {
1354 /* no siblings, go back through parents */
1355 if (elem->parent == top_sibling->parent) {
1356 /* we are done, no next element to process */
1357 elem = NULL;
1358 break;
1359 }
1360 /* parent is already processed, go to its sibling */
1361 elem = (struct lyd_node *)elem->parent;
1362 next = elem->next;
1363 }
1364 }
1365
1366 /* node found */
1367 if (elem) {
1368 break;
1369 }
1370 }
1371
1372 if (!elem) {
1373 if (!(*prev)) {
1374 /* we went from root and failed to find it, cannot be */
1375 LOGINT(node->schema->module->ctx);
1376 return 0;
1377 } else {
1378 *prev = NULL;
1379 *prev_pos = 0;
1380
1381 elem = next = top_sibling = root;
1382 pos = 1;
1383 goto dfs_search;
1384 }
1385 }
1386
1387 /* remember the last found node for next time */
1388 *prev = node;
1389 *prev_pos = pos;
1390
1391 return pos;
1392}
1393
1394/**
1395 * @brief Assign (fill) missing node positions.
1396 *
1397 * @param[in] set Set to fill positions in.
1398 * @param[in] root Context root node.
1399 * @param[in] root_type Context root type.
1400 * @return LY_ERR
1401 */
1402static LY_ERR
1403set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1404{
1405 const struct lyd_node *prev = NULL, *tmp_node;
1406 uint32_t i, tmp_pos = 0;
1407
1408 for (i = 0; i < set->used; ++i) {
1409 if (!set->val.nodes[i].pos) {
1410 tmp_node = NULL;
1411 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001412 case LYXP_NODE_META:
1413 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001414 if (!tmp_node) {
1415 LOGINT_RET(root->schema->module->ctx);
1416 }
1417 /* fallthrough */
1418 case LYXP_NODE_ELEM:
1419 case LYXP_NODE_TEXT:
1420 if (!tmp_node) {
1421 tmp_node = set->val.nodes[i].node;
1422 }
1423 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1424 break;
1425 default:
1426 /* all roots have position 0 */
1427 break;
1428 }
1429 }
1430 }
1431
1432 return LY_SUCCESS;
1433}
1434
1435/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001436 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001438 * @param[in] meta Metadata to use.
1439 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001440 */
1441static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001442get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443{
1444 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001445 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001446
Michal Vasko9f96a052020-03-10 09:41:45 +01001447 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001448 ++pos;
1449 }
1450
Michal Vasko9f96a052020-03-10 09:41:45 +01001451 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001452 return pos;
1453}
1454
1455/**
1456 * @brief Compare 2 nodes in respect to XPath document order.
1457 *
1458 * @param[in] item1 1st node.
1459 * @param[in] item2 2nd node.
1460 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1461 */
1462static int
1463set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1464{
Michal Vasko9f96a052020-03-10 09:41:45 +01001465 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001466
1467 if (item1->pos < item2->pos) {
1468 return -1;
1469 }
1470
1471 if (item1->pos > item2->pos) {
1472 return 1;
1473 }
1474
1475 /* node positions are equal, the fun case */
1476
1477 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1478 /* special case since text nodes are actually saved as their parents */
1479 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1480 if (item1->type == LYXP_NODE_ELEM) {
1481 assert(item2->type == LYXP_NODE_TEXT);
1482 return -1;
1483 } else {
1484 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1485 return 1;
1486 }
1487 }
1488
Michal Vasko9f96a052020-03-10 09:41:45 +01001489 /* we need meta positions now */
1490 if (item1->type == LYXP_NODE_META) {
1491 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001492 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001493 if (item2->type == LYXP_NODE_META) {
1494 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001495 }
1496
Michal Vasko9f96a052020-03-10 09:41:45 +01001497 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001498 /* check for duplicates */
1499 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001500 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001501 return 0;
1502 }
1503
Michal Vasko9f96a052020-03-10 09:41:45 +01001504 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001505 /* elem is always first, 2nd node is after it */
1506 if (item1->type == LYXP_NODE_ELEM) {
1507 assert(item2->type != LYXP_NODE_ELEM);
1508 return -1;
1509 }
1510
Michal Vasko9f96a052020-03-10 09:41:45 +01001511 /* 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 +02001512 /* 2nd is before 1st */
1513 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001514 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1515 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1516 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1517 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518 return 1;
1519 }
1520
Michal Vasko9f96a052020-03-10 09:41:45 +01001521 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001522 /* 2nd is after 1st */
1523 return -1;
1524}
1525
1526/**
1527 * @brief Set cast for comparisons.
1528 *
1529 * @param[in] trg Target set to cast source into.
1530 * @param[in] src Source set.
1531 * @param[in] type Target set type.
1532 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001533 * @return LY_ERR
1534 */
1535static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001536set_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 +02001537{
1538 assert(src->type == LYXP_SET_NODE_SET);
1539
1540 set_init(trg, src);
1541
1542 /* insert node into target set */
1543 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1544
1545 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001546 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001547}
1548
1549#ifndef NDEBUG
1550
1551/**
1552 * @brief Bubble sort @p set into XPath document order.
1553 * Context position aware. Unused in the 'Release' build target.
1554 *
1555 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001556 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1557 */
1558static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001559set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001560{
1561 uint32_t i, j;
1562 int ret = 0, cmp, inverted, change;
1563 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001564 struct lyxp_set_node item;
1565 struct lyxp_set_hash_node hnode;
1566 uint64_t hash;
1567
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001568 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001569 return 0;
1570 }
1571
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001572 /* find first top-level node to be used as anchor for positions */
1573 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1574 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001575
1576 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001577 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001578 return -1;
1579 }
1580
1581 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1582 print_set_debug(set);
1583
1584 for (i = 0; i < set->used; ++i) {
1585 inverted = 0;
1586 change = 0;
1587
1588 for (j = 1; j < set->used - i; ++j) {
1589 /* compare node positions */
1590 if (inverted) {
1591 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1592 } else {
1593 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1594 }
1595
1596 /* swap if needed */
1597 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1598 change = 1;
1599
1600 item = set->val.nodes[j - 1];
1601 set->val.nodes[j - 1] = set->val.nodes[j];
1602 set->val.nodes[j] = item;
1603 } else {
1604 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1605 inverted = !inverted;
1606 }
1607 }
1608
1609 ++ret;
1610
1611 if (!change) {
1612 break;
1613 }
1614 }
1615
1616 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1617 print_set_debug(set);
1618
1619 /* check node hashes */
1620 if (set->used >= LYD_HT_MIN_ITEMS) {
1621 assert(set->ht);
1622 for (i = 0; i < set->used; ++i) {
1623 hnode.node = set->val.nodes[i].node;
1624 hnode.type = set->val.nodes[i].type;
1625
1626 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1627 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1628 hash = dict_hash_multi(hash, NULL, 0);
1629
1630 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1631 }
1632 }
1633
1634 return ret - 1;
1635}
1636
1637/**
1638 * @brief Remove duplicate entries in a sorted node set.
1639 *
1640 * @param[in] set Sorted set to check.
1641 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1642 */
1643static LY_ERR
1644set_sorted_dup_node_clean(struct lyxp_set *set)
1645{
1646 uint32_t i = 0;
1647 LY_ERR ret = LY_SUCCESS;
1648
1649 if (set->used > 1) {
1650 while (i < set->used - 1) {
1651 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1652 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001653 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001654 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001655 }
Michal Vasko57eab132019-09-24 11:46:26 +02001656 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001657 }
1658 }
1659
Michal Vasko2caefc12019-11-14 16:07:56 +01001660 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001661 return ret;
1662}
1663
1664#endif
1665
1666/**
1667 * @brief Merge 2 sorted sets into one.
1668 *
1669 * @param[in,out] trg Set to merge into. Duplicates are removed.
1670 * @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 +02001671 * @return LY_ERR
1672 */
1673static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001674set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001675{
1676 uint32_t i, j, k, count, dup_count;
1677 int cmp;
1678 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001679
Michal Vaskod3678892020-05-21 10:06:58 +02001680 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001681 return LY_EINVAL;
1682 }
1683
Michal Vaskod3678892020-05-21 10:06:58 +02001684 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001685 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001686 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001687 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001688 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001689 return LY_SUCCESS;
1690 }
1691
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001692 /* find first top-level node to be used as anchor for positions */
1693 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1694 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001695
1696 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001697 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001698 return LY_EINT;
1699 }
1700
1701#ifndef NDEBUG
1702 LOGDBG(LY_LDGXPATH, "MERGE target");
1703 print_set_debug(trg);
1704 LOGDBG(LY_LDGXPATH, "MERGE source");
1705 print_set_debug(src);
1706#endif
1707
1708 /* make memory for the merge (duplicates are not detected yet, so space
1709 * will likely be wasted on them, too bad) */
1710 if (trg->size - trg->used < src->used) {
1711 trg->size = trg->used + src->used;
1712
1713 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1714 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1715 }
1716
1717 i = 0;
1718 j = 0;
1719 count = 0;
1720 dup_count = 0;
1721 do {
1722 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1723 if (!cmp) {
1724 if (!count) {
1725 /* duplicate, just skip it */
1726 ++i;
1727 ++j;
1728 } else {
1729 /* we are copying something already, so let's copy the duplicate too,
1730 * we are hoping that afterwards there are some more nodes to
1731 * copy and this way we can copy them all together */
1732 ++count;
1733 ++dup_count;
1734 ++i;
1735 ++j;
1736 }
1737 } else if (cmp < 0) {
1738 /* inserting src node into trg, just remember it for now */
1739 ++count;
1740 ++i;
1741
1742 /* insert the hash now */
1743 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1744 } else if (count) {
1745copy_nodes:
1746 /* time to actually copy the nodes, we have found the largest block of nodes */
1747 memmove(&trg->val.nodes[j + (count - dup_count)],
1748 &trg->val.nodes[j],
1749 (trg->used - j) * sizeof *trg->val.nodes);
1750 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1751
1752 trg->used += count - dup_count;
1753 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1754 j += count - dup_count;
1755
1756 count = 0;
1757 dup_count = 0;
1758 } else {
1759 ++j;
1760 }
1761 } while ((i < src->used) && (j < trg->used));
1762
1763 if ((i < src->used) || count) {
1764 /* insert all the hashes first */
1765 for (k = i; k < src->used; ++k) {
1766 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1767 }
1768
1769 /* loop ended, but we need to copy something at trg end */
1770 count += src->used - i;
1771 i = src->used;
1772 goto copy_nodes;
1773 }
1774
1775 /* we are inserting hashes before the actual node insert, which causes
1776 * situations when there were initially not enough items for a hash table,
1777 * but even after some were inserted, hash table was not created (during
1778 * insertion the number of items is not updated yet) */
1779 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1780 set_insert_node_hash(trg, NULL, 0);
1781 }
1782
1783#ifndef NDEBUG
1784 LOGDBG(LY_LDGXPATH, "MERGE result");
1785 print_set_debug(trg);
1786#endif
1787
Michal Vaskod3678892020-05-21 10:06:58 +02001788 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001789 return LY_SUCCESS;
1790}
1791
1792/*
1793 * (re)parse functions
1794 *
1795 * Parse functions parse the expression into
1796 * tokens (syntactic analysis).
1797 *
1798 * Reparse functions perform semantic analysis
1799 * (do not save the result, just a check) of
1800 * the expression and fill repeat indices.
1801 */
1802
Michal Vasko14676352020-05-29 11:35:55 +02001803LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001804lyxp_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 +02001805{
Michal Vasko004d3152020-06-11 19:59:22 +02001806 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001807 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001808 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1809 }
Michal Vasko14676352020-05-29 11:35:55 +02001810 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001811 }
1812
Michal Vasko004d3152020-06-11 19:59:22 +02001813 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001814 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001815 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001816 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001817 }
Michal Vasko14676352020-05-29 11:35:55 +02001818 return LY_ENOT;
1819 }
1820
1821 return LY_SUCCESS;
1822}
1823
Michal Vasko004d3152020-06-11 19:59:22 +02001824LY_ERR
1825lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1826{
1827 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1828
1829 /* skip the token */
1830 ++(*tok_idx);
1831
1832 return LY_SUCCESS;
1833}
1834
Michal Vasko14676352020-05-29 11:35:55 +02001835/* just like lyxp_check_token() but tests for 2 tokens */
1836static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001837exp_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 +02001838 enum lyxp_token want_tok2)
1839{
Michal Vasko004d3152020-06-11 19:59:22 +02001840 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001841 if (ctx) {
1842 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1843 }
1844 return LY_EINCOMPLETE;
1845 }
1846
Michal Vasko004d3152020-06-11 19:59:22 +02001847 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001848 if (ctx) {
1849 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001850 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001851 }
1852 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001853 }
1854
1855 return LY_SUCCESS;
1856}
1857
1858/**
1859 * @brief Stack operation push on the repeat array.
1860 *
1861 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001862 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001863 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1864 */
1865static void
Michal Vasko004d3152020-06-11 19:59:22 +02001866exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001867{
1868 uint16_t i;
1869
Michal Vasko004d3152020-06-11 19:59:22 +02001870 if (exp->repeat[tok_idx]) {
1871 for (i = 0; exp->repeat[tok_idx][i]; ++i);
1872 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1873 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1874 exp->repeat[tok_idx][i] = repeat_op_idx;
1875 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001877 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1878 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1879 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001880 }
1881}
1882
1883/**
1884 * @brief Reparse Predicate. Logs directly on error.
1885 *
1886 * [7] Predicate ::= '[' Expr ']'
1887 *
1888 * @param[in] ctx Context for logging.
1889 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001890 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001891 * @return LY_ERR
1892 */
1893static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001894reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001895{
1896 LY_ERR rc;
1897
Michal Vasko004d3152020-06-11 19:59:22 +02001898 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001899 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001900 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001901
Michal Vasko004d3152020-06-11 19:59:22 +02001902 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001903 LY_CHECK_RET(rc);
1904
Michal Vasko004d3152020-06-11 19:59:22 +02001905 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001906 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001907 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001908
1909 return LY_SUCCESS;
1910}
1911
1912/**
1913 * @brief Reparse RelativeLocationPath. Logs directly on error.
1914 *
1915 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1916 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1917 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1918 *
1919 * @param[in] ctx Context for logging.
1920 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001921 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1923 */
1924static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001925reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001926{
1927 LY_ERR rc;
1928
Michal Vasko004d3152020-06-11 19:59:22 +02001929 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 LY_CHECK_RET(rc);
1931
1932 goto step;
1933 do {
1934 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001935 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936
Michal Vasko004d3152020-06-11 19:59:22 +02001937 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001938 LY_CHECK_RET(rc);
1939step:
1940 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001941 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001942 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001943 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001944 break;
1945
1946 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001947 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001948 break;
1949
1950 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001951 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001952
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);
Michal Vasko004d3152020-06-11 19:59:22 +02001955 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001956 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001957 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958 return LY_EVALID;
1959 }
1960 /* fall through */
1961 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001962 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001963 goto reparse_predicate;
1964 break;
1965
1966 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02001967 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968
1969 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02001970 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001971 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001972 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001973
1974 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02001975 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001977 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978
1979reparse_predicate:
1980 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02001981 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
1982 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983 LY_CHECK_RET(rc);
1984 }
1985 break;
1986 default:
1987 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001988 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 return LY_EVALID;
1990 }
Michal Vasko004d3152020-06-11 19:59:22 +02001991 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992
1993 return LY_SUCCESS;
1994}
1995
1996/**
1997 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
1998 *
1999 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2000 *
2001 * @param[in] ctx Context for logging.
2002 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002003 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004 * @return LY_ERR
2005 */
2006static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002007reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008{
2009 LY_ERR rc;
2010
Michal Vasko004d3152020-06-11 19:59:22 +02002011 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 +02002012
2013 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002014 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002016 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017
Michal Vasko004d3152020-06-11 19:59:22 +02002018 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019 return LY_SUCCESS;
2020 }
Michal Vasko004d3152020-06-11 19:59:22 +02002021 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 case LYXP_TOKEN_DOT:
2023 case LYXP_TOKEN_DDOT:
2024 case LYXP_TOKEN_AT:
2025 case LYXP_TOKEN_NAMETEST:
2026 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002027 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 LY_CHECK_RET(rc);
2029 /* fall through */
2030 default:
2031 break;
2032 }
2033
2034 /* '//' RelativeLocationPath */
2035 } else {
2036 /* '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002037 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038
Michal Vasko004d3152020-06-11 19:59:22 +02002039 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 LY_CHECK_RET(rc);
2041 }
2042
2043 return LY_SUCCESS;
2044}
2045
2046/**
2047 * @brief Reparse FunctionCall. Logs directly on error.
2048 *
2049 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2050 *
2051 * @param[in] ctx Context for logging.
2052 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002053 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 * @return LY_ERR
2055 */
2056static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002057reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058{
2059 int min_arg_count = -1, max_arg_count, arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002060 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061 LY_ERR rc;
2062
Michal Vasko004d3152020-06-11 19:59:22 +02002063 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002065 func_tok_idx = *tok_idx;
2066 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002068 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069 min_arg_count = 1;
2070 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002071 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072 min_arg_count = 1;
2073 max_arg_count = 1;
2074 }
2075 break;
2076 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002077 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 min_arg_count = 1;
2079 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002080 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 min_arg_count = 0;
2082 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002083 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 min_arg_count = 0;
2085 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002086 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087 min_arg_count = 0;
2088 max_arg_count = 0;
2089 }
2090 break;
2091 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002092 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
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]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 min_arg_count = 0;
2097 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002098 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 min_arg_count = 1;
2100 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002101 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
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]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 min_arg_count = 1;
2106 max_arg_count = 1;
2107 }
2108 break;
2109 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002110 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002112 max_arg_count = INT_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002113 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 min_arg_count = 0;
2115 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002116 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 min_arg_count = 0;
2118 max_arg_count = 1;
2119 }
2120 break;
2121 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002122 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
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]], "ceiling", 7)) {
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]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 0;
2130 max_arg_count = 0;
2131 }
2132 break;
2133 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002134 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 2;
2136 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 0;
2139 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 2;
2142 max_arg_count = 2;
2143 }
2144 break;
2145 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002146 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 2;
2148 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002149 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 3;
2151 max_arg_count = 3;
2152 }
2153 break;
2154 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002155 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156 min_arg_count = 0;
2157 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002158 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 1;
2160 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 2;
2163 max_arg_count = 2;
2164 }
2165 break;
2166 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002167 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 2;
2169 max_arg_count = 2;
2170 }
2171 break;
2172 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002173 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 2;
2175 max_arg_count = 2;
2176 }
2177 break;
2178 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002179 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
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]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 0;
2184 max_arg_count = 1;
2185 }
2186 break;
2187 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002188 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 min_arg_count = 0;
2190 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002191 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
2193 max_arg_count = 2;
2194 }
2195 break;
2196 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002197 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 2;
2199 max_arg_count = 2;
2200 }
2201 break;
2202 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 2;
2205 max_arg_count = 2;
2206 }
2207 break;
2208 }
2209 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002210 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 +02002211 return LY_EINVAL;
2212 }
Michal Vasko004d3152020-06-11 19:59:22 +02002213 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214
2215 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002216 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002218 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219
2220 /* ( Expr ( ',' Expr )* )? */
2221 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002222 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002224 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002226 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 LY_CHECK_RET(rc);
2228 }
Michal Vasko004d3152020-06-11 19:59:22 +02002229 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2230 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231
2232 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002233 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002234 LY_CHECK_RET(rc);
2235 }
2236
2237 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002238 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002240 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241
2242 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002243 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
2244 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 return LY_EVALID;
2246 }
2247
2248 return LY_SUCCESS;
2249}
2250
2251/**
2252 * @brief Reparse PathExpr. Logs directly on error.
2253 *
2254 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2255 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2256 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2257 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2258 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2259 *
2260 * @param[in] ctx Context for logging.
2261 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002262 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 * @return LY_ERR
2264 */
2265static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002266reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267{
2268 LY_ERR rc;
2269
Michal Vasko004d3152020-06-11 19:59:22 +02002270 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002271 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 }
2273
Michal Vasko004d3152020-06-11 19:59:22 +02002274 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 case LYXP_TOKEN_PAR1:
2276 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002277 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278
Michal Vasko004d3152020-06-11 19:59:22 +02002279 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 LY_CHECK_RET(rc);
2281
Michal Vasko004d3152020-06-11 19:59:22 +02002282 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002283 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002284 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002285 goto predicate;
2286 break;
2287 case LYXP_TOKEN_DOT:
2288 case LYXP_TOKEN_DDOT:
2289 case LYXP_TOKEN_AT:
2290 case LYXP_TOKEN_NAMETEST:
2291 case LYXP_TOKEN_NODETYPE:
2292 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002293 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002294 LY_CHECK_RET(rc);
2295 break;
2296 case LYXP_TOKEN_FUNCNAME:
2297 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002298 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002299 LY_CHECK_RET(rc);
2300 goto predicate;
2301 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002302 case LYXP_TOKEN_OPER_PATH:
2303 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002305 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 LY_CHECK_RET(rc);
2307 break;
2308 case LYXP_TOKEN_LITERAL:
2309 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002310 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311 goto predicate;
2312 break;
2313 case LYXP_TOKEN_NUMBER:
2314 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002315 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316 goto predicate;
2317 break;
2318 default:
2319 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002320 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321 return LY_EVALID;
2322 }
2323
2324 return LY_SUCCESS;
2325
2326predicate:
2327 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002328 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2329 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 LY_CHECK_RET(rc);
2331 }
2332
2333 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002334 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335
2336 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002337 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002338
Michal Vasko004d3152020-06-11 19:59:22 +02002339 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002340 LY_CHECK_RET(rc);
2341 }
2342
2343 return LY_SUCCESS;
2344}
2345
2346/**
2347 * @brief Reparse UnaryExpr. Logs directly on error.
2348 *
2349 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2350 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2351 *
2352 * @param[in] ctx Context for logging.
2353 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002354 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 * @return LY_ERR
2356 */
2357static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002358reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359{
2360 uint16_t prev_exp;
2361 LY_ERR rc;
2362
2363 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002364 prev_exp = *tok_idx;
2365 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2366 && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002367 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002368 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369 }
2370
2371 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002372 prev_exp = *tok_idx;
2373 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374 LY_CHECK_RET(rc);
2375
2376 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002377 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002378 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002379 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002380
Michal Vasko004d3152020-06-11 19:59:22 +02002381 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002382 LY_CHECK_RET(rc);
2383 }
2384
2385 return LY_SUCCESS;
2386}
2387
2388/**
2389 * @brief Reparse AdditiveExpr. Logs directly on error.
2390 *
2391 * [15] AdditiveExpr ::= MultiplicativeExpr
2392 * | AdditiveExpr '+' MultiplicativeExpr
2393 * | AdditiveExpr '-' MultiplicativeExpr
2394 * [16] MultiplicativeExpr ::= UnaryExpr
2395 * | MultiplicativeExpr '*' UnaryExpr
2396 * | MultiplicativeExpr 'div' UnaryExpr
2397 * | MultiplicativeExpr 'mod' UnaryExpr
2398 *
2399 * @param[in] ctx Context for logging.
2400 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002401 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 * @return LY_ERR
2403 */
2404static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002405reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406{
2407 uint16_t prev_add_exp, prev_mul_exp;
2408 LY_ERR rc;
2409
Michal Vasko004d3152020-06-11 19:59:22 +02002410 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002411 goto reparse_multiplicative_expr;
2412
2413 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002414 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2415 && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418
2419reparse_multiplicative_expr:
2420 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002421 prev_mul_exp = *tok_idx;
2422 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423 LY_CHECK_RET(rc);
2424
2425 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002426 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2427 && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002429 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430
Michal Vasko004d3152020-06-11 19:59:22 +02002431 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002432 LY_CHECK_RET(rc);
2433 }
2434 }
2435
2436 return LY_SUCCESS;
2437}
2438
2439/**
2440 * @brief Reparse EqualityExpr. Logs directly on error.
2441 *
2442 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2443 * | EqualityExpr '!=' RelationalExpr
2444 * [14] RelationalExpr ::= AdditiveExpr
2445 * | RelationalExpr '<' AdditiveExpr
2446 * | RelationalExpr '>' AdditiveExpr
2447 * | RelationalExpr '<=' AdditiveExpr
2448 * | RelationalExpr '>=' AdditiveExpr
2449 *
2450 * @param[in] ctx Context for logging.
2451 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002452 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 * @return LY_ERR
2454 */
2455static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002456reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002457{
2458 uint16_t prev_eq_exp, prev_rel_exp;
2459 LY_ERR rc;
2460
Michal Vasko004d3152020-06-11 19:59:22 +02002461 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462 goto reparse_additive_expr;
2463
2464 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002465 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002467 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002468
2469reparse_additive_expr:
2470 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002471 prev_rel_exp = *tok_idx;
2472 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002473 LY_CHECK_RET(rc);
2474
2475 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002476 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002477 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002478 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002479
Michal Vasko004d3152020-06-11 19:59:22 +02002480 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481 LY_CHECK_RET(rc);
2482 }
2483 }
2484
2485 return LY_SUCCESS;
2486}
2487
2488/**
2489 * @brief Reparse OrExpr. Logs directly on error.
2490 *
2491 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2492 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2493 *
2494 * @param[in] ctx Context for logging.
2495 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002496 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002497 * @return LY_ERR
2498 */
2499static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002500reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501{
2502 uint16_t prev_or_exp, prev_and_exp;
2503 LY_ERR rc;
2504
Michal Vasko004d3152020-06-11 19:59:22 +02002505 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002506 goto reparse_equality_expr;
2507
2508 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002509 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 +02002510 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002511 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512
2513reparse_equality_expr:
2514 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002515 prev_and_exp = *tok_idx;
2516 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002517 LY_CHECK_RET(rc);
2518
2519 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002520 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 +02002521 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002522 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002523
Michal Vasko004d3152020-06-11 19:59:22 +02002524 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525 LY_CHECK_RET(rc);
2526 }
2527 }
2528
2529 return LY_SUCCESS;
2530}
Radek Krejcib1646a92018-11-02 16:08:26 +01002531
2532/**
2533 * @brief Parse NCName.
2534 *
2535 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002537 */
Radek Krejcid4270262019-01-07 15:07:25 +01002538static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002539parse_ncname(const char *ncname)
2540{
2541 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002542 size_t size;
2543 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002544
2545 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2546 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2547 return len;
2548 }
2549
2550 do {
2551 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002552 if (!*ncname) {
2553 break;
2554 }
Radek Krejcid4270262019-01-07 15:07:25 +01002555 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002556 } while (is_xmlqnamechar(uc) && (uc != ':'));
2557
2558 return len;
2559}
2560
2561/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002562 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002563 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002565 * @param[in] exp Expression to use.
2566 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002567 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002568 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002569 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002570 */
2571static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002572exp_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 +01002573{
2574 uint32_t prev;
2575
2576 if (exp->used == exp->size) {
2577 prev = exp->size;
2578 exp->size += LYXP_EXPR_SIZE_STEP;
2579 if (prev > exp->size) {
2580 LOGINT(ctx);
2581 return LY_EINT;
2582 }
2583
2584 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2585 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2586 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2587 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2588 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2589 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2590 }
2591
2592 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002593 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002594 exp->tok_len[exp->used] = tok_len;
2595 ++exp->used;
2596 return LY_SUCCESS;
2597}
2598
2599void
Michal Vasko14676352020-05-29 11:35:55 +02002600lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002601{
2602 uint16_t i;
2603
2604 if (!expr) {
2605 return;
2606 }
2607
2608 lydict_remove(ctx, expr->expr);
2609 free(expr->tokens);
2610 free(expr->tok_pos);
2611 free(expr->tok_len);
2612 if (expr->repeat) {
2613 for (i = 0; i < expr->used; ++i) {
2614 free(expr->repeat[i]);
2615 }
2616 }
2617 free(expr->repeat);
2618 free(expr);
2619}
2620
2621struct lyxp_expr *
Michal Vasko004d3152020-06-11 19:59:22 +02002622lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse)
Radek Krejcib1646a92018-11-02 16:08:26 +01002623{
2624 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002625 size_t parsed = 0, tok_len;
2626 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002627 enum lyxp_token tok_type;
2628 int prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002629 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002630
Michal Vasko004d3152020-06-11 19:59:22 +02002631 if (!expr[0]) {
2632 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
2633 return NULL;
2634 }
2635
2636 if (!expr_len) {
2637 expr_len = strlen(expr);
2638 }
2639 if (expr_len > UINT16_MAX) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002640 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2641 return NULL;
2642 }
2643
2644 /* init lyxp_expr structure */
2645 ret = calloc(1, sizeof *ret);
2646 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
Michal Vasko004d3152020-06-11 19:59:22 +02002647 ret->expr = lydict_insert(ctx, expr, expr_len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002648 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2649 ret->used = 0;
2650 ret->size = LYXP_EXPR_SIZE_START;
2651 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2652 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2653
2654 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2655 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2656
2657 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2658 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2659
Michal Vasko004d3152020-06-11 19:59:22 +02002660 /* make expr 0-terminated */
2661 expr = ret->expr;
2662
Radek Krejcib1646a92018-11-02 16:08:26 +01002663 while (is_xmlws(expr[parsed])) {
2664 ++parsed;
2665 }
2666
2667 do {
2668 if (expr[parsed] == '(') {
2669
2670 /* '(' */
2671 tok_len = 1;
2672 tok_type = LYXP_TOKEN_PAR1;
2673
2674 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2675 /* it is a NodeType/FunctionName after all */
2676 if (((ret->tok_len[ret->used - 1] == 4)
2677 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2678 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2679 ((ret->tok_len[ret->used - 1] == 7)
2680 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2681 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2682 } else {
2683 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2684 }
2685 prev_function_check = 0;
2686 }
2687
2688 } else if (expr[parsed] == ')') {
2689
2690 /* ')' */
2691 tok_len = 1;
2692 tok_type = LYXP_TOKEN_PAR2;
2693
2694 } else if (expr[parsed] == '[') {
2695
2696 /* '[' */
2697 tok_len = 1;
2698 tok_type = LYXP_TOKEN_BRACK1;
2699
2700 } else if (expr[parsed] == ']') {
2701
2702 /* ']' */
2703 tok_len = 1;
2704 tok_type = LYXP_TOKEN_BRACK2;
2705
2706 } else if (!strncmp(&expr[parsed], "..", 2)) {
2707
2708 /* '..' */
2709 tok_len = 2;
2710 tok_type = LYXP_TOKEN_DDOT;
2711
2712 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2713
2714 /* '.' */
2715 tok_len = 1;
2716 tok_type = LYXP_TOKEN_DOT;
2717
2718 } else if (expr[parsed] == '@') {
2719
2720 /* '@' */
2721 tok_len = 1;
2722 tok_type = LYXP_TOKEN_AT;
2723
2724 } else if (expr[parsed] == ',') {
2725
2726 /* ',' */
2727 tok_len = 1;
2728 tok_type = LYXP_TOKEN_COMMA;
2729
2730 } else if (expr[parsed] == '\'') {
2731
2732 /* Literal with ' */
2733 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len);
2734 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2735 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2736 ++tok_len;
2737 tok_type = LYXP_TOKEN_LITERAL;
2738
2739 } else if (expr[parsed] == '\"') {
2740
2741 /* Literal with " */
2742 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len);
2743 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2744 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2745 ++tok_len;
2746 tok_type = LYXP_TOKEN_LITERAL;
2747
2748 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2749
2750 /* Number */
2751 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len);
2752 if (expr[parsed + tok_len] == '.') {
2753 ++tok_len;
2754 for (; isdigit(expr[parsed + tok_len]); ++tok_len);
2755 }
2756 tok_type = LYXP_TOKEN_NUMBER;
2757
2758 } else if (expr[parsed] == '/') {
2759
2760 /* Operator '/', '//' */
2761 if (!strncmp(&expr[parsed], "//", 2)) {
2762 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002763 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002764 } else {
2765 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002766 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002767 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002768
Michal Vasko3e48bf32020-06-01 08:39:07 +02002769 } else if (!strncmp(&expr[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002770
Michal Vasko3e48bf32020-06-01 08:39:07 +02002771 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002772 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002773 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2774
2775 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2776
2777 /* Operator '<=', '>=' */
2778 tok_len = 2;
2779 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002780
2781 } else if (expr[parsed] == '|') {
2782
2783 /* Operator '|' */
2784 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002785 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002786
2787 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2788
2789 /* Operator '+', '-' */
2790 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002791 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002792
Michal Vasko3e48bf32020-06-01 08:39:07 +02002793 } else if (expr[parsed] == '=') {
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 = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002797 tok_type = LYXP_TOKEN_OPER_EQUAL;
2798
2799 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2800
2801 /* Operator '<', '>' */
2802 tok_len = 1;
2803 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002804
2805 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2806 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2807 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2808 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002809 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2810 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2811 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2812 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2813 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2814 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2815 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2816 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002817
2818 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2819 if (expr[parsed] == '*') {
2820 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002821 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002822
2823 } else if (!strncmp(&expr[parsed], "or", 2)) {
2824 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002825 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002826
2827 } else if (!strncmp(&expr[parsed], "and", 3)) {
2828 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002829 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002830
2831 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2832 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002833 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002834
2835 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002836 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2837 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 +01002838 goto error;
2839 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002840 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002841 goto error;
2842 }
2843 } else if (expr[parsed] == '*') {
2844
2845 /* NameTest '*' */
2846 tok_len = 1;
2847 tok_type = LYXP_TOKEN_NAMETEST;
2848
2849 } else {
2850
2851 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2852 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002853 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 +01002854 tok_len = ncname_len;
2855
2856 if (expr[parsed + tok_len] == ':') {
2857 ++tok_len;
2858 if (expr[parsed + tok_len] == '*') {
2859 ++tok_len;
2860 } else {
2861 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002862 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 +01002863 tok_len += ncname_len;
2864 }
2865 /* remove old flag to prevent ambiguities */
2866 prev_function_check = 0;
2867 tok_type = LYXP_TOKEN_NAMETEST;
2868 } else {
2869 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2870 prev_function_check = 1;
2871 tok_type = LYXP_TOKEN_NAMETEST;
2872 }
2873 }
2874
2875 /* store the token, move on to the next one */
2876 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2877 parsed += tok_len;
2878 while (is_xmlws(expr[parsed])) {
2879 ++parsed;
2880 }
2881
2882 } while (expr[parsed]);
2883
Michal Vasko004d3152020-06-11 19:59:22 +02002884 if (reparse) {
2885 /* prealloc repeat */
2886 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2887 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002888
Michal Vasko004d3152020-06-11 19:59:22 +02002889 /* fill repeat */
2890 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error);
2891 if (ret->used > tok_idx) {
2892 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2893 &ret->expr[ret->tok_pos[tok_idx]]);
2894 goto error;
2895 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002896 }
2897
2898 print_expr_struct_debug(ret);
2899
Radek Krejcib1646a92018-11-02 16:08:26 +01002900 return ret;
2901
2902error:
2903 lyxp_expr_free(ctx, ret);
2904 return NULL;
2905}
2906
Michal Vasko004d3152020-06-11 19:59:22 +02002907struct lyxp_expr *
2908lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp)
2909{
2910 struct lyxp_expr *dup;
2911 uint32_t i, j;
2912
2913 dup = calloc(1, sizeof *dup);
2914 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error);
2915
2916 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2917 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error);
2918 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2919
2920 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2921 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error);
2922 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2923
2924 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
2925 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error);
2926 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2927
2928 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
2929 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error);
2930 for (i = 0; i < exp->used; ++i) {
2931 if (!exp->repeat[i]) {
2932 dup->repeat[i] = NULL;
2933 } else {
2934 for (j = 0; exp->repeat[i][j]; ++j);
2935 /* the ending 0 as well */
2936 ++j;
2937
Michal Vasko99c71642020-07-03 13:33:36 +02002938 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko004d3152020-06-11 19:59:22 +02002939 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error);
2940 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2941 dup->repeat[i][j - 1] = 0;
2942 }
2943 }
2944
2945 dup->used = exp->used;
2946 dup->size = exp->used;
2947 dup->expr = lydict_insert(ctx, exp->expr, 0);
2948
2949 return dup;
2950
2951error:
2952 lyxp_expr_free(ctx, dup);
2953 return NULL;
2954}
2955
Michal Vasko03ff5a72019-09-11 13:49:33 +02002956/*
2957 * warn functions
2958 *
2959 * Warn functions check specific reasonable conditions for schema XPath
2960 * and print a warning if they are not satisfied.
2961 */
2962
2963/**
2964 * @brief Get the last-added schema node that is currently in the context.
2965 *
2966 * @param[in] set Set to search in.
2967 * @return Last-added schema context node, NULL if no node is in context.
2968 */
2969static struct lysc_node *
2970warn_get_scnode_in_ctx(struct lyxp_set *set)
2971{
2972 uint32_t i;
2973
2974 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2975 return NULL;
2976 }
2977
2978 i = set->used;
2979 do {
2980 --i;
2981 if (set->val.scnodes[i].in_ctx == 1) {
2982 /* if there are more, simply return the first found (last added) */
2983 return set->val.scnodes[i].scnode;
2984 }
2985 } while (i);
2986
2987 return NULL;
2988}
2989
2990/**
2991 * @brief Test whether a type is numeric - integer type or decimal64.
2992 *
2993 * @param[in] type Type to test.
2994 * @return 1 if numeric, 0 otherwise.
2995 */
2996static int
2997warn_is_numeric_type(struct lysc_type *type)
2998{
2999 struct lysc_type_union *uni;
3000 int ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003001 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003002
3003 switch (type->basetype) {
3004 case LY_TYPE_DEC64:
3005 case LY_TYPE_INT8:
3006 case LY_TYPE_UINT8:
3007 case LY_TYPE_INT16:
3008 case LY_TYPE_UINT16:
3009 case LY_TYPE_INT32:
3010 case LY_TYPE_UINT32:
3011 case LY_TYPE_INT64:
3012 case LY_TYPE_UINT64:
3013 return 1;
3014 case LY_TYPE_UNION:
3015 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003016 LY_ARRAY_FOR(uni->types, u) {
3017 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003018 if (ret) {
3019 /* found a suitable type */
3020 return 1;
3021 }
3022 }
3023 /* did not find any suitable type */
3024 return 0;
3025 case LY_TYPE_LEAFREF:
3026 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3027 default:
3028 return 0;
3029 }
3030}
3031
3032/**
3033 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3034 *
3035 * @param[in] type Type to test.
3036 * @return 1 if string, 0 otherwise.
3037 */
3038static int
3039warn_is_string_type(struct lysc_type *type)
3040{
3041 struct lysc_type_union *uni;
3042 int ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003043 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003044
3045 switch (type->basetype) {
3046 case LY_TYPE_BITS:
3047 case LY_TYPE_ENUM:
3048 case LY_TYPE_IDENT:
3049 case LY_TYPE_INST:
3050 case LY_TYPE_STRING:
3051 return 1;
3052 case LY_TYPE_UNION:
3053 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003054 LY_ARRAY_FOR(uni->types, u) {
3055 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003056 if (ret) {
3057 /* found a suitable type */
3058 return 1;
3059 }
3060 }
3061 /* did not find any suitable type */
3062 return 0;
3063 case LY_TYPE_LEAFREF:
3064 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3065 default:
3066 return 0;
3067 }
3068}
3069
3070/**
3071 * @brief Test whether a type is one specific type.
3072 *
3073 * @param[in] type Type to test.
3074 * @param[in] base Expected type.
3075 * @return 1 if it is, 0 otherwise.
3076 */
3077static int
3078warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3079{
3080 struct lysc_type_union *uni;
3081 int ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003082 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003083
3084 if (type->basetype == base) {
3085 return 1;
3086 } else if (type->basetype == LY_TYPE_UNION) {
3087 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003088 LY_ARRAY_FOR(uni->types, u) {
3089 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003090 if (ret) {
3091 /* found a suitable type */
3092 return 1;
3093 }
3094 }
3095 /* did not find any suitable type */
3096 return 0;
3097 } else if (type->basetype == LY_TYPE_LEAFREF) {
3098 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3099 }
3100
3101 return 0;
3102}
3103
3104/**
3105 * @brief Get next type of a (union) type.
3106 *
3107 * @param[in] type Base type.
3108 * @param[in] prev_type Previously returned type.
3109 * @return Next type or NULL.
3110 */
3111static struct lysc_type *
3112warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3113{
3114 struct lysc_type_union *uni;
3115 int found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003116 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003117
3118 switch (type->basetype) {
3119 case LY_TYPE_UNION:
3120 uni = (struct lysc_type_union *)type;
3121 if (!prev_type) {
3122 return uni->types[0];
3123 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003124 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003125 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003126 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003127 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003128 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003129 found = 1;
3130 }
3131 }
3132 return NULL;
3133 default:
3134 if (prev_type) {
3135 assert(type == prev_type);
3136 return NULL;
3137 } else {
3138 return type;
3139 }
3140 }
3141}
3142
3143/**
3144 * @brief Test whether 2 types have a common type.
3145 *
3146 * @param[in] type1 First type.
3147 * @param[in] type2 Second type.
3148 * @return 1 if they do, 0 otherwise.
3149 */
3150static int
3151warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3152{
3153 struct lysc_type *t1, *rt1, *t2, *rt2;
3154
3155 t1 = NULL;
3156 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3157 if (t1->basetype == LY_TYPE_LEAFREF) {
3158 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3159 } else {
3160 rt1 = t1;
3161 }
3162
3163 t2 = NULL;
3164 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3165 if (t2->basetype == LY_TYPE_LEAFREF) {
3166 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3167 } else {
3168 rt2 = t2;
3169 }
3170
3171 if (rt2->basetype == rt1->basetype) {
3172 /* match found */
3173 return 1;
3174 }
3175 }
3176 }
3177
3178 return 0;
3179}
3180
3181/**
3182 * @brief Check both operands of comparison operators.
3183 *
3184 * @param[in] ctx Context for errors.
3185 * @param[in] set1 First operand set.
3186 * @param[in] set2 Second operand set.
3187 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3188 * @param[in] expr Start of the expression to print with the warning.
3189 * @param[in] tok_pos Token position.
3190 */
3191static void
3192warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3193{
3194 struct lysc_node_leaf *node1, *node2;
3195 int leaves = 1, warning = 0;
3196
3197 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3198 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3199
3200 if (!node1 && !node2) {
3201 /* no node-sets involved, nothing to do */
3202 return;
3203 }
3204
3205 if (node1) {
3206 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3207 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3208 warning = 1;
3209 leaves = 0;
3210 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3211 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3212 warning = 1;
3213 }
3214 }
3215
3216 if (node2) {
3217 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3218 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3219 warning = 1;
3220 leaves = 0;
3221 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3222 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3223 warning = 1;
3224 }
3225 }
3226
3227 if (node1 && node2 && leaves && !numbers_only) {
3228 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3229 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3230 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3231 && !warn_is_equal_type(node1->type, node2->type))) {
3232 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3233 warning = 1;
3234 }
3235 }
3236
3237 if (warning) {
3238 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3239 }
3240}
3241
3242/**
3243 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3244 *
3245 * @param[in] exp Parsed XPath expression.
3246 * @param[in] set Set with the leaf/leaf-list.
3247 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3248 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3249 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3250 */
3251static void
3252warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3253{
3254 struct lysc_node *scnode;
3255 struct lysc_type *type;
3256 char *value;
3257 LY_ERR rc;
3258 struct ly_err_item *err = NULL;
3259
3260 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3261 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3262 /* check that the node can have the specified value */
3263 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3264 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3265 } else {
3266 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3267 }
3268 if (!value) {
3269 LOGMEM(set->ctx);
3270 return;
3271 }
3272
3273 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3274 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3275 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3276 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3277 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3278 exp->expr + exp->tok_pos[equal_exp]);
3279 }
3280
3281 type = ((struct lysc_node_leaf *)scnode)->type;
3282 if (type->basetype != LY_TYPE_IDENT) {
3283 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA,
3284 lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err);
3285
3286 if (err) {
3287 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3288 ly_err_free(err);
3289 } else if (rc != LY_SUCCESS) {
3290 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3291 }
3292 if (rc != LY_SUCCESS) {
3293 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3294 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3295 exp->expr + exp->tok_pos[equal_exp]);
3296 }
3297 }
3298 free(value);
3299 }
3300}
3301
3302/*
3303 * XPath functions
3304 */
3305
3306/**
3307 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3308 * depending on whether the first node bit value from the second argument is set.
3309 *
3310 * @param[in] args Array of arguments.
3311 * @param[in] arg_count Count of elements in @p args.
3312 * @param[in,out] set Context and result set at the same time.
3313 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003314 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003315 */
3316static LY_ERR
3317xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3318{
3319 struct lyd_node_term *leaf;
3320 struct lysc_node_leaf *sleaf;
3321 struct lysc_type_bits *bits;
3322 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003323 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003324
3325 if (options & LYXP_SCNODE_ALL) {
3326 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3327 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003328 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3329 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 +02003330 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3331 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003332 }
3333
3334 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3335 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3336 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 +02003337 } else if (!warn_is_string_type(sleaf->type)) {
3338 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003339 }
3340 }
3341 set_scnode_clear_ctx(set);
3342 return rc;
3343 }
3344
Michal Vaskod3678892020-05-21 10:06:58 +02003345 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003346 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)");
3347 return LY_EVALID;
3348 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003349 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003350 LY_CHECK_RET(rc);
3351
3352 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003353 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003354 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3355 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3356 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3357 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003358 LY_ARRAY_FOR(bits->bits, u) {
3359 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003360 set_fill_boolean(set, 1);
3361 break;
3362 }
3363 }
3364 }
3365 }
3366
3367 return LY_SUCCESS;
3368}
3369
3370/**
3371 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3372 * with the argument converted to boolean.
3373 *
3374 * @param[in] args Array of arguments.
3375 * @param[in] arg_count Count of elements in @p args.
3376 * @param[in,out] set Context and result set at the same time.
3377 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003378 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003379 */
3380static LY_ERR
3381xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3382{
3383 LY_ERR rc;
3384
3385 if (options & LYXP_SCNODE_ALL) {
3386 set_scnode_clear_ctx(set);
3387 return LY_SUCCESS;
3388 }
3389
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003390 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003391 LY_CHECK_RET(rc);
3392 set_fill_set(set, args[0]);
3393
3394 return LY_SUCCESS;
3395}
3396
3397/**
3398 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3399 * with the first argument rounded up to the nearest integer.
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_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3409{
3410 struct lysc_node_leaf *sleaf;
3411 LY_ERR rc = LY_SUCCESS;
3412
3413 if (options & LYXP_SCNODE_ALL) {
3414 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3415 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003416 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3417 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 +02003418 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3419 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003420 }
3421 set_scnode_clear_ctx(set);
3422 return rc;
3423 }
3424
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003425 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 LY_CHECK_RET(rc);
3427 if ((long long)args[0]->val.num != args[0]->val.num) {
3428 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3429 } else {
3430 set_fill_number(set, args[0]->val.num);
3431 }
3432
3433 return LY_SUCCESS;
3434}
3435
3436/**
3437 * @brief Execute the XPath concat(string, string, string*) function.
3438 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3439 *
3440 * @param[in] args Array of arguments.
3441 * @param[in] arg_count Count of elements in @p args.
3442 * @param[in,out] set Context and result set at the same time.
3443 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003444 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003445 */
3446static LY_ERR
3447xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3448{
3449 uint16_t i;
3450 char *str = NULL;
3451 size_t used = 1;
3452 LY_ERR rc = LY_SUCCESS;
3453 struct lysc_node_leaf *sleaf;
3454
3455 if (options & LYXP_SCNODE_ALL) {
3456 for (i = 0; i < arg_count; ++i) {
3457 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3458 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3459 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3460 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003461 } else if (!warn_is_string_type(sleaf->type)) {
3462 LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", __func__, i + 1, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003463 }
3464 }
3465 }
3466 set_scnode_clear_ctx(set);
3467 return rc;
3468 }
3469
3470 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003471 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003472 if (rc != LY_SUCCESS) {
3473 free(str);
3474 return rc;
3475 }
3476
3477 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3478 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3479 strcpy(str + used - 1, args[i]->val.str);
3480 used += strlen(args[i]->val.str);
3481 }
3482
3483 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003484 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003485 set->type = LYXP_SET_STRING;
3486 set->val.str = str;
3487
3488 return LY_SUCCESS;
3489}
3490
3491/**
3492 * @brief Execute the XPath contains(string, string) function.
3493 * Returns LYXP_SET_BOOLEAN whether the second argument can
3494 * be found in the first or not.
3495 *
3496 * @param[in] args Array of arguments.
3497 * @param[in] arg_count Count of elements in @p args.
3498 * @param[in,out] set Context and result set at the same time.
3499 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003500 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003501 */
3502static LY_ERR
3503xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3504{
3505 struct lysc_node_leaf *sleaf;
3506 LY_ERR rc = LY_SUCCESS;
3507
3508 if (options & LYXP_SCNODE_ALL) {
3509 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3510 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3511 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 +02003512 } else if (!warn_is_string_type(sleaf->type)) {
3513 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003514 }
3515 }
3516
3517 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3518 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3519 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 +02003520 } else if (!warn_is_string_type(sleaf->type)) {
3521 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522 }
3523 }
3524 set_scnode_clear_ctx(set);
3525 return rc;
3526 }
3527
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003528 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003529 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003530 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003531 LY_CHECK_RET(rc);
3532
3533 if (strstr(args[0]->val.str, args[1]->val.str)) {
3534 set_fill_boolean(set, 1);
3535 } else {
3536 set_fill_boolean(set, 0);
3537 }
3538
3539 return LY_SUCCESS;
3540}
3541
3542/**
3543 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3544 * with the size of the node-set from the argument.
3545 *
3546 * @param[in] args Array of arguments.
3547 * @param[in] arg_count Count of elements in @p args.
3548 * @param[in,out] set Context and result set at the same time.
3549 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003550 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003551 */
3552static LY_ERR
3553xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3554{
3555 struct lysc_node *scnode = NULL;
3556 LY_ERR rc = LY_SUCCESS;
3557
3558 if (options & LYXP_SCNODE_ALL) {
3559 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3560 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003561 }
3562 set_scnode_clear_ctx(set);
3563 return rc;
3564 }
3565
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566 if (args[0]->type != LYXP_SET_NODE_SET) {
3567 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3568 return LY_EVALID;
3569 }
3570
3571 set_fill_number(set, args[0]->used);
3572 return LY_SUCCESS;
3573}
3574
3575/**
3576 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3577 * with the context with the intial node.
3578 *
3579 * @param[in] args Array of arguments.
3580 * @param[in] arg_count Count of elements in @p args.
3581 * @param[in,out] set Context and result set at the same time.
3582 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003583 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584 */
3585static LY_ERR
3586xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3587{
3588 if (arg_count || args) {
3589 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3590 return LY_EVALID;
3591 }
3592
3593 if (options & LYXP_SCNODE_ALL) {
3594 set_scnode_clear_ctx(set);
3595
Michal Vaskoecd62de2019-11-13 12:35:11 +01003596 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003597 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003598 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003599
3600 /* position is filled later */
3601 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3602 }
3603
3604 return LY_SUCCESS;
3605}
3606
3607/**
3608 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3609 * leafref or instance-identifier target node(s).
3610 *
3611 * @param[in] args Array of arguments.
3612 * @param[in] arg_count Count of elements in @p args.
3613 * @param[in,out] set Context and result set at the same time.
3614 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003615 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616 */
3617static LY_ERR
3618xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3619{
3620 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003621 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003622 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003623 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003624 struct ly_path *p;
3625 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626 char *errmsg = NULL;
3627 const char *val;
3628 int dynamic;
Michal Vasko00cbf532020-06-15 13:58:47 +02003629 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003630 LY_ERR rc = LY_SUCCESS;
3631
3632 if (options & LYXP_SCNODE_ALL) {
3633 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3634 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003635 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3636 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 +02003637 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3638 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3639 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 }
3641 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003642 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003643 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003644 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003645
3646 /* it was already evaluated on schema, it must succeed */
3647 if (set->format == LYD_JSON) {
Michal Vasko00cbf532020-06-15 13:58:47 +02003648 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3649 oper, LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003650 } else {
3651 assert(set->format == LYD_SCHEMA);
Michal Vasko00cbf532020-06-15 13:58:47 +02003652 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3653 oper, LY_PATH_TARGET_MANY, lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003654 }
Michal Vasko004d3152020-06-11 19:59:22 +02003655 assert(!rc);
3656
3657 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003658 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003659 ly_path_free(set->ctx, p);
3660
3661 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003662 }
3663
Michal Vasko03ff5a72019-09-11 13:49:33 +02003664 return rc;
3665 }
3666
Michal Vaskod3678892020-05-21 10:06:58 +02003667 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3669 return LY_EVALID;
3670 }
3671
Michal Vaskod3678892020-05-21 10:06:58 +02003672 lyxp_set_free_content(set);
3673 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3675 sleaf = (struct lysc_node_leaf *)leaf->schema;
3676 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3677 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3678 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003679 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3680 &leaf->value, set->tree, &node, &errmsg)) {
3681 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003683 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003684 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 } else {
3686 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003687 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 val = lyd_value2str(leaf, &dynamic);
3689 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3690 if (dynamic) {
3691 free((char *)val);
3692 }
3693 return LY_EVALID;
3694 }
3695 }
Michal Vasko004d3152020-06-11 19:59:22 +02003696
3697 /* insert it */
3698 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003699 }
3700 }
3701
3702 return LY_SUCCESS;
3703}
3704
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003705static LY_ERR
3706xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, int options, int self_match, const char *func)
3707{
3708 uint16_t i;
3709 LY_ARRAY_COUNT_TYPE u;
3710 struct lyd_node_term *leaf;
3711 struct lysc_node_leaf *sleaf;
3712 struct lyd_meta *meta;
3713 struct lyd_value data = {0}, *val;
3714 struct ly_err_item *err = NULL;
3715 LY_ERR rc = LY_SUCCESS;
3716 int found;
3717
3718 if (options & LYXP_SCNODE_ALL) {
3719 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3720 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3721 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3722 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3723 sleaf->name);
3724 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3725 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3726 }
3727
3728 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3729 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3730 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3731 sleaf->name);
3732 } else if (!warn_is_string_type(sleaf->type)) {
3733 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3734 }
3735 }
3736 set_scnode_clear_ctx(set);
3737 return rc;
3738 }
3739
3740 if (args[0]->type != LYXP_SET_NODE_SET) {
3741 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3742 "derived-from(-or-self)(node-set, string)");
3743 return LY_EVALID;
3744 }
3745 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3746 LY_CHECK_RET(rc);
3747
3748 set_fill_boolean(set, 0);
3749 found = 0;
3750 for (i = 0; i < args[0]->used; ++i) {
3751 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3752 continue;
3753 }
3754
3755 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3756 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3757 sleaf = (struct lysc_node_leaf *)leaf->schema;
3758 val = &leaf->value;
3759 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3760 /* uninteresting */
3761 continue;
3762 }
3763
3764 /* store args[1] as ident */
3765 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
3766 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod,
3767 set->format, (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3768 } else {
3769 meta = args[0]->val.meta[i].meta;
3770 val = &meta->value;
3771 if (val->realtype->basetype != LY_TYPE_IDENT) {
3772 /* uninteresting */
3773 continue;
3774 }
3775
3776 /* store args[1] as ident */
3777 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
3778 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)meta->annotation->module,
3779 set->format, meta->parent, set->tree, &data, NULL, &err);
3780 }
3781
3782 if (err) {
3783 ly_err_print(err);
3784 ly_err_free(err);
3785 }
3786 LY_CHECK_RET(rc);
3787
3788 /* finally check the identity itself */
3789 if (self_match && (data.ident == val->ident)) {
3790 set_fill_boolean(set, 1);
3791 found = 1;
3792 }
3793 if (!found) {
3794 LY_ARRAY_FOR(data.ident->derived, u) {
3795 if (data.ident->derived[u] == val->ident) {
3796 set_fill_boolean(set, 1);
3797 found = 1;
3798 break;
3799 }
3800 }
3801 }
3802
3803 /* free temporary value */
3804 val->realtype->plugin->free(set->ctx, &data);
3805 if (found) {
3806 break;
3807 }
3808 }
3809
3810 return LY_SUCCESS;
3811}
3812
Michal Vasko03ff5a72019-09-11 13:49:33 +02003813/**
3814 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3815 * on whether the first argument nodes contain a node of an identity derived from the second
3816 * argument identity.
3817 *
3818 * @param[in] args Array of arguments.
3819 * @param[in] arg_count Count of elements in @p args.
3820 * @param[in,out] set Context and result set at the same time.
3821 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003822 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003823 */
3824static LY_ERR
3825xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3826{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003827 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003828}
3829
3830/**
3831 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3832 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3833 * the second argument identity.
3834 *
3835 * @param[in] args Array of arguments.
3836 * @param[in] arg_count Count of elements in @p args.
3837 * @param[in,out] set Context and result set at the same time.
3838 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003839 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003840 */
3841static LY_ERR
3842xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3843{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003844 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003845}
3846
3847/**
3848 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3849 * with the integer value of the first node's enum value, otherwise NaN.
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_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3859{
3860 struct lyd_node_term *leaf;
3861 struct lysc_node_leaf *sleaf;
3862 LY_ERR rc = LY_SUCCESS;
3863
3864 if (options & LYXP_SCNODE_ALL) {
3865 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3866 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003867 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3868 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 +02003869 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3870 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871 }
3872 set_scnode_clear_ctx(set);
3873 return rc;
3874 }
3875
Michal Vaskod3678892020-05-21 10:06:58 +02003876 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003877 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3878 return LY_EVALID;
3879 }
3880
3881 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003882 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003883 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3884 sleaf = (struct lysc_node_leaf *)leaf->schema;
3885 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3886 set_fill_number(set, leaf->value.enum_item->value);
3887 }
3888 }
3889
3890 return LY_SUCCESS;
3891}
3892
3893/**
3894 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3895 * with false value.
3896 *
3897 * @param[in] args Array of arguments.
3898 * @param[in] arg_count Count of elements in @p args.
3899 * @param[in,out] set Context and result set at the same time.
3900 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003901 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003902 */
3903static LY_ERR
3904xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3905{
3906 if (options & LYXP_SCNODE_ALL) {
3907 set_scnode_clear_ctx(set);
3908 return LY_SUCCESS;
3909 }
3910
3911 set_fill_boolean(set, 0);
3912 return LY_SUCCESS;
3913}
3914
3915/**
3916 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3917 * with the first argument floored (truncated).
3918 *
3919 * @param[in] args Array of arguments.
3920 * @param[in] arg_count Count of elements in @p args.
3921 * @param[in,out] set Context and result set at the same time.
3922 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003923 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003924 */
3925static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003926xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003927{
3928 LY_ERR rc;
3929
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003930 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931 LY_CHECK_RET(rc);
3932 if (isfinite(args[0]->val.num)) {
3933 set_fill_number(set, (long long)args[0]->val.num);
3934 }
3935
3936 return LY_SUCCESS;
3937}
3938
3939/**
3940 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3941 * whether the language of the text matches the one from the argument.
3942 *
3943 * @param[in] args Array of arguments.
3944 * @param[in] arg_count Count of elements in @p args.
3945 * @param[in,out] set Context and result set at the same time.
3946 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003947 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003948 */
3949static LY_ERR
3950xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3951{
3952 const struct lyd_node *node;
3953 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003954 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003955 const char *val;
3956 int i, dynamic;
3957 LY_ERR rc = LY_SUCCESS;
3958
3959 if (options & LYXP_SCNODE_ALL) {
3960 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3961 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3962 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 +02003963 } else if (!warn_is_string_type(sleaf->type)) {
3964 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003965 }
3966 }
3967 set_scnode_clear_ctx(set);
3968 return rc;
3969 }
3970
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003971 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 LY_CHECK_RET(rc);
3973
Michal Vasko03ff5a72019-09-11 13:49:33 +02003974 if (set->type != LYXP_SET_NODE_SET) {
3975 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3976 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003977 } else if (!set->used) {
3978 set_fill_boolean(set, 0);
3979 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980 }
3981
3982 switch (set->val.nodes[0].type) {
3983 case LYXP_NODE_ELEM:
3984 case LYXP_NODE_TEXT:
3985 node = set->val.nodes[0].node;
3986 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01003987 case LYXP_NODE_META:
3988 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003989 break;
3990 default:
3991 /* nothing to do with roots */
3992 set_fill_boolean(set, 0);
3993 return LY_SUCCESS;
3994 }
3995
Michal Vasko9f96a052020-03-10 09:41:45 +01003996 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02003997 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01003998 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003999 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004000 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004001 break;
4002 }
4003 }
4004
Michal Vasko9f96a052020-03-10 09:41:45 +01004005 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004006 break;
4007 }
4008 }
4009
4010 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004011 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 set_fill_boolean(set, 0);
4013 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01004014 val = lyd_meta2str(meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 for (i = 0; args[0]->val.str[i]; ++i) {
4016 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4017 set_fill_boolean(set, 0);
4018 break;
4019 }
4020 }
4021 if (!args[0]->val.str[i]) {
4022 if (!val[i] || (val[i] == '-')) {
4023 set_fill_boolean(set, 1);
4024 } else {
4025 set_fill_boolean(set, 0);
4026 }
4027 }
4028 if (dynamic) {
4029 free((char *)val);
4030 }
4031 }
4032
4033 return LY_SUCCESS;
4034}
4035
4036/**
4037 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4038 * with the context size.
4039 *
4040 * @param[in] args Array of arguments.
4041 * @param[in] arg_count Count of elements in @p args.
4042 * @param[in,out] set Context and result set at the same time.
4043 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004044 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 */
4046static LY_ERR
4047xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4048{
4049 if (options & LYXP_SCNODE_ALL) {
4050 set_scnode_clear_ctx(set);
4051 return LY_SUCCESS;
4052 }
4053
Michal Vasko03ff5a72019-09-11 13:49:33 +02004054 if (set->type != LYXP_SET_NODE_SET) {
4055 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4056 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004057 } else if (!set->used) {
4058 set_fill_number(set, 0);
4059 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004060 }
4061
4062 set_fill_number(set, set->ctx_size);
4063 return LY_SUCCESS;
4064}
4065
4066/**
4067 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4068 * with the node name without namespace from the argument or the context.
4069 *
4070 * @param[in] args Array of arguments.
4071 * @param[in] arg_count Count of elements in @p args.
4072 * @param[in,out] set Context and result set at the same time.
4073 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004074 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 */
4076static LY_ERR
4077xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4078{
4079 struct lyxp_set_node *item;
4080 /* suppress unused variable warning */
4081 (void)options;
4082
4083 if (options & LYXP_SCNODE_ALL) {
4084 set_scnode_clear_ctx(set);
4085 return LY_SUCCESS;
4086 }
4087
4088 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004089 if (args[0]->type != LYXP_SET_NODE_SET) {
4090 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4091 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004092 } else if (!args[0]->used) {
4093 set_fill_string(set, "", 0);
4094 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 }
4096
4097 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004098 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004099
4100 item = &args[0]->val.nodes[0];
4101 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 if (set->type != LYXP_SET_NODE_SET) {
4103 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4104 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004105 } else if (!set->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(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004112
4113 item = &set->val.nodes[0];
4114 }
4115
4116 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004117 case LYXP_NODE_NONE:
4118 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004119 case LYXP_NODE_ROOT:
4120 case LYXP_NODE_ROOT_CONFIG:
4121 case LYXP_NODE_TEXT:
4122 set_fill_string(set, "", 0);
4123 break;
4124 case LYXP_NODE_ELEM:
4125 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4126 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004127 case LYXP_NODE_META:
4128 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004129 break;
4130 }
4131
4132 return LY_SUCCESS;
4133}
4134
4135/**
4136 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4137 * with the node name fully qualified (with namespace) from the argument or the context.
4138 *
4139 * @param[in] args Array of arguments.
4140 * @param[in] arg_count Count of elements in @p args.
4141 * @param[in,out] set Context and result set at the same time.
4142 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004143 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004144 */
4145static LY_ERR
4146xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4147{
4148 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004149 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004150 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004151 const char *name = NULL;
4152 int rc = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153
4154 if (options & LYXP_SCNODE_ALL) {
4155 set_scnode_clear_ctx(set);
4156 return LY_SUCCESS;
4157 }
4158
4159 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004160 if (args[0]->type != LYXP_SET_NODE_SET) {
4161 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4162 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004163 } else if (!args[0]->used) {
4164 set_fill_string(set, "", 0);
4165 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166 }
4167
4168 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004169 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004170
4171 item = &args[0]->val.nodes[0];
4172 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004173 if (set->type != LYXP_SET_NODE_SET) {
4174 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4175 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004176 } else if (!set->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(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183
4184 item = &set->val.nodes[0];
4185 }
4186
4187 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004188 case LYXP_NODE_NONE:
4189 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004190 case LYXP_NODE_ROOT:
4191 case LYXP_NODE_ROOT_CONFIG:
4192 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004193 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194 break;
4195 case LYXP_NODE_ELEM:
4196 mod = item->node->schema->module;
4197 name = item->node->schema->name;
4198 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004199 case LYXP_NODE_META:
4200 mod = ((struct lyd_meta *)item->node)->annotation->module;
4201 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 break;
4203 }
4204
4205 if (mod && name) {
4206 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004207 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4209 break;
4210 case LYD_JSON:
4211 rc = asprintf(&str, "%s:%s", mod->name, name);
4212 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004213 case LYD_XML:
Michal Vasko60ea6352020-06-29 13:39:39 +02004214 case LYD_LYB:
Michal Vasko9409ef62019-09-12 11:47:17 +02004215 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216 }
4217 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4218 set_fill_string(set, str, strlen(str));
4219 free(str);
4220 } else {
4221 set_fill_string(set, "", 0);
4222 }
4223
4224 return LY_SUCCESS;
4225}
4226
4227/**
4228 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4229 * with the namespace of the node from the argument or the context.
4230 *
4231 * @param[in] args Array of arguments.
4232 * @param[in] arg_count Count of elements in @p args.
4233 * @param[in,out] set Context and result set at the same time.
4234 * @param[in] options XPath options.
4235 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4236 */
4237static LY_ERR
4238xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4239{
4240 struct lyxp_set_node *item;
4241 struct lys_module *mod;
4242 /* suppress unused variable warning */
4243 (void)options;
4244
4245 if (options & LYXP_SCNODE_ALL) {
4246 set_scnode_clear_ctx(set);
4247 return LY_SUCCESS;
4248 }
4249
4250 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004251 if (args[0]->type != LYXP_SET_NODE_SET) {
4252 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4253 "namespace-uri(node-set?)");
4254 return LY_EVALID;
4255 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004256 set_fill_string(set, "", 0);
4257 return LY_SUCCESS;
4258 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004259
4260 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004261 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004262
4263 item = &args[0]->val.nodes[0];
4264 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004265 if (set->type != LYXP_SET_NODE_SET) {
4266 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4267 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004268 } else if (!set->used) {
4269 set_fill_string(set, "", 0);
4270 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004271 }
4272
4273 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004274 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275
4276 item = &set->val.nodes[0];
4277 }
4278
4279 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004280 case LYXP_NODE_NONE:
4281 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282 case LYXP_NODE_ROOT:
4283 case LYXP_NODE_ROOT_CONFIG:
4284 case LYXP_NODE_TEXT:
4285 set_fill_string(set, "", 0);
4286 break;
4287 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004288 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289 if (item->type == LYXP_NODE_ELEM) {
4290 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004291 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004293 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 }
4295
4296 set_fill_string(set, mod->ns, strlen(mod->ns));
4297 break;
4298 }
4299
4300 return LY_SUCCESS;
4301}
4302
4303/**
4304 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4305 * with only nodes from the context. In practice it either leaves the context
4306 * as it is or returns an empty node set.
4307 *
4308 * @param[in] args Array of arguments.
4309 * @param[in] arg_count Count of elements in @p args.
4310 * @param[in,out] set Context and result set at the same time.
4311 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004312 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 */
4314static LY_ERR
4315xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4316{
4317 if (options & LYXP_SCNODE_ALL) {
4318 set_scnode_clear_ctx(set);
4319 return LY_SUCCESS;
4320 }
4321
4322 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004323 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004324 }
4325 return LY_SUCCESS;
4326}
4327
4328/**
4329 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4330 * with normalized value (no leading, trailing, double white spaces) of the node
4331 * from the argument or the context.
4332 *
4333 * @param[in] args Array of arguments.
4334 * @param[in] arg_count Count of elements in @p args.
4335 * @param[in,out] set Context and result set at the same time.
4336 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004337 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004338 */
4339static LY_ERR
4340xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4341{
4342 uint16_t i, new_used;
4343 char *new;
4344 int have_spaces = 0, space_before = 0;
4345 struct lysc_node_leaf *sleaf;
4346 LY_ERR rc = LY_SUCCESS;
4347
4348 if (options & LYXP_SCNODE_ALL) {
4349 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4350 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4351 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 +02004352 } else if (!warn_is_string_type(sleaf->type)) {
4353 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004354 }
4355 }
4356 set_scnode_clear_ctx(set);
4357 return rc;
4358 }
4359
4360 if (arg_count) {
4361 set_fill_set(set, args[0]);
4362 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004363 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004364 LY_CHECK_RET(rc);
4365
4366 /* is there any normalization necessary? */
4367 for (i = 0; set->val.str[i]; ++i) {
4368 if (is_xmlws(set->val.str[i])) {
4369 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4370 have_spaces = 1;
4371 break;
4372 }
4373 space_before = 1;
4374 } else {
4375 space_before = 0;
4376 }
4377 }
4378
4379 /* yep, there is */
4380 if (have_spaces) {
4381 /* it's enough, at least one character will go, makes space for ending '\0' */
4382 new = malloc(strlen(set->val.str) * sizeof(char));
4383 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4384 new_used = 0;
4385
4386 space_before = 0;
4387 for (i = 0; set->val.str[i]; ++i) {
4388 if (is_xmlws(set->val.str[i])) {
4389 if ((i == 0) || space_before) {
4390 space_before = 1;
4391 continue;
4392 } else {
4393 space_before = 1;
4394 }
4395 } else {
4396 space_before = 0;
4397 }
4398
4399 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4400 ++new_used;
4401 }
4402
4403 /* at worst there is one trailing space now */
4404 if (new_used && is_xmlws(new[new_used - 1])) {
4405 --new_used;
4406 }
4407
4408 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4409 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4410 new[new_used] = '\0';
4411
4412 free(set->val.str);
4413 set->val.str = new;
4414 }
4415
4416 return LY_SUCCESS;
4417}
4418
4419/**
4420 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4421 * with the argument converted to boolean and logically inverted.
4422 *
4423 * @param[in] args Array of arguments.
4424 * @param[in] arg_count Count of elements in @p args.
4425 * @param[in,out] set Context and result set at the same time.
4426 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004427 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004428 */
4429static LY_ERR
4430xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4431{
4432 if (options & LYXP_SCNODE_ALL) {
4433 set_scnode_clear_ctx(set);
4434 return LY_SUCCESS;
4435 }
4436
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004437 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004438 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 set_fill_boolean(set, 0);
4440 } else {
4441 set_fill_boolean(set, 1);
4442 }
4443
4444 return LY_SUCCESS;
4445}
4446
4447/**
4448 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4449 * with the number representation of either the argument or the context.
4450 *
4451 * @param[in] args Array of arguments.
4452 * @param[in] arg_count Count of elements in @p args.
4453 * @param[in,out] set Context and result set at the same time.
4454 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004455 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004456 */
4457static LY_ERR
4458xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4459{
4460 LY_ERR rc;
4461
4462 if (options & LYXP_SCNODE_ALL) {
4463 set_scnode_clear_ctx(set);
4464 return LY_SUCCESS;
4465 }
4466
4467 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004468 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 LY_CHECK_RET(rc);
4470 set_fill_set(set, args[0]);
4471 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004472 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004473 LY_CHECK_RET(rc);
4474 }
4475
4476 return LY_SUCCESS;
4477}
4478
4479/**
4480 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4481 * with the context position.
4482 *
4483 * @param[in] args Array of arguments.
4484 * @param[in] arg_count Count of elements in @p args.
4485 * @param[in,out] set Context and result set at the same time.
4486 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004487 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004488 */
4489static LY_ERR
4490xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4491{
4492 if (options & LYXP_SCNODE_ALL) {
4493 set_scnode_clear_ctx(set);
4494 return LY_SUCCESS;
4495 }
4496
Michal Vasko03ff5a72019-09-11 13:49:33 +02004497 if (set->type != LYXP_SET_NODE_SET) {
4498 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4499 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004500 } else if (!set->used) {
4501 set_fill_number(set, 0);
4502 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004503 }
4504
4505 set_fill_number(set, set->ctx_pos);
4506
4507 /* UNUSED in 'Release' build type */
4508 (void)options;
4509 return LY_SUCCESS;
4510}
4511
4512/**
4513 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4514 * depending on whether the second argument regex matches the first argument string. For details refer to
4515 * YANG 1.1 RFC section 10.2.1.
4516 *
4517 * @param[in] args Array of arguments.
4518 * @param[in] arg_count Count of elements in @p args.
4519 * @param[in,out] set Context and result set at the same time.
4520 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004521 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004522 */
4523static LY_ERR
4524xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4525{
4526 struct lysc_pattern **patterns = NULL, **pattern;
4527 struct lysc_node_leaf *sleaf;
4528 char *path;
4529 LY_ERR rc = LY_SUCCESS;
4530 struct ly_err_item *err;
4531
4532 if (options & LYXP_SCNODE_ALL) {
4533 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4534 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4535 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 +02004536 } else if (!warn_is_string_type(sleaf->type)) {
4537 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004538 }
4539 }
4540
4541 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4542 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4543 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 +02004544 } else if (!warn_is_string_type(sleaf->type)) {
4545 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 }
4547 }
4548 set_scnode_clear_ctx(set);
4549 return rc;
4550 }
4551
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004552 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004553 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004554 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004555 LY_CHECK_RET(rc);
4556
4557 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4558 *pattern = malloc(sizeof **pattern);
4559 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4560 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4561 free(path);
4562 if (rc != LY_SUCCESS) {
4563 LY_ARRAY_FREE(patterns);
4564 return rc;
4565 }
4566
4567 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4568 pcre2_code_free((*pattern)->code);
4569 free(*pattern);
4570 LY_ARRAY_FREE(patterns);
4571 if (rc && (rc != LY_EVALID)) {
4572 ly_err_print(err);
4573 ly_err_free(err);
4574 return rc;
4575 }
4576
4577 if (rc == LY_EVALID) {
4578 ly_err_free(err);
4579 set_fill_boolean(set, 0);
4580 } else {
4581 set_fill_boolean(set, 1);
4582 }
4583
4584 return LY_SUCCESS;
4585}
4586
4587/**
4588 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4589 * with the rounded first argument. For details refer to
4590 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4591 *
4592 * @param[in] args Array of arguments.
4593 * @param[in] arg_count Count of elements in @p args.
4594 * @param[in,out] set Context and result set at the same time.
4595 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004596 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004597 */
4598static LY_ERR
4599xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4600{
4601 struct lysc_node_leaf *sleaf;
4602 LY_ERR rc = LY_SUCCESS;
4603
4604 if (options & LYXP_SCNODE_ALL) {
4605 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4606 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004607 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4608 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 +02004609 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4610 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 }
4612 set_scnode_clear_ctx(set);
4613 return rc;
4614 }
4615
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004616 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004617 LY_CHECK_RET(rc);
4618
4619 /* cover only the cases where floor can't be used */
4620 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4621 set_fill_number(set, -0.0f);
4622 } else {
4623 args[0]->val.num += 0.5;
4624 rc = xpath_floor(args, 1, args[0], options);
4625 LY_CHECK_RET(rc);
4626 set_fill_number(set, args[0]->val.num);
4627 }
4628
4629 return LY_SUCCESS;
4630}
4631
4632/**
4633 * @brief Execute the XPath starts-with(string, string) function.
4634 * Returns LYXP_SET_BOOLEAN whether the second argument is
4635 * the prefix of the first or not.
4636 *
4637 * @param[in] args Array of arguments.
4638 * @param[in] arg_count Count of elements in @p args.
4639 * @param[in,out] set Context and result set at the same time.
4640 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004641 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 */
4643static LY_ERR
4644xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4645{
4646 struct lysc_node_leaf *sleaf;
4647 LY_ERR rc = LY_SUCCESS;
4648
4649 if (options & LYXP_SCNODE_ALL) {
4650 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4651 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4652 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 +02004653 } else if (!warn_is_string_type(sleaf->type)) {
4654 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004655 }
4656 }
4657
4658 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4659 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4660 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 +02004661 } else if (!warn_is_string_type(sleaf->type)) {
4662 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004663 }
4664 }
4665 set_scnode_clear_ctx(set);
4666 return rc;
4667 }
4668
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004669 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004670 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004671 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004672 LY_CHECK_RET(rc);
4673
4674 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4675 set_fill_boolean(set, 0);
4676 } else {
4677 set_fill_boolean(set, 1);
4678 }
4679
4680 return LY_SUCCESS;
4681}
4682
4683/**
4684 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4685 * with the string representation of either the argument or the context.
4686 *
4687 * @param[in] args Array of arguments.
4688 * @param[in] arg_count Count of elements in @p args.
4689 * @param[in,out] set Context and result set at the same time.
4690 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004691 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004692 */
4693static LY_ERR
4694xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4695{
4696 LY_ERR rc;
4697
4698 if (options & LYXP_SCNODE_ALL) {
4699 set_scnode_clear_ctx(set);
4700 return LY_SUCCESS;
4701 }
4702
4703 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004704 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004705 LY_CHECK_RET(rc);
4706 set_fill_set(set, args[0]);
4707 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004708 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004709 LY_CHECK_RET(rc);
4710 }
4711
4712 return LY_SUCCESS;
4713}
4714
4715/**
4716 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4717 * with the length of the string in either the argument or the context.
4718 *
4719 * @param[in] args Array of arguments.
4720 * @param[in] arg_count Count of elements in @p args.
4721 * @param[in,out] set Context and result set at the same time.
4722 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004723 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004724 */
4725static LY_ERR
4726xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4727{
4728 struct lysc_node_leaf *sleaf;
4729 LY_ERR rc = LY_SUCCESS;
4730
4731 if (options & LYXP_SCNODE_ALL) {
4732 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4733 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4734 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 +02004735 } else if (!warn_is_string_type(sleaf->type)) {
4736 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004737 }
4738 }
4739 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4740 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4741 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 +02004742 } else if (!warn_is_string_type(sleaf->type)) {
4743 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004744 }
4745 }
4746 set_scnode_clear_ctx(set);
4747 return rc;
4748 }
4749
4750 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004751 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 LY_CHECK_RET(rc);
4753 set_fill_number(set, strlen(args[0]->val.str));
4754 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004755 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756 LY_CHECK_RET(rc);
4757 set_fill_number(set, strlen(set->val.str));
4758 }
4759
4760 return LY_SUCCESS;
4761}
4762
4763/**
4764 * @brief Execute the XPath substring(string, number, number?) function.
4765 * Returns LYXP_SET_STRING substring of the first argument starting
4766 * on the second argument index ending on the third argument index,
4767 * indexed from 1. For exact definition refer to
4768 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4769 *
4770 * @param[in] args Array of arguments.
4771 * @param[in] arg_count Count of elements in @p args.
4772 * @param[in,out] set Context and result set at the same time.
4773 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004774 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004775 */
4776static LY_ERR
4777xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4778{
4779 int start, len;
4780 uint16_t str_start, str_len, pos;
4781 struct lysc_node_leaf *sleaf;
4782 LY_ERR rc = LY_SUCCESS;
4783
4784 if (options & LYXP_SCNODE_ALL) {
4785 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4786 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4787 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 +02004788 } else if (!warn_is_string_type(sleaf->type)) {
4789 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004790 }
4791 }
4792
4793 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4794 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4795 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 +02004796 } else if (!warn_is_numeric_type(sleaf->type)) {
4797 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004798 }
4799 }
4800
4801 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4802 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4803 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4804 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 +02004805 } else if (!warn_is_numeric_type(sleaf->type)) {
4806 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004807 }
4808 }
4809 set_scnode_clear_ctx(set);
4810 return rc;
4811 }
4812
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004813 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004814 LY_CHECK_RET(rc);
4815
4816 /* start */
4817 if (xpath_round(&args[1], 1, args[1], options)) {
4818 return -1;
4819 }
4820 if (isfinite(args[1]->val.num)) {
4821 start = args[1]->val.num - 1;
4822 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4823 start = INT_MIN;
4824 } else {
4825 start = INT_MAX;
4826 }
4827
4828 /* len */
4829 if (arg_count == 3) {
4830 rc = xpath_round(&args[2], 1, args[2], options);
4831 LY_CHECK_RET(rc);
4832 if (isfinite(args[2]->val.num)) {
4833 len = args[2]->val.num;
4834 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4835 len = 0;
4836 } else {
4837 len = INT_MAX;
4838 }
4839 } else {
4840 len = INT_MAX;
4841 }
4842
4843 /* find matching character positions */
4844 str_start = 0;
4845 str_len = 0;
4846 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4847 if (pos < start) {
4848 ++str_start;
4849 } else if (pos < start + len) {
4850 ++str_len;
4851 } else {
4852 break;
4853 }
4854 }
4855
4856 set_fill_string(set, args[0]->val.str + str_start, str_len);
4857 return LY_SUCCESS;
4858}
4859
4860/**
4861 * @brief Execute the XPath substring-after(string, string) function.
4862 * Returns LYXP_SET_STRING with the string succeeding the occurance
4863 * of the second argument in the first or an empty string.
4864 *
4865 * @param[in] args Array of arguments.
4866 * @param[in] arg_count Count of elements in @p args.
4867 * @param[in,out] set Context and result set at the same time.
4868 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004869 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 */
4871static LY_ERR
4872xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4873{
4874 char *ptr;
4875 struct lysc_node_leaf *sleaf;
4876 LY_ERR rc = LY_SUCCESS;
4877
4878 if (options & LYXP_SCNODE_ALL) {
4879 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4880 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4881 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 +02004882 } else if (!warn_is_string_type(sleaf->type)) {
4883 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004884 }
4885 }
4886
4887 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4888 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4889 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 +02004890 } else if (!warn_is_string_type(sleaf->type)) {
4891 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 }
4893 }
4894 set_scnode_clear_ctx(set);
4895 return rc;
4896 }
4897
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004898 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004900 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004901 LY_CHECK_RET(rc);
4902
4903 ptr = strstr(args[0]->val.str, args[1]->val.str);
4904 if (ptr) {
4905 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4906 } else {
4907 set_fill_string(set, "", 0);
4908 }
4909
4910 return LY_SUCCESS;
4911}
4912
4913/**
4914 * @brief Execute the XPath substring-before(string, string) function.
4915 * Returns LYXP_SET_STRING with the string preceding the occurance
4916 * of the second argument in the first or an empty string.
4917 *
4918 * @param[in] args Array of arguments.
4919 * @param[in] arg_count Count of elements in @p args.
4920 * @param[in,out] set Context and result set at the same time.
4921 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004922 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004923 */
4924static LY_ERR
4925xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4926{
4927 char *ptr;
4928 struct lysc_node_leaf *sleaf;
4929 LY_ERR rc = LY_SUCCESS;
4930
4931 if (options & LYXP_SCNODE_ALL) {
4932 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4933 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4934 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 +02004935 } else if (!warn_is_string_type(sleaf->type)) {
4936 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 }
4938 }
4939
4940 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4941 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4942 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 +02004943 } else if (!warn_is_string_type(sleaf->type)) {
4944 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004945 }
4946 }
4947 set_scnode_clear_ctx(set);
4948 return rc;
4949 }
4950
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004951 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004952 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004953 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004954 LY_CHECK_RET(rc);
4955
4956 ptr = strstr(args[0]->val.str, args[1]->val.str);
4957 if (ptr) {
4958 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4959 } else {
4960 set_fill_string(set, "", 0);
4961 }
4962
4963 return LY_SUCCESS;
4964}
4965
4966/**
4967 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4968 * with the sum of all the nodes in the context.
4969 *
4970 * @param[in] args Array of arguments.
4971 * @param[in] arg_count Count of elements in @p args.
4972 * @param[in,out] set Context and result set at the same time.
4973 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004974 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004975 */
4976static LY_ERR
4977xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4978{
4979 long double num;
4980 char *str;
4981 uint16_t i;
4982 struct lyxp_set set_item;
4983 struct lysc_node_leaf *sleaf;
4984 LY_ERR rc = LY_SUCCESS;
4985
4986 if (options & LYXP_SCNODE_ALL) {
4987 if (args[0]->type == LYXP_SET_SCNODE_SET) {
4988 for (i = 0; i < args[0]->used; ++i) {
4989 if (args[0]->val.scnodes[i].in_ctx == 1) {
4990 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
4991 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4992 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
4993 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004994 } else if (!warn_is_numeric_type(sleaf->type)) {
4995 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004996 }
4997 }
4998 }
4999 }
5000 set_scnode_clear_ctx(set);
5001 return rc;
5002 }
5003
5004 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005005
5006 if (args[0]->type != LYXP_SET_NODE_SET) {
5007 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5008 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005009 } else if (!args[0]->used) {
5010 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005011 }
5012
Michal Vasko5c4e5892019-11-14 12:31:38 +01005013 set_init(&set_item, set);
5014
Michal Vasko03ff5a72019-09-11 13:49:33 +02005015 set_item.type = LYXP_SET_NODE_SET;
5016 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5017 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5018
5019 set_item.used = 1;
5020 set_item.size = 1;
5021
5022 for (i = 0; i < args[0]->used; ++i) {
5023 set_item.val.nodes[0] = args[0]->val.nodes[i];
5024
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005025 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005026 LY_CHECK_RET(rc);
5027 num = cast_string_to_number(str);
5028 free(str);
5029 set->val.num += num;
5030 }
5031
5032 free(set_item.val.nodes);
5033
5034 return LY_SUCCESS;
5035}
5036
5037/**
5038 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5039 * with the text content of the nodes in the context.
5040 *
5041 * @param[in] args Array of arguments.
5042 * @param[in] arg_count Count of elements in @p args.
5043 * @param[in,out] set Context and result set at the same time.
5044 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005045 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005046 */
5047static LY_ERR
5048xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5049{
5050 uint32_t i;
5051
5052 if (options & LYXP_SCNODE_ALL) {
5053 set_scnode_clear_ctx(set);
5054 return LY_SUCCESS;
5055 }
5056
Michal Vasko03ff5a72019-09-11 13:49:33 +02005057 if (set->type != LYXP_SET_NODE_SET) {
5058 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5059 return LY_EVALID;
5060 }
5061
5062 for (i = 0; i < set->used;) {
5063 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005064 case LYXP_NODE_NONE:
5065 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005067 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5068 set->val.nodes[i].type = LYXP_NODE_TEXT;
5069 ++i;
5070 break;
5071 }
5072 /* fall through */
5073 case LYXP_NODE_ROOT:
5074 case LYXP_NODE_ROOT_CONFIG:
5075 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005076 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005077 set_remove_node(set, i);
5078 break;
5079 }
5080 }
5081
5082 return LY_SUCCESS;
5083}
5084
5085/**
5086 * @brief Execute the XPath translate(string, string, string) function.
5087 * Returns LYXP_SET_STRING with the first argument with the characters
5088 * from the second argument replaced by those on the corresponding
5089 * positions in the third argument.
5090 *
5091 * @param[in] args Array of arguments.
5092 * @param[in] arg_count Count of elements in @p args.
5093 * @param[in,out] set Context and result set at the same time.
5094 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005095 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 */
5097static LY_ERR
5098xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5099{
5100 uint16_t i, j, new_used;
5101 char *new;
5102 int found, have_removed;
5103 struct lysc_node_leaf *sleaf;
5104 LY_ERR rc = LY_SUCCESS;
5105
5106 if (options & LYXP_SCNODE_ALL) {
5107 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5108 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5109 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 +02005110 } else if (!warn_is_string_type(sleaf->type)) {
5111 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 }
5113 }
5114
5115 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5116 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5117 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 +02005118 } else if (!warn_is_string_type(sleaf->type)) {
5119 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120 }
5121 }
5122
5123 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5124 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5125 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 +02005126 } else if (!warn_is_string_type(sleaf->type)) {
5127 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128 }
5129 }
5130 set_scnode_clear_ctx(set);
5131 return rc;
5132 }
5133
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005134 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005136 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005137 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005138 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 LY_CHECK_RET(rc);
5140
5141 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5142 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5143 new_used = 0;
5144
5145 have_removed = 0;
5146 for (i = 0; args[0]->val.str[i]; ++i) {
5147 found = 0;
5148
5149 for (j = 0; args[1]->val.str[j]; ++j) {
5150 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5151 /* removing this char */
5152 if (j >= strlen(args[2]->val.str)) {
5153 have_removed = 1;
5154 found = 1;
5155 break;
5156 }
5157 /* replacing this char */
5158 new[new_used] = args[2]->val.str[j];
5159 ++new_used;
5160 found = 1;
5161 break;
5162 }
5163 }
5164
5165 /* copying this char */
5166 if (!found) {
5167 new[new_used] = args[0]->val.str[i];
5168 ++new_used;
5169 }
5170 }
5171
5172 if (have_removed) {
5173 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5174 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5175 }
5176 new[new_used] = '\0';
5177
Michal Vaskod3678892020-05-21 10:06:58 +02005178 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005179 set->type = LYXP_SET_STRING;
5180 set->val.str = new;
5181
5182 return LY_SUCCESS;
5183}
5184
5185/**
5186 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5187 * with true value.
5188 *
5189 * @param[in] args Array of arguments.
5190 * @param[in] arg_count Count of elements in @p args.
5191 * @param[in,out] set Context and result set at the same time.
5192 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005193 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005194 */
5195static LY_ERR
5196xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5197{
5198 if (options & LYXP_SCNODE_ALL) {
5199 set_scnode_clear_ctx(set);
5200 return LY_SUCCESS;
5201 }
5202
5203 set_fill_boolean(set, 1);
5204 return LY_SUCCESS;
5205}
5206
5207/*
5208 * moveto functions
5209 *
5210 * They and only they actually change the context (set).
5211 */
5212
5213/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005214 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005216 * XPath @p set is expected to be a (sc)node set!
5217 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005218 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5219 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5220 * @param[in] set Set with XPath context.
5221 * @param[out] moveto_mod Expected module of a matching node.
5222 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005223 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005224static LY_ERR
5225moveto_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 +02005226{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005227 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005228 const char *ptr;
5229 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005230 char *str;
5231
Michal Vasko2104e9f2020-03-06 08:23:25 +01005232 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5233
Michal Vasko6346ece2019-09-24 13:12:53 +02005234 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5235 /* specific module */
5236 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237
Michal Vasko6346ece2019-09-24 13:12:53 +02005238 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005239 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005240 /* schema, search all local module imports */
5241 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5242 break;
5243 case LYD_JSON:
5244 /* JSON data, search in context */
5245 str = strndup(*qname, pref_len);
Michal Vasko97e76fd2020-05-27 15:22:01 +02005246 mod = ly_ctx_get_module_implemented(set->ctx, str);
Michal Vasko6346ece2019-09-24 13:12:53 +02005247 free(str);
5248 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005249 case LYD_XML:
Michal Vasko60ea6352020-06-29 13:39:39 +02005250 case LYD_LYB:
Michal Vasko6346ece2019-09-24 13:12:53 +02005251 LOGINT_RET(set->ctx);
5252 }
5253
Michal Vasko004d3152020-06-11 19:59:22 +02005254 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005255 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005256 if (set->type == LYXP_SET_SCNODE_SET) {
5257 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5258 } else {
5259 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5260 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 return LY_EVALID;
5262 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005263
Michal Vasko6346ece2019-09-24 13:12:53 +02005264 *qname += pref_len + 1;
5265 *qname_len -= pref_len + 1;
5266 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5267 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005268 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005269 } else if (set->type == LYXP_SET_SCNODE_SET) {
5270 /* current node module */
5271 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005272 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005273 /* current node module */
5274 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005275 }
5276
Michal Vasko6346ece2019-09-24 13:12:53 +02005277 *moveto_mod = mod;
5278 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005279}
5280
5281/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005282 * @brief Move context @p set to the root. Handles absolute path.
5283 * Result is LYXP_SET_NODE_SET.
5284 *
5285 * @param[in,out] set Set to use.
5286 * @param[in] options Xpath options.
5287 */
5288static void
5289moveto_root(struct lyxp_set *set, int options)
5290{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005291 if (!set) {
5292 return;
5293 }
5294
5295 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005297 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005298 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005299 set->type = LYXP_SET_NODE_SET;
5300 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005301 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005302 }
5303}
5304
5305/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005306 * @brief Check whether a node has some unresolved "when".
5307 *
5308 * @param[in] node Node to check.
5309 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5310 */
5311static LY_ERR
5312moveto_when_check(const struct lyd_node *node)
5313{
5314 const struct lysc_node *schema;
5315
5316 if (!node) {
5317 return LY_SUCCESS;
5318 }
5319
5320 schema = node->schema;
5321 do {
5322 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5323 return LY_EINCOMPLETE;
5324 }
5325 schema = schema->parent;
5326 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5327
5328 return LY_SUCCESS;
5329}
5330
5331/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005332 * @brief Check @p node as a part of NameTest processing.
5333 *
5334 * @param[in] node Node to check.
5335 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005336 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005337 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5338 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005339 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5340 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341 */
5342static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005343moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op,
5344 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345{
5346 /* module check */
5347 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005348 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005349 }
5350
Michal Vasko5c4e5892019-11-14 12:31:38 +01005351 /* context check */
5352 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005354 } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) {
5355 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356 }
5357
5358 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005359 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005360 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005361 }
5362
Michal Vaskoa1424542019-11-14 16:08:52 +01005363 /* when check */
5364 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005365 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005366 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005367
5368 /* match */
5369 return LY_SUCCESS;
5370}
5371
5372/**
5373 * @brief Check @p node as a part of schema NameTest processing.
5374 *
5375 * @param[in] node Schema node to check.
5376 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005377 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005378 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5379 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005380 * @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 +02005381 */
5382static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005383moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op,
5384 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005387 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005388 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389 }
5390
5391 /* context check */
5392 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5393 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005394 } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) {
5395 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005396 }
5397
5398 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005399 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005400 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005401 }
5402
5403 /* match */
5404 return LY_SUCCESS;
5405}
5406
5407/**
Michal Vaskod3678892020-05-21 10:06:58 +02005408 * @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 +02005409 *
5410 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005411 * @param[in] mod Matching node module, NULL for any.
5412 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005413 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5414 */
5415static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005416moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417{
Michal Vaskof03ed032020-03-04 13:31:44 +01005418 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005419 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005420 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005421 LY_ERR rc;
5422
Michal Vaskod3678892020-05-21 10:06:58 +02005423 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005424 return LY_SUCCESS;
5425 }
5426
5427 if (set->type != LYXP_SET_NODE_SET) {
5428 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5429 return LY_EVALID;
5430 }
5431
Michal Vasko03ff5a72019-09-11 13:49:33 +02005432 for (i = 0; i < set->used; ) {
5433 replaced = 0;
5434
5435 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 +01005436 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005437
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005438 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005439 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005440 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005441 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005442 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005443 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005444
Michal Vaskod3678892020-05-21 10:06:58 +02005445 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005446 rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005447 if (rc == LY_SUCCESS) {
5448 if (!replaced) {
5449 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5450 replaced = 1;
5451 } else {
5452 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005453 }
Michal Vaskod3678892020-05-21 10:06:58 +02005454 ++i;
5455 } else if (rc == LY_EINCOMPLETE) {
5456 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005457 }
5458 }
5459
5460 if (!replaced) {
5461 /* no match */
5462 set_remove_node(set, i);
5463 }
5464 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465
5466 return LY_SUCCESS;
5467}
5468
5469/**
Michal Vaskod3678892020-05-21 10:06:58 +02005470 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5471 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472 *
5473 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005474 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005475 * @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 +02005476 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5477 */
5478static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005479moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005480{
Michal Vasko004d3152020-06-11 19:59:22 +02005481 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005482 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005483 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005484 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005485
Michal Vasko004d3152020-06-11 19:59:22 +02005486 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005487
5488 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005489 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005490 }
5491
5492 if (set->type != LYXP_SET_NODE_SET) {
5493 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 +02005494 ret = LY_EVALID;
5495 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005496 }
5497
5498 /* context check for all the nodes since we have the schema node */
5499 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5500 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005501 goto cleanup;
Michal Vasko6b26e742020-07-17 15:02:10 +02005502 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
5503 && (scnode != set->context_op)) {
5504 lyxp_set_free_content(set);
5505 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005506 }
5507
5508 /* create specific data instance if needed */
5509 if (scnode->nodetype == LYS_LIST) {
5510 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5511 } else if (scnode->nodetype == LYS_LEAFLIST) {
5512 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005513 }
5514
5515 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005516 siblings = NULL;
5517
5518 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5519 assert(!set->val.nodes[i].node);
5520
5521 /* search in all the trees */
5522 siblings = set->tree;
5523 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5524 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005525 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005526 }
5527
5528 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005529 if (inst) {
5530 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005531 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005532 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005533 }
5534
5535 /* when check */
5536 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005537 ret = LY_EINCOMPLETE;
5538 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005539 }
5540
5541 if (sub) {
5542 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005543 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005544 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005545 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005546 /* no match */
5547 set_remove_node(set, i);
5548 }
5549 }
5550
Michal Vasko004d3152020-06-11 19:59:22 +02005551cleanup:
5552 lyd_free_tree(inst);
5553 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005554}
5555
5556/**
5557 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5558 *
5559 * @param[in,out] set Set to use.
5560 * @param[in] mod Matching node module, NULL for any.
5561 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005562 * @param[in] options XPath options.
5563 * @return LY_ERR
5564 */
5565static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005566moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005567{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005568 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005569 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005570 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005571
Michal Vaskod3678892020-05-21 10:06:58 +02005572 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005573 return LY_SUCCESS;
5574 }
5575
5576 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005577 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 +02005578 return LY_EVALID;
5579 }
5580
Michal Vaskocafad9d2019-11-07 15:20:03 +01005581 /* getnext opts */
5582 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5583 if (options & LYXP_SCNODE_OUTPUT) {
5584 getnext_opts |= LYS_GETNEXT_OUTPUT;
5585 }
5586
Michal Vasko03ff5a72019-09-11 13:49:33 +02005587 orig_used = set->used;
5588 for (i = 0; i < orig_used; ++i) {
5589 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005590 if (set->val.scnodes[i].in_ctx != -2) {
5591 continue;
5592 }
5593
5594 /* remember context node */
5595 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005596 } else {
5597 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005598 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005599
5600 start_parent = set->val.scnodes[i].scnode;
5601
5602 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 +02005603 /* 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 +02005604 * so use it directly (root node itself is useless in this case) */
5605 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005606 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005607 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005608 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005609 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005610 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, mod)) {
Michal Vasko519fd602020-05-26 12:17:39 +02005611 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005612 /* we need to prevent these nodes from being considered in this moveto */
5613 if ((idx < orig_used) && (idx > i)) {
5614 set->val.scnodes[idx].in_ctx = 2;
5615 temp_ctx = 1;
5616 }
5617 }
5618 }
5619
5620 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005621 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005622 break;
5623 }
5624 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005625 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005626 }
5627
Michal Vasko519fd602020-05-26 12:17:39 +02005628 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5629 iter = NULL;
5630 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005631 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005632 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005633 if ((idx < orig_used) && (idx > i)) {
5634 set->val.scnodes[idx].in_ctx = 2;
5635 temp_ctx = 1;
5636 }
5637 }
5638 }
5639 }
5640 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005641
5642 /* correct temporary in_ctx values */
5643 if (temp_ctx) {
5644 for (i = 0; i < orig_used; ++i) {
5645 if (set->val.scnodes[i].in_ctx == 2) {
5646 set->val.scnodes[i].in_ctx = 1;
5647 }
5648 }
5649 }
5650
5651 return LY_SUCCESS;
5652}
5653
5654/**
Michal Vaskod3678892020-05-21 10:06:58 +02005655 * @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 +02005656 * Context position aware.
5657 *
5658 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005659 * @param[in] mod Matching node module, NULL for any.
5660 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005661 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5662 */
5663static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005664moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005665{
5666 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005668 struct lyxp_set ret_set;
5669 LY_ERR rc;
5670
Michal Vaskod3678892020-05-21 10:06:58 +02005671 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005672 return LY_SUCCESS;
5673 }
5674
5675 if (set->type != LYXP_SET_NODE_SET) {
5676 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5677 return LY_EVALID;
5678 }
5679
Michal Vasko9f96a052020-03-10 09:41:45 +01005680 /* 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 +02005681 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682 LY_CHECK_RET(rc);
5683
Michal Vasko6346ece2019-09-24 13:12:53 +02005684 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685 set_init(&ret_set, set);
5686 for (i = 0; i < set->used; ++i) {
5687
5688 /* TREE DFS */
5689 start = set->val.nodes[i].node;
5690 for (elem = next = start; elem; elem = next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005691 rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005692 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 /* add matching node into result set */
5694 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5695 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5696 /* the node is a duplicate, we'll process it later in the set */
5697 goto skip_children;
5698 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005699 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005700 return rc;
5701 } else if (rc == LY_EINVAL) {
5702 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703 }
5704
5705 /* TREE DFS NEXT ELEM */
5706 /* select element for the next run - children first */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005707 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005708 if (!next) {
5709skip_children:
5710 /* no children, so try siblings, but only if it's not the start,
5711 * that is considered to be the root and it's siblings are not traversed */
5712 if (elem != start) {
5713 next = elem->next;
5714 } else {
5715 break;
5716 }
5717 }
5718 while (!next) {
5719 /* no siblings, go back through the parents */
5720 if ((struct lyd_node *)elem->parent == start) {
5721 /* we are done, no next element to process */
5722 break;
5723 }
5724 /* parent is already processed, go to its sibling */
5725 elem = (struct lyd_node *)elem->parent;
5726 next = elem->next;
5727 }
5728 }
5729 }
5730
5731 /* make the temporary set the current one */
5732 ret_set.ctx_pos = set->ctx_pos;
5733 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005734 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005735 memcpy(set, &ret_set, sizeof *set);
5736
5737 return LY_SUCCESS;
5738}
5739
5740/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005741 * @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 +02005742 *
5743 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005744 * @param[in] mod Matching node module, NULL for any.
5745 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 * @param[in] options XPath options.
5747 * @return LY_ERR
5748 */
5749static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005750moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005751{
Michal Vasko6346ece2019-09-24 13:12:53 +02005752 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005753 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005754 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755
Michal Vaskod3678892020-05-21 10:06:58 +02005756 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005757 return LY_SUCCESS;
5758 }
5759
5760 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005761 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 +02005762 return LY_EVALID;
5763 }
5764
Michal Vasko03ff5a72019-09-11 13:49:33 +02005765 orig_used = set->used;
5766 for (i = 0; i < orig_used; ++i) {
5767 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005768 if (set->val.scnodes[i].in_ctx != -2) {
5769 continue;
5770 }
5771
5772 /* remember context node */
5773 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005774 } else {
5775 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005776 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005777
5778 /* TREE DFS */
5779 start = set->val.scnodes[i].scnode;
5780 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005781 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5782 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005783 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 }
5785
Michal Vasko6b26e742020-07-17 15:02:10 +02005786 rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005787 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005788 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789 set->val.scnodes[idx].in_ctx = 1;
5790 if (idx > i) {
5791 /* we will process it later in the set */
5792 goto skip_children;
5793 }
5794 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005795 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005796 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005797 } else if (rc == LY_EINVAL) {
5798 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005799 }
5800
5801next_iter:
5802 /* TREE DFS NEXT ELEM */
5803 /* select element for the next run - children first */
5804 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005805 if (!next) {
5806skip_children:
5807 /* no children, so try siblings, but only if it's not the start,
5808 * that is considered to be the root and it's siblings are not traversed */
5809 if (elem != start) {
5810 next = elem->next;
5811 } else {
5812 break;
5813 }
5814 }
5815 while (!next) {
5816 /* no siblings, go back through the parents */
5817 if (elem->parent == start) {
5818 /* we are done, no next element to process */
5819 break;
5820 }
5821 /* parent is already processed, go to its sibling */
5822 elem = elem->parent;
5823 next = elem->next;
5824 }
5825 }
5826 }
5827
5828 return LY_SUCCESS;
5829}
5830
5831/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005832 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833 * Indirectly context position aware.
5834 *
5835 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005836 * @param[in] mod Matching metadata module, NULL for any.
5837 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 * @return LY_ERR
5839 */
5840static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005841moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005842{
5843 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005844 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005845 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846
Michal Vaskod3678892020-05-21 10:06:58 +02005847 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 return LY_SUCCESS;
5849 }
5850
5851 if (set->type != LYXP_SET_NODE_SET) {
5852 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5853 return LY_EVALID;
5854 }
5855
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 for (i = 0; i < set->used; ) {
5857 replaced = 0;
5858
5859 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5860 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005861 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005862 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005863
5864 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005865 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 continue;
5867 }
5868
Michal Vaskod3678892020-05-21 10:06:58 +02005869 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005870 /* match */
5871 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005872 set->val.meta[i].meta = sub;
5873 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874 /* pos does not change */
5875 replaced = 1;
5876 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005877 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 +02005878 }
5879 ++i;
5880 }
5881 }
5882 }
5883
5884 if (!replaced) {
5885 /* no match */
5886 set_remove_node(set, i);
5887 }
5888 }
5889
5890 return LY_SUCCESS;
5891}
5892
5893/**
5894 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005895 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896 *
5897 * @param[in,out] set1 Set to use for the result.
5898 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899 * @return LY_ERR
5900 */
5901static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005902moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903{
5904 LY_ERR rc;
5905
Michal Vaskod3678892020-05-21 10:06:58 +02005906 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005907 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5908 return LY_EVALID;
5909 }
5910
5911 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005912 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 return LY_SUCCESS;
5914 }
5915
Michal Vaskod3678892020-05-21 10:06:58 +02005916 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917 memcpy(set1, set2, sizeof *set1);
5918 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005919 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005920 return LY_SUCCESS;
5921 }
5922
5923 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005924 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925
5926 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005927 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928 LY_CHECK_RET(rc);
5929
5930 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005931 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005932
5933 return LY_SUCCESS;
5934}
5935
5936/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005937 * @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 +02005938 * Context position aware.
5939 *
5940 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005941 * @param[in] mod Matching metadata module, NULL for any.
5942 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5944 */
5945static int
Michal Vaskod3678892020-05-21 10:06:58 +02005946moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005947{
5948 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005949 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005950 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005951 struct lyxp_set *set_all_desc = NULL;
5952 LY_ERR rc;
5953
Michal Vaskod3678892020-05-21 10:06:58 +02005954 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005955 return LY_SUCCESS;
5956 }
5957
5958 if (set->type != LYXP_SET_NODE_SET) {
5959 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5960 return LY_EVALID;
5961 }
5962
Michal Vasko03ff5a72019-09-11 13:49:33 +02005963 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5964 * but it likely won't be used much, so it's a waste of time */
5965 /* copy the context */
5966 set_all_desc = set_copy(set);
5967 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005968 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969 if (rc != LY_SUCCESS) {
5970 lyxp_set_free(set_all_desc);
5971 return rc;
5972 }
5973 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005974 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005975 if (rc != LY_SUCCESS) {
5976 lyxp_set_free(set_all_desc);
5977 return rc;
5978 }
5979 lyxp_set_free(set_all_desc);
5980
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981 for (i = 0; i < set->used; ) {
5982 replaced = 0;
5983
5984 /* only attributes of an elem can be in the result, skip all the rest,
5985 * we have all attributes qualified in lyd tree */
5986 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005987 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005988 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005989 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990 continue;
5991 }
5992
Michal Vaskod3678892020-05-21 10:06:58 +02005993 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994 /* match */
5995 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005996 set->val.meta[i].meta = sub;
5997 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 /* pos does not change */
5999 replaced = 1;
6000 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006001 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 +02006002 }
6003 ++i;
6004 }
6005 }
6006 }
6007
6008 if (!replaced) {
6009 /* no match */
6010 set_remove_node(set, i);
6011 }
6012 }
6013
6014 return LY_SUCCESS;
6015}
6016
6017/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006018 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6019 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 *
6021 * @param[in] parent Current parent.
6022 * @param[in] parent_pos Position of @p parent.
6023 * @param[in] parent_type Node type of @p parent.
6024 * @param[in,out] to_set Set to use.
6025 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006026 * @param[in] options XPath options.
6027 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6028 */
6029static LY_ERR
6030moveto_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 +01006031 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006033 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034 LY_ERR rc;
6035
6036 switch (parent_type) {
6037 case LYXP_NODE_ROOT:
6038 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006039 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040
Michal Vasko61ac2f62020-05-25 12:39:51 +02006041 /* add all top-level nodes as elements */
6042 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 break;
6044 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006045 /* add just the text node of this term element node */
6046 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6048 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6049 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006050 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006052
6053 /* add all the children of this node */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02006054 first = lyd_node_children(parent, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006055 break;
6056 default:
6057 LOGINT_RET(parent->schema->module->ctx);
6058 }
6059
Michal Vasko61ac2f62020-05-25 12:39:51 +02006060 /* add all top-level nodes as elements */
6061 LY_LIST_FOR(first, iter) {
6062 /* context check */
6063 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6064 continue;
6065 }
6066
6067 /* when check */
6068 if (moveto_when_check(iter)) {
6069 return LY_EINCOMPLETE;
6070 }
6071
6072 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6073 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6074
6075 /* also add all the children of this node, recursively */
6076 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6077 LY_CHECK_RET(rc);
6078 }
6079 }
6080
Michal Vasko03ff5a72019-09-11 13:49:33 +02006081 return LY_SUCCESS;
6082}
6083
6084/**
6085 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6086 * (or LYXP_SET_EMPTY). Context position aware.
6087 *
6088 * @param[in,out] set Set to use.
6089 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6090 * @param[in] options XPath options.
6091 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6092 */
6093static LY_ERR
6094moveto_self(struct lyxp_set *set, int all_desc, int options)
6095{
6096 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097 struct lyxp_set ret_set;
6098 LY_ERR rc;
6099
Michal Vaskod3678892020-05-21 10:06:58 +02006100 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101 return LY_SUCCESS;
6102 }
6103
6104 if (set->type != LYXP_SET_NODE_SET) {
6105 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6106 return LY_EVALID;
6107 }
6108
6109 /* nothing to do */
6110 if (!all_desc) {
6111 return LY_SUCCESS;
6112 }
6113
Michal Vasko03ff5a72019-09-11 13:49:33 +02006114 /* add all the children, they get added recursively */
6115 set_init(&ret_set, set);
6116 for (i = 0; i < set->used; ++i) {
6117 /* copy the current node to tmp */
6118 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6119
6120 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006121 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006122 continue;
6123 }
6124
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 /* add all the children */
6126 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 +01006127 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006128 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006129 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006130 return rc;
6131 }
6132 }
6133
6134 /* use the temporary set as the current one */
6135 ret_set.ctx_pos = set->ctx_pos;
6136 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006137 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 memcpy(set, &ret_set, sizeof *set);
6139
6140 return LY_SUCCESS;
6141}
6142
6143/**
6144 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6145 * (or LYXP_SET_EMPTY).
6146 *
6147 * @param[in,out] set Set to use.
6148 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6149 * @param[in] options XPath options.
6150 * @return LY_ERR
6151 */
6152static LY_ERR
6153moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6154{
Michal Vasko519fd602020-05-26 12:17:39 +02006155 int getnext_opts;
6156 uint32_t i, mod_idx;
6157 const struct lysc_node *iter, *start_parent;
6158 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006159
Michal Vaskod3678892020-05-21 10:06:58 +02006160 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006161 return LY_SUCCESS;
6162 }
6163
6164 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006165 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 +02006166 return LY_EVALID;
6167 }
6168
6169 /* nothing to do */
6170 if (!all_desc) {
6171 return LY_SUCCESS;
6172 }
6173
Michal Vasko519fd602020-05-26 12:17:39 +02006174 /* getnext opts */
6175 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6176 if (options & LYXP_SCNODE_OUTPUT) {
6177 getnext_opts |= LYS_GETNEXT_OUTPUT;
6178 }
6179
6180 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006181 for (i = 0; i < set->used; ++i) {
6182 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006183 if (set->val.scnodes[i].in_ctx != -2) {
6184 continue;
6185 }
6186
Michal Vasko519fd602020-05-26 12:17:39 +02006187 /* remember context node */
6188 set->val.scnodes[i].in_ctx = -1;
6189 } else {
6190 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006191 }
6192
Michal Vasko519fd602020-05-26 12:17:39 +02006193 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006194
Michal Vasko519fd602020-05-26 12:17:39 +02006195 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6196 /* it can actually be in any module, it's all <running> */
6197 mod_idx = 0;
6198 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6199 iter = NULL;
6200 /* module may not be implemented */
6201 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6202 /* context check */
6203 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6204 continue;
6205 }
6206
6207 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6208 /* throw away the insert index, we want to consider that node again, recursively */
6209 }
6210 }
6211
6212 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6213 iter = NULL;
6214 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006215 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006216 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006217 continue;
6218 }
6219
Michal Vasko519fd602020-05-26 12:17:39 +02006220 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006221 }
6222 }
6223 }
6224
6225 return LY_SUCCESS;
6226}
6227
6228/**
6229 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6230 * (or LYXP_SET_EMPTY). Context position aware.
6231 *
6232 * @param[in] set Set to use.
6233 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6234 * @param[in] options XPath options.
6235 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6236 */
6237static LY_ERR
6238moveto_parent(struct lyxp_set *set, int all_desc, int options)
6239{
6240 LY_ERR rc;
6241 uint32_t i;
6242 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006243 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244
Michal Vaskod3678892020-05-21 10:06:58 +02006245 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006246 return LY_SUCCESS;
6247 }
6248
6249 if (set->type != LYXP_SET_NODE_SET) {
6250 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6251 return LY_EVALID;
6252 }
6253
6254 if (all_desc) {
6255 /* <path>//.. == <path>//./.. */
6256 rc = moveto_self(set, 1, options);
6257 LY_CHECK_RET(rc);
6258 }
6259
Michal Vasko57eab132019-09-24 11:46:26 +02006260 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006261 node = set->val.nodes[i].node;
6262
6263 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6264 new_node = (struct lyd_node *)node->parent;
6265 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6266 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006267 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6268 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006269 if (!new_node) {
6270 LOGINT_RET(set->ctx);
6271 }
6272 } else {
6273 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006274 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006275 continue;
6276 }
6277
Michal Vaskoa1424542019-11-14 16:08:52 +01006278 /* when check */
6279 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006280 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006281 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006282
6283 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006284 if (!new_node) {
6285 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006286
6287 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6288 } else {
6289 new_type = LYXP_NODE_ELEM;
6290 }
6291
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006293 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006294 } else {
6295 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006296 }
6297 }
6298
Michal Vasko2caefc12019-11-14 16:07:56 +01006299 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006300 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006301
6302 return LY_SUCCESS;
6303}
6304
6305/**
6306 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6307 * (or LYXP_SET_EMPTY).
6308 *
6309 * @param[in] set Set to use.
6310 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6311 * @param[in] options XPath options.
6312 * @return LY_ERR
6313 */
6314static LY_ERR
6315moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6316{
6317 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006318 const struct lysc_node *node, *new_node;
6319 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320 LY_ERR rc;
6321
Michal Vaskod3678892020-05-21 10:06:58 +02006322 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006323 return LY_SUCCESS;
6324 }
6325
6326 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006327 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 +02006328 return LY_EVALID;
6329 }
6330
6331 if (all_desc) {
6332 /* <path>//.. == <path>//./.. */
6333 rc = moveto_scnode_self(set, 1, options);
6334 LY_CHECK_RET(rc);
6335 }
6336
Michal Vasko03ff5a72019-09-11 13:49:33 +02006337 orig_used = set->used;
6338 for (i = 0; i < orig_used; ++i) {
6339 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006340 if (set->val.scnodes[i].in_ctx != -2) {
6341 continue;
6342 }
6343
6344 /* remember context node */
6345 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006346 } else {
6347 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006348 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349
6350 node = set->val.scnodes[i].scnode;
6351
6352 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006353 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006354 } else {
6355 /* root does not have a parent */
6356 continue;
6357 }
6358
Michal Vasko03ff5a72019-09-11 13:49:33 +02006359 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006360 if (!new_node) {
6361 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362
6363 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6364 } else {
6365 new_type = LYXP_NODE_ELEM;
6366 }
6367
Michal Vaskoecd62de2019-11-13 12:35:11 +01006368 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006369 if ((idx < orig_used) && (idx > i)) {
6370 set->val.scnodes[idx].in_ctx = 2;
6371 temp_ctx = 1;
6372 }
6373 }
6374
6375 if (temp_ctx) {
6376 for (i = 0; i < orig_used; ++i) {
6377 if (set->val.scnodes[i].in_ctx == 2) {
6378 set->val.scnodes[i].in_ctx = 1;
6379 }
6380 }
6381 }
6382
6383 return LY_SUCCESS;
6384}
6385
6386/**
6387 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6388 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6389 *
6390 * @param[in,out] set1 Set to use for the result.
6391 * @param[in] set2 Set acting as the second operand for @p op.
6392 * @param[in] op Comparison operator to process.
6393 * @param[in] options XPath options.
6394 * @return LY_ERR
6395 */
6396static LY_ERR
6397moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6398{
6399 /*
6400 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6401 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6402 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6403 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6404 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6405 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6406 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6407 *
6408 * '=' or '!='
6409 * BOOLEAN + BOOLEAN
6410 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6411 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6412 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6413 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6414 * NUMBER + NUMBER
6415 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6416 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6417 * STRING + STRING
6418 *
6419 * '<=', '<', '>=', '>'
6420 * NUMBER + NUMBER
6421 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6422 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6423 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6424 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6425 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6426 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6427 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6428 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6429 */
6430 struct lyxp_set iter1, iter2;
6431 int result;
6432 int64_t i;
6433 LY_ERR rc;
6434
Michal Vasko004d3152020-06-11 19:59:22 +02006435 memset(&iter1, 0, sizeof iter1);
6436 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006437
6438 /* iterative evaluation with node-sets */
6439 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6440 if (set1->type == LYXP_SET_NODE_SET) {
6441 for (i = 0; i < set1->used; ++i) {
6442 switch (set2->type) {
6443 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006444 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006445 break;
6446 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006447 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006448 break;
6449 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006450 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006451 break;
6452 }
6453 LY_CHECK_RET(rc);
6454
6455 rc = moveto_op_comp(&iter1, set2, op, options);
6456 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006457 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006458 return rc;
6459 }
6460
6461 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006462 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006463 set_fill_boolean(set1, 1);
6464 return LY_SUCCESS;
6465 }
6466 }
6467 } else {
6468 for (i = 0; i < set2->used; ++i) {
6469 switch (set1->type) {
6470 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006471 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472 break;
6473 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006474 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006475 break;
6476 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006477 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478 break;
6479 }
6480 LY_CHECK_RET(rc);
6481
6482 set_fill_set(&iter1, set1);
6483
6484 rc = moveto_op_comp(&iter1, &iter2, op, options);
6485 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006486 lyxp_set_free_content(&iter1);
6487 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006488 return rc;
6489 }
Michal Vaskod3678892020-05-21 10:06:58 +02006490 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491
6492 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006493 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006494 set_fill_boolean(set1, 1);
6495 return LY_SUCCESS;
6496 }
6497 }
6498 }
6499
6500 /* false for all nodes */
6501 set_fill_boolean(set1, 0);
6502 return LY_SUCCESS;
6503 }
6504
6505 /* first convert properly */
6506 if ((op[0] == '=') || (op[0] == '!')) {
6507 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006508 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6509 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006510 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006511 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006512 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006513 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006514 LY_CHECK_RET(rc);
6515 } /* else we have 2 strings */
6516 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006517 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006518 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006519 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006520 LY_CHECK_RET(rc);
6521 }
6522
6523 assert(set1->type == set2->type);
6524
6525 /* compute result */
6526 if (op[0] == '=') {
6527 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006528 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006529 } else if (set1->type == LYXP_SET_NUMBER) {
6530 result = (set1->val.num == set2->val.num);
6531 } else {
6532 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006533 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006534 }
6535 } else if (op[0] == '!') {
6536 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006537 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006538 } else if (set1->type == LYXP_SET_NUMBER) {
6539 result = (set1->val.num != set2->val.num);
6540 } else {
6541 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006542 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006543 }
6544 } else {
6545 assert(set1->type == LYXP_SET_NUMBER);
6546 if (op[0] == '<') {
6547 if (op[1] == '=') {
6548 result = (set1->val.num <= set2->val.num);
6549 } else {
6550 result = (set1->val.num < set2->val.num);
6551 }
6552 } else {
6553 if (op[1] == '=') {
6554 result = (set1->val.num >= set2->val.num);
6555 } else {
6556 result = (set1->val.num > set2->val.num);
6557 }
6558 }
6559 }
6560
6561 /* assign result */
6562 if (result) {
6563 set_fill_boolean(set1, 1);
6564 } else {
6565 set_fill_boolean(set1, 0);
6566 }
6567
6568 return LY_SUCCESS;
6569}
6570
6571/**
6572 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6573 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6574 *
6575 * @param[in,out] set1 Set to use for the result.
6576 * @param[in] set2 Set acting as the second operand for @p op.
6577 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006578 * @return LY_ERR
6579 */
6580static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006581moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582{
6583 LY_ERR rc;
6584
6585 /* unary '-' */
6586 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006587 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006588 LY_CHECK_RET(rc);
6589 set1->val.num *= -1;
6590 lyxp_set_free(set2);
6591 return LY_SUCCESS;
6592 }
6593
6594 assert(set1 && set2);
6595
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006596 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006597 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006598 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006599 LY_CHECK_RET(rc);
6600
6601 switch (op[0]) {
6602 /* '+' */
6603 case '+':
6604 set1->val.num += set2->val.num;
6605 break;
6606
6607 /* '-' */
6608 case '-':
6609 set1->val.num -= set2->val.num;
6610 break;
6611
6612 /* '*' */
6613 case '*':
6614 set1->val.num *= set2->val.num;
6615 break;
6616
6617 /* 'div' */
6618 case 'd':
6619 set1->val.num /= set2->val.num;
6620 break;
6621
6622 /* 'mod' */
6623 case 'm':
6624 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6625 break;
6626
6627 default:
6628 LOGINT_RET(set1->ctx);
6629 }
6630
6631 return LY_SUCCESS;
6632}
6633
6634/*
6635 * eval functions
6636 *
6637 * They execute a parsed XPath expression on some data subtree.
6638 */
6639
6640/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006641 * @brief Evaluate Predicate. Logs directly on error.
6642 *
Michal Vaskod3678892020-05-21 10:06:58 +02006643 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006644 *
6645 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006646 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006647 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6648 * @param[in] options XPath options.
6649 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6650 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6651 */
6652static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006653eval_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 +02006654{
6655 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006656 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006657 uint32_t orig_pos, orig_size;
6658 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659 struct lyxp_set set2;
6660 struct lyd_node *orig_parent;
6661
6662 /* '[' */
6663 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006664 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6665 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006666
6667 if (!set) {
6668only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006669 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006670 LY_CHECK_RET(rc);
6671 } else if (set->type == LYXP_SET_NODE_SET) {
6672 /* 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 +01006673 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006674
6675 /* empty set, nothing to evaluate */
6676 if (!set->used) {
6677 goto only_parse;
6678 }
6679
Michal Vasko004d3152020-06-11 19:59:22 +02006680 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006681 orig_pos = 0;
6682 orig_size = set->used;
6683 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006684 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006685 set_init(&set2, set);
6686 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6687 /* remember the node context position for position() and context size for last(),
6688 * predicates should always be evaluated with respect to the child axis (since we do
6689 * not support explicit axes) so we assign positions based on their parents */
6690 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6691 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6692 orig_pos = 1;
6693 } else {
6694 ++orig_pos;
6695 }
6696
6697 set2.ctx_pos = orig_pos;
6698 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006699 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700
Michal Vasko004d3152020-06-11 19:59:22 +02006701 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006702 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006703 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006704 return rc;
6705 }
6706
6707 /* number is a position */
6708 if (set2.type == LYXP_SET_NUMBER) {
6709 if ((long long)set2.val.num == orig_pos) {
6710 set2.val.num = 1;
6711 } else {
6712 set2.val.num = 0;
6713 }
6714 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006715 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006716
6717 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006718 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006719 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006720 }
6721 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006722 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006723
6724 } else if (set->type == LYXP_SET_SCNODE_SET) {
6725 for (i = 0; i < set->used; ++i) {
6726 if (set->val.scnodes[i].in_ctx == 1) {
6727 /* there is a currently-valid node */
6728 break;
6729 }
6730 }
6731 /* empty set, nothing to evaluate */
6732 if (i == set->used) {
6733 goto only_parse;
6734 }
6735
Michal Vasko004d3152020-06-11 19:59:22 +02006736 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737
Michal Vasko03ff5a72019-09-11 13:49:33 +02006738 /* set special in_ctx to all the valid snodes */
6739 pred_in_ctx = set_scnode_new_in_ctx(set);
6740
6741 /* use the valid snodes one-by-one */
6742 for (i = 0; i < set->used; ++i) {
6743 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6744 continue;
6745 }
6746 set->val.scnodes[i].in_ctx = 1;
6747
Michal Vasko004d3152020-06-11 19:59:22 +02006748 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006749
Michal Vasko004d3152020-06-11 19:59:22 +02006750 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006751 LY_CHECK_RET(rc);
6752
6753 set->val.scnodes[i].in_ctx = pred_in_ctx;
6754 }
6755
6756 /* restore the state as it was before the predicate */
6757 for (i = 0; i < set->used; ++i) {
6758 if (set->val.scnodes[i].in_ctx == 1) {
6759 set->val.scnodes[i].in_ctx = 0;
6760 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6761 set->val.scnodes[i].in_ctx = 1;
6762 }
6763 }
6764
6765 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006766 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006767 set_fill_set(&set2, set);
6768
Michal Vasko004d3152020-06-11 19:59:22 +02006769 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006770 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006771 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772 return rc;
6773 }
6774
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006775 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006776 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006777 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006778 }
Michal Vaskod3678892020-05-21 10:06:58 +02006779 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780 }
6781
6782 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006783 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006784 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006785 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6786 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787
6788 return LY_SUCCESS;
6789}
6790
6791/**
Michal Vaskod3678892020-05-21 10:06:58 +02006792 * @brief Evaluate Literal. Logs directly on error.
6793 *
6794 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006795 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006796 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6797 */
6798static void
Michal Vasko004d3152020-06-11 19:59:22 +02006799eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006800{
6801 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006802 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006803 set_fill_string(set, "", 0);
6804 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006805 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 +02006806 }
6807 }
6808 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006809 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6810 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006811}
6812
6813/**
Michal Vasko004d3152020-06-11 19:59:22 +02006814 * @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 +02006815 *
Michal Vasko004d3152020-06-11 19:59:22 +02006816 * @param[in] exp Full parsed XPath expression.
6817 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6818 * @param[in] scnode Found schema node as context for the predicate.
6819 * @param[in] format Format of any prefixes in key names/values.
6820 * @param[out] predicates Parsed predicates.
6821 * @param[out] pred_type Type of @p predicates.
6822 * @return LY_SUCCESS on success,
6823 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006824 */
6825static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006826eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode,
6827 LYD_FORMAT format, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006828{
Michal Vasko004d3152020-06-11 19:59:22 +02006829 LY_ERR ret = LY_SUCCESS;
6830 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006831 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006832 size_t pred_len;
6833 int prev_lo;
6834 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006835
Michal Vasko004d3152020-06-11 19:59:22 +02006836 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006837
Michal Vasko004d3152020-06-11 19:59:22 +02006838 if (scnode->nodetype == LYS_LIST) {
6839 /* get key count */
6840 if (scnode->flags & LYS_KEYLESS) {
6841 return LY_EINVAL;
6842 }
6843 for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count);
6844 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006845
Michal Vasko004d3152020-06-11 19:59:22 +02006846 /* learn where the predicates end */
6847 e_idx = *tok_idx;
6848 while (key_count) {
6849 /* '[' */
6850 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6851 return LY_EINVAL;
6852 }
6853 ++e_idx;
6854
6855 /* ']' */
6856 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6857 ++e_idx;
6858 }
6859 ++e_idx;
6860
6861 /* another presumably key predicate parsed */
6862 --key_count;
6863 }
Michal Vasko004d3152020-06-11 19:59:22 +02006864 } else {
6865 /* learn just where this single predicate ends */
6866 e_idx = *tok_idx;
6867
Michal Vaskod3678892020-05-21 10:06:58 +02006868 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006869 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6870 return LY_EINVAL;
6871 }
6872 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006873
6874 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006875 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6876 ++e_idx;
6877 }
6878 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006879 }
6880
Michal Vasko004d3152020-06-11 19:59:22 +02006881 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6882 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6883
6884 /* turn logging off */
6885 prev_lo = ly_log_options(0);
6886
6887 /* parse the predicate(s) */
Michal Vasko6b26e742020-07-17 15:02:10 +02006888 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 +02006889 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6890
6891 /* compile */
6892 switch (format) {
6893 case LYD_SCHEMA:
Michal Vasko6b26e742020-07-17 15:02:10 +02006894 ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx,
6895 lys_resolve_prefix, scnode->module, LYD_SCHEMA, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006896 break;
6897 case LYD_JSON:
Michal Vasko6b26e742020-07-17 15:02:10 +02006898 ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx,
Michal Vasko00cbf532020-06-15 13:58:47 +02006899 lydjson_resolve_prefix, NULL, LYD_JSON, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006900 break;
6901 case LYD_XML:
Michal Vasko60ea6352020-06-29 13:39:39 +02006902 case LYD_LYB:
Michal Vasko004d3152020-06-11 19:59:22 +02006903 ret = LY_EINT;
6904 goto cleanup;
6905 }
6906 LY_CHECK_GOTO(ret, cleanup);
6907
6908 /* success, the predicate must include all the needed information for hash-based search */
6909 *tok_idx = e_idx;
6910
6911cleanup:
6912 ly_log_options(prev_lo);
6913 lyxp_expr_free(scnode->module->ctx, exp2);
6914 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006915}
6916
6917/**
6918 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6919 *
6920 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6921 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6922 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6923 *
6924 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006925 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006926 * @param[in] attr_axis Whether to search attributes or standard nodes.
6927 * @param[in] all_desc Whether to search all the descendants or children only.
6928 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6929 * @param[in] options XPath options.
6930 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6931 */
6932static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006933eval_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 +02006934 int options)
6935{
6936 int i;
6937 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006938 const char *ncname, *ncname_dict = NULL;
6939 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006940 const struct lys_module *moveto_mod;
6941 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006942 struct ly_path_predicate *predicates = NULL;
6943 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006944 LY_ERR rc = LY_SUCCESS;
6945
6946 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006947 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6948 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006949
6950 if (!set) {
6951 goto moveto;
6952 }
6953
Michal Vasko004d3152020-06-11 19:59:22 +02006954 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6955 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006956
6957 /* parse (and skip) module name */
6958 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6959 LY_CHECK_GOTO(rc, cleanup);
6960
6961 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6962 /* find the matching schema node in some parent in the context */
6963 for (i = 0; i < (signed)set->used; ++i) {
6964 if (set->val.nodes[i].type == set->root_type) {
6965 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6966 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6967 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6968 /* do not repeat the same search */
6969 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
Radek Krejcib1247842020-07-02 16:22:38 +02006970 } else {
6971 tmp = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006972 }
6973
6974 /* additional context check */
6975 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6976 tmp = NULL;
6977 }
6978
6979 if (tmp) {
6980 if (scnode) {
6981 /* we found a schema node with the same name but at different level, give up, too complicated */
6982 scnode = NULL;
6983 break;
6984 } else {
6985 /* remember the found schema node and continue to make sure it can be used */
6986 scnode = tmp;
6987 }
Michal Vaskod3678892020-05-21 10:06:58 +02006988 }
6989 }
6990
Michal Vasko004d3152020-06-11 19:59:22 +02006991 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
6992 /* try to create the predicates */
6993 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
6994 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02006995 scnode = NULL;
6996 }
6997 }
6998 }
6999
Michal Vasko004d3152020-06-11 19:59:22 +02007000 if (!scnode && moveto_mod) {
7001 /* we are not able to match based on a schema node and not all the modules match,
7002 * use dictionary for efficient comparison */
7003 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02007004 }
7005
7006moveto:
7007 /* move to the attribute(s), data node(s), or schema node(s) */
7008 if (attr_axis) {
7009 if (set && (options & LYXP_SCNODE_ALL)) {
7010 set_scnode_clear_ctx(set);
7011 } else {
7012 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007013 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007014 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007015 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007016 }
7017 LY_CHECK_GOTO(rc, cleanup);
7018 }
7019 } else {
7020 if (set && (options & LYXP_SCNODE_ALL)) {
7021 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007022 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007023 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007024 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007025 }
7026 LY_CHECK_GOTO(rc, cleanup);
7027
7028 for (i = set->used - 1; i > -1; --i) {
7029 if (set->val.scnodes[i].in_ctx > 0) {
7030 break;
7031 }
7032 }
7033 if (i == -1) {
7034 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7035 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko6b26e742020-07-17 15:02:10 +02007036 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007037 free(path);
7038 }
7039 } else {
7040 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007041 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007042 } else {
7043 if (scnode) {
7044 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007045 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007046 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007047 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007048 }
7049 }
7050 LY_CHECK_GOTO(rc, cleanup);
7051 }
7052 }
7053
7054 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007055 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7056 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007057 LY_CHECK_RET(rc);
7058 }
7059
7060cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007061 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007062 lydict_remove(set->ctx, ncname_dict);
7063 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007064 }
Michal Vaskod3678892020-05-21 10:06:58 +02007065 return rc;
7066}
7067
7068/**
7069 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7070 *
7071 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7072 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7073 * [8] NodeType ::= 'text' | 'node'
7074 *
7075 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007076 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007077 * @param[in] attr_axis Whether to search attributes or standard nodes.
7078 * @param[in] all_desc Whether to search all the descendants or children only.
7079 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7080 * @param[in] options XPath options.
7081 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7082 */
7083static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007084eval_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 +02007085 struct lyxp_set *set, int options)
7086{
7087 LY_ERR rc;
7088
7089 /* TODO */
7090 (void)attr_axis;
7091 (void)all_desc;
7092
7093 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007094 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007095 if (set->type == LYXP_SET_SCNODE_SET) {
7096 set_scnode_clear_ctx(set);
7097 /* just for the debug message below */
7098 set = NULL;
7099 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007100 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007101 rc = xpath_node(NULL, 0, set, options);
7102 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007103 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007104 rc = xpath_text(NULL, 0, set, options);
7105 }
7106 LY_CHECK_RET(rc);
7107 }
7108 }
7109 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007110 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7111 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007112
7113 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007114 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007115 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007116 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7117 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007118
7119 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007120 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007121 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007122 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7123 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007124
7125 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007126 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7127 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007128 LY_CHECK_RET(rc);
7129 }
7130
7131 return LY_SUCCESS;
7132}
7133
7134/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007135 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7136 *
7137 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7138 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007139 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007140 *
7141 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007142 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007143 * @param[in] all_desc Whether to search all the descendants or children only.
7144 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7145 * @param[in] options XPath options.
7146 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7147 */
7148static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007149eval_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 +02007150{
7151 int attr_axis;
7152 LY_ERR rc;
7153
7154 goto step;
7155 do {
7156 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007157 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007158 all_desc = 0;
7159 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007160 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007161 all_desc = 1;
7162 }
7163 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007164 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7165 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007166
7167step:
Michal Vaskod3678892020-05-21 10:06:58 +02007168 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007169 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007170 attr_axis = 1;
7171
7172 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007173 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7174 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007175 } else {
7176 attr_axis = 0;
7177 }
7178
Michal Vasko03ff5a72019-09-11 13:49:33 +02007179 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007180 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007181 case LYXP_TOKEN_DOT:
7182 /* evaluate '.' */
7183 if (set && (options & LYXP_SCNODE_ALL)) {
7184 rc = moveto_scnode_self(set, all_desc, options);
7185 } else {
7186 rc = moveto_self(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
7194 case LYXP_TOKEN_DDOT:
7195 /* evaluate '..' */
7196 if (set && (options & LYXP_SCNODE_ALL)) {
7197 rc = moveto_scnode_parent(set, all_desc, options);
7198 } else {
7199 rc = moveto_parent(set, all_desc, options);
7200 }
7201 LY_CHECK_RET(rc);
7202 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007203 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7204 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007205 break;
7206
Michal Vasko03ff5a72019-09-11 13:49:33 +02007207 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007208 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007209 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007210 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007211 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007212
Michal Vaskod3678892020-05-21 10:06:58 +02007213 case LYXP_TOKEN_NODETYPE:
7214 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007215 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007216 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007217 break;
7218
7219 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007220 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007221 }
Michal Vasko004d3152020-06-11 19:59:22 +02007222 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007223
7224 return LY_SUCCESS;
7225}
7226
7227/**
7228 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7229 *
7230 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7231 *
7232 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007233 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007234 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7235 * @param[in] options XPath options.
7236 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7237 */
7238static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007239eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007240{
7241 int all_desc;
7242 LY_ERR rc;
7243
7244 if (set) {
7245 /* no matter what tokens follow, we need to be at the root */
7246 moveto_root(set, options);
7247 }
7248
7249 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007250 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007251 /* evaluate '/' - deferred */
7252 all_desc = 0;
7253 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007254 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7255 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007256
Michal Vasko004d3152020-06-11 19:59:22 +02007257 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007258 return LY_SUCCESS;
7259 }
Michal Vasko004d3152020-06-11 19:59:22 +02007260 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007261 case LYXP_TOKEN_DOT:
7262 case LYXP_TOKEN_DDOT:
7263 case LYXP_TOKEN_AT:
7264 case LYXP_TOKEN_NAMETEST:
7265 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007266 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007267 LY_CHECK_RET(rc);
7268 break;
7269 default:
7270 break;
7271 }
7272
7273 /* '//' RelativeLocationPath */
7274 } else {
7275 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7276 all_desc = 1;
7277 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007278 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7279 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007280
Michal Vasko004d3152020-06-11 19:59:22 +02007281 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007282 LY_CHECK_RET(rc);
7283 }
7284
7285 return LY_SUCCESS;
7286}
7287
7288/**
7289 * @brief Evaluate FunctionCall. Logs directly on error.
7290 *
Michal Vaskod3678892020-05-21 10:06:58 +02007291 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007292 *
7293 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007294 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007295 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7296 * @param[in] options XPath options.
7297 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7298 */
7299static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007300eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007301{
7302 LY_ERR rc;
7303 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007304 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 struct lyxp_set **args = NULL, **args_aux;
7306
7307 if (set) {
7308 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007309 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007310 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007311 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007312 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007313 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007314 xpath_func = &xpath_sum;
7315 }
7316 break;
7317 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007318 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007320 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007321 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007322 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007323 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007324 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007325 xpath_func = &xpath_true;
7326 }
7327 break;
7328 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007329 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007331 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007333 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007335 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007336 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007337 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 xpath_func = &xpath_deref;
7339 }
7340 break;
7341 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007342 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007344 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007346 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347 xpath_func = &xpath_string;
7348 }
7349 break;
7350 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007351 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007353 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007355 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 xpath_func = &xpath_current;
7357 }
7358 break;
7359 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007360 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007362 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007364 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 xpath_func = &xpath_re_match;
7366 }
7367 break;
7368 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007369 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007371 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 xpath_func = &xpath_translate;
7373 }
7374 break;
7375 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007376 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007378 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007380 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 xpath_func = &xpath_bit_is_set;
7382 }
7383 break;
7384 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007385 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007386 xpath_func = &xpath_starts_with;
7387 }
7388 break;
7389 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007390 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 xpath_func = &xpath_derived_from;
7392 }
7393 break;
7394 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007395 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007397 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 xpath_func = &xpath_string_length;
7399 }
7400 break;
7401 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007402 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007404 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 xpath_func = &xpath_substring_after;
7406 }
7407 break;
7408 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007409 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 xpath_func = &xpath_substring_before;
7411 }
7412 break;
7413 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007414 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 xpath_func = &xpath_derived_from_or_self;
7416 }
7417 break;
7418 }
7419
7420 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007421 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 +02007422 return LY_EVALID;
7423 }
7424 }
7425
7426 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007427 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7428 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429
7430 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007431 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007432 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007433 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7434 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435
7436 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007437 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 if (set) {
7439 args = malloc(sizeof *args);
7440 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7441 arg_count = 1;
7442 args[0] = set_copy(set);
7443 if (!args[0]) {
7444 rc = LY_EMEM;
7445 goto cleanup;
7446 }
7447
Michal Vasko004d3152020-06-11 19:59:22 +02007448 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449 LY_CHECK_GOTO(rc, cleanup);
7450 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007451 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 LY_CHECK_GOTO(rc, cleanup);
7453 }
7454 }
Michal Vasko004d3152020-06-11 19:59:22 +02007455 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007457 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7458 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459
7460 if (set) {
7461 ++arg_count;
7462 args_aux = realloc(args, arg_count * sizeof *args);
7463 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7464 args = args_aux;
7465 args[arg_count - 1] = set_copy(set);
7466 if (!args[arg_count - 1]) {
7467 rc = LY_EMEM;
7468 goto cleanup;
7469 }
7470
Michal Vasko004d3152020-06-11 19:59:22 +02007471 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 LY_CHECK_GOTO(rc, cleanup);
7473 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007474 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 LY_CHECK_GOTO(rc, cleanup);
7476 }
7477 }
7478
7479 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007480 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007482 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7483 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484
7485 if (set) {
7486 /* evaluate function */
7487 rc = xpath_func(args, arg_count, set, options);
7488
7489 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 /* merge all nodes from arg evaluations */
7491 for (i = 0; i < arg_count; ++i) {
7492 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007493 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007494 }
7495 }
7496 } else {
7497 rc = LY_SUCCESS;
7498 }
7499
7500cleanup:
7501 for (i = 0; i < arg_count; ++i) {
7502 lyxp_set_free(args[i]);
7503 }
7504 free(args);
7505
7506 return rc;
7507}
7508
7509/**
7510 * @brief Evaluate Number. Logs directly on error.
7511 *
7512 * @param[in] ctx Context for errors.
7513 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007514 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7516 * @return LY_ERR
7517 */
7518static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007519eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520{
7521 long double num;
7522 char *endptr;
7523
7524 if (set) {
7525 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007526 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007528 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 +02007529 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 +02007530 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007532 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7533 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 +02007534 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 +02007535 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 return LY_EVALID;
7537 }
7538
7539 set_fill_number(set, num);
7540 }
7541
7542 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007543 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7544 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 return LY_SUCCESS;
7546}
7547
7548/**
7549 * @brief Evaluate PathExpr. Logs directly on error.
7550 *
Michal Vaskod3678892020-05-21 10:06:58 +02007551 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7553 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7554 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007555 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 *
7557 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007558 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7560 * @param[in] options XPath options.
7561 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7562 */
7563static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007564eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565{
7566 int all_desc, parent_pos_pred;
7567 LY_ERR rc;
7568
Michal Vasko004d3152020-06-11 19:59:22 +02007569 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 case LYXP_TOKEN_PAR1:
7571 /* '(' Expr ')' */
7572
7573 /* '(' */
7574 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007575 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7576 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577
7578 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007579 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007580 LY_CHECK_RET(rc);
7581
7582 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007583 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007585 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7586 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587
7588 parent_pos_pred = 0;
7589 goto predicate;
7590
7591 case LYXP_TOKEN_DOT:
7592 case LYXP_TOKEN_DDOT:
7593 case LYXP_TOKEN_AT:
7594 case LYXP_TOKEN_NAMETEST:
7595 case LYXP_TOKEN_NODETYPE:
7596 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007597 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598 LY_CHECK_RET(rc);
7599 break;
7600
7601 case LYXP_TOKEN_FUNCNAME:
7602 /* FunctionCall */
7603 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007604 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007606 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 }
7608 LY_CHECK_RET(rc);
7609
7610 parent_pos_pred = 1;
7611 goto predicate;
7612
Michal Vasko3e48bf32020-06-01 08:39:07 +02007613 case LYXP_TOKEN_OPER_PATH:
7614 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007616 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007617 LY_CHECK_RET(rc);
7618 break;
7619
7620 case LYXP_TOKEN_LITERAL:
7621 /* Literal */
7622 if (!set || (options & LYXP_SCNODE_ALL)) {
7623 if (set) {
7624 set_scnode_clear_ctx(set);
7625 }
Michal Vasko004d3152020-06-11 19:59:22 +02007626 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007628 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 }
7630
7631 parent_pos_pred = 1;
7632 goto predicate;
7633
7634 case LYXP_TOKEN_NUMBER:
7635 /* Number */
7636 if (!set || (options & LYXP_SCNODE_ALL)) {
7637 if (set) {
7638 set_scnode_clear_ctx(set);
7639 }
Michal Vasko004d3152020-06-11 19:59:22 +02007640 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007642 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007643 }
7644 LY_CHECK_RET(rc);
7645
7646 parent_pos_pred = 1;
7647 goto predicate;
7648
7649 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007650 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7651 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007652 return LY_EVALID;
7653 }
7654
7655 return LY_SUCCESS;
7656
7657predicate:
7658 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007659 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7660 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 LY_CHECK_RET(rc);
7662 }
7663
7664 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007665 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666
7667 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007668 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007669 all_desc = 0;
7670 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007671 all_desc = 1;
7672 }
7673
7674 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007675 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7676 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677
Michal Vasko004d3152020-06-11 19:59:22 +02007678 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 LY_CHECK_RET(rc);
7680 }
7681
7682 return LY_SUCCESS;
7683}
7684
7685/**
7686 * @brief Evaluate UnionExpr. Logs directly on error.
7687 *
Michal Vaskod3678892020-05-21 10:06:58 +02007688 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007689 *
7690 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007691 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007692 * @param[in] repeat How many times this expression is repeated.
7693 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7694 * @param[in] options XPath options.
7695 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7696 */
7697static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007698eval_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 +02007699{
7700 LY_ERR rc = LY_SUCCESS;
7701 struct lyxp_set orig_set, set2;
7702 uint16_t i;
7703
7704 assert(repeat);
7705
7706 set_init(&orig_set, set);
7707 set_init(&set2, set);
7708
7709 set_fill_set(&orig_set, set);
7710
Michal Vasko004d3152020-06-11 19:59:22 +02007711 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007712 LY_CHECK_GOTO(rc, cleanup);
7713
7714 /* ('|' PathExpr)* */
7715 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007716 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007717 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007718 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7719 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007720
7721 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007722 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 LY_CHECK_GOTO(rc, cleanup);
7724 continue;
7725 }
7726
7727 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007728 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007729 LY_CHECK_GOTO(rc, cleanup);
7730
7731 /* eval */
7732 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007733 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007735 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736 LY_CHECK_GOTO(rc, cleanup);
7737 }
7738 }
7739
7740cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007741 lyxp_set_free_content(&orig_set);
7742 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007743 return rc;
7744}
7745
7746/**
7747 * @brief Evaluate UnaryExpr. Logs directly on error.
7748 *
Michal Vaskod3678892020-05-21 10:06:58 +02007749 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 *
7751 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007752 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007753 * @param[in] repeat How many times this expression is repeated.
7754 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7755 * @param[in] options XPath options.
7756 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7757 */
7758static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007759eval_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 +02007760{
7761 LY_ERR rc;
7762 uint16_t this_op, i;
7763
7764 assert(repeat);
7765
7766 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007767 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007769 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 +02007770
7771 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007772 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7773 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 }
7775
Michal Vasko004d3152020-06-11 19:59:22 +02007776 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777 LY_CHECK_RET(rc);
7778
7779 if (set && (repeat % 2)) {
7780 if (options & LYXP_SCNODE_ALL) {
7781 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7782 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007783 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 LY_CHECK_RET(rc);
7785 }
7786 }
7787
7788 return LY_SUCCESS;
7789}
7790
7791/**
7792 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7793 *
Michal Vaskod3678892020-05-21 10:06:58 +02007794 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 * | MultiplicativeExpr '*' UnaryExpr
7796 * | MultiplicativeExpr 'div' UnaryExpr
7797 * | MultiplicativeExpr 'mod' UnaryExpr
7798 *
7799 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007800 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801 * @param[in] repeat How many times this expression is repeated.
7802 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7803 * @param[in] options XPath options.
7804 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7805 */
7806static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007807eval_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 +02007808{
7809 LY_ERR rc;
7810 uint16_t this_op;
7811 struct lyxp_set orig_set, set2;
7812 uint16_t i;
7813
7814 assert(repeat);
7815
7816 set_init(&orig_set, set);
7817 set_init(&set2, set);
7818
7819 set_fill_set(&orig_set, set);
7820
Michal Vasko004d3152020-06-11 19:59:22 +02007821 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 LY_CHECK_GOTO(rc, cleanup);
7823
7824 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7825 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007826 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827
Michal Vasko004d3152020-06-11 19:59:22 +02007828 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007829 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007830 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7831 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007832
7833 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007834 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007835 LY_CHECK_GOTO(rc, cleanup);
7836 continue;
7837 }
7838
7839 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007840 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007841 LY_CHECK_GOTO(rc, cleanup);
7842
7843 /* eval */
7844 if (options & LYXP_SCNODE_ALL) {
7845 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007846 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 set_scnode_clear_ctx(set);
7848 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007849 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850 LY_CHECK_GOTO(rc, cleanup);
7851 }
7852 }
7853
7854cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007855 lyxp_set_free_content(&orig_set);
7856 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007857 return rc;
7858}
7859
7860/**
7861 * @brief Evaluate AdditiveExpr. Logs directly on error.
7862 *
Michal Vaskod3678892020-05-21 10:06:58 +02007863 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864 * | AdditiveExpr '+' MultiplicativeExpr
7865 * | AdditiveExpr '-' MultiplicativeExpr
7866 *
7867 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007868 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007869 * @param[in] repeat How many times this expression is repeated.
7870 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7871 * @param[in] options XPath options.
7872 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7873 */
7874static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007875eval_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 +02007876{
7877 LY_ERR rc;
7878 uint16_t this_op;
7879 struct lyxp_set orig_set, set2;
7880 uint16_t i;
7881
7882 assert(repeat);
7883
7884 set_init(&orig_set, set);
7885 set_init(&set2, set);
7886
7887 set_fill_set(&orig_set, set);
7888
Michal Vasko004d3152020-06-11 19:59:22 +02007889 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007890 LY_CHECK_GOTO(rc, cleanup);
7891
7892 /* ('+' / '-' MultiplicativeExpr)* */
7893 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007894 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007895
Michal Vasko004d3152020-06-11 19:59:22 +02007896 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007898 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7899 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007900
7901 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007902 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007903 LY_CHECK_GOTO(rc, cleanup);
7904 continue;
7905 }
7906
7907 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007908 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007909 LY_CHECK_GOTO(rc, cleanup);
7910
7911 /* eval */
7912 if (options & LYXP_SCNODE_ALL) {
7913 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007914 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007915 set_scnode_clear_ctx(set);
7916 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007917 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918 LY_CHECK_GOTO(rc, cleanup);
7919 }
7920 }
7921
7922cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007923 lyxp_set_free_content(&orig_set);
7924 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 return rc;
7926}
7927
7928/**
7929 * @brief Evaluate RelationalExpr. Logs directly on error.
7930 *
Michal Vaskod3678892020-05-21 10:06:58 +02007931 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932 * | RelationalExpr '<' AdditiveExpr
7933 * | RelationalExpr '>' AdditiveExpr
7934 * | RelationalExpr '<=' AdditiveExpr
7935 * | RelationalExpr '>=' AdditiveExpr
7936 *
7937 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007938 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007939 * @param[in] repeat How many times this expression is repeated.
7940 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7941 * @param[in] options XPath options.
7942 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7943 */
7944static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007945eval_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 +02007946{
7947 LY_ERR rc;
7948 uint16_t this_op;
7949 struct lyxp_set orig_set, set2;
7950 uint16_t i;
7951
7952 assert(repeat);
7953
7954 set_init(&orig_set, set);
7955 set_init(&set2, set);
7956
7957 set_fill_set(&orig_set, set);
7958
Michal Vasko004d3152020-06-11 19:59:22 +02007959 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 LY_CHECK_GOTO(rc, cleanup);
7961
7962 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7963 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007964 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965
Michal Vasko004d3152020-06-11 19:59:22 +02007966 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007967 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007968 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7969 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007970
7971 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007972 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 LY_CHECK_GOTO(rc, cleanup);
7974 continue;
7975 }
7976
7977 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007978 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007979 LY_CHECK_GOTO(rc, cleanup);
7980
7981 /* eval */
7982 if (options & LYXP_SCNODE_ALL) {
7983 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007984 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 set_scnode_clear_ctx(set);
7986 } else {
7987 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7988 LY_CHECK_GOTO(rc, cleanup);
7989 }
7990 }
7991
7992cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007993 lyxp_set_free_content(&orig_set);
7994 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007995 return rc;
7996}
7997
7998/**
7999 * @brief Evaluate EqualityExpr. Logs directly on error.
8000 *
Michal Vaskod3678892020-05-21 10:06:58 +02008001 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008002 * | EqualityExpr '!=' RelationalExpr
8003 *
8004 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008005 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008006 * @param[in] repeat How many times this expression is repeated.
8007 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8008 * @param[in] options XPath options.
8009 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8010 */
8011static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008012eval_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 +02008013{
8014 LY_ERR rc;
8015 uint16_t this_op;
8016 struct lyxp_set orig_set, set2;
8017 uint16_t i;
8018
8019 assert(repeat);
8020
8021 set_init(&orig_set, set);
8022 set_init(&set2, set);
8023
8024 set_fill_set(&orig_set, set);
8025
Michal Vasko004d3152020-06-11 19:59:22 +02008026 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008027 LY_CHECK_GOTO(rc, cleanup);
8028
8029 /* ('=' / '!=' RelationalExpr)* */
8030 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008031 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032
Michal Vasko004d3152020-06-11 19:59:22 +02008033 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008035 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8036 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008037
8038 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008039 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040 LY_CHECK_GOTO(rc, cleanup);
8041 continue;
8042 }
8043
8044 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008045 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046 LY_CHECK_GOTO(rc, cleanup);
8047
8048 /* eval */
8049 if (options & LYXP_SCNODE_ALL) {
8050 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008051 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8052 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008053 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 set_scnode_clear_ctx(set);
8055 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008056 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8057 LY_CHECK_GOTO(rc, cleanup);
8058 }
8059 }
8060
8061cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008062 lyxp_set_free_content(&orig_set);
8063 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008064 return rc;
8065}
8066
8067/**
8068 * @brief Evaluate AndExpr. Logs directly on error.
8069 *
Michal Vaskod3678892020-05-21 10:06:58 +02008070 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008071 *
8072 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008073 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008074 * @param[in] repeat How many times this expression is repeated.
8075 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8076 * @param[in] options XPath options.
8077 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8078 */
8079static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008080eval_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 +02008081{
8082 LY_ERR rc;
8083 struct lyxp_set orig_set, set2;
8084 uint16_t i;
8085
8086 assert(repeat);
8087
8088 set_init(&orig_set, set);
8089 set_init(&set2, set);
8090
8091 set_fill_set(&orig_set, set);
8092
Michal Vasko004d3152020-06-11 19:59:22 +02008093 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094 LY_CHECK_GOTO(rc, cleanup);
8095
8096 /* cast to boolean, we know that will be the final result */
8097 if (set && (options & LYXP_SCNODE_ALL)) {
8098 set_scnode_clear_ctx(set);
8099 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008100 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 }
8102
8103 /* ('and' EqualityExpr)* */
8104 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008105 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8106 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8107 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8108 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109
8110 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008111 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8112 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113 LY_CHECK_GOTO(rc, cleanup);
8114 continue;
8115 }
8116
8117 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008118 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119 LY_CHECK_GOTO(rc, cleanup);
8120
8121 /* eval - just get boolean value actually */
8122 if (set->type == LYXP_SET_SCNODE_SET) {
8123 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008124 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008126 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 set_fill_set(set, &set2);
8128 }
8129 }
8130
8131cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008132 lyxp_set_free_content(&orig_set);
8133 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008134 return rc;
8135}
8136
8137/**
8138 * @brief Evaluate OrExpr. Logs directly on error.
8139 *
Michal Vaskod3678892020-05-21 10:06:58 +02008140 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008141 *
8142 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008143 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008144 * @param[in] repeat How many times this expression is repeated.
8145 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8146 * @param[in] options XPath options.
8147 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8148 */
8149static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008150eval_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 +02008151{
8152 LY_ERR rc;
8153 struct lyxp_set orig_set, set2;
8154 uint16_t i;
8155
8156 assert(repeat);
8157
8158 set_init(&orig_set, set);
8159 set_init(&set2, set);
8160
8161 set_fill_set(&orig_set, set);
8162
Michal Vasko004d3152020-06-11 19:59:22 +02008163 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008164 LY_CHECK_GOTO(rc, cleanup);
8165
8166 /* cast to boolean, we know that will be the final result */
8167 if (set && (options & LYXP_SCNODE_ALL)) {
8168 set_scnode_clear_ctx(set);
8169 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008170 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008171 }
8172
8173 /* ('or' AndExpr)* */
8174 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008175 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8176 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8177 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8178 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008179
8180 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008181 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8182 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008183 LY_CHECK_GOTO(rc, cleanup);
8184 continue;
8185 }
8186
8187 set_fill_set(&set2, &orig_set);
8188 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8189 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008190 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008191 LY_CHECK_GOTO(rc, cleanup);
8192
8193 /* eval - just get boolean value actually */
8194 if (set->type == LYXP_SET_SCNODE_SET) {
8195 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008196 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008198 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008199 set_fill_set(set, &set2);
8200 }
8201 }
8202
8203cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008204 lyxp_set_free_content(&orig_set);
8205 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008206 return rc;
8207}
8208
8209/**
Michal Vasko004d3152020-06-11 19:59:22 +02008210 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 *
8212 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008213 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008214 * @param[in] etype Expression type to evaluate.
8215 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8216 * @param[in] options XPath options.
8217 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8218 */
8219static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008220eval_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 +02008221{
8222 uint16_t i, count;
8223 enum lyxp_expr_type next_etype;
8224 LY_ERR rc;
8225
8226 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008227 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008228 next_etype = LYXP_EXPR_NONE;
8229 } else {
8230 /* find etype repeat */
Michal Vasko004d3152020-06-11 19:59:22 +02008231 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008232
8233 /* select one-priority lower because etype expression called us */
8234 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008235 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 /* count repeats for that expression */
Michal Vasko004d3152020-06-11 19:59:22 +02008237 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 } else {
8239 next_etype = LYXP_EXPR_NONE;
8240 }
8241 }
8242
8243 /* decide what expression are we parsing based on the repeat */
8244 switch (next_etype) {
8245 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008246 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008247 break;
8248 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008249 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250 break;
8251 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008252 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 break;
8254 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008255 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 break;
8257 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008258 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008259 break;
8260 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008261 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008262 break;
8263 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008264 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 break;
8266 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008267 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 break;
8269 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008270 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 break;
8272 default:
8273 LOGINT_RET(set->ctx);
8274 }
8275
8276 return rc;
8277}
8278
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008279/**
8280 * @brief Get root type.
8281 *
8282 * @param[in] ctx_node Context node.
8283 * @param[in] ctx_scnode Schema context node.
8284 * @param[in] options XPath options.
8285 * @return Root type.
8286 */
8287static enum lyxp_node_type
8288lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8289{
Michal Vasko6b26e742020-07-17 15:02:10 +02008290 const struct lysc_node *op;
8291
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008292 if (options & LYXP_SCNODE_ALL) {
Michal Vasko6b26e742020-07-17 15:02:10 +02008293 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent);
8294
8295 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008296 /* general root that can access everything */
8297 return LYXP_NODE_ROOT;
8298 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8299 /* root context node can access only config data (because we said so, it is unspecified) */
8300 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008301 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008302 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008303 }
8304
Michal Vasko6b26e742020-07-17 15:02:10 +02008305 op = ctx_node ? ctx_node->schema : NULL;
8306 for (; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent);
8307
8308 if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008309 /* root context node can access only config data (because we said so, it is unspecified) */
8310 return LYXP_NODE_ROOT_CONFIG;
8311 }
8312
8313 return LYXP_NODE_ROOT;
8314}
8315
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008317lyxp_eval(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node,
Michal Vaskof03ed032020-03-04 13:31:44 +01008318 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 +02008319{
Michal Vasko004d3152020-06-11 19:59:22 +02008320 uint16_t tok_idx = 0;
8321 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008322 LY_ERR rc;
8323
Michal Vasko004d3152020-06-11 19:59:22 +02008324 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8325
8326 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8327 /* we always need some context node because it is used for resolving unqualified names */
8328 real_ctx_node = NULL;
8329 } else {
8330 real_ctx_node = ctx_node;
8331 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332
8333 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008334 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008336 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008337 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008338 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008339 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008340 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008341 for (set->context_op = ctx_node->schema;
8342 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8343 set->context_op = set->context_op->parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008345 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008346 set->format = format;
8347
8348 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008349 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008351 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008352 }
8353
Michal Vasko03ff5a72019-09-11 13:49:33 +02008354 return rc;
8355}
8356
8357#if 0
8358
8359/* full xml printing of set elements, not used currently */
8360
8361void
8362lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8363{
8364 uint32_t i;
8365 char *str_num;
8366 struct lyout out;
8367
8368 memset(&out, 0, sizeof out);
8369
8370 out.type = LYOUT_STREAM;
8371 out.method.f = f;
8372
8373 switch (set->type) {
8374 case LYXP_SET_EMPTY:
8375 ly_print(&out, "Empty XPath set\n\n");
8376 break;
8377 case LYXP_SET_BOOLEAN:
8378 ly_print(&out, "Boolean XPath set:\n");
8379 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8380 break;
8381 case LYXP_SET_STRING:
8382 ly_print(&out, "String XPath set:\n");
8383 ly_print(&out, "\"%s\"\n\n", set->value.str);
8384 break;
8385 case LYXP_SET_NUMBER:
8386 ly_print(&out, "Number XPath set:\n");
8387
8388 if (isnan(set->value.num)) {
8389 str_num = strdup("NaN");
8390 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8391 str_num = strdup("0");
8392 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8393 str_num = strdup("Infinity");
8394 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8395 str_num = strdup("-Infinity");
8396 } else if ((long long)set->value.num == set->value.num) {
8397 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8398 str_num = NULL;
8399 }
8400 } else {
8401 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8402 str_num = NULL;
8403 }
8404 }
8405 if (!str_num) {
8406 LOGMEM;
8407 return;
8408 }
8409 ly_print(&out, "%s\n\n", str_num);
8410 free(str_num);
8411 break;
8412 case LYXP_SET_NODE_SET:
8413 ly_print(&out, "Node XPath set:\n");
8414
8415 for (i = 0; i < set->used; ++i) {
8416 ly_print(&out, "%d. ", i + 1);
8417 switch (set->node_type[i]) {
8418 case LYXP_NODE_ROOT_ALL:
8419 ly_print(&out, "ROOT all\n\n");
8420 break;
8421 case LYXP_NODE_ROOT_CONFIG:
8422 ly_print(&out, "ROOT config\n\n");
8423 break;
8424 case LYXP_NODE_ROOT_STATE:
8425 ly_print(&out, "ROOT state\n\n");
8426 break;
8427 case LYXP_NODE_ROOT_NOTIF:
8428 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8429 break;
8430 case LYXP_NODE_ROOT_RPC:
8431 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8432 break;
8433 case LYXP_NODE_ROOT_OUTPUT:
8434 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8435 break;
8436 case LYXP_NODE_ELEM:
8437 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8438 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8439 ly_print(&out, "\n");
8440 break;
8441 case LYXP_NODE_TEXT:
8442 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8443 break;
8444 case LYXP_NODE_ATTR:
8445 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8446 break;
8447 }
8448 }
8449 break;
8450 }
8451}
8452
8453#endif
8454
8455LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008456lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457{
8458 long double num;
8459 char *str;
8460 LY_ERR rc;
8461
8462 if (!set || (set->type == target)) {
8463 return LY_SUCCESS;
8464 }
8465
8466 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008467 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468
8469 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008470 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 return LY_EINVAL;
8472 }
8473
8474 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008475 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 switch (set->type) {
8477 case LYXP_SET_NUMBER:
8478 if (isnan(set->val.num)) {
8479 set->val.str = strdup("NaN");
8480 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8481 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8482 set->val.str = strdup("0");
8483 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8484 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8485 set->val.str = strdup("Infinity");
8486 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8487 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8488 set->val.str = strdup("-Infinity");
8489 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8490 } else if ((long long)set->val.num == set->val.num) {
8491 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8492 LOGMEM_RET(set->ctx);
8493 }
8494 set->val.str = str;
8495 } else {
8496 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8497 LOGMEM_RET(set->ctx);
8498 }
8499 set->val.str = str;
8500 }
8501 break;
8502 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008503 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 set->val.str = strdup("true");
8505 } else {
8506 set->val.str = strdup("false");
8507 }
8508 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8509 break;
8510 case LYXP_SET_NODE_SET:
8511 assert(set->used);
8512
8513 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008514 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008516 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008518 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 set->val.str = str;
8520 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521 default:
8522 LOGINT_RET(set->ctx);
8523 }
8524 set->type = LYXP_SET_STRING;
8525 }
8526
8527 /* to NUMBER */
8528 if (target == LYXP_SET_NUMBER) {
8529 switch (set->type) {
8530 case LYXP_SET_STRING:
8531 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008532 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 set->val.num = num;
8534 break;
8535 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008536 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 set->val.num = 1;
8538 } else {
8539 set->val.num = 0;
8540 }
8541 break;
8542 default:
8543 LOGINT_RET(set->ctx);
8544 }
8545 set->type = LYXP_SET_NUMBER;
8546 }
8547
8548 /* to BOOLEAN */
8549 if (target == LYXP_SET_BOOLEAN) {
8550 switch (set->type) {
8551 case LYXP_SET_NUMBER:
8552 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008553 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008555 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008556 }
8557 break;
8558 case LYXP_SET_STRING:
8559 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008560 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008561 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008563 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008564 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 }
8566 break;
8567 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008568 if (set->used) {
8569 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008570 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008571 } else {
8572 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008573 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008574 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575 break;
8576 default:
8577 LOGINT_RET(set->ctx);
8578 }
8579 set->type = LYXP_SET_BOOLEAN;
8580 }
8581
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 return LY_SUCCESS;
8583}
8584
8585LY_ERR
8586lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8587 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8588{
8589 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008590 const struct lysc_node *real_ctx_scnode;
8591 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008592
Michal Vasko004d3152020-06-11 19:59:22 +02008593 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594
8595 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008596 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8597 /* we always need some context node because it is used for resolving unqualified names */
8598 real_ctx_scnode = NULL;
8599 } else {
8600 real_ctx_scnode = ctx_scnode;
8601 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008602
8603 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008604 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605 memset(set, 0, sizeof *set);
8606 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008607 lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008608 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609 set->ctx = ctx;
8610 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008611 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008612 for (set->context_op = ctx_scnode;
8613 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8614 set->context_op = set->context_op->parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 set->local_mod = local_mod;
8616 set->format = format;
8617
8618 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008619 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008620}