blob: b98197ffbda0769dab1aeb6ff1b178100eadeea0 [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 Vasko03ff5a72019-09-11 13:49:33 +02006 * Copyright (c) 2015 - 2019 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
19#include "common.h"
20
Michal Vasko03ff5a72019-09-11 13:49:33 +020021#include <math.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026#include <string.h>
Michal Vasko03ff5a72019-09-11 13:49:33 +020027#include <errno.h>
28#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029
30#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "dict.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010032#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020033#include "printer_data.h"
34#include "tree_schema_internal.h"
35#include "plugins_types.h"
36
Michal Vasko03ff5a72019-09-11 13:49:33 +020037static LY_ERR reparse_or_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +020038static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options);
39
40/**
41 * @brief Print the type of an XPath \p set.
42 *
43 * @param[in] set Set to use.
44 * @return Set type string.
45 */
46static const char *
47print_set_type(struct lyxp_set *set)
48{
49 switch (set->type) {
50 case LYXP_SET_EMPTY:
51 return "empty";
52 case LYXP_SET_NODE_SET:
53 return "node set";
54 case LYXP_SET_SCNODE_SET:
55 return "schema node set";
56 case LYXP_SET_BOOLEAN:
57 return "boolean";
58 case LYXP_SET_NUMBER:
59 return "number";
60 case LYXP_SET_STRING:
61 return "string";
62 }
63
64 return NULL;
65}
66
67/**
68 * @brief Print an XPath token \p tok type.
69 *
70 * @param[in] tok Token to use.
71 * @return Token type string.
72 */
73static const char *
74print_token(enum lyxp_token tok)
75{
76 switch (tok) {
77 case LYXP_TOKEN_PAR1:
78 return "(";
79 case LYXP_TOKEN_PAR2:
80 return ")";
81 case LYXP_TOKEN_BRACK1:
82 return "[";
83 case LYXP_TOKEN_BRACK2:
84 return "]";
85 case LYXP_TOKEN_DOT:
86 return ".";
87 case LYXP_TOKEN_DDOT:
88 return "..";
89 case LYXP_TOKEN_AT:
90 return "@";
91 case LYXP_TOKEN_COMMA:
92 return ",";
93 case LYXP_TOKEN_NAMETEST:
94 return "NameTest";
95 case LYXP_TOKEN_NODETYPE:
96 return "NodeType";
97 case LYXP_TOKEN_FUNCNAME:
98 return "FunctionName";
99 case LYXP_TOKEN_OPERATOR_LOG:
100 return "Operator(Logic)";
101 case LYXP_TOKEN_OPERATOR_COMP:
102 return "Operator(Comparison)";
103 case LYXP_TOKEN_OPERATOR_MATH:
104 return "Operator(Math)";
105 case LYXP_TOKEN_OPERATOR_UNI:
106 return "Operator(Union)";
107 case LYXP_TOKEN_OPERATOR_PATH:
108 return "Operator(Path)";
109 case LYXP_TOKEN_LITERAL:
110 return "Literal";
111 case LYXP_TOKEN_NUMBER:
112 return "Number";
113 default:
114 LOGINT(NULL);
115 return "";
116 }
117}
118
119/**
120 * @brief Print the whole expression \p exp to debug output.
121 *
122 * @param[in] exp Expression to use.
123 */
124static void
125print_expr_struct_debug(struct lyxp_expr *exp)
126{
127 uint16_t i, j;
128 char tmp[128];
129
130 if (!exp || (ly_log_level < LY_LLDBG)) {
131 return;
132 }
133
134 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
135 for (i = 0; i < exp->used; ++i) {
136 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", print_token(exp->tokens[i]), exp->tok_len[i],
137 &exp->expr[exp->tok_pos[i]]);
138 if (exp->repeat[i]) {
139 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
140 for (j = 1; exp->repeat[i][j]; ++j) {
141 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
142 }
143 strcat(tmp, ")");
144 }
145 LOGDBG(LY_LDGXPATH, tmp);
146 }
147}
148
149#ifndef NDEBUG
150
151/**
152 * @brief Print XPath set content to debug output.
153 *
154 * @param[in] set Set to print.
155 */
156static void
157print_set_debug(struct lyxp_set *set)
158{
159 uint32_t i;
160 char *str;
161 int dynamic;
162 struct lyxp_set_node *item;
163 struct lyxp_set_scnode *sitem;
164
165 if (ly_log_level < LY_LLDBG) {
166 return;
167 }
168
169 switch (set->type) {
170 case LYXP_SET_NODE_SET:
171 LOGDBG(LY_LDGXPATH, "set NODE SET:");
172 for (i = 0; i < set->used; ++i) {
173 item = &set->val.nodes[i];
174
175 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100176 case LYXP_NODE_NONE:
177 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
178 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200179 case LYXP_NODE_ROOT:
180 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
181 break;
182 case LYXP_NODE_ROOT_CONFIG:
183 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
184 break;
185 case LYXP_NODE_ELEM:
186 if ((item->node->schema->nodetype == LYS_LIST)
187 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
188 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
189 item->node->schema->name,
190 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node), &dynamic)));
191 if (dynamic) {
192 free(str);
193 }
194 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
195 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
196 item->node->schema->name,
197 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
198 if (dynamic) {
199 free(str);
200 }
201 } else {
202 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
203 }
204 break;
205 case LYXP_NODE_TEXT:
206 if (item->node->schema->nodetype & LYS_ANYDATA) {
207 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
208 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
209 } else {
210 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
211 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
212 if (dynamic) {
213 free(str);
214 }
215 }
216 break;
217 case LYXP_NODE_ATTR:
218 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ATTR %s = %s", i + 1, item->pos, set->val.attrs[i].attr->name,
219 set->val.attrs[i].attr->value);
220 break;
221 }
222 }
223 break;
224
225 case LYXP_SET_SCNODE_SET:
226 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
227 for (i = 0; i < set->used; ++i) {
228 sitem = &set->val.scnodes[i];
229
230 switch (sitem->type) {
231 case LYXP_NODE_ROOT:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ROOT_CONFIG:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
236 break;
237 case LYXP_NODE_ELEM:
238 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
239 break;
240 default:
241 LOGINT(NULL);
242 break;
243 }
244 }
245 break;
246
247 case LYXP_SET_EMPTY:
248 LOGDBG(LY_LDGXPATH, "set EMPTY");
249 break;
250
251 case LYXP_SET_BOOLEAN:
252 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
253 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bool ? "true" : "false"));
254 break;
255
256 case LYXP_SET_STRING:
257 LOGDBG(LY_LDGXPATH, "set STRING");
258 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
259 break;
260
261 case LYXP_SET_NUMBER:
262 LOGDBG(LY_LDGXPATH, "set NUMBER");
263
264 if (isnan(set->val.num)) {
265 str = strdup("NaN");
266 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
267 str = strdup("0");
268 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
269 str = strdup("Infinity");
270 } else if (isinf(set->val.num) && signbit(set->val.num)) {
271 str = strdup("-Infinity");
272 } else if ((long long)set->val.num == set->val.num) {
273 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
274 str = NULL;
275 }
276 } else {
277 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
278 str = NULL;
279 }
280 }
281 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
282
283 LOGDBG(LY_LDGXPATH, "\t%s", str);
284 free(str);
285 }
286}
287
288#endif
289
290/**
291 * @brief Realloc the string \p str.
292 *
293 * @param[in] ctx libyang context for logging.
294 * @param[in] needed How much free space is required.
295 * @param[in,out] str Pointer to the string to use.
296 * @param[in,out] used Used bytes in \p str.
297 * @param[in,out] size Allocated bytes in \p str.
298 * @return LY_ERR
299 */
300static LY_ERR
301cast_string_realloc(struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
302{
303 if (*size - *used < needed) {
304 do {
305 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
306 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
307 return LY_EINVAL;
308 }
309 *size += LYXP_STRING_CAST_SIZE_STEP;
310 } while (*size - *used < needed);
311 *str = ly_realloc(*str, *size * sizeof(char));
312 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
313 }
314
315 return LY_SUCCESS;
316}
317
318/**
319 * @brief Cast nodes recursively to one string @p str.
320 *
321 * @param[in] node Node to cast.
322 * @param[in] fake_cont Whether to put the data into a "fake" container.
323 * @param[in] root_type Type of the XPath root.
324 * @param[in] indent Current indent.
325 * @param[in,out] str Resulting string.
326 * @param[in,out] used Used bytes in @p str.
327 * @param[in,out] size Allocated bytes in @p str.
328 * @return LY_ERR
329 */
330static LY_ERR
331cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
332 uint16_t *used, uint16_t *size)
333{
334 char *buf, *line, *ptr;
335 const char *value_str;
336 int dynamic;
337 const struct lyd_node *child;
338 struct lyd_node_any *any;
339 LY_ERR rc;
340
341 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
342 return LY_SUCCESS;
343 }
344
345 if (fake_cont) {
346 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
347 LY_CHECK_RET(rc);
348 strcpy(*str + (*used - 1), "\n");
349 ++(*used);
350
351 ++indent;
352 }
353
354 switch (node->schema->nodetype) {
355 case LYS_CONTAINER:
356 case LYS_LIST:
357 case LYS_RPC:
358 case LYS_NOTIF:
359 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
360 LY_CHECK_RET(rc);
361 strcpy(*str + (*used - 1), "\n");
362 ++(*used);
363
364 for (child = lyd_node_children(node); child; child = child->next) {
365 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
366 LY_CHECK_RET(rc);
367 }
368
369 break;
370
371 case LYS_LEAF:
372 case LYS_LEAFLIST:
373 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
374
375 /* print indent */
376 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
377 if (rc != LY_SUCCESS) {
378 if (dynamic) {
379 free((char *)value_str);
380 }
381 return rc;
382 }
383 memset(*str + (*used - 1), ' ', indent * 2);
384 *used += indent * 2;
385
386 /* print value */
387 if (*used == 1) {
388 sprintf(*str + (*used - 1), "%s", value_str);
389 *used += strlen(value_str);
390 } else {
391 sprintf(*str + (*used - 1), "%s\n", value_str);
392 *used += strlen(value_str) + 1;
393 }
394 if (dynamic) {
395 free((char *)value_str);
396 }
397
398 break;
399
400 case LYS_ANYXML:
401 case LYS_ANYDATA:
402 any = (struct lyd_node_any *)node;
403 if (!(void *)any->value.tree) {
404 /* no content */
405 buf = strdup("");
406 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
407 } else {
408 switch (any->value_type) {
409 case LYD_ANYDATA_STRING:
410 case LYD_ANYDATA_XML:
411 case LYD_ANYDATA_JSON:
412 buf = strdup(any->value.json);
413 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
414 break;
415 case LYD_ANYDATA_DATATREE:
416 rc = lyd_print_mem(&buf, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS);
417 LY_CHECK_RET(rc);
418 break;
419 /* TODO case LYD_ANYDATA_LYB:
420 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
421 return -1;*/
422 }
423 }
424
425 line = strtok_r(buf, "\n", &ptr);
426 do {
427 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
428 if (rc != LY_SUCCESS) {
429 free(buf);
430 return rc;
431 }
432 memset(*str + (*used - 1), ' ', indent * 2);
433 *used += indent * 2;
434
435 strcpy(*str + (*used - 1), line);
436 *used += strlen(line);
437
438 strcpy(*str + (*used - 1), "\n");
439 *used += 1;
440 } while ((line = strtok_r(NULL, "\n", &ptr)));
441
442 free(buf);
443 break;
444
445 default:
446 LOGINT_RET(LYD_NODE_CTX(node));
447 }
448
449 if (fake_cont) {
450 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
451 LY_CHECK_RET(rc);
452 strcpy(*str + (*used - 1), "\n");
453 ++(*used);
454
455 --indent;
456 }
457
458 return LY_SUCCESS;
459}
460
461/**
462 * @brief Cast an element into a string.
463 *
464 * @param[in] node Node to cast.
465 * @param[in] fake_cont Whether to put the data into a "fake" container.
466 * @param[in] root_type Type of the XPath root.
467 * @param[out] str Element cast to dynamically-allocated string.
468 * @return LY_ERR
469 */
470static LY_ERR
471cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
472{
473 uint16_t used, size;
474 LY_ERR rc;
475
476 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
477 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
478 (*str)[0] = '\0';
479 used = 1;
480 size = LYXP_STRING_CAST_SIZE_START;
481
482 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
483 if (rc != LY_SUCCESS) {
484 free(*str);
485 return rc;
486 }
487
488 if (size > used) {
489 *str = ly_realloc(*str, used * sizeof(char));
490 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
491 }
492 return LY_SUCCESS;
493}
494
495/**
496 * @brief Cast a LYXP_SET_NODE_SET set into a string.
497 * Context position aware.
498 *
499 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200500 * @param[out] str Cast dynamically-allocated string.
501 * @return LY_ERR
502 */
503static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100504cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200505{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 int dynamic;
507
Michal Vasko03ff5a72019-09-11 13:49:33 +0200508 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100509 case LYXP_NODE_NONE:
510 /* invalid */
511 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200512 case LYXP_NODE_ROOT:
513 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100514 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200515 case LYXP_NODE_ELEM:
516 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100517 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200518 case LYXP_NODE_ATTR:
519 *str = (char *)lyd_attr2str(set->val.attrs[0].attr, &dynamic);
520 if (!dynamic) {
521 *str = strdup(*str);
522 if (!*str) {
523 LOGMEM_RET(set->ctx);
524 }
525 }
526 return LY_SUCCESS;
527 }
528
529 LOGINT_RET(set->ctx);
530}
531
532/**
533 * @brief Cast a string into an XPath number.
534 *
535 * @param[in] str String to use.
536 * @return Cast number.
537 */
538static long double
539cast_string_to_number(const char *str)
540{
541 long double num;
542 char *ptr;
543
544 errno = 0;
545 num = strtold(str, &ptr);
546 if (errno || *ptr) {
547 num = NAN;
548 }
549 return num;
550}
551
552/**
553 * @brief Callback for checking value equality.
554 *
555 * @param[in] val1_p First value.
556 * @param[in] val2_p Second value.
557 * @param[in] mod Whether hash table is being modified.
558 * @param[in] cb_data Callback data.
559 * @return 0 if not equal, non-zero if equal.
560 */
561static int
562set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
563{
564 struct lyxp_set_hash_node *val1, *val2;
565
566 val1 = (struct lyxp_set_hash_node *)val1_p;
567 val2 = (struct lyxp_set_hash_node *)val2_p;
568
569 if ((val1->node == val2->node) && (val1->type == val2->type)) {
570 return 1;
571 }
572
573 return 0;
574}
575
576/**
577 * @brief Insert node and its hash into set.
578 *
579 * @param[in] set et to insert to.
580 * @param[in] node Node with hash.
581 * @param[in] type Node type.
582 */
583static void
584set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
585{
586 int r;
587 uint32_t i, hash;
588 struct lyxp_set_hash_node hnode;
589
590 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
591 /* create hash table and add all the nodes */
592 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
593 for (i = 0; i < set->used; ++i) {
594 hnode.node = set->val.nodes[i].node;
595 hnode.type = set->val.nodes[i].type;
596
597 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
598 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
599 hash = dict_hash_multi(hash, NULL, 0);
600
601 r = lyht_insert(set->ht, &hnode, hash, NULL);
602 assert(!r);
603 (void)r;
604 }
605 } else if (set->ht) {
606 assert(node);
607
608 /* add the new node into hash table */
609 hnode.node = node;
610 hnode.type = type;
611
612 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
613 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
614 hash = dict_hash_multi(hash, NULL, 0);
615
616 r = lyht_insert(set->ht, &hnode, hash, NULL);
617 assert(!r);
618 (void)r;
619 }
620}
621
622/**
623 * @brief Remove node and its hash from set.
624 *
625 * @param[in] set Set to remove from.
626 * @param[in] node Node to remove.
627 * @param[in] type Node type.
628 */
629static void
630set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
631{
632 int r;
633 struct lyxp_set_hash_node hnode;
634 uint32_t hash;
635
636 if (set->ht) {
637 hnode.node = node;
638 hnode.type = type;
639
640 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
641 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
642 hash = dict_hash_multi(hash, NULL, 0);
643
644 r = lyht_remove(set->ht, &hnode, hash);
645 assert(!r);
646 (void)r;
647
648 if (!set->ht->used) {
649 lyht_free(set->ht);
650 set->ht = NULL;
651 }
652 }
653}
654
655/**
656 * @brief Check whether node is in set based on its hash.
657 *
658 * @param[in] set Set to search in.
659 * @param[in] node Node to search for.
660 * @param[in] type Node type.
661 * @param[in] skip_idx Index in @p set to skip.
662 * @return LY_ERR
663 */
664static LY_ERR
665set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
666{
667 struct lyxp_set_hash_node hnode, *match_p;
668 uint32_t hash;
669
670 hnode.node = node;
671 hnode.type = type;
672
673 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
674 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
675 hash = dict_hash_multi(hash, NULL, 0);
676
677 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
678 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
679 /* we found it on the index that should be skipped, find another */
680 hnode = *match_p;
681 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
682 /* none other found */
683 return LY_SUCCESS;
684 }
685 }
686
687 return LY_EEXIST;
688 }
689
690 /* not found */
691 return LY_SUCCESS;
692}
693
694/**
695 * @brief Free dynamic content of a set.
696 *
697 * @param[in] set Set to modify.
698 */
699static void
700set_free_content(struct lyxp_set *set)
701{
702 if (!set) {
703 return;
704 }
705
706 if (set->type == LYXP_SET_NODE_SET) {
707 free(set->val.nodes);
708 lyht_free(set->ht);
709 set->ht = NULL;
710 } else if (set->type == LYXP_SET_SCNODE_SET) {
711 free(set->val.scnodes);
712 } else if (set->type == LYXP_SET_STRING) {
713 free(set->val.str);
714 }
715 set->type = LYXP_SET_EMPTY;
716}
717
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100718/**
719 * @brief Free dynamically-allocated set.
720 *
721 * @param[in] set Set to free.
722 */
723static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200724lyxp_set_free(struct lyxp_set *set)
725{
726 if (!set) {
727 return;
728 }
729
730 set_free_content(set);
731 free(set);
732}
733
734/**
735 * @brief Initialize set context.
736 *
737 * @param[in] new Set to initialize.
738 * @param[in] set Arbitrary initialized set.
739 */
740static void
741set_init(struct lyxp_set *new, struct lyxp_set *set)
742{
743 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200744 if (set) {
745 new->ctx = set->ctx;
746 new->ctx_node = set->ctx_node;
747 new->local_mod = set->local_mod;
748 new->trees = set->trees;
749 new->format = set->format;
750 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200751}
752
753/**
754 * @brief Create a deep copy of a set.
755 *
756 * @param[in] set Set to copy.
757 * @return Copy of @p set.
758 */
759static struct lyxp_set *
760set_copy(struct lyxp_set *set)
761{
762 struct lyxp_set *ret;
763 uint16_t i;
764
765 if (!set) {
766 return NULL;
767 }
768
769 ret = malloc(sizeof *ret);
770 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
771 set_init(ret, set);
772
773 if (set->type == LYXP_SET_SCNODE_SET) {
774 ret->type = set->type;
775
776 for (i = 0; i < set->used; ++i) {
777 if (set->val.scnodes[i].in_ctx == 1) {
Michal Vaskoecd62de2019-11-13 12:35:11 +0100778 if (lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200779 lyxp_set_free(ret);
780 return NULL;
781 }
782 }
783 }
784 } else if (set->type == LYXP_SET_NODE_SET) {
785 ret->type = set->type;
786 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
787 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
788 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
789
790 ret->used = ret->size = set->used;
791 ret->ctx_pos = set->ctx_pos;
792 ret->ctx_size = set->ctx_size;
793 ret->ht = lyht_dup(set->ht);
794 } else {
795 memcpy(ret, set, sizeof *ret);
796 if (set->type == LYXP_SET_STRING) {
797 ret->val.str = strdup(set->val.str);
798 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
799 }
800 }
801
802 return ret;
803}
804
805/**
806 * @brief Fill XPath set with a string. Any current data are disposed of.
807 *
808 * @param[in] set Set to fill.
809 * @param[in] string String to fill into \p set.
810 * @param[in] str_len Length of \p string. 0 is a valid value!
811 */
812static void
813set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
814{
815 set_free_content(set);
816
817 set->type = LYXP_SET_STRING;
818 if ((str_len == 0) && (string[0] != '\0')) {
819 string = "";
820 }
821 set->val.str = strndup(string, str_len);
822}
823
824/**
825 * @brief Fill XPath set with a number. Any current data are disposed of.
826 *
827 * @param[in] set Set to fill.
828 * @param[in] number Number to fill into \p set.
829 */
830static void
831set_fill_number(struct lyxp_set *set, long double number)
832{
833 set_free_content(set);
834
835 set->type = LYXP_SET_NUMBER;
836 set->val.num = number;
837}
838
839/**
840 * @brief Fill XPath set with a boolean. Any current data are disposed of.
841 *
842 * @param[in] set Set to fill.
843 * @param[in] boolean Boolean to fill into \p set.
844 */
845static void
846set_fill_boolean(struct lyxp_set *set, int boolean)
847{
848 set_free_content(set);
849
850 set->type = LYXP_SET_BOOLEAN;
851 set->val.bool = boolean;
852}
853
854/**
855 * @brief Fill XPath set with the value from another set (deep assign).
856 * Any current data are disposed of.
857 *
858 * @param[in] trg Set to fill.
859 * @param[in] src Source set to copy into \p trg.
860 */
861static void
862set_fill_set(struct lyxp_set *trg, struct lyxp_set *src)
863{
864 if (!trg || !src) {
865 return;
866 }
867
868 if (trg->type == LYXP_SET_NODE_SET) {
869 free(trg->val.nodes);
870 } else if (trg->type == LYXP_SET_STRING) {
871 free(trg->val.str);
872 }
873 set_init(trg, src);
874
875 if (src->type == LYXP_SET_SCNODE_SET) {
876 trg->type = LYXP_SET_SCNODE_SET;
877 trg->used = src->used;
878 trg->size = src->used;
879
880 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
881 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
882 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
883 } else if (src->type == LYXP_SET_BOOLEAN) {
884 set_fill_boolean(trg, src->val.bool);
885 } else if (src->type == LYXP_SET_NUMBER) {
886 set_fill_number(trg, src->val.num);
887 } else if (src->type == LYXP_SET_STRING) {
888 set_fill_string(trg, src->val.str, strlen(src->val.str));
889 } else {
890 if (trg->type == LYXP_SET_NODE_SET) {
891 free(trg->val.nodes);
892 } else if (trg->type == LYXP_SET_STRING) {
893 free(trg->val.str);
894 }
895
896 if (src->type == LYXP_SET_EMPTY) {
897 trg->type = LYXP_SET_EMPTY;
898 } else {
899 assert(src->type == LYXP_SET_NODE_SET);
900
901 trg->type = LYXP_SET_NODE_SET;
902 trg->used = src->used;
903 trg->size = src->used;
904 trg->ctx_pos = src->ctx_pos;
905 trg->ctx_size = src->ctx_size;
906
907 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
908 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
909 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
910 trg->ht = lyht_dup(src->ht);
911 }
912 }
913}
914
915/**
916 * @brief Clear context of all schema nodes.
917 *
918 * @param[in] set Set to clear.
919 */
920static void
921set_scnode_clear_ctx(struct lyxp_set *set)
922{
923 uint32_t i;
924
925 for (i = 0; i < set->used; ++i) {
926 if (set->val.scnodes[i].in_ctx == 1) {
927 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100928 } else if (set->val.scnodes[i].in_ctx == -2) {
929 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200930 }
931 }
932}
933
934/**
935 * @brief Remove a node from a set. Removing last node changes
936 * set into LYXP_SET_EMPTY. Context position aware.
937 *
938 * @param[in] set Set to use.
939 * @param[in] idx Index from @p set of the node to be removed.
940 */
941static void
942set_remove_node(struct lyxp_set *set, uint32_t idx)
943{
944 assert(set && (set->type == LYXP_SET_NODE_SET));
945 assert(idx < set->used);
946
947 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
948
949 --set->used;
950 if (set->used) {
951 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
952 (set->used - idx) * sizeof *set->val.nodes);
953 } else {
954 set_free_content(set);
955 set->type = LYXP_SET_EMPTY;
956 }
957}
958
959/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100960 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200961 *
962 * @param[in] set Set to use.
963 * @param[in] idx Index from @p set of the node to be removed.
964 */
965static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100966set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200967{
968 assert(set && (set->type == LYXP_SET_NODE_SET));
969 assert(idx < set->used);
970
Michal Vasko2caefc12019-11-14 16:07:56 +0100971 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
972 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
973 }
974 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200975}
976
977/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100978 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200979 * set into LYXP_SET_EMPTY. Context position aware.
980 *
981 * @param[in] set Set to consolidate.
982 */
983static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100984set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200985{
986 uint16_t i, orig_used, end;
987 int32_t start;
988
Michal Vasko2caefc12019-11-14 16:07:56 +0100989 assert(set && (set->type != LYXP_SET_EMPTY));
Michal Vasko57eab132019-09-24 11:46:26 +0200990
991 orig_used = set->used;
992 set->used = 0;
993 for (i = 0; i < orig_used;) {
994 start = -1;
995 do {
Michal Vasko2caefc12019-11-14 16:07:56 +0100996 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +0200997 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +0100998 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +0200999 end = i;
1000 ++i;
1001 break;
1002 }
1003
1004 ++i;
1005 if (i == orig_used) {
1006 end = i;
1007 }
1008 } while (i < orig_used);
1009
1010 if (start > -1) {
1011 /* move the whole chunk of valid nodes together */
1012 if (set->used != (unsigned)start) {
1013 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1014 }
1015 set->used += end - start;
1016 }
1017 }
1018
1019 if (!set->used) {
1020 set_free_content(set);
1021 /* this changes it to LYXP_SET_EMPTY */
1022 memset(set, 0, sizeof *set);
1023 }
1024}
1025
1026/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001027 * @brief Check for duplicates in a node set.
1028 *
1029 * @param[in] set Set to check.
1030 * @param[in] node Node to look for in @p set.
1031 * @param[in] node_type Type of @p node.
1032 * @param[in] skip_idx Index from @p set to skip.
1033 * @return LY_ERR
1034 */
1035static LY_ERR
1036set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1037{
1038 uint32_t i;
1039
Michal Vasko2caefc12019-11-14 16:07:56 +01001040 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001041 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1042 }
1043
1044 for (i = 0; i < set->used; ++i) {
1045 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1046 continue;
1047 }
1048
1049 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1050 return LY_EEXIST;
1051 }
1052 }
1053
1054 return LY_SUCCESS;
1055}
1056
Michal Vaskoecd62de2019-11-13 12:35:11 +01001057int
1058lyxp_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 +02001059{
1060 uint32_t i;
1061
1062 for (i = 0; i < set->used; ++i) {
1063 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1064 continue;
1065 }
1066
1067 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1068 return i;
1069 }
1070 }
1071
1072 return -1;
1073}
1074
Michal Vaskoecd62de2019-11-13 12:35:11 +01001075void
1076lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001077{
1078 uint32_t orig_used, i, j;
1079
1080 assert(((set1->type == LYXP_SET_SCNODE_SET) || (set1->type == LYXP_SET_EMPTY))
1081 && ((set2->type == LYXP_SET_SCNODE_SET) || (set2->type == LYXP_SET_EMPTY)));
1082
1083 if (set2->type == LYXP_SET_EMPTY) {
1084 return;
1085 }
1086
1087 if (set1->type == LYXP_SET_EMPTY) {
1088 memcpy(set1, set2, sizeof *set1);
1089 return;
1090 }
1091
1092 if (set1->used + set2->used > set1->size) {
1093 set1->size = set1->used + set2->used;
1094 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1095 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1096 }
1097
1098 orig_used = set1->used;
1099
1100 for (i = 0; i < set2->used; ++i) {
1101 for (j = 0; j < orig_used; ++j) {
1102 /* detect duplicities */
1103 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1104 break;
1105 }
1106 }
1107
1108 if (j == orig_used) {
1109 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1110 ++set1->used;
1111 }
1112 }
1113
1114 set_free_content(set2);
1115 set2->type = LYXP_SET_EMPTY;
1116}
1117
1118/**
1119 * @brief Insert a node into a set. Context position aware.
1120 *
1121 * @param[in] set Set to use.
1122 * @param[in] node Node to insert to @p set.
1123 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1124 * @param[in] node_type Node type of @p node.
1125 * @param[in] idx Index in @p set to insert into.
1126 */
1127static void
1128set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1129{
1130 assert(set && ((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_EMPTY)));
1131
1132 if (set->type == LYXP_SET_EMPTY) {
1133 /* first item */
1134 if (idx) {
1135 /* no real harm done, but it is a bug */
1136 LOGINT(set->ctx);
1137 idx = 0;
1138 }
1139 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1140 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1141 set->type = LYXP_SET_NODE_SET;
1142 set->used = 0;
1143 set->size = LYXP_SET_SIZE_START;
1144 set->ctx_pos = 1;
1145 set->ctx_size = 1;
1146 set->ht = NULL;
1147 } else {
1148 /* not an empty set */
1149 if (set->used == set->size) {
1150
1151 /* set is full */
1152 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1153 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1154 set->size += LYXP_SET_SIZE_STEP;
1155 }
1156
1157 if (idx > set->used) {
1158 LOGINT(set->ctx);
1159 idx = set->used;
1160 }
1161
1162 /* make space for the new node */
1163 if (idx < set->used) {
1164 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1165 }
1166 }
1167
1168 /* finally assign the value */
1169 set->val.nodes[idx].node = (struct lyd_node *)node;
1170 set->val.nodes[idx].type = node_type;
1171 set->val.nodes[idx].pos = pos;
1172 ++set->used;
1173
Michal Vasko2caefc12019-11-14 16:07:56 +01001174 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1175 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1176 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001177}
1178
Michal Vaskoecd62de2019-11-13 12:35:11 +01001179int
1180lyxp_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 +02001181{
1182 int ret;
1183
1184 assert(set->type == LYXP_SET_SCNODE_SET);
1185
Michal Vaskoecd62de2019-11-13 12:35:11 +01001186 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001187 if (ret > -1) {
1188 set->val.scnodes[ret].in_ctx = 1;
1189 } else {
1190 if (set->used == set->size) {
1191 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1192 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1193 set->size += LYXP_SET_SIZE_STEP;
1194 }
1195
1196 ret = set->used;
1197 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1198 set->val.scnodes[ret].type = node_type;
1199 set->val.scnodes[ret].in_ctx = 1;
1200 ++set->used;
1201 }
1202
1203 return ret;
1204}
1205
1206/**
1207 * @brief Replace a node in a set with another. Context position aware.
1208 *
1209 * @param[in] set Set to use.
1210 * @param[in] node Node to insert to @p set.
1211 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1212 * @param[in] node_type Node type of @p node.
1213 * @param[in] idx Index in @p set of the node to replace.
1214 */
1215static void
1216set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1217{
1218 assert(set && (idx < set->used));
1219
Michal Vasko2caefc12019-11-14 16:07:56 +01001220 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1221 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1222 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001223 set->val.nodes[idx].node = (struct lyd_node *)node;
1224 set->val.nodes[idx].type = node_type;
1225 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001226 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1227 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1228 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001229}
1230
1231/**
1232 * @brief Set all nodes with ctx 1 to a new unique context value.
1233 *
1234 * @param[in] set Set to modify.
1235 * @return New context value.
1236 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001237static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001238set_scnode_new_in_ctx(struct lyxp_set *set)
1239{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001240 uint32_t i;
1241 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001242
1243 assert(set->type == LYXP_SET_SCNODE_SET);
1244
1245 ret_ctx = 3;
1246retry:
1247 for (i = 0; i < set->used; ++i) {
1248 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1249 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1250 goto retry;
1251 }
1252 }
1253 for (i = 0; i < set->used; ++i) {
1254 if (set->val.scnodes[i].in_ctx == 1) {
1255 set->val.scnodes[i].in_ctx = ret_ctx;
1256 }
1257 }
1258
1259 return ret_ctx;
1260}
1261
1262/**
1263 * @brief Get unique @p node position in the data.
1264 *
1265 * @param[in] node Node to find.
1266 * @param[in] node_type Node type of @p node.
1267 * @param[in] root Root node.
1268 * @param[in] root_type Type of the XPath @p root node.
1269 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1270 * be used to increase efficiency and start the DFS from this node.
1271 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1272 * @return Node position.
1273 */
1274static uint32_t
1275get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1276 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1277{
1278 const struct lyd_node *next, *elem, *top_sibling;
1279 uint32_t pos = 1;
1280
1281 assert(prev && prev_pos && !root->prev->next);
1282
1283 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1284 return 0;
1285 }
1286
1287 if (*prev) {
1288 /* start from the previous element instead from the root */
1289 elem = next = *prev;
1290 pos = *prev_pos;
1291 for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent);
1292 goto dfs_search;
1293 }
1294
1295 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
1296 /* TREE DFS */
1297 LYD_TREE_DFS_BEGIN(top_sibling, next, elem) {
1298dfs_search:
1299 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
1300 goto skip_children;
1301 }
1302
1303 if (elem == node) {
1304 break;
1305 }
1306 ++pos;
1307
1308 /* TREE DFS END */
1309 /* select element for the next run - children first,
1310 * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */
1311 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1312 next = NULL;
1313 } else {
1314 next = lyd_node_children(elem);
1315 }
1316 if (!next) {
1317skip_children:
1318 /* no children */
1319 if (elem == top_sibling) {
1320 /* we are done, root has no children */
1321 elem = NULL;
1322 break;
1323 }
1324 /* try siblings */
1325 next = elem->next;
1326 }
1327 while (!next) {
1328 /* no siblings, go back through parents */
1329 if (elem->parent == top_sibling->parent) {
1330 /* we are done, no next element to process */
1331 elem = NULL;
1332 break;
1333 }
1334 /* parent is already processed, go to its sibling */
1335 elem = (struct lyd_node *)elem->parent;
1336 next = elem->next;
1337 }
1338 }
1339
1340 /* node found */
1341 if (elem) {
1342 break;
1343 }
1344 }
1345
1346 if (!elem) {
1347 if (!(*prev)) {
1348 /* we went from root and failed to find it, cannot be */
1349 LOGINT(node->schema->module->ctx);
1350 return 0;
1351 } else {
1352 *prev = NULL;
1353 *prev_pos = 0;
1354
1355 elem = next = top_sibling = root;
1356 pos = 1;
1357 goto dfs_search;
1358 }
1359 }
1360
1361 /* remember the last found node for next time */
1362 *prev = node;
1363 *prev_pos = pos;
1364
1365 return pos;
1366}
1367
1368/**
1369 * @brief Assign (fill) missing node positions.
1370 *
1371 * @param[in] set Set to fill positions in.
1372 * @param[in] root Context root node.
1373 * @param[in] root_type Context root type.
1374 * @return LY_ERR
1375 */
1376static LY_ERR
1377set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1378{
1379 const struct lyd_node *prev = NULL, *tmp_node;
1380 uint32_t i, tmp_pos = 0;
1381
1382 for (i = 0; i < set->used; ++i) {
1383 if (!set->val.nodes[i].pos) {
1384 tmp_node = NULL;
1385 switch (set->val.nodes[i].type) {
1386 case LYXP_NODE_ATTR:
1387 tmp_node = set->val.attrs[i].attr->parent;
1388 if (!tmp_node) {
1389 LOGINT_RET(root->schema->module->ctx);
1390 }
1391 /* fallthrough */
1392 case LYXP_NODE_ELEM:
1393 case LYXP_NODE_TEXT:
1394 if (!tmp_node) {
1395 tmp_node = set->val.nodes[i].node;
1396 }
1397 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1398 break;
1399 default:
1400 /* all roots have position 0 */
1401 break;
1402 }
1403 }
1404 }
1405
1406 return LY_SUCCESS;
1407}
1408
1409/**
1410 * @brief Get unique @p attr position in the parent attributes.
1411 *
1412 * @param[in] attr Attr to use.
1413 * @return Attribute position.
1414 */
1415static uint16_t
1416get_attr_pos(struct lyd_attr *attr)
1417{
1418 uint16_t pos = 0;
1419 struct lyd_attr *attr2;
1420
1421 for (attr2 = attr->parent->attr; attr2 && (attr2 != attr); attr2 = attr2->next) {
1422 ++pos;
1423 }
1424
1425 assert(attr2);
1426 return pos;
1427}
1428
1429/**
1430 * @brief Compare 2 nodes in respect to XPath document order.
1431 *
1432 * @param[in] item1 1st node.
1433 * @param[in] item2 2nd node.
1434 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1435 */
1436static int
1437set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1438{
1439 uint32_t attr_pos1 = 0, attr_pos2 = 0;
1440
1441 if (item1->pos < item2->pos) {
1442 return -1;
1443 }
1444
1445 if (item1->pos > item2->pos) {
1446 return 1;
1447 }
1448
1449 /* node positions are equal, the fun case */
1450
1451 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1452 /* special case since text nodes are actually saved as their parents */
1453 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1454 if (item1->type == LYXP_NODE_ELEM) {
1455 assert(item2->type == LYXP_NODE_TEXT);
1456 return -1;
1457 } else {
1458 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1459 return 1;
1460 }
1461 }
1462
1463 /* we need attr positions now */
1464 if (item1->type == LYXP_NODE_ATTR) {
1465 attr_pos1 = get_attr_pos((struct lyd_attr *)item1->node);
1466 }
1467 if (item2->type == LYXP_NODE_ATTR) {
1468 attr_pos2 = get_attr_pos((struct lyd_attr *)item2->node);
1469 }
1470
1471 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st ATTR - =pos= - 2nd ATTR */
1472 /* check for duplicates */
1473 if (item1->node == item2->node) {
1474 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_ATTR) || (attr_pos1 == attr_pos2)));
1475 return 0;
1476 }
1477
1478 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd ATTR */
1479 /* elem is always first, 2nd node is after it */
1480 if (item1->type == LYXP_NODE_ELEM) {
1481 assert(item2->type != LYXP_NODE_ELEM);
1482 return -1;
1483 }
1484
1485 /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd ATTR, 1st ATTR - any pos - 2nd ELEM, 1st ATTR - >pos> - 2nd ATTR */
1486 /* 2nd is before 1st */
1487 if (((item1->type == LYXP_NODE_TEXT)
1488 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_ATTR)))
1489 || ((item1->type == LYXP_NODE_ATTR) && (item2->type == LYXP_NODE_ELEM))
1490 || (((item1->type == LYXP_NODE_ATTR) && (item2->type == LYXP_NODE_ATTR))
1491 && (attr_pos1 > attr_pos2))) {
1492 return 1;
1493 }
1494
1495 /* 1st ATTR - any pos - 2nd TEXT, 1st ATTR <pos< - 2nd ATTR */
1496 /* 2nd is after 1st */
1497 return -1;
1498}
1499
1500/**
1501 * @brief Set cast for comparisons.
1502 *
1503 * @param[in] trg Target set to cast source into.
1504 * @param[in] src Source set.
1505 * @param[in] type Target set type.
1506 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001507 * @return LY_ERR
1508 */
1509static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001510set_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 +02001511{
1512 assert(src->type == LYXP_SET_NODE_SET);
1513
1514 set_init(trg, src);
1515
1516 /* insert node into target set */
1517 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1518
1519 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001520 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001521}
1522
1523#ifndef NDEBUG
1524
1525/**
1526 * @brief Bubble sort @p set into XPath document order.
1527 * Context position aware. Unused in the 'Release' build target.
1528 *
1529 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001530 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1531 */
1532static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001533set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001534{
1535 uint32_t i, j;
1536 int ret = 0, cmp, inverted, change;
1537 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001538 struct lyxp_set_node item;
1539 struct lyxp_set_hash_node hnode;
1540 uint64_t hash;
1541
1542 if ((set->type != LYXP_SET_NODE_SET) || (set->used == 1)) {
1543 return 0;
1544 }
1545
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001546 /* find first top-level node to be used as anchor for positions */
1547 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1548 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001549
1550 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001551 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001552 return -1;
1553 }
1554
1555 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1556 print_set_debug(set);
1557
1558 for (i = 0; i < set->used; ++i) {
1559 inverted = 0;
1560 change = 0;
1561
1562 for (j = 1; j < set->used - i; ++j) {
1563 /* compare node positions */
1564 if (inverted) {
1565 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1566 } else {
1567 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1568 }
1569
1570 /* swap if needed */
1571 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1572 change = 1;
1573
1574 item = set->val.nodes[j - 1];
1575 set->val.nodes[j - 1] = set->val.nodes[j];
1576 set->val.nodes[j] = item;
1577 } else {
1578 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1579 inverted = !inverted;
1580 }
1581 }
1582
1583 ++ret;
1584
1585 if (!change) {
1586 break;
1587 }
1588 }
1589
1590 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1591 print_set_debug(set);
1592
1593 /* check node hashes */
1594 if (set->used >= LYD_HT_MIN_ITEMS) {
1595 assert(set->ht);
1596 for (i = 0; i < set->used; ++i) {
1597 hnode.node = set->val.nodes[i].node;
1598 hnode.type = set->val.nodes[i].type;
1599
1600 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1601 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1602 hash = dict_hash_multi(hash, NULL, 0);
1603
1604 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1605 }
1606 }
1607
1608 return ret - 1;
1609}
1610
1611/**
1612 * @brief Remove duplicate entries in a sorted node set.
1613 *
1614 * @param[in] set Sorted set to check.
1615 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1616 */
1617static LY_ERR
1618set_sorted_dup_node_clean(struct lyxp_set *set)
1619{
1620 uint32_t i = 0;
1621 LY_ERR ret = LY_SUCCESS;
1622
1623 if (set->used > 1) {
1624 while (i < set->used - 1) {
1625 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1626 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001627 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001628 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629 }
Michal Vasko57eab132019-09-24 11:46:26 +02001630 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631 }
1632 }
1633
Michal Vasko2caefc12019-11-14 16:07:56 +01001634 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001635 return ret;
1636}
1637
1638#endif
1639
1640/**
1641 * @brief Merge 2 sorted sets into one.
1642 *
1643 * @param[in,out] trg Set to merge into. Duplicates are removed.
1644 * @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 +02001645 * @return LY_ERR
1646 */
1647static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001648set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001649{
1650 uint32_t i, j, k, count, dup_count;
1651 int cmp;
1652 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001653
1654 if (((trg->type != LYXP_SET_NODE_SET) && (trg->type != LYXP_SET_EMPTY))
1655 || ((src->type != LYXP_SET_NODE_SET) && (src->type != LYXP_SET_EMPTY))) {
1656 return LY_EINVAL;
1657 }
1658
1659 if (src->type == LYXP_SET_EMPTY) {
1660 return LY_SUCCESS;
1661 } else if (trg->type == LYXP_SET_EMPTY) {
1662 set_fill_set(trg, src);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001663 lyxp_set_cast(src, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001664 return LY_SUCCESS;
1665 }
1666
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001667 /* find first top-level node to be used as anchor for positions */
1668 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1669 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001670
1671 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001672 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001673 return LY_EINT;
1674 }
1675
1676#ifndef NDEBUG
1677 LOGDBG(LY_LDGXPATH, "MERGE target");
1678 print_set_debug(trg);
1679 LOGDBG(LY_LDGXPATH, "MERGE source");
1680 print_set_debug(src);
1681#endif
1682
1683 /* make memory for the merge (duplicates are not detected yet, so space
1684 * will likely be wasted on them, too bad) */
1685 if (trg->size - trg->used < src->used) {
1686 trg->size = trg->used + src->used;
1687
1688 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1689 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1690 }
1691
1692 i = 0;
1693 j = 0;
1694 count = 0;
1695 dup_count = 0;
1696 do {
1697 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1698 if (!cmp) {
1699 if (!count) {
1700 /* duplicate, just skip it */
1701 ++i;
1702 ++j;
1703 } else {
1704 /* we are copying something already, so let's copy the duplicate too,
1705 * we are hoping that afterwards there are some more nodes to
1706 * copy and this way we can copy them all together */
1707 ++count;
1708 ++dup_count;
1709 ++i;
1710 ++j;
1711 }
1712 } else if (cmp < 0) {
1713 /* inserting src node into trg, just remember it for now */
1714 ++count;
1715 ++i;
1716
1717 /* insert the hash now */
1718 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1719 } else if (count) {
1720copy_nodes:
1721 /* time to actually copy the nodes, we have found the largest block of nodes */
1722 memmove(&trg->val.nodes[j + (count - dup_count)],
1723 &trg->val.nodes[j],
1724 (trg->used - j) * sizeof *trg->val.nodes);
1725 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1726
1727 trg->used += count - dup_count;
1728 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1729 j += count - dup_count;
1730
1731 count = 0;
1732 dup_count = 0;
1733 } else {
1734 ++j;
1735 }
1736 } while ((i < src->used) && (j < trg->used));
1737
1738 if ((i < src->used) || count) {
1739 /* insert all the hashes first */
1740 for (k = i; k < src->used; ++k) {
1741 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1742 }
1743
1744 /* loop ended, but we need to copy something at trg end */
1745 count += src->used - i;
1746 i = src->used;
1747 goto copy_nodes;
1748 }
1749
1750 /* we are inserting hashes before the actual node insert, which causes
1751 * situations when there were initially not enough items for a hash table,
1752 * but even after some were inserted, hash table was not created (during
1753 * insertion the number of items is not updated yet) */
1754 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1755 set_insert_node_hash(trg, NULL, 0);
1756 }
1757
1758#ifndef NDEBUG
1759 LOGDBG(LY_LDGXPATH, "MERGE result");
1760 print_set_debug(trg);
1761#endif
1762
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001763 lyxp_set_cast(src, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001764 return LY_SUCCESS;
1765}
1766
1767/*
1768 * (re)parse functions
1769 *
1770 * Parse functions parse the expression into
1771 * tokens (syntactic analysis).
1772 *
1773 * Reparse functions perform semantic analysis
1774 * (do not save the result, just a check) of
1775 * the expression and fill repeat indices.
1776 */
1777
1778/**
1779 * @brief Look at the next token and check its kind.
1780 *
1781 * @param[in] ctx Context for logging.
1782 * @param[in] exp Expression to use.
1783 * @param[in] exp_idx Position in the expression \p exp.
1784 * @param[in] want_tok Expected token.
1785 * @param[in] strict Whether the token is strictly required (print error if
1786 * not the next one) or we simply want to check whether it is the next or not.
1787 * @return LY_ERR
1788 */
1789static LY_ERR
1790exp_check_token(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t exp_idx, enum lyxp_token want_tok, int strict)
1791{
1792 if (exp->used == exp_idx) {
1793 if (strict) {
1794 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1795 }
1796 return LY_EINVAL;
1797 }
1798
1799 if (want_tok && (exp->tokens[exp_idx] != want_tok)) {
1800 if (strict) {
1801 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1802 print_token(exp->tokens[exp_idx]), &exp->expr[exp->tok_pos[exp_idx]]);
1803 }
1804 return LY_EINVAL;
1805 }
1806
1807 return LY_SUCCESS;
1808}
1809
1810/**
1811 * @brief Stack operation push on the repeat array.
1812 *
1813 * @param[in] exp Expression to use.
1814 * @param[in] exp_idx Position in the expresion \p exp.
1815 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1816 */
1817static void
1818exp_repeat_push(struct lyxp_expr *exp, uint16_t exp_idx, uint16_t repeat_op_idx)
1819{
1820 uint16_t i;
1821
1822 if (exp->repeat[exp_idx]) {
1823 for (i = 0; exp->repeat[exp_idx][i]; ++i);
1824 exp->repeat[exp_idx] = realloc(exp->repeat[exp_idx], (i + 2) * sizeof *exp->repeat[exp_idx]);
1825 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1826 exp->repeat[exp_idx][i] = repeat_op_idx;
1827 exp->repeat[exp_idx][i + 1] = 0;
1828 } else {
1829 exp->repeat[exp_idx] = calloc(2, sizeof *exp->repeat[exp_idx]);
1830 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1831 exp->repeat[exp_idx][0] = repeat_op_idx;
1832 }
1833}
1834
1835/**
1836 * @brief Reparse Predicate. Logs directly on error.
1837 *
1838 * [7] Predicate ::= '[' Expr ']'
1839 *
1840 * @param[in] ctx Context for logging.
1841 * @param[in] exp Parsed XPath expression.
1842 * @param[in] exp_idx Position in the expression @p exp.
1843 * @return LY_ERR
1844 */
1845static LY_ERR
1846reparse_predicate(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
1847{
1848 LY_ERR rc;
1849
1850 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK1, 1);
1851 LY_CHECK_RET(rc);
1852 ++(*exp_idx);
1853
1854 rc = reparse_or_expr(ctx, exp, exp_idx);
1855 LY_CHECK_RET(rc);
1856
1857 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK2, 1);
1858 LY_CHECK_RET(rc);
1859 ++(*exp_idx);
1860
1861 return LY_SUCCESS;
1862}
1863
1864/**
1865 * @brief Reparse RelativeLocationPath. Logs directly on error.
1866 *
1867 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1868 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1869 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1870 *
1871 * @param[in] ctx Context for logging.
1872 * @param[in] exp Parsed XPath expression.
1873 * @param[in] exp_idx Position in the expression \p exp.
1874 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1875 */
1876static LY_ERR
1877reparse_relative_location_path(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
1878{
1879 LY_ERR rc;
1880
1881 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1);
1882 LY_CHECK_RET(rc);
1883
1884 goto step;
1885 do {
1886 /* '/' or '//' */
1887 ++(*exp_idx);
1888
1889 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1);
1890 LY_CHECK_RET(rc);
1891step:
1892 /* Step */
1893 switch (exp->tokens[*exp_idx]) {
1894 case LYXP_TOKEN_DOT:
1895 ++(*exp_idx);
1896 break;
1897
1898 case LYXP_TOKEN_DDOT:
1899 ++(*exp_idx);
1900 break;
1901
1902 case LYXP_TOKEN_AT:
1903 ++(*exp_idx);
1904
1905 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1);
1906 LY_CHECK_RET(rc);
1907 if ((exp->tokens[*exp_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*exp_idx] != LYXP_TOKEN_NODETYPE)) {
1908 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1909 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
1910 return LY_EVALID;
1911 }
1912 /* fall through */
1913 case LYXP_TOKEN_NAMETEST:
1914 ++(*exp_idx);
1915 goto reparse_predicate;
1916 break;
1917
1918 case LYXP_TOKEN_NODETYPE:
1919 ++(*exp_idx);
1920
1921 /* '(' */
1922 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1, 1);
1923 LY_CHECK_RET(rc);
1924 ++(*exp_idx);
1925
1926 /* ')' */
1927 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2, 1);
1928 LY_CHECK_RET(rc);
1929 ++(*exp_idx);
1930
1931reparse_predicate:
1932 /* Predicate* */
1933 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
1934 rc = reparse_predicate(ctx, exp, exp_idx);
1935 LY_CHECK_RET(rc);
1936 }
1937 break;
1938 default:
1939 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1940 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
1941 return LY_EVALID;
1942 }
1943 } while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH));
1944
1945 return LY_SUCCESS;
1946}
1947
1948/**
1949 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
1950 *
1951 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
1952 *
1953 * @param[in] ctx Context for logging.
1954 * @param[in] exp Parsed XPath expression.
1955 * @param[in] exp_idx Position in the expression \p exp.
1956 * @return LY_ERR
1957 */
1958static LY_ERR
1959reparse_absolute_location_path(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
1960{
1961 LY_ERR rc;
1962
1963 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, 1);
1964 LY_CHECK_RET(rc);
1965
1966 /* '/' RelativeLocationPath? */
1967 if (exp->tok_len[*exp_idx] == 1) {
1968 /* '/' */
1969 ++(*exp_idx);
1970
1971 if (exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 0)) {
1972 return LY_SUCCESS;
1973 }
1974 switch (exp->tokens[*exp_idx]) {
1975 case LYXP_TOKEN_DOT:
1976 case LYXP_TOKEN_DDOT:
1977 case LYXP_TOKEN_AT:
1978 case LYXP_TOKEN_NAMETEST:
1979 case LYXP_TOKEN_NODETYPE:
1980 rc = reparse_relative_location_path(ctx, exp, exp_idx);
1981 LY_CHECK_RET(rc);
1982 /* fall through */
1983 default:
1984 break;
1985 }
1986
1987 /* '//' RelativeLocationPath */
1988 } else {
1989 /* '//' */
1990 ++(*exp_idx);
1991
1992 rc = reparse_relative_location_path(ctx, exp, exp_idx);
1993 LY_CHECK_RET(rc);
1994 }
1995
1996 return LY_SUCCESS;
1997}
1998
1999/**
2000 * @brief Reparse FunctionCall. Logs directly on error.
2001 *
2002 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2003 *
2004 * @param[in] ctx Context for logging.
2005 * @param[in] exp Parsed XPath expression.
2006 * @param[in] exp_idx Position in the expression @p exp.
2007 * @return LY_ERR
2008 */
2009static LY_ERR
2010reparse_function_call(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2011{
2012 int min_arg_count = -1, max_arg_count, arg_count;
2013 uint16_t func_exp_idx;
2014 LY_ERR rc;
2015
2016 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_FUNCNAME, 1);
2017 LY_CHECK_RET(rc);
2018 func_exp_idx = *exp_idx;
2019 switch (exp->tok_len[*exp_idx]) {
2020 case 3:
2021 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
2022 min_arg_count = 1;
2023 max_arg_count = 1;
2024 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
2025 min_arg_count = 1;
2026 max_arg_count = 1;
2027 }
2028 break;
2029 case 4:
2030 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
2031 min_arg_count = 1;
2032 max_arg_count = 1;
2033 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
2034 min_arg_count = 0;
2035 max_arg_count = 0;
2036 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
2037 min_arg_count = 0;
2038 max_arg_count = 1;
2039 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
2040 min_arg_count = 0;
2041 max_arg_count = 0;
2042 }
2043 break;
2044 case 5:
2045 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
2046 min_arg_count = 1;
2047 max_arg_count = 1;
2048 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
2049 min_arg_count = 0;
2050 max_arg_count = 0;
2051 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
2052 min_arg_count = 1;
2053 max_arg_count = 1;
2054 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
2055 min_arg_count = 1;
2056 max_arg_count = 1;
2057 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
2058 min_arg_count = 1;
2059 max_arg_count = 1;
2060 }
2061 break;
2062 case 6:
2063 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
2064 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002065 max_arg_count = INT_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
2067 min_arg_count = 0;
2068 max_arg_count = 1;
2069 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
2070 min_arg_count = 0;
2071 max_arg_count = 1;
2072 }
2073 break;
2074 case 7:
2075 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
2076 min_arg_count = 1;
2077 max_arg_count = 1;
2078 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
2079 min_arg_count = 1;
2080 max_arg_count = 1;
2081 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
2082 min_arg_count = 0;
2083 max_arg_count = 0;
2084 }
2085 break;
2086 case 8:
2087 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
2088 min_arg_count = 2;
2089 max_arg_count = 2;
2090 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
2091 min_arg_count = 0;
2092 max_arg_count = 0;
2093 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
2094 min_arg_count = 2;
2095 max_arg_count = 2;
2096 }
2097 break;
2098 case 9:
2099 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
2100 min_arg_count = 2;
2101 max_arg_count = 3;
2102 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
2103 min_arg_count = 3;
2104 max_arg_count = 3;
2105 }
2106 break;
2107 case 10:
2108 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
2109 min_arg_count = 0;
2110 max_arg_count = 1;
2111 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
2112 min_arg_count = 1;
2113 max_arg_count = 1;
2114 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
2115 min_arg_count = 2;
2116 max_arg_count = 2;
2117 }
2118 break;
2119 case 11:
2120 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
2121 min_arg_count = 2;
2122 max_arg_count = 2;
2123 }
2124 break;
2125 case 12:
2126 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
2127 min_arg_count = 2;
2128 max_arg_count = 2;
2129 }
2130 break;
2131 case 13:
2132 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
2133 min_arg_count = 0;
2134 max_arg_count = 1;
2135 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
2136 min_arg_count = 0;
2137 max_arg_count = 1;
2138 }
2139 break;
2140 case 15:
2141 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
2142 min_arg_count = 0;
2143 max_arg_count = 1;
2144 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
2145 min_arg_count = 2;
2146 max_arg_count = 2;
2147 }
2148 break;
2149 case 16:
2150 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
2151 min_arg_count = 2;
2152 max_arg_count = 2;
2153 }
2154 break;
2155 case 20:
2156 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
2157 min_arg_count = 2;
2158 max_arg_count = 2;
2159 }
2160 break;
2161 }
2162 if (min_arg_count == -1) {
2163 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
2164 return LY_EINVAL;
2165 }
2166 ++(*exp_idx);
2167
2168 /* '(' */
2169 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1, 1);
2170 LY_CHECK_RET(rc);
2171 ++(*exp_idx);
2172
2173 /* ( Expr ( ',' Expr )* )? */
2174 arg_count = 0;
2175 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1);
2176 LY_CHECK_RET(rc);
2177 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
2178 ++arg_count;
2179 rc = reparse_or_expr(ctx, exp, exp_idx);
2180 LY_CHECK_RET(rc);
2181 }
2182 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_COMMA)) {
2183 ++(*exp_idx);
2184
2185 ++arg_count;
2186 rc = reparse_or_expr(ctx, exp, exp_idx);
2187 LY_CHECK_RET(rc);
2188 }
2189
2190 /* ')' */
2191 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2, 1);
2192 LY_CHECK_RET(rc);
2193 ++(*exp_idx);
2194
2195 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
2196 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_exp_idx],
2197 &exp->expr[exp->tok_pos[func_exp_idx]]);
2198 return LY_EVALID;
2199 }
2200
2201 return LY_SUCCESS;
2202}
2203
2204/**
2205 * @brief Reparse PathExpr. Logs directly on error.
2206 *
2207 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2208 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2209 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2210 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2211 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2212 *
2213 * @param[in] ctx Context for logging.
2214 * @param[in] exp Parsed XPath expression.
2215 * @param[in] exp_idx Position in the expression @p exp.
2216 * @return LY_ERR
2217 */
2218static LY_ERR
2219reparse_path_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2220{
2221 LY_ERR rc;
2222
2223 if (exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1)) {
2224 return -1;
2225 }
2226
2227 switch (exp->tokens[*exp_idx]) {
2228 case LYXP_TOKEN_PAR1:
2229 /* '(' Expr ')' Predicate* */
2230 ++(*exp_idx);
2231
2232 rc = reparse_or_expr(ctx, exp, exp_idx);
2233 LY_CHECK_RET(rc);
2234
2235 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2, 1);
2236 LY_CHECK_RET(rc);
2237 ++(*exp_idx);
2238 goto predicate;
2239 break;
2240 case LYXP_TOKEN_DOT:
2241 case LYXP_TOKEN_DDOT:
2242 case LYXP_TOKEN_AT:
2243 case LYXP_TOKEN_NAMETEST:
2244 case LYXP_TOKEN_NODETYPE:
2245 /* RelativeLocationPath */
2246 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2247 LY_CHECK_RET(rc);
2248 break;
2249 case LYXP_TOKEN_FUNCNAME:
2250 /* FunctionCall */
2251 rc = reparse_function_call(ctx, exp, exp_idx);
2252 LY_CHECK_RET(rc);
2253 goto predicate;
2254 break;
2255 case LYXP_TOKEN_OPERATOR_PATH:
2256 /* AbsoluteLocationPath */
2257 rc = reparse_absolute_location_path(ctx, exp, exp_idx);
2258 LY_CHECK_RET(rc);
2259 break;
2260 case LYXP_TOKEN_LITERAL:
2261 /* Literal */
2262 ++(*exp_idx);
2263 goto predicate;
2264 break;
2265 case LYXP_TOKEN_NUMBER:
2266 /* Number */
2267 ++(*exp_idx);
2268 goto predicate;
2269 break;
2270 default:
2271 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
2272 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
2273 return LY_EVALID;
2274 }
2275
2276 return LY_SUCCESS;
2277
2278predicate:
2279 /* Predicate* */
2280 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
2281 rc = reparse_predicate(ctx, exp, exp_idx);
2282 LY_CHECK_RET(rc);
2283 }
2284
2285 /* ('/' or '//') RelativeLocationPath */
2286 if ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)) {
2287
2288 /* '/' or '//' */
2289 ++(*exp_idx);
2290
2291 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2292 LY_CHECK_RET(rc);
2293 }
2294
2295 return LY_SUCCESS;
2296}
2297
2298/**
2299 * @brief Reparse UnaryExpr. Logs directly on error.
2300 *
2301 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2302 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2303 *
2304 * @param[in] ctx Context for logging.
2305 * @param[in] exp Parsed XPath expression.
2306 * @param[in] exp_idx Position in the expression @p exp.
2307 * @return LY_ERR
2308 */
2309static LY_ERR
2310reparse_unary_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2311{
2312 uint16_t prev_exp;
2313 LY_ERR rc;
2314
2315 /* ('-')* */
2316 prev_exp = *exp_idx;
2317 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0)
2318 && (exp->expr[exp->tok_pos[*exp_idx]] == '-')) {
2319 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
2320 ++(*exp_idx);
2321 }
2322
2323 /* PathExpr */
2324 prev_exp = *exp_idx;
2325 rc = reparse_path_expr(ctx, exp, exp_idx);
2326 LY_CHECK_RET(rc);
2327
2328 /* ('|' PathExpr)* */
2329 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_UNI, 0)) {
2330 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
2331 ++(*exp_idx);
2332
2333 rc = reparse_path_expr(ctx, exp, exp_idx);
2334 LY_CHECK_RET(rc);
2335 }
2336
2337 return LY_SUCCESS;
2338}
2339
2340/**
2341 * @brief Reparse AdditiveExpr. Logs directly on error.
2342 *
2343 * [15] AdditiveExpr ::= MultiplicativeExpr
2344 * | AdditiveExpr '+' MultiplicativeExpr
2345 * | AdditiveExpr '-' MultiplicativeExpr
2346 * [16] MultiplicativeExpr ::= UnaryExpr
2347 * | MultiplicativeExpr '*' UnaryExpr
2348 * | MultiplicativeExpr 'div' UnaryExpr
2349 * | MultiplicativeExpr 'mod' UnaryExpr
2350 *
2351 * @param[in] ctx Context for logging.
2352 * @param[in] exp Parsed XPath expression.
2353 * @param[in] exp_idx Position in the expression @p exp.
2354 * @return LY_ERR
2355 */
2356static LY_ERR
2357reparse_additive_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2358{
2359 uint16_t prev_add_exp, prev_mul_exp;
2360 LY_ERR rc;
2361
2362 prev_add_exp = *exp_idx;
2363 goto reparse_multiplicative_expr;
2364
2365 /* ('+' / '-' MultiplicativeExpr)* */
2366 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0)
2367 && ((exp->expr[exp->tok_pos[*exp_idx]] == '+') || (exp->expr[exp->tok_pos[*exp_idx]] == '-'))) {
2368 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
2369 ++(*exp_idx);
2370
2371reparse_multiplicative_expr:
2372 /* UnaryExpr */
2373 prev_mul_exp = *exp_idx;
2374 rc = reparse_unary_expr(ctx, exp, exp_idx);
2375 LY_CHECK_RET(rc);
2376
2377 /* ('*' / 'div' / 'mod' UnaryExpr)* */
2378 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0)
2379 && ((exp->expr[exp->tok_pos[*exp_idx]] == '*') || (exp->tok_len[*exp_idx] == 3))) {
2380 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
2381 ++(*exp_idx);
2382
2383 rc = reparse_unary_expr(ctx, exp, exp_idx);
2384 LY_CHECK_RET(rc);
2385 }
2386 }
2387
2388 return LY_SUCCESS;
2389}
2390
2391/**
2392 * @brief Reparse EqualityExpr. Logs directly on error.
2393 *
2394 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2395 * | EqualityExpr '!=' RelationalExpr
2396 * [14] RelationalExpr ::= AdditiveExpr
2397 * | RelationalExpr '<' AdditiveExpr
2398 * | RelationalExpr '>' AdditiveExpr
2399 * | RelationalExpr '<=' AdditiveExpr
2400 * | RelationalExpr '>=' AdditiveExpr
2401 *
2402 * @param[in] ctx Context for logging.
2403 * @param[in] exp Parsed XPath expression.
2404 * @param[in] exp_idx Position in the expression @p exp.
2405 * @return LY_ERR
2406 */
2407static LY_ERR
2408reparse_equality_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2409{
2410 uint16_t prev_eq_exp, prev_rel_exp;
2411 LY_ERR rc;
2412
2413 prev_eq_exp = *exp_idx;
2414 goto reparse_additive_expr;
2415
2416 /* ('=' / '!=' RelationalExpr)* */
2417 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP, 0)
2418 && ((exp->expr[exp->tok_pos[*exp_idx]] == '=') || (exp->expr[exp->tok_pos[*exp_idx]] == '!'))) {
2419 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
2420 ++(*exp_idx);
2421
2422reparse_additive_expr:
2423 /* AdditiveExpr */
2424 prev_rel_exp = *exp_idx;
2425 rc = reparse_additive_expr(ctx, exp, exp_idx);
2426 LY_CHECK_RET(rc);
2427
2428 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
2429 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP, 0)
2430 && ((exp->expr[exp->tok_pos[*exp_idx]] == '<') || (exp->expr[exp->tok_pos[*exp_idx]] == '>'))) {
2431 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
2432 ++(*exp_idx);
2433
2434 rc = reparse_additive_expr(ctx, exp, exp_idx);
2435 LY_CHECK_RET(rc);
2436 }
2437 }
2438
2439 return LY_SUCCESS;
2440}
2441
2442/**
2443 * @brief Reparse OrExpr. Logs directly on error.
2444 *
2445 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2446 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2447 *
2448 * @param[in] ctx Context for logging.
2449 * @param[in] exp Parsed XPath expression.
2450 * @param[in] exp_idx Position in the expression @p exp.
2451 * @return LY_ERR
2452 */
2453static LY_ERR
2454reparse_or_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2455{
2456 uint16_t prev_or_exp, prev_and_exp;
2457 LY_ERR rc;
2458
2459 prev_or_exp = *exp_idx;
2460 goto reparse_equality_expr;
2461
2462 /* ('or' AndExpr)* */
2463 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG, 0) && (exp->tok_len[*exp_idx] == 2)) {
2464 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
2465 ++(*exp_idx);
2466
2467reparse_equality_expr:
2468 /* EqualityExpr */
2469 prev_and_exp = *exp_idx;
2470 rc = reparse_equality_expr(ctx, exp, exp_idx);
2471 LY_CHECK_RET(rc);
2472
2473 /* ('and' EqualityExpr)* */
2474 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG, 0) && (exp->tok_len[*exp_idx] == 3)) {
2475 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
2476 ++(*exp_idx);
2477
2478 rc = reparse_equality_expr(ctx, exp, exp_idx);
2479 LY_CHECK_RET(rc);
2480 }
2481 }
2482
2483 return LY_SUCCESS;
2484}
Radek Krejcib1646a92018-11-02 16:08:26 +01002485
2486/**
2487 * @brief Parse NCName.
2488 *
2489 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002491 */
Radek Krejcid4270262019-01-07 15:07:25 +01002492static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002493parse_ncname(const char *ncname)
2494{
2495 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002496 size_t size;
2497 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002498
2499 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2500 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2501 return len;
2502 }
2503
2504 do {
2505 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002506 if (!*ncname) {
2507 break;
2508 }
Radek Krejcid4270262019-01-07 15:07:25 +01002509 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002510 } while (is_xmlqnamechar(uc) && (uc != ':'));
2511
2512 return len;
2513}
2514
2515/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002517 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002518 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002519 * @param[in] exp Expression to use.
2520 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002522 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002523 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002524 */
2525static LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526exp_add_token(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 +01002527{
2528 uint32_t prev;
2529
2530 if (exp->used == exp->size) {
2531 prev = exp->size;
2532 exp->size += LYXP_EXPR_SIZE_STEP;
2533 if (prev > exp->size) {
2534 LOGINT(ctx);
2535 return LY_EINT;
2536 }
2537
2538 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2539 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2540 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2541 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2542 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2543 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2544 }
2545
2546 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002548 exp->tok_len[exp->used] = tok_len;
2549 ++exp->used;
2550 return LY_SUCCESS;
2551}
2552
2553void
2554lyxp_expr_free(struct ly_ctx *ctx, struct lyxp_expr *expr)
2555{
2556 uint16_t i;
2557
2558 if (!expr) {
2559 return;
2560 }
2561
2562 lydict_remove(ctx, expr->expr);
2563 free(expr->tokens);
2564 free(expr->tok_pos);
2565 free(expr->tok_len);
2566 if (expr->repeat) {
2567 for (i = 0; i < expr->used; ++i) {
2568 free(expr->repeat[i]);
2569 }
2570 }
2571 free(expr->repeat);
2572 free(expr);
2573}
2574
2575struct lyxp_expr *
2576lyxp_expr_parse(struct ly_ctx *ctx, const char *expr)
2577{
2578 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002579 size_t parsed = 0, tok_len;
2580 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002581 enum lyxp_token tok_type;
2582 int prev_function_check = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002583 uint16_t exp_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002584
2585 if (strlen(expr) > UINT16_MAX) {
2586 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2587 return NULL;
2588 }
2589
2590 /* init lyxp_expr structure */
2591 ret = calloc(1, sizeof *ret);
2592 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
2593 ret->expr = lydict_insert(ctx, expr, strlen(expr));
2594 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2595 ret->used = 0;
2596 ret->size = LYXP_EXPR_SIZE_START;
2597 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2598 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2599
2600 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2601 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2602
2603 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2604 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2605
2606 while (is_xmlws(expr[parsed])) {
2607 ++parsed;
2608 }
2609
2610 do {
2611 if (expr[parsed] == '(') {
2612
2613 /* '(' */
2614 tok_len = 1;
2615 tok_type = LYXP_TOKEN_PAR1;
2616
2617 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2618 /* it is a NodeType/FunctionName after all */
2619 if (((ret->tok_len[ret->used - 1] == 4)
2620 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2621 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2622 ((ret->tok_len[ret->used - 1] == 7)
2623 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2624 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2625 } else {
2626 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2627 }
2628 prev_function_check = 0;
2629 }
2630
2631 } else if (expr[parsed] == ')') {
2632
2633 /* ')' */
2634 tok_len = 1;
2635 tok_type = LYXP_TOKEN_PAR2;
2636
2637 } else if (expr[parsed] == '[') {
2638
2639 /* '[' */
2640 tok_len = 1;
2641 tok_type = LYXP_TOKEN_BRACK1;
2642
2643 } else if (expr[parsed] == ']') {
2644
2645 /* ']' */
2646 tok_len = 1;
2647 tok_type = LYXP_TOKEN_BRACK2;
2648
2649 } else if (!strncmp(&expr[parsed], "..", 2)) {
2650
2651 /* '..' */
2652 tok_len = 2;
2653 tok_type = LYXP_TOKEN_DDOT;
2654
2655 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2656
2657 /* '.' */
2658 tok_len = 1;
2659 tok_type = LYXP_TOKEN_DOT;
2660
2661 } else if (expr[parsed] == '@') {
2662
2663 /* '@' */
2664 tok_len = 1;
2665 tok_type = LYXP_TOKEN_AT;
2666
2667 } else if (expr[parsed] == ',') {
2668
2669 /* ',' */
2670 tok_len = 1;
2671 tok_type = LYXP_TOKEN_COMMA;
2672
2673 } else if (expr[parsed] == '\'') {
2674
2675 /* Literal with ' */
2676 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len);
2677 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2678 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2679 ++tok_len;
2680 tok_type = LYXP_TOKEN_LITERAL;
2681
2682 } else if (expr[parsed] == '\"') {
2683
2684 /* Literal with " */
2685 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len);
2686 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2687 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2688 ++tok_len;
2689 tok_type = LYXP_TOKEN_LITERAL;
2690
2691 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2692
2693 /* Number */
2694 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len);
2695 if (expr[parsed + tok_len] == '.') {
2696 ++tok_len;
2697 for (; isdigit(expr[parsed + tok_len]); ++tok_len);
2698 }
2699 tok_type = LYXP_TOKEN_NUMBER;
2700
2701 } else if (expr[parsed] == '/') {
2702
2703 /* Operator '/', '//' */
2704 if (!strncmp(&expr[parsed], "//", 2)) {
2705 tok_len = 2;
2706 } else {
2707 tok_len = 1;
2708 }
2709 tok_type = LYXP_TOKEN_OPERATOR_PATH;
2710
2711 } else if (!strncmp(&expr[parsed], "!=", 2) || !strncmp(&expr[parsed], "<=", 2)
2712 || !strncmp(&expr[parsed], ">=", 2)) {
2713
2714 /* Operator '!=', '<=', '>=' */
2715 tok_len = 2;
2716 tok_type = LYXP_TOKEN_OPERATOR_COMP;
2717
2718 } else if (expr[parsed] == '|') {
2719
2720 /* Operator '|' */
2721 tok_len = 1;
2722 tok_type = LYXP_TOKEN_OPERATOR_UNI;
2723
2724 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2725
2726 /* Operator '+', '-' */
2727 tok_len = 1;
2728 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2729
2730 } else if ((expr[parsed] == '=') || (expr[parsed] == '<') || (expr[parsed] == '>')) {
2731
2732 /* Operator '=', '<', '>' */
2733 tok_len = 1;
2734 tok_type = LYXP_TOKEN_OPERATOR_COMP;
2735
2736 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2737 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2738 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2739 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
2740 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_LOG)
2741 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_COMP)
2742 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_MATH)
2743 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_UNI)
2744 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_PATH)) {
2745
2746 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2747 if (expr[parsed] == '*') {
2748 tok_len = 1;
2749 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2750
2751 } else if (!strncmp(&expr[parsed], "or", 2)) {
2752 tok_len = 2;
2753 tok_type = LYXP_TOKEN_OPERATOR_LOG;
2754
2755 } else if (!strncmp(&expr[parsed], "and", 3)) {
2756 tok_len = 3;
2757 tok_type = LYXP_TOKEN_OPERATOR_LOG;
2758
2759 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2760 tok_len = 3;
2761 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2762
2763 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002764 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2765 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 +01002766 goto error;
2767 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002768 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002769 goto error;
2770 }
2771 } else if (expr[parsed] == '*') {
2772
2773 /* NameTest '*' */
2774 tok_len = 1;
2775 tok_type = LYXP_TOKEN_NAMETEST;
2776
2777 } else {
2778
2779 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2780 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002781 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 +01002782 tok_len = ncname_len;
2783
2784 if (expr[parsed + tok_len] == ':') {
2785 ++tok_len;
2786 if (expr[parsed + tok_len] == '*') {
2787 ++tok_len;
2788 } else {
2789 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002790 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 +01002791 tok_len += ncname_len;
2792 }
2793 /* remove old flag to prevent ambiguities */
2794 prev_function_check = 0;
2795 tok_type = LYXP_TOKEN_NAMETEST;
2796 } else {
2797 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2798 prev_function_check = 1;
2799 tok_type = LYXP_TOKEN_NAMETEST;
2800 }
2801 }
2802
2803 /* store the token, move on to the next one */
2804 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2805 parsed += tok_len;
2806 while (is_xmlws(expr[parsed])) {
2807 ++parsed;
2808 }
2809
2810 } while (expr[parsed]);
2811
2812 /* prealloc repeat */
2813 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2814 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
2815
Michal Vasko03ff5a72019-09-11 13:49:33 +02002816 /* fill repeat */
2817 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &exp_idx), error);
2818 if (ret->used > exp_idx) {
2819 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, "Unknown", &ret->expr[ret->tok_pos[exp_idx]]);
2820 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2821 &ret->expr[ret->tok_pos[exp_idx]]);
2822 goto error;
2823 }
2824
2825 print_expr_struct_debug(ret);
2826
Radek Krejcib1646a92018-11-02 16:08:26 +01002827 return ret;
2828
2829error:
2830 lyxp_expr_free(ctx, ret);
2831 return NULL;
2832}
2833
Michal Vasko03ff5a72019-09-11 13:49:33 +02002834/*
2835 * warn functions
2836 *
2837 * Warn functions check specific reasonable conditions for schema XPath
2838 * and print a warning if they are not satisfied.
2839 */
2840
2841/**
2842 * @brief Get the last-added schema node that is currently in the context.
2843 *
2844 * @param[in] set Set to search in.
2845 * @return Last-added schema context node, NULL if no node is in context.
2846 */
2847static struct lysc_node *
2848warn_get_scnode_in_ctx(struct lyxp_set *set)
2849{
2850 uint32_t i;
2851
2852 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2853 return NULL;
2854 }
2855
2856 i = set->used;
2857 do {
2858 --i;
2859 if (set->val.scnodes[i].in_ctx == 1) {
2860 /* if there are more, simply return the first found (last added) */
2861 return set->val.scnodes[i].scnode;
2862 }
2863 } while (i);
2864
2865 return NULL;
2866}
2867
2868/**
2869 * @brief Test whether a type is numeric - integer type or decimal64.
2870 *
2871 * @param[in] type Type to test.
2872 * @return 1 if numeric, 0 otherwise.
2873 */
2874static int
2875warn_is_numeric_type(struct lysc_type *type)
2876{
2877 struct lysc_type_union *uni;
2878 int ret;
2879 uint32_t i;
2880
2881 switch (type->basetype) {
2882 case LY_TYPE_DEC64:
2883 case LY_TYPE_INT8:
2884 case LY_TYPE_UINT8:
2885 case LY_TYPE_INT16:
2886 case LY_TYPE_UINT16:
2887 case LY_TYPE_INT32:
2888 case LY_TYPE_UINT32:
2889 case LY_TYPE_INT64:
2890 case LY_TYPE_UINT64:
2891 return 1;
2892 case LY_TYPE_UNION:
2893 uni = (struct lysc_type_union *)type;
2894 LY_ARRAY_FOR(uni->types, i) {
2895 ret = warn_is_numeric_type(uni->types[i]);
2896 if (ret) {
2897 /* found a suitable type */
2898 return 1;
2899 }
2900 }
2901 /* did not find any suitable type */
2902 return 0;
2903 case LY_TYPE_LEAFREF:
2904 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
2905 default:
2906 return 0;
2907 }
2908}
2909
2910/**
2911 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
2912 *
2913 * @param[in] type Type to test.
2914 * @return 1 if string, 0 otherwise.
2915 */
2916static int
2917warn_is_string_type(struct lysc_type *type)
2918{
2919 struct lysc_type_union *uni;
2920 int ret;
2921 uint32_t i;
2922
2923 switch (type->basetype) {
2924 case LY_TYPE_BITS:
2925 case LY_TYPE_ENUM:
2926 case LY_TYPE_IDENT:
2927 case LY_TYPE_INST:
2928 case LY_TYPE_STRING:
2929 return 1;
2930 case LY_TYPE_UNION:
2931 uni = (struct lysc_type_union *)type;
2932 LY_ARRAY_FOR(uni->types, i) {
2933 ret = warn_is_string_type(uni->types[i]);
2934 if (ret) {
2935 /* found a suitable type */
2936 return 1;
2937 }
2938 }
2939 /* did not find any suitable type */
2940 return 0;
2941 case LY_TYPE_LEAFREF:
2942 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
2943 default:
2944 return 0;
2945 }
2946}
2947
2948/**
2949 * @brief Test whether a type is one specific type.
2950 *
2951 * @param[in] type Type to test.
2952 * @param[in] base Expected type.
2953 * @return 1 if it is, 0 otherwise.
2954 */
2955static int
2956warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
2957{
2958 struct lysc_type_union *uni;
2959 int ret;
2960 uint32_t i;
2961
2962 if (type->basetype == base) {
2963 return 1;
2964 } else if (type->basetype == LY_TYPE_UNION) {
2965 uni = (struct lysc_type_union *)type;
2966 LY_ARRAY_FOR(uni->types, i) {
2967 ret = warn_is_specific_type(uni->types[i], base);
2968 if (ret) {
2969 /* found a suitable type */
2970 return 1;
2971 }
2972 }
2973 /* did not find any suitable type */
2974 return 0;
2975 } else if (type->basetype == LY_TYPE_LEAFREF) {
2976 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
2977 }
2978
2979 return 0;
2980}
2981
2982/**
2983 * @brief Get next type of a (union) type.
2984 *
2985 * @param[in] type Base type.
2986 * @param[in] prev_type Previously returned type.
2987 * @return Next type or NULL.
2988 */
2989static struct lysc_type *
2990warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
2991{
2992 struct lysc_type_union *uni;
2993 int found = 0;
2994 uint32_t i;
2995
2996 switch (type->basetype) {
2997 case LY_TYPE_UNION:
2998 uni = (struct lysc_type_union *)type;
2999 if (!prev_type) {
3000 return uni->types[0];
3001 }
3002 LY_ARRAY_FOR(uni->types, i) {
3003 if (found) {
3004 return uni->types[i];
3005 }
3006 if (prev_type == uni->types[i]) {
3007 found = 1;
3008 }
3009 }
3010 return NULL;
3011 default:
3012 if (prev_type) {
3013 assert(type == prev_type);
3014 return NULL;
3015 } else {
3016 return type;
3017 }
3018 }
3019}
3020
3021/**
3022 * @brief Test whether 2 types have a common type.
3023 *
3024 * @param[in] type1 First type.
3025 * @param[in] type2 Second type.
3026 * @return 1 if they do, 0 otherwise.
3027 */
3028static int
3029warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3030{
3031 struct lysc_type *t1, *rt1, *t2, *rt2;
3032
3033 t1 = NULL;
3034 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3035 if (t1->basetype == LY_TYPE_LEAFREF) {
3036 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3037 } else {
3038 rt1 = t1;
3039 }
3040
3041 t2 = NULL;
3042 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3043 if (t2->basetype == LY_TYPE_LEAFREF) {
3044 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3045 } else {
3046 rt2 = t2;
3047 }
3048
3049 if (rt2->basetype == rt1->basetype) {
3050 /* match found */
3051 return 1;
3052 }
3053 }
3054 }
3055
3056 return 0;
3057}
3058
3059/**
3060 * @brief Check both operands of comparison operators.
3061 *
3062 * @param[in] ctx Context for errors.
3063 * @param[in] set1 First operand set.
3064 * @param[in] set2 Second operand set.
3065 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3066 * @param[in] expr Start of the expression to print with the warning.
3067 * @param[in] tok_pos Token position.
3068 */
3069static void
3070warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3071{
3072 struct lysc_node_leaf *node1, *node2;
3073 int leaves = 1, warning = 0;
3074
3075 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3076 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3077
3078 if (!node1 && !node2) {
3079 /* no node-sets involved, nothing to do */
3080 return;
3081 }
3082
3083 if (node1) {
3084 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3085 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3086 warning = 1;
3087 leaves = 0;
3088 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3089 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3090 warning = 1;
3091 }
3092 }
3093
3094 if (node2) {
3095 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3096 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3097 warning = 1;
3098 leaves = 0;
3099 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3100 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3101 warning = 1;
3102 }
3103 }
3104
3105 if (node1 && node2 && leaves && !numbers_only) {
3106 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3107 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3108 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3109 && !warn_is_equal_type(node1->type, node2->type))) {
3110 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3111 warning = 1;
3112 }
3113 }
3114
3115 if (warning) {
3116 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3117 }
3118}
3119
3120/**
3121 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3122 *
3123 * @param[in] exp Parsed XPath expression.
3124 * @param[in] set Set with the leaf/leaf-list.
3125 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3126 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3127 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3128 */
3129static void
3130warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3131{
3132 struct lysc_node *scnode;
3133 struct lysc_type *type;
3134 char *value;
3135 LY_ERR rc;
3136 struct ly_err_item *err = NULL;
3137
3138 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3139 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3140 /* check that the node can have the specified value */
3141 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3142 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3143 } else {
3144 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3145 }
3146 if (!value) {
3147 LOGMEM(set->ctx);
3148 return;
3149 }
3150
3151 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3152 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3153 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3154 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3155 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3156 exp->expr + exp->tok_pos[equal_exp]);
3157 }
3158
3159 type = ((struct lysc_node_leaf *)scnode)->type;
3160 if (type->basetype != LY_TYPE_IDENT) {
3161 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA,
3162 lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err);
3163
3164 if (err) {
3165 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3166 ly_err_free(err);
3167 } else if (rc != LY_SUCCESS) {
3168 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3169 }
3170 if (rc != LY_SUCCESS) {
3171 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3172 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3173 exp->expr + exp->tok_pos[equal_exp]);
3174 }
3175 }
3176 free(value);
3177 }
3178}
3179
3180/*
3181 * XPath functions
3182 */
3183
3184/**
3185 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3186 * depending on whether the first node bit value from the second argument is set.
3187 *
3188 * @param[in] args Array of arguments.
3189 * @param[in] arg_count Count of elements in @p args.
3190 * @param[in,out] set Context and result set at the same time.
3191 * @param[in] options XPath options.
3192 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3193 */
3194static LY_ERR
3195xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3196{
3197 struct lyd_node_term *leaf;
3198 struct lysc_node_leaf *sleaf;
3199 struct lysc_type_bits *bits;
3200 LY_ERR rc = LY_SUCCESS;
3201 uint32_t i;
3202
3203 if (options & LYXP_SCNODE_ALL) {
3204 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3205 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
3206 rc = LY_EINVAL;
3207 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3208 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3209 rc = LY_EINVAL;
3210 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3211 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3212 rc = LY_EINVAL;
3213 }
3214
3215 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3216 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3217 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3218 rc = LY_EINVAL;
3219 } else if (!warn_is_string_type(sleaf->type)) {
3220 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
3221 rc = LY_EINVAL;
3222 }
3223 }
3224 set_scnode_clear_ctx(set);
3225 return rc;
3226 }
3227
3228 if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) {
3229 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)");
3230 return LY_EVALID;
3231 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003232 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003233 LY_CHECK_RET(rc);
3234
3235 set_fill_boolean(set, 0);
3236 if (args[0]->type == LYXP_SET_NODE_SET) {
3237 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3238 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3239 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3240 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
3241 LY_ARRAY_FOR(bits->bits, i) {
3242 if (!strcmp(bits->bits[i].name, args[1]->val.str)) {
3243 set_fill_boolean(set, 1);
3244 break;
3245 }
3246 }
3247 }
3248 }
3249
3250 return LY_SUCCESS;
3251}
3252
3253/**
3254 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3255 * with the argument converted to boolean.
3256 *
3257 * @param[in] args Array of arguments.
3258 * @param[in] arg_count Count of elements in @p args.
3259 * @param[in,out] set Context and result set at the same time.
3260 * @param[in] options XPath options.
3261 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3262 */
3263static LY_ERR
3264xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3265{
3266 LY_ERR rc;
3267
3268 if (options & LYXP_SCNODE_ALL) {
3269 set_scnode_clear_ctx(set);
3270 return LY_SUCCESS;
3271 }
3272
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003273 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003274 LY_CHECK_RET(rc);
3275 set_fill_set(set, args[0]);
3276
3277 return LY_SUCCESS;
3278}
3279
3280/**
3281 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3282 * with the first argument rounded up to the nearest integer.
3283 *
3284 * @param[in] args Array of arguments.
3285 * @param[in] arg_count Count of elements in @p args.
3286 * @param[in,out] set Context and result set at the same time.
3287 * @param[in] options XPath options.
3288 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3289 */
3290static LY_ERR
3291xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3292{
3293 struct lysc_node_leaf *sleaf;
3294 LY_ERR rc = LY_SUCCESS;
3295
3296 if (options & LYXP_SCNODE_ALL) {
3297 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3298 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
3299 rc = LY_EINVAL;
3300 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3301 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3302 rc = LY_EINVAL;
3303 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3304 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3305 rc = LY_EINVAL;
3306 }
3307 set_scnode_clear_ctx(set);
3308 return rc;
3309 }
3310
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003311 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003312 LY_CHECK_RET(rc);
3313 if ((long long)args[0]->val.num != args[0]->val.num) {
3314 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3315 } else {
3316 set_fill_number(set, args[0]->val.num);
3317 }
3318
3319 return LY_SUCCESS;
3320}
3321
3322/**
3323 * @brief Execute the XPath concat(string, string, string*) function.
3324 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3325 *
3326 * @param[in] args Array of arguments.
3327 * @param[in] arg_count Count of elements in @p args.
3328 * @param[in,out] set Context and result set at the same time.
3329 * @param[in] options XPath options.
3330 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3331 */
3332static LY_ERR
3333xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3334{
3335 uint16_t i;
3336 char *str = NULL;
3337 size_t used = 1;
3338 LY_ERR rc = LY_SUCCESS;
3339 struct lysc_node_leaf *sleaf;
3340
3341 if (options & LYXP_SCNODE_ALL) {
3342 for (i = 0; i < arg_count; ++i) {
3343 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3344 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3345 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3346 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3347 rc = LY_EINVAL;
3348 } else if (!warn_is_string_type(sleaf->type)) {
3349 LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", __func__, i + 1, sleaf->name);
3350 rc = LY_EINVAL;
3351 }
3352 }
3353 }
3354 set_scnode_clear_ctx(set);
3355 return rc;
3356 }
3357
3358 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003359 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003360 if (rc != LY_SUCCESS) {
3361 free(str);
3362 return rc;
3363 }
3364
3365 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3366 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3367 strcpy(str + used - 1, args[i]->val.str);
3368 used += strlen(args[i]->val.str);
3369 }
3370
3371 /* free, kind of */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003372 lyxp_set_cast(set, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003373 set->type = LYXP_SET_STRING;
3374 set->val.str = str;
3375
3376 return LY_SUCCESS;
3377}
3378
3379/**
3380 * @brief Execute the XPath contains(string, string) function.
3381 * Returns LYXP_SET_BOOLEAN whether the second argument can
3382 * be found in the first or not.
3383 *
3384 * @param[in] args Array of arguments.
3385 * @param[in] arg_count Count of elements in @p args.
3386 * @param[in,out] set Context and result set at the same time.
3387 * @param[in] options XPath options.
3388 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3389 */
3390static LY_ERR
3391xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3392{
3393 struct lysc_node_leaf *sleaf;
3394 LY_ERR rc = LY_SUCCESS;
3395
3396 if (options & LYXP_SCNODE_ALL) {
3397 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3398 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3399 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3400 rc = LY_EINVAL;
3401 } else if (!warn_is_string_type(sleaf->type)) {
3402 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
3403 rc = LY_EINVAL;
3404 }
3405 }
3406
3407 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3408 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3409 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3410 rc = LY_EINVAL;
3411 } else if (!warn_is_string_type(sleaf->type)) {
3412 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
3413 rc = LY_EINVAL;
3414 }
3415 }
3416 set_scnode_clear_ctx(set);
3417 return rc;
3418 }
3419
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003420 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003421 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003422 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003423 LY_CHECK_RET(rc);
3424
3425 if (strstr(args[0]->val.str, args[1]->val.str)) {
3426 set_fill_boolean(set, 1);
3427 } else {
3428 set_fill_boolean(set, 0);
3429 }
3430
3431 return LY_SUCCESS;
3432}
3433
3434/**
3435 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3436 * with the size of the node-set from the argument.
3437 *
3438 * @param[in] args Array of arguments.
3439 * @param[in] arg_count Count of elements in @p args.
3440 * @param[in,out] set Context and result set at the same time.
3441 * @param[in] options XPath options.
3442 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3443 */
3444static LY_ERR
3445xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3446{
3447 struct lysc_node *scnode = NULL;
3448 LY_ERR rc = LY_SUCCESS;
3449
3450 if (options & LYXP_SCNODE_ALL) {
3451 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3452 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
3453 rc = LY_EINVAL;
3454 }
3455 set_scnode_clear_ctx(set);
3456 return rc;
3457 }
3458
3459 if (args[0]->type == LYXP_SET_EMPTY) {
3460 set_fill_number(set, 0);
3461 return LY_SUCCESS;
3462 }
3463
3464 if (args[0]->type != LYXP_SET_NODE_SET) {
3465 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3466 return LY_EVALID;
3467 }
3468
3469 set_fill_number(set, args[0]->used);
3470 return LY_SUCCESS;
3471}
3472
3473/**
3474 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3475 * with the context with the intial node.
3476 *
3477 * @param[in] args Array of arguments.
3478 * @param[in] arg_count Count of elements in @p args.
3479 * @param[in,out] set Context and result set at the same time.
3480 * @param[in] options XPath options.
3481 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3482 */
3483static LY_ERR
3484xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3485{
3486 if (arg_count || args) {
3487 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3488 return LY_EVALID;
3489 }
3490
3491 if (options & LYXP_SCNODE_ALL) {
3492 set_scnode_clear_ctx(set);
3493
Michal Vaskoecd62de2019-11-13 12:35:11 +01003494 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003495 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003496 lyxp_set_cast(set, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003497
3498 /* position is filled later */
3499 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3500 }
3501
3502 return LY_SUCCESS;
3503}
3504
3505/**
3506 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3507 * leafref or instance-identifier target node(s).
3508 *
3509 * @param[in] args Array of arguments.
3510 * @param[in] arg_count Count of elements in @p args.
3511 * @param[in,out] set Context and result set at the same time.
3512 * @param[in] options XPath options.
3513 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3514 */
3515static LY_ERR
3516xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3517{
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003518 struct lysc_ctx cctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003519 struct lyd_node_term *leaf;
3520 struct lysc_node_leaf *sleaf;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003521 const struct lysc_node *target;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003522 const struct lyd_node *node;
3523 char *errmsg = NULL;
3524 const char *val;
3525 int dynamic;
3526 LY_ERR rc = LY_SUCCESS;
3527
3528 if (options & LYXP_SCNODE_ALL) {
3529 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3530 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
3531 rc = LY_EINVAL;
3532 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3533 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3534 rc = LY_EINVAL;
3535 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3536 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3537 __func__, sleaf->name);
3538 rc = LY_EINVAL;
3539 }
3540 set_scnode_clear_ctx(set);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003541 if ((rc == LY_SUCCESS) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
3542 cctx.ctx = set->ctx;
3543 rc = lys_compile_leafref_validate(&cctx, (struct lysc_node *)sleaf, (struct lysc_type_leafref *)sleaf->type, &target);
3544 /* it was already validated, it must succeed */
3545 if (rc == LY_SUCCESS) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01003546 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003547 }
3548 }
3549
Michal Vasko03ff5a72019-09-11 13:49:33 +02003550 return rc;
3551 }
3552
3553 if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) {
3554 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3555 return LY_EVALID;
3556 }
3557
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003558 lyxp_set_cast(set, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003559 if (args[0]->type != LYXP_SET_EMPTY) {
3560 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3561 sleaf = (struct lysc_node_leaf *)leaf->schema;
3562 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3563 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3564 /* find leafref target */
3565 val = lyd_value2str(leaf, &dynamic);
3566 node = ly_type_find_leafref(set->ctx, sleaf->type, val, strlen(val), (struct lyd_node *)leaf,
3567 set->trees, &leaf->value, &errmsg);
3568 if (dynamic) {
3569 free((char *)val);
3570 }
3571 if (!node) {
3572 LOGERR(set->ctx, LY_EINVAL, errmsg);
3573 free(errmsg);
3574 return LY_EINVAL;
3575 }
3576
3577 /* insert it */
3578 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
3579 } else {
3580 assert(sleaf->type->basetype == LY_TYPE_INST);
3581 node = (struct lyd_node *)lyd_target(leaf->value.target, set->trees);
3582 if (!node) {
3583 val = lyd_value2str(leaf, &dynamic);
3584 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3585 if (dynamic) {
3586 free((char *)val);
3587 }
3588 return LY_EVALID;
3589 }
3590 }
3591 }
3592 }
3593
3594 return LY_SUCCESS;
3595}
3596
3597/**
3598 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3599 * on whether the first argument nodes contain a node of an identity derived from the second
3600 * argument identity.
3601 *
3602 * @param[in] args Array of arguments.
3603 * @param[in] arg_count Count of elements in @p args.
3604 * @param[in,out] set Context and result set at the same time.
3605 * @param[in] options XPath options.
3606 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3607 */
3608static LY_ERR
3609xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3610{
3611 uint16_t i;
3612 struct lyd_node_term *leaf;
3613 struct lysc_node_leaf *sleaf;
3614 struct lyd_value data = {0};
3615 struct ly_err_item *err = NULL;
3616 LY_ERR rc = LY_SUCCESS;
3617 int found;
3618
3619 if (options & LYXP_SCNODE_ALL) {
3620 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3621 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
3622 rc = LY_EINVAL;
3623 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3624 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3625 rc = LY_EINVAL;
3626 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3627 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
3628 rc = LY_EINVAL;
3629 }
3630
3631 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3632 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3633 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3634 rc = LY_EINVAL;
3635 } else if (!warn_is_string_type(sleaf->type)) {
3636 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
3637 rc = LY_EINVAL;
3638 }
3639 }
3640 set_scnode_clear_ctx(set);
3641 return rc;
3642 }
3643
3644 if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) {
3645 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(node-set, string)");
3646 return LY_EVALID;
3647 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003648 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 LY_CHECK_RET(rc);
3650
3651 set_fill_boolean(set, 0);
3652 if (args[0]->type != LYXP_SET_EMPTY) {
3653 found = 0;
3654 for (i = 0; i < args[0]->used; ++i) {
3655 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3656 sleaf = (struct lysc_node_leaf *)leaf->schema;
3657 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3658 /* store args[1] into ident */
3659 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3660 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3661 (struct lyd_node *)leaf, set->trees, &data, NULL, &err);
3662 if (err) {
3663 ly_err_print(err);
3664 ly_err_free(err);
3665 }
3666 LY_CHECK_RET(rc);
3667
3668 LY_ARRAY_FOR(data.ident->derived, i) {
3669 if (data.ident->derived[i] == leaf->value.ident) {
3670 set_fill_boolean(set, 1);
3671 found = 1;
3672 break;
3673 }
3674 }
3675 if (found) {
3676 break;
3677 }
3678 }
3679 }
3680 }
3681
3682 return LY_SUCCESS;
3683}
3684
3685/**
3686 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3687 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3688 * the second argument identity.
3689 *
3690 * @param[in] args Array of arguments.
3691 * @param[in] arg_count Count of elements in @p args.
3692 * @param[in,out] set Context and result set at the same time.
3693 * @param[in] options XPath options.
3694 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3695 */
3696static LY_ERR
3697xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3698{
3699 uint16_t i;
3700 struct lyd_node_term *leaf;
3701 struct lysc_node_leaf *sleaf;
3702 struct lyd_value data = {0};
3703 struct ly_err_item *err = NULL;
3704 LY_ERR rc = LY_SUCCESS;
3705 int found;
3706
3707 if (options & LYXP_SCNODE_ALL) {
3708 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3709 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
3710 rc = LY_EINVAL;
3711 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3712 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3713 rc = LY_EINVAL;
3714 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3715 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
3716 rc = LY_EINVAL;
3717 }
3718
3719 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3720 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3721 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3722 rc = LY_EINVAL;
3723 } else if (!warn_is_string_type(sleaf->type)) {
3724 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
3725 rc = LY_EINVAL;
3726 }
3727 }
3728 set_scnode_clear_ctx(set);
3729 return rc;
3730 }
3731
3732 if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) {
3733 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from-or-self(node-set, string)");
3734 return LY_EVALID;
3735 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003736 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 LY_CHECK_RET(rc);
3738
3739 set_fill_boolean(set, 0);
3740 if (args[0]->type != LYXP_SET_EMPTY) {
3741 found = 0;
3742 for (i = 0; i < args[0]->used; ++i) {
3743 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3744 sleaf = (struct lysc_node_leaf *)leaf->schema;
3745 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3746 /* store args[1] into ident */
3747 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3748 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3749 (struct lyd_node *)leaf, set->trees, &data, NULL, &err);
3750 if (err) {
3751 ly_err_print(err);
3752 ly_err_free(err);
3753 }
3754 LY_CHECK_RET(rc);
3755
3756 if (data.ident == leaf->value.ident) {
3757 set_fill_boolean(set, 1);
3758 break;
3759 }
3760 LY_ARRAY_FOR(data.ident->derived, i) {
3761 if (data.ident->derived[i] == leaf->value.ident) {
3762 set_fill_boolean(set, 1);
3763 found = 1;
3764 break;
3765 }
3766 }
3767 if (found) {
3768 break;
3769 }
3770 }
3771 }
3772 }
3773
3774 return LY_SUCCESS;
3775}
3776
3777/**
3778 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3779 * with the integer value of the first node's enum value, otherwise NaN.
3780 *
3781 * @param[in] args Array of arguments.
3782 * @param[in] arg_count Count of elements in @p args.
3783 * @param[in,out] set Context and result set at the same time.
3784 * @param[in] options XPath options.
3785 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3786 */
3787static LY_ERR
3788xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3789{
3790 struct lyd_node_term *leaf;
3791 struct lysc_node_leaf *sleaf;
3792 LY_ERR rc = LY_SUCCESS;
3793
3794 if (options & LYXP_SCNODE_ALL) {
3795 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3796 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
3797 rc = LY_EINVAL;
3798 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3799 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3800 rc = LY_EINVAL;
3801 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3802 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3803 rc = LY_EINVAL;
3804 }
3805 set_scnode_clear_ctx(set);
3806 return rc;
3807 }
3808
3809 if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) {
3810 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3811 return LY_EVALID;
3812 }
3813
3814 set_fill_number(set, NAN);
3815 if (args[0]->type == LYXP_SET_NODE_SET) {
3816 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3817 sleaf = (struct lysc_node_leaf *)leaf->schema;
3818 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3819 set_fill_number(set, leaf->value.enum_item->value);
3820 }
3821 }
3822
3823 return LY_SUCCESS;
3824}
3825
3826/**
3827 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3828 * with false value.
3829 *
3830 * @param[in] args Array of arguments.
3831 * @param[in] arg_count Count of elements in @p args.
3832 * @param[in,out] set Context and result set at the same time.
3833 * @param[in] options XPath options.
3834 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3835 */
3836static LY_ERR
3837xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3838{
3839 if (options & LYXP_SCNODE_ALL) {
3840 set_scnode_clear_ctx(set);
3841 return LY_SUCCESS;
3842 }
3843
3844 set_fill_boolean(set, 0);
3845 return LY_SUCCESS;
3846}
3847
3848/**
3849 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3850 * with the first argument floored (truncated).
3851 *
3852 * @param[in] args Array of arguments.
3853 * @param[in] arg_count Count of elements in @p args.
3854 * @param[in,out] set Context and result set at the same time.
3855 * @param[in] options XPath options.
3856 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3857 */
3858static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003859xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860{
3861 LY_ERR rc;
3862
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003863 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003864 LY_CHECK_RET(rc);
3865 if (isfinite(args[0]->val.num)) {
3866 set_fill_number(set, (long long)args[0]->val.num);
3867 }
3868
3869 return LY_SUCCESS;
3870}
3871
3872/**
3873 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3874 * whether the language of the text matches the one from the argument.
3875 *
3876 * @param[in] args Array of arguments.
3877 * @param[in] arg_count Count of elements in @p args.
3878 * @param[in,out] set Context and result set at the same time.
3879 * @param[in] options XPath options.
3880 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3881 */
3882static LY_ERR
3883xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3884{
3885 const struct lyd_node *node;
3886 struct lysc_node_leaf *sleaf;
3887 struct lyd_attr *attr = NULL;
3888 const char *val;
3889 int i, dynamic;
3890 LY_ERR rc = LY_SUCCESS;
3891
3892 if (options & LYXP_SCNODE_ALL) {
3893 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3894 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3895 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
3896 rc = LY_EINVAL;
3897 } else if (!warn_is_string_type(sleaf->type)) {
3898 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
3899 rc = LY_EINVAL;
3900 }
3901 }
3902 set_scnode_clear_ctx(set);
3903 return rc;
3904 }
3905
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003906 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003907 LY_CHECK_RET(rc);
3908
3909 if (set->type == LYXP_SET_EMPTY) {
3910 set_fill_boolean(set, 0);
3911 return LY_SUCCESS;
3912 }
3913 if (set->type != LYXP_SET_NODE_SET) {
3914 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3915 return LY_EVALID;
3916 }
3917
3918 switch (set->val.nodes[0].type) {
3919 case LYXP_NODE_ELEM:
3920 case LYXP_NODE_TEXT:
3921 node = set->val.nodes[0].node;
3922 break;
3923 case LYXP_NODE_ATTR:
3924 node = set->val.attrs[0].attr->parent;
3925 break;
3926 default:
3927 /* nothing to do with roots */
3928 set_fill_boolean(set, 0);
3929 return LY_SUCCESS;
3930 }
3931
3932 /* find lang attribute */
3933 for (; node; node = (struct lyd_node *)node->parent) {
3934 for (attr = node->attr; attr; attr = attr->next) {
3935 /* annotations */
3936 if (attr->name && !strcmp(attr->name, "lang") && !strcmp(attr->annotation->module->name, "xml")) {
3937 break;
3938 }
3939 }
3940
3941 if (attr) {
3942 break;
3943 }
3944 }
3945
3946 /* compare languages */
3947 if (!attr) {
3948 set_fill_boolean(set, 0);
3949 } else {
3950 val = lyd_attr2str(attr, &dynamic);
3951 for (i = 0; args[0]->val.str[i]; ++i) {
3952 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
3953 set_fill_boolean(set, 0);
3954 break;
3955 }
3956 }
3957 if (!args[0]->val.str[i]) {
3958 if (!val[i] || (val[i] == '-')) {
3959 set_fill_boolean(set, 1);
3960 } else {
3961 set_fill_boolean(set, 0);
3962 }
3963 }
3964 if (dynamic) {
3965 free((char *)val);
3966 }
3967 }
3968
3969 return LY_SUCCESS;
3970}
3971
3972/**
3973 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
3974 * with the context size.
3975 *
3976 * @param[in] args Array of arguments.
3977 * @param[in] arg_count Count of elements in @p args.
3978 * @param[in,out] set Context and result set at the same time.
3979 * @param[in] options XPath options.
3980 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
3981 */
3982static LY_ERR
3983xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3984{
3985 if (options & LYXP_SCNODE_ALL) {
3986 set_scnode_clear_ctx(set);
3987 return LY_SUCCESS;
3988 }
3989
3990 if (set->type == LYXP_SET_EMPTY) {
3991 set_fill_number(set, 0);
3992 return LY_SUCCESS;
3993 }
3994 if (set->type != LYXP_SET_NODE_SET) {
3995 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
3996 return LY_EVALID;
3997 }
3998
3999 set_fill_number(set, set->ctx_size);
4000 return LY_SUCCESS;
4001}
4002
4003/**
4004 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4005 * with the node name without namespace from the argument or the context.
4006 *
4007 * @param[in] args Array of arguments.
4008 * @param[in] arg_count Count of elements in @p args.
4009 * @param[in,out] set Context and result set at the same time.
4010 * @param[in] options XPath options.
4011 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4012 */
4013static LY_ERR
4014xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4015{
4016 struct lyxp_set_node *item;
4017 /* suppress unused variable warning */
4018 (void)options;
4019
4020 if (options & LYXP_SCNODE_ALL) {
4021 set_scnode_clear_ctx(set);
4022 return LY_SUCCESS;
4023 }
4024
4025 if (arg_count) {
4026 if (args[0]->type == LYXP_SET_EMPTY) {
4027 set_fill_string(set, "", 0);
4028 return LY_SUCCESS;
4029 }
4030 if (args[0]->type != LYXP_SET_NODE_SET) {
4031 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4032 return LY_EVALID;
4033 }
4034
4035 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004036 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004037
4038 item = &args[0]->val.nodes[0];
4039 } else {
4040 if (set->type == LYXP_SET_EMPTY) {
4041 set_fill_string(set, "", 0);
4042 return LY_SUCCESS;
4043 }
4044 if (set->type != LYXP_SET_NODE_SET) {
4045 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4046 return LY_EVALID;
4047 }
4048
4049 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004050 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004051
4052 item = &set->val.nodes[0];
4053 }
4054
4055 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004056 case LYXP_NODE_NONE:
4057 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 case LYXP_NODE_ROOT:
4059 case LYXP_NODE_ROOT_CONFIG:
4060 case LYXP_NODE_TEXT:
4061 set_fill_string(set, "", 0);
4062 break;
4063 case LYXP_NODE_ELEM:
4064 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4065 break;
4066 case LYXP_NODE_ATTR:
4067 set_fill_string(set, ((struct lyd_attr *)item->node)->name, strlen(((struct lyd_attr *)item->node)->name));
4068 break;
4069 }
4070
4071 return LY_SUCCESS;
4072}
4073
4074/**
4075 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4076 * with the node name fully qualified (with namespace) from the argument or the context.
4077 *
4078 * @param[in] args Array of arguments.
4079 * @param[in] arg_count Count of elements in @p args.
4080 * @param[in,out] set Context and result set at the same time.
4081 * @param[in] options XPath options.
4082 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4083 */
4084static LY_ERR
4085xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4086{
4087 struct lyxp_set_node *item;
4088 struct lys_module *mod;
4089 char *str;
4090 const char *name;
4091 int rc;
4092
4093 if (options & LYXP_SCNODE_ALL) {
4094 set_scnode_clear_ctx(set);
4095 return LY_SUCCESS;
4096 }
4097
4098 if (arg_count) {
4099 if (args[0]->type == LYXP_SET_EMPTY) {
4100 set_fill_string(set, "", 0);
4101 return LY_SUCCESS;
4102 }
4103 if (args[0]->type != LYXP_SET_NODE_SET) {
4104 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4105 return LY_EVALID;
4106 }
4107
4108 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004109 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004110
4111 item = &args[0]->val.nodes[0];
4112 } else {
4113 if (set->type == LYXP_SET_EMPTY) {
4114 set_fill_string(set, "", 0);
4115 return LY_SUCCESS;
4116 }
4117 if (set->type != LYXP_SET_NODE_SET) {
4118 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4119 return LY_EVALID;
4120 }
4121
4122 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004123 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004124
4125 item = &set->val.nodes[0];
4126 }
4127
4128 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004129 case LYXP_NODE_NONE:
4130 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004131 case LYXP_NODE_ROOT:
4132 case LYXP_NODE_ROOT_CONFIG:
4133 case LYXP_NODE_TEXT:
4134 mod = NULL;
4135 name = NULL;
4136 break;
4137 case LYXP_NODE_ELEM:
4138 mod = item->node->schema->module;
4139 name = item->node->schema->name;
4140 break;
4141 case LYXP_NODE_ATTR:
4142 mod = ((struct lyd_attr *)item->node)->annotation->module;
4143 name = ((struct lyd_attr *)item->node)->name;
4144 break;
4145 }
4146
4147 if (mod && name) {
4148 switch (set->format) {
4149 case LYD_UNKNOWN:
4150 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4151 break;
4152 case LYD_JSON:
4153 rc = asprintf(&str, "%s:%s", mod->name, name);
4154 break;
Michal Vasko9409ef62019-09-12 11:47:17 +02004155 default:
4156 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157 }
4158 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4159 set_fill_string(set, str, strlen(str));
4160 free(str);
4161 } else {
4162 set_fill_string(set, "", 0);
4163 }
4164
4165 return LY_SUCCESS;
4166}
4167
4168/**
4169 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4170 * with the namespace of the node from the argument or the context.
4171 *
4172 * @param[in] args Array of arguments.
4173 * @param[in] arg_count Count of elements in @p args.
4174 * @param[in,out] set Context and result set at the same time.
4175 * @param[in] options XPath options.
4176 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4177 */
4178static LY_ERR
4179xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4180{
4181 struct lyxp_set_node *item;
4182 struct lys_module *mod;
4183 /* suppress unused variable warning */
4184 (void)options;
4185
4186 if (options & LYXP_SCNODE_ALL) {
4187 set_scnode_clear_ctx(set);
4188 return LY_SUCCESS;
4189 }
4190
4191 if (arg_count) {
4192 if (args[0]->type == LYXP_SET_EMPTY) {
4193 set_fill_string(set, "", 0);
4194 return LY_SUCCESS;
4195 }
4196 if (args[0]->type != LYXP_SET_NODE_SET) {
4197 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "namespace-uri(node-set?)");
4198 return LY_EVALID;
4199 }
4200
4201 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004202 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203
4204 item = &args[0]->val.nodes[0];
4205 } else {
4206 if (set->type == LYXP_SET_EMPTY) {
4207 set_fill_string(set, "", 0);
4208 return LY_SUCCESS;
4209 }
4210 if (set->type != LYXP_SET_NODE_SET) {
4211 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4212 return LY_EVALID;
4213 }
4214
4215 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004216 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217
4218 item = &set->val.nodes[0];
4219 }
4220
4221 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004222 case LYXP_NODE_NONE:
4223 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 case LYXP_NODE_ROOT:
4225 case LYXP_NODE_ROOT_CONFIG:
4226 case LYXP_NODE_TEXT:
4227 set_fill_string(set, "", 0);
4228 break;
4229 case LYXP_NODE_ELEM:
4230 case LYXP_NODE_ATTR:
4231 if (item->type == LYXP_NODE_ELEM) {
4232 mod = item->node->schema->module;
4233 } else { /* LYXP_NODE_ATTR */
4234 /* annotations */
4235 mod = ((struct lyd_attr *)item->node)->annotation->module;
4236 }
4237
4238 set_fill_string(set, mod->ns, strlen(mod->ns));
4239 break;
4240 }
4241
4242 return LY_SUCCESS;
4243}
4244
4245/**
4246 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4247 * with only nodes from the context. In practice it either leaves the context
4248 * as it is or returns an empty node set.
4249 *
4250 * @param[in] args Array of arguments.
4251 * @param[in] arg_count Count of elements in @p args.
4252 * @param[in,out] set Context and result set at the same time.
4253 * @param[in] options XPath options.
4254 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4255 */
4256static LY_ERR
4257xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4258{
4259 if (options & LYXP_SCNODE_ALL) {
4260 set_scnode_clear_ctx(set);
4261 return LY_SUCCESS;
4262 }
4263
4264 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004265 lyxp_set_cast(set, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004266 }
4267 return LY_SUCCESS;
4268}
4269
4270/**
4271 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4272 * with normalized value (no leading, trailing, double white spaces) of the node
4273 * from the argument or the context.
4274 *
4275 * @param[in] args Array of arguments.
4276 * @param[in] arg_count Count of elements in @p args.
4277 * @param[in,out] set Context and result set at the same time.
4278 * @param[in] options XPath options.
4279 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4280 */
4281static LY_ERR
4282xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4283{
4284 uint16_t i, new_used;
4285 char *new;
4286 int have_spaces = 0, space_before = 0;
4287 struct lysc_node_leaf *sleaf;
4288 LY_ERR rc = LY_SUCCESS;
4289
4290 if (options & LYXP_SCNODE_ALL) {
4291 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4292 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4293 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4294 rc = LY_EINVAL;
4295 } else if (!warn_is_string_type(sleaf->type)) {
4296 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4297 rc = LY_EINVAL;
4298 }
4299 }
4300 set_scnode_clear_ctx(set);
4301 return rc;
4302 }
4303
4304 if (arg_count) {
4305 set_fill_set(set, args[0]);
4306 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004307 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 LY_CHECK_RET(rc);
4309
4310 /* is there any normalization necessary? */
4311 for (i = 0; set->val.str[i]; ++i) {
4312 if (is_xmlws(set->val.str[i])) {
4313 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4314 have_spaces = 1;
4315 break;
4316 }
4317 space_before = 1;
4318 } else {
4319 space_before = 0;
4320 }
4321 }
4322
4323 /* yep, there is */
4324 if (have_spaces) {
4325 /* it's enough, at least one character will go, makes space for ending '\0' */
4326 new = malloc(strlen(set->val.str) * sizeof(char));
4327 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4328 new_used = 0;
4329
4330 space_before = 0;
4331 for (i = 0; set->val.str[i]; ++i) {
4332 if (is_xmlws(set->val.str[i])) {
4333 if ((i == 0) || space_before) {
4334 space_before = 1;
4335 continue;
4336 } else {
4337 space_before = 1;
4338 }
4339 } else {
4340 space_before = 0;
4341 }
4342
4343 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4344 ++new_used;
4345 }
4346
4347 /* at worst there is one trailing space now */
4348 if (new_used && is_xmlws(new[new_used - 1])) {
4349 --new_used;
4350 }
4351
4352 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4353 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4354 new[new_used] = '\0';
4355
4356 free(set->val.str);
4357 set->val.str = new;
4358 }
4359
4360 return LY_SUCCESS;
4361}
4362
4363/**
4364 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4365 * with the argument converted to boolean and logically inverted.
4366 *
4367 * @param[in] args Array of arguments.
4368 * @param[in] arg_count Count of elements in @p args.
4369 * @param[in,out] set Context and result set at the same time.
4370 * @param[in] options XPath options.
4371 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4372 */
4373static LY_ERR
4374xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4375{
4376 if (options & LYXP_SCNODE_ALL) {
4377 set_scnode_clear_ctx(set);
4378 return LY_SUCCESS;
4379 }
4380
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004381 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004382 if (args[0]->val.bool) {
4383 set_fill_boolean(set, 0);
4384 } else {
4385 set_fill_boolean(set, 1);
4386 }
4387
4388 return LY_SUCCESS;
4389}
4390
4391/**
4392 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4393 * with the number representation of either the argument or the context.
4394 *
4395 * @param[in] args Array of arguments.
4396 * @param[in] arg_count Count of elements in @p args.
4397 * @param[in,out] set Context and result set at the same time.
4398 * @param[in] options XPath options.
4399 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4400 */
4401static LY_ERR
4402xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4403{
4404 LY_ERR rc;
4405
4406 if (options & LYXP_SCNODE_ALL) {
4407 set_scnode_clear_ctx(set);
4408 return LY_SUCCESS;
4409 }
4410
4411 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004412 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004413 LY_CHECK_RET(rc);
4414 set_fill_set(set, args[0]);
4415 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004416 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004417 LY_CHECK_RET(rc);
4418 }
4419
4420 return LY_SUCCESS;
4421}
4422
4423/**
4424 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4425 * with the context position.
4426 *
4427 * @param[in] args Array of arguments.
4428 * @param[in] arg_count Count of elements in @p args.
4429 * @param[in,out] set Context and result set at the same time.
4430 * @param[in] options XPath options.
4431 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4432 */
4433static LY_ERR
4434xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4435{
4436 if (options & LYXP_SCNODE_ALL) {
4437 set_scnode_clear_ctx(set);
4438 return LY_SUCCESS;
4439 }
4440
4441 if (set->type == LYXP_SET_EMPTY) {
4442 set_fill_number(set, 0);
4443 return LY_SUCCESS;
4444 }
4445 if (set->type != LYXP_SET_NODE_SET) {
4446 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4447 return LY_EVALID;
4448 }
4449
4450 set_fill_number(set, set->ctx_pos);
4451
4452 /* UNUSED in 'Release' build type */
4453 (void)options;
4454 return LY_SUCCESS;
4455}
4456
4457/**
4458 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4459 * depending on whether the second argument regex matches the first argument string. For details refer to
4460 * YANG 1.1 RFC section 10.2.1.
4461 *
4462 * @param[in] args Array of arguments.
4463 * @param[in] arg_count Count of elements in @p args.
4464 * @param[in,out] set Context and result set at the same time.
4465 * @param[in] options XPath options.
4466 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4467 */
4468static LY_ERR
4469xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4470{
4471 struct lysc_pattern **patterns = NULL, **pattern;
4472 struct lysc_node_leaf *sleaf;
4473 char *path;
4474 LY_ERR rc = LY_SUCCESS;
4475 struct ly_err_item *err;
4476
4477 if (options & LYXP_SCNODE_ALL) {
4478 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4479 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4480 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4481 rc = LY_EINVAL;
4482 } else if (!warn_is_string_type(sleaf->type)) {
4483 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4484 rc = LY_EINVAL;
4485 }
4486 }
4487
4488 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4489 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4490 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4491 rc = LY_EINVAL;
4492 } else if (!warn_is_string_type(sleaf->type)) {
4493 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4494 rc = LY_EINVAL;
4495 }
4496 }
4497 set_scnode_clear_ctx(set);
4498 return rc;
4499 }
4500
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004501 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004502 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004503 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004504 LY_CHECK_RET(rc);
4505
4506 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4507 *pattern = malloc(sizeof **pattern);
4508 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4509 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4510 free(path);
4511 if (rc != LY_SUCCESS) {
4512 LY_ARRAY_FREE(patterns);
4513 return rc;
4514 }
4515
4516 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4517 pcre2_code_free((*pattern)->code);
4518 free(*pattern);
4519 LY_ARRAY_FREE(patterns);
4520 if (rc && (rc != LY_EVALID)) {
4521 ly_err_print(err);
4522 ly_err_free(err);
4523 return rc;
4524 }
4525
4526 if (rc == LY_EVALID) {
4527 ly_err_free(err);
4528 set_fill_boolean(set, 0);
4529 } else {
4530 set_fill_boolean(set, 1);
4531 }
4532
4533 return LY_SUCCESS;
4534}
4535
4536/**
4537 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4538 * with the rounded first argument. For details refer to
4539 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4540 *
4541 * @param[in] args Array of arguments.
4542 * @param[in] arg_count Count of elements in @p args.
4543 * @param[in,out] set Context and result set at the same time.
4544 * @param[in] options XPath options.
4545 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4546 */
4547static LY_ERR
4548xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4549{
4550 struct lysc_node_leaf *sleaf;
4551 LY_ERR rc = LY_SUCCESS;
4552
4553 if (options & LYXP_SCNODE_ALL) {
4554 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4555 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
4556 rc = LY_EINVAL;
4557 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4558 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4559 rc = LY_EINVAL;
4560 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4561 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4562 rc = LY_EINVAL;
4563 }
4564 set_scnode_clear_ctx(set);
4565 return rc;
4566 }
4567
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004568 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569 LY_CHECK_RET(rc);
4570
4571 /* cover only the cases where floor can't be used */
4572 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4573 set_fill_number(set, -0.0f);
4574 } else {
4575 args[0]->val.num += 0.5;
4576 rc = xpath_floor(args, 1, args[0], options);
4577 LY_CHECK_RET(rc);
4578 set_fill_number(set, args[0]->val.num);
4579 }
4580
4581 return LY_SUCCESS;
4582}
4583
4584/**
4585 * @brief Execute the XPath starts-with(string, string) function.
4586 * Returns LYXP_SET_BOOLEAN whether the second argument is
4587 * the prefix of the first or not.
4588 *
4589 * @param[in] args Array of arguments.
4590 * @param[in] arg_count Count of elements in @p args.
4591 * @param[in,out] set Context and result set at the same time.
4592 * @param[in] options XPath options.
4593 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4594 */
4595static LY_ERR
4596xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4597{
4598 struct lysc_node_leaf *sleaf;
4599 LY_ERR rc = LY_SUCCESS;
4600
4601 if (options & LYXP_SCNODE_ALL) {
4602 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4603 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4604 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4605 rc = LY_EINVAL;
4606 } else if (!warn_is_string_type(sleaf->type)) {
4607 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4608 rc = LY_EINVAL;
4609 }
4610 }
4611
4612 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4613 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4614 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4615 rc = LY_EINVAL;
4616 } else if (!warn_is_string_type(sleaf->type)) {
4617 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4618 rc = LY_EINVAL;
4619 }
4620 }
4621 set_scnode_clear_ctx(set);
4622 return rc;
4623 }
4624
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004625 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004626 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004627 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 LY_CHECK_RET(rc);
4629
4630 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4631 set_fill_boolean(set, 0);
4632 } else {
4633 set_fill_boolean(set, 1);
4634 }
4635
4636 return LY_SUCCESS;
4637}
4638
4639/**
4640 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4641 * with the string representation of either the argument or the context.
4642 *
4643 * @param[in] args Array of arguments.
4644 * @param[in] arg_count Count of elements in @p args.
4645 * @param[in,out] set Context and result set at the same time.
4646 * @param[in] options XPath options.
4647 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4648 */
4649static LY_ERR
4650xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4651{
4652 LY_ERR rc;
4653
4654 if (options & LYXP_SCNODE_ALL) {
4655 set_scnode_clear_ctx(set);
4656 return LY_SUCCESS;
4657 }
4658
4659 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004660 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004661 LY_CHECK_RET(rc);
4662 set_fill_set(set, args[0]);
4663 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004664 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004665 LY_CHECK_RET(rc);
4666 }
4667
4668 return LY_SUCCESS;
4669}
4670
4671/**
4672 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4673 * with the length of the string in either the argument or the context.
4674 *
4675 * @param[in] args Array of arguments.
4676 * @param[in] arg_count Count of elements in @p args.
4677 * @param[in,out] set Context and result set at the same time.
4678 * @param[in] options XPath options.
4679 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4680 */
4681static LY_ERR
4682xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4683{
4684 struct lysc_node_leaf *sleaf;
4685 LY_ERR rc = LY_SUCCESS;
4686
4687 if (options & LYXP_SCNODE_ALL) {
4688 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4689 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4690 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4691 rc = LY_EINVAL;
4692 } else if (!warn_is_string_type(sleaf->type)) {
4693 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4694 rc = LY_EINVAL;
4695 }
4696 }
4697 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4698 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4699 LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4700 rc = LY_EINVAL;
4701 } else if (!warn_is_string_type(sleaf->type)) {
4702 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4703 rc = LY_EINVAL;
4704 }
4705 }
4706 set_scnode_clear_ctx(set);
4707 return rc;
4708 }
4709
4710 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004711 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004712 LY_CHECK_RET(rc);
4713 set_fill_number(set, strlen(args[0]->val.str));
4714 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004715 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004716 LY_CHECK_RET(rc);
4717 set_fill_number(set, strlen(set->val.str));
4718 }
4719
4720 return LY_SUCCESS;
4721}
4722
4723/**
4724 * @brief Execute the XPath substring(string, number, number?) function.
4725 * Returns LYXP_SET_STRING substring of the first argument starting
4726 * on the second argument index ending on the third argument index,
4727 * indexed from 1. For exact definition refer to
4728 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4729 *
4730 * @param[in] args Array of arguments.
4731 * @param[in] arg_count Count of elements in @p args.
4732 * @param[in,out] set Context and result set at the same time.
4733 * @param[in] options XPath options.
4734 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4735 */
4736static LY_ERR
4737xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4738{
4739 int start, len;
4740 uint16_t str_start, str_len, pos;
4741 struct lysc_node_leaf *sleaf;
4742 LY_ERR rc = LY_SUCCESS;
4743
4744 if (options & LYXP_SCNODE_ALL) {
4745 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4746 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4747 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4748 rc = LY_EINVAL;
4749 } else if (!warn_is_string_type(sleaf->type)) {
4750 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4751 rc = LY_EINVAL;
4752 }
4753 }
4754
4755 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4756 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4757 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4758 rc = LY_EINVAL;
4759 } else if (!warn_is_numeric_type(sleaf->type)) {
4760 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
4761 rc = LY_EINVAL;
4762 }
4763 }
4764
4765 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4766 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4767 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4768 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4769 rc = LY_EINVAL;
4770 } else if (!warn_is_numeric_type(sleaf->type)) {
4771 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
4772 rc = LY_EINVAL;
4773 }
4774 }
4775 set_scnode_clear_ctx(set);
4776 return rc;
4777 }
4778
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004779 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 LY_CHECK_RET(rc);
4781
4782 /* start */
4783 if (xpath_round(&args[1], 1, args[1], options)) {
4784 return -1;
4785 }
4786 if (isfinite(args[1]->val.num)) {
4787 start = args[1]->val.num - 1;
4788 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4789 start = INT_MIN;
4790 } else {
4791 start = INT_MAX;
4792 }
4793
4794 /* len */
4795 if (arg_count == 3) {
4796 rc = xpath_round(&args[2], 1, args[2], options);
4797 LY_CHECK_RET(rc);
4798 if (isfinite(args[2]->val.num)) {
4799 len = args[2]->val.num;
4800 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4801 len = 0;
4802 } else {
4803 len = INT_MAX;
4804 }
4805 } else {
4806 len = INT_MAX;
4807 }
4808
4809 /* find matching character positions */
4810 str_start = 0;
4811 str_len = 0;
4812 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4813 if (pos < start) {
4814 ++str_start;
4815 } else if (pos < start + len) {
4816 ++str_len;
4817 } else {
4818 break;
4819 }
4820 }
4821
4822 set_fill_string(set, args[0]->val.str + str_start, str_len);
4823 return LY_SUCCESS;
4824}
4825
4826/**
4827 * @brief Execute the XPath substring-after(string, string) function.
4828 * Returns LYXP_SET_STRING with the string succeeding the occurance
4829 * of the second argument in the first or an empty string.
4830 *
4831 * @param[in] args Array of arguments.
4832 * @param[in] arg_count Count of elements in @p args.
4833 * @param[in,out] set Context and result set at the same time.
4834 * @param[in] options XPath options.
4835 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4836 */
4837static LY_ERR
4838xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4839{
4840 char *ptr;
4841 struct lysc_node_leaf *sleaf;
4842 LY_ERR rc = LY_SUCCESS;
4843
4844 if (options & LYXP_SCNODE_ALL) {
4845 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4846 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4847 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4848 rc = LY_EINVAL;
4849 } else if (!warn_is_string_type(sleaf->type)) {
4850 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4851 rc = LY_EINVAL;
4852 }
4853 }
4854
4855 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4856 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4857 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4858 rc = LY_EINVAL;
4859 } else if (!warn_is_string_type(sleaf->type)) {
4860 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4861 rc = LY_EINVAL;
4862 }
4863 }
4864 set_scnode_clear_ctx(set);
4865 return rc;
4866 }
4867
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004868 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004870 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 LY_CHECK_RET(rc);
4872
4873 ptr = strstr(args[0]->val.str, args[1]->val.str);
4874 if (ptr) {
4875 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4876 } else {
4877 set_fill_string(set, "", 0);
4878 }
4879
4880 return LY_SUCCESS;
4881}
4882
4883/**
4884 * @brief Execute the XPath substring-before(string, string) function.
4885 * Returns LYXP_SET_STRING with the string preceding the occurance
4886 * of the second argument in the first or an empty string.
4887 *
4888 * @param[in] args Array of arguments.
4889 * @param[in] arg_count Count of elements in @p args.
4890 * @param[in,out] set Context and result set at the same time.
4891 * @param[in] options XPath options.
4892 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4893 */
4894static LY_ERR
4895xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4896{
4897 char *ptr;
4898 struct lysc_node_leaf *sleaf;
4899 LY_ERR rc = LY_SUCCESS;
4900
4901 if (options & LYXP_SCNODE_ALL) {
4902 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4903 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4904 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4905 rc = LY_EINVAL;
4906 } else if (!warn_is_string_type(sleaf->type)) {
4907 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4908 rc = LY_EINVAL;
4909 }
4910 }
4911
4912 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4913 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4914 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
4915 rc = LY_EINVAL;
4916 } else if (!warn_is_string_type(sleaf->type)) {
4917 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
4918 rc = LY_EINVAL;
4919 }
4920 }
4921 set_scnode_clear_ctx(set);
4922 return rc;
4923 }
4924
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004925 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004926 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004927 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 LY_CHECK_RET(rc);
4929
4930 ptr = strstr(args[0]->val.str, args[1]->val.str);
4931 if (ptr) {
4932 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4933 } else {
4934 set_fill_string(set, "", 0);
4935 }
4936
4937 return LY_SUCCESS;
4938}
4939
4940/**
4941 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4942 * with the sum of all the nodes in the context.
4943 *
4944 * @param[in] args Array of arguments.
4945 * @param[in] arg_count Count of elements in @p args.
4946 * @param[in,out] set Context and result set at the same time.
4947 * @param[in] options XPath options.
4948 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4949 */
4950static LY_ERR
4951xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4952{
4953 long double num;
4954 char *str;
4955 uint16_t i;
4956 struct lyxp_set set_item;
4957 struct lysc_node_leaf *sleaf;
4958 LY_ERR rc = LY_SUCCESS;
4959
4960 if (options & LYXP_SCNODE_ALL) {
4961 if (args[0]->type == LYXP_SET_SCNODE_SET) {
4962 for (i = 0; i < args[0]->used; ++i) {
4963 if (args[0]->val.scnodes[i].in_ctx == 1) {
4964 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
4965 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4966 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
4967 lys_nodetype2str(sleaf->nodetype), sleaf->name);
4968 rc = LY_EINVAL;
4969 } else if (!warn_is_numeric_type(sleaf->type)) {
4970 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
4971 rc = LY_EINVAL;
4972 }
4973 }
4974 }
4975 }
4976 set_scnode_clear_ctx(set);
4977 return rc;
4978 }
4979
4980 set_fill_number(set, 0);
4981 if (args[0]->type == LYXP_SET_EMPTY) {
4982 return LY_SUCCESS;
4983 }
4984
4985 if (args[0]->type != LYXP_SET_NODE_SET) {
4986 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
4987 return LY_EVALID;
4988 }
4989
Michal Vasko5c4e5892019-11-14 12:31:38 +01004990 set_init(&set_item, set);
4991
Michal Vasko03ff5a72019-09-11 13:49:33 +02004992 set_item.type = LYXP_SET_NODE_SET;
4993 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
4994 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
4995
4996 set_item.used = 1;
4997 set_item.size = 1;
4998
4999 for (i = 0; i < args[0]->used; ++i) {
5000 set_item.val.nodes[0] = args[0]->val.nodes[i];
5001
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005002 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005003 LY_CHECK_RET(rc);
5004 num = cast_string_to_number(str);
5005 free(str);
5006 set->val.num += num;
5007 }
5008
5009 free(set_item.val.nodes);
5010
5011 return LY_SUCCESS;
5012}
5013
5014/**
5015 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5016 * with the text content of the nodes in the context.
5017 *
5018 * @param[in] args Array of arguments.
5019 * @param[in] arg_count Count of elements in @p args.
5020 * @param[in,out] set Context and result set at the same time.
5021 * @param[in] options XPath options.
5022 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
5023 */
5024static LY_ERR
5025xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5026{
5027 uint32_t i;
5028
5029 if (options & LYXP_SCNODE_ALL) {
5030 set_scnode_clear_ctx(set);
5031 return LY_SUCCESS;
5032 }
5033
5034 if (set->type == LYXP_SET_EMPTY) {
5035 return LY_SUCCESS;
5036 }
5037 if (set->type != LYXP_SET_NODE_SET) {
5038 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5039 return LY_EVALID;
5040 }
5041
5042 for (i = 0; i < set->used;) {
5043 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005044 case LYXP_NODE_NONE:
5045 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005046 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005047 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5048 set->val.nodes[i].type = LYXP_NODE_TEXT;
5049 ++i;
5050 break;
5051 }
5052 /* fall through */
5053 case LYXP_NODE_ROOT:
5054 case LYXP_NODE_ROOT_CONFIG:
5055 case LYXP_NODE_TEXT:
5056 case LYXP_NODE_ATTR:
5057 set_remove_node(set, i);
5058 break;
5059 }
5060 }
5061
5062 return LY_SUCCESS;
5063}
5064
5065/**
5066 * @brief Execute the XPath translate(string, string, string) function.
5067 * Returns LYXP_SET_STRING with the first argument with the characters
5068 * from the second argument replaced by those on the corresponding
5069 * positions in the third argument.
5070 *
5071 * @param[in] args Array of arguments.
5072 * @param[in] arg_count Count of elements in @p args.
5073 * @param[in,out] set Context and result set at the same time.
5074 * @param[in] options XPath options.
5075 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
5076 */
5077static LY_ERR
5078xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5079{
5080 uint16_t i, j, new_used;
5081 char *new;
5082 int found, have_removed;
5083 struct lysc_node_leaf *sleaf;
5084 LY_ERR rc = LY_SUCCESS;
5085
5086 if (options & LYXP_SCNODE_ALL) {
5087 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5088 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5089 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
5090 rc = LY_EINVAL;
5091 } else if (!warn_is_string_type(sleaf->type)) {
5092 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
5093 rc = LY_EINVAL;
5094 }
5095 }
5096
5097 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5098 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5099 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
5100 rc = LY_EINVAL;
5101 } else if (!warn_is_string_type(sleaf->type)) {
5102 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
5103 rc = LY_EINVAL;
5104 }
5105 }
5106
5107 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5108 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5109 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
5110 rc = LY_EINVAL;
5111 } else if (!warn_is_string_type(sleaf->type)) {
5112 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
5113 rc = LY_EINVAL;
5114 }
5115 }
5116 set_scnode_clear_ctx(set);
5117 return rc;
5118 }
5119
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005120 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005121 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005122 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005124 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 LY_CHECK_RET(rc);
5126
5127 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5128 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5129 new_used = 0;
5130
5131 have_removed = 0;
5132 for (i = 0; args[0]->val.str[i]; ++i) {
5133 found = 0;
5134
5135 for (j = 0; args[1]->val.str[j]; ++j) {
5136 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5137 /* removing this char */
5138 if (j >= strlen(args[2]->val.str)) {
5139 have_removed = 1;
5140 found = 1;
5141 break;
5142 }
5143 /* replacing this char */
5144 new[new_used] = args[2]->val.str[j];
5145 ++new_used;
5146 found = 1;
5147 break;
5148 }
5149 }
5150
5151 /* copying this char */
5152 if (!found) {
5153 new[new_used] = args[0]->val.str[i];
5154 ++new_used;
5155 }
5156 }
5157
5158 if (have_removed) {
5159 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5160 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5161 }
5162 new[new_used] = '\0';
5163
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005164 lyxp_set_cast(set, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165 set->type = LYXP_SET_STRING;
5166 set->val.str = new;
5167
5168 return LY_SUCCESS;
5169}
5170
5171/**
5172 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5173 * with true value.
5174 *
5175 * @param[in] args Array of arguments.
5176 * @param[in] arg_count Count of elements in @p args.
5177 * @param[in,out] set Context and result set at the same time.
5178 * @param[in] options XPath options.
5179 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
5180 */
5181static LY_ERR
5182xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5183{
5184 if (options & LYXP_SCNODE_ALL) {
5185 set_scnode_clear_ctx(set);
5186 return LY_SUCCESS;
5187 }
5188
5189 set_fill_boolean(set, 1);
5190 return LY_SUCCESS;
5191}
5192
5193/*
5194 * moveto functions
5195 *
5196 * They and only they actually change the context (set).
5197 */
5198
5199/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005200 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005201 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005202 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5203 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5204 * @param[in] set Set with XPath context.
5205 * @param[out] moveto_mod Expected module of a matching node.
5206 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005207 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005208static LY_ERR
5209moveto_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 +02005210{
Michal Vasko6346ece2019-09-24 13:12:53 +02005211 const struct lys_module *mod;
5212 const char *ptr;
5213 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005214 char *str;
5215
Michal Vasko6346ece2019-09-24 13:12:53 +02005216 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5217 /* specific module */
5218 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005219
Michal Vasko6346ece2019-09-24 13:12:53 +02005220 switch (set->format) {
5221 case LYD_UNKNOWN:
5222 /* schema, search all local module imports */
5223 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5224 break;
5225 case LYD_JSON:
5226 /* JSON data, search in context */
5227 str = strndup(*qname, pref_len);
5228 mod = ly_ctx_get_module(set->ctx, str, NULL);
5229 free(str);
5230 break;
5231 default:
5232 LOGINT_RET(set->ctx);
5233 }
5234
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005235 /* Check for errors and non-implemented modules, as they are not valid */
5236 if (!mod || !mod->implemented) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005237 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5238 return LY_EVALID;
5239 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005240
Michal Vasko6346ece2019-09-24 13:12:53 +02005241 *qname += pref_len + 1;
5242 *qname_len -= pref_len + 1;
5243 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5244 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005245 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005246 } else {
5247 /* local module */
5248 mod = set->local_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249 }
5250
Michal Vasko6346ece2019-09-24 13:12:53 +02005251 *moveto_mod = mod;
5252 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005253}
5254
5255/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005256 * @brief Move context @p set to the root. Handles absolute path.
5257 * Result is LYXP_SET_NODE_SET.
5258 *
5259 * @param[in,out] set Set to use.
5260 * @param[in] options Xpath options.
5261 */
5262static void
5263moveto_root(struct lyxp_set *set, int options)
5264{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265 if (!set) {
5266 return;
5267 }
5268
5269 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005270 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005271 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005272 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005273 lyxp_set_cast(set, LYXP_SET_EMPTY);
5274 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005275 }
5276}
5277
5278/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005279 * @brief Check whether a node has some unresolved "when".
5280 *
5281 * @param[in] node Node to check.
5282 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5283 */
5284static LY_ERR
5285moveto_when_check(const struct lyd_node *node)
5286{
5287 const struct lysc_node *schema;
5288
5289 if (!node) {
5290 return LY_SUCCESS;
5291 }
5292
5293 schema = node->schema;
5294 do {
5295 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5296 return LY_EINCOMPLETE;
5297 }
5298 schema = schema->parent;
5299 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5300
5301 return LY_SUCCESS;
5302}
5303
5304/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005305 * @brief Check @p node as a part of NameTest processing.
5306 *
5307 * @param[in] node Node to check.
5308 * @param[in] root_type XPath root node type.
5309 * @param[in] node_name Node name to move to. Must be in the dictionary!
5310 * @param[in] moveto_mod Expected module of the node.
Michal Vasko6346ece2019-09-24 13:12:53 +02005311 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5312 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005313 */
5314static LY_ERR
5315moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5316 const struct lys_module *moveto_mod)
5317{
5318 /* module check */
5319 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005320 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321 }
5322
Michal Vasko5c4e5892019-11-14 12:31:38 +01005323 /* context check */
5324 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005325 return LY_EINVAL;
5326 }
5327
5328 /* name check */
Michal Vasko465a0e12019-11-07 11:11:58 +01005329 if (strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005330 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005331 }
5332
Michal Vaskoa1424542019-11-14 16:08:52 +01005333 /* when check */
5334 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005335 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005336 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337
5338 /* match */
5339 return LY_SUCCESS;
5340}
5341
5342/**
5343 * @brief Check @p node as a part of schema NameTest processing.
5344 *
5345 * @param[in] node Schema node to check.
5346 * @param[in] root_type XPath root node type.
5347 * @param[in] node_name Node name to move to. Must be in the dictionary!
5348 * @param[in] moveto_mod Expected module of the node.
Michal Vasko6346ece2019-09-24 13:12:53 +02005349 * @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 +02005350 */
5351static LY_ERR
5352moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005353 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005354{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005355 /* module check */
5356 if (strcmp(node_name, "*") && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005357 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005358 }
5359
5360 /* context check */
5361 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5362 return LY_EINVAL;
5363 }
5364
5365 /* name check */
Michal Vasko465a0e12019-11-07 11:11:58 +01005366 if (strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005367 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005368 }
5369
5370 /* match */
5371 return LY_SUCCESS;
5372}
5373
5374/**
5375 * @brief Move context @p set to a node. Handles '/' and '*', 'NAME', 'PREFIX:*', or 'PREFIX:NAME'.
5376 * Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware.
5377 *
5378 * @param[in,out] set Set to use.
5379 * @param[in] qname Qualified node name to move to.
5380 * @param[in] qname_len Length of @p qname.
5381 * @param[in] options XPath options.
5382 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5383 */
5384static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005385moveto_node(struct lyxp_set *set, const char *qname, uint16_t qname_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386{
Michal Vasko79bebfd2019-11-14 16:09:19 +01005387 uint32_t i, j;
Michal Vasko6346ece2019-09-24 13:12:53 +02005388 int replaced;
5389 const char *name_dict = NULL; /* optimization - so we can do (==) instead (!strncmp(...)) in moveto_node_check() */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 const struct lys_module *moveto_mod;
5391 const struct lyd_node *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 LY_ERR rc;
5393
5394 if (!set || (set->type == LYXP_SET_EMPTY)) {
5395 return LY_SUCCESS;
5396 }
5397
5398 if (set->type != LYXP_SET_NODE_SET) {
5399 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5400 return LY_EVALID;
5401 }
5402
Michal Vasko6346ece2019-09-24 13:12:53 +02005403 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5404 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405
5406 /* name */
5407 name_dict = lydict_insert(set->ctx, qname, qname_len);
5408
5409 for (i = 0; i < set->used; ) {
5410 replaced = 0;
5411
5412 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 +01005413 assert(!set->val.nodes[i].node);
5414 /* search in all the trees */
Michal Vasko79bebfd2019-11-14 16:09:19 +01005415 LY_ARRAY_FOR(set->trees, j) {
5416 for (sub = set->trees[j]; sub; sub = sub->next) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005417 rc = moveto_node_check(sub, set->root_type, name_dict, moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005418 if (rc == LY_SUCCESS) {
5419 /* pos filled later */
5420 if (!replaced) {
5421 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5422 replaced = 1;
5423 } else {
5424 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5425 }
5426 ++i;
5427 } else if (rc == LY_EINCOMPLETE) {
5428 lydict_remove(set->ctx, name_dict);
5429 return rc;
5430 }
5431 }
5432 }
5433
Michal Vasko5c4e5892019-11-14 12:31:38 +01005434 /* skip nodes without children - leaves, leaflists, anyxmls (ouput root will eval to true) */
5435 } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005436
5437 for (sub = lyd_node_children(set->val.nodes[i].node); sub; sub = sub->next) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005438 rc = moveto_node_check(sub, set->root_type, name_dict, moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439 if (rc == LY_SUCCESS) {
5440 if (!replaced) {
5441 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5442 replaced = 1;
5443 } else {
5444 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5445 }
5446 ++i;
5447 } else if (rc == LY_EINCOMPLETE) {
5448 lydict_remove(set->ctx, name_dict);
5449 return rc;
5450 }
5451 }
5452 }
5453
5454 if (!replaced) {
5455 /* no match */
5456 set_remove_node(set, i);
5457 }
5458 }
5459 lydict_remove(set->ctx, name_dict);
5460
5461 return LY_SUCCESS;
5462}
5463
5464/**
5465 * @brief Move context @p set to a schema node. Handles '/' and '*', 'NAME', 'PREFIX:*', or 'PREFIX:NAME'.
5466 * Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5467 *
5468 * @param[in,out] set Set to use.
5469 * @param[in] qname Qualified node name to move to.
5470 * @param[in] qname_len Length of @p qname.
5471 * @param[in] options XPath options.
5472 * @return LY_ERR
5473 */
5474static LY_ERR
5475moveto_scnode(struct lyxp_set *set, const char *qname, uint16_t qname_len, int options)
5476{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005477 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 uint32_t mod_idx;
Michal Vasko6346ece2019-09-24 13:12:53 +02005479 const char *name_dict = NULL; /* optimization - so we can do (==) instead (!strncmp(...)) in moveto_node_check() */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480 const struct lys_module *moveto_mod;
5481 const struct lysc_node *sub, *start_parent;
Michal Vasko6346ece2019-09-24 13:12:53 +02005482 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005483
5484 if (!set || (set->type == LYXP_SET_EMPTY)) {
5485 return LY_SUCCESS;
5486 }
5487
5488 if (set->type != LYXP_SET_SCNODE_SET) {
5489 LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5490 return LY_EVALID;
5491 }
5492
Michal Vasko6346ece2019-09-24 13:12:53 +02005493 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5494 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005495
5496 /* name */
5497 name_dict = lydict_insert(set->ctx, qname, qname_len);
5498
Michal Vaskocafad9d2019-11-07 15:20:03 +01005499 /* getnext opts */
5500 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5501 if (options & LYXP_SCNODE_OUTPUT) {
5502 getnext_opts |= LYS_GETNEXT_OUTPUT;
5503 }
5504
Michal Vasko03ff5a72019-09-11 13:49:33 +02005505 orig_used = set->used;
5506 for (i = 0; i < orig_used; ++i) {
5507 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005508 if (set->val.scnodes[i].in_ctx != -2) {
5509 continue;
5510 }
5511
5512 /* remember context node */
5513 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005514 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005515
5516 start_parent = set->val.scnodes[i].scnode;
5517
5518 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
5519 /* it can actually be in any module, it's all <running>, but we know it's moveto_mod (if set),
5520 * so use it directly (root node itself is useless in this case) */
5521 mod_idx = 0;
5522 while (moveto_mod || (moveto_mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
5523 sub = NULL;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005524 while ((sub = lys_getnext(sub, NULL, moveto_mod->compiled, getnext_opts))) {
5525 if (!moveto_scnode_check(sub, set->root_type, name_dict, moveto_mod)) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005526 idx = lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005527 /* we need to prevent these nodes from being considered in this moveto */
5528 if ((idx < orig_used) && (idx > i)) {
5529 set->val.scnodes[idx].in_ctx = 2;
5530 temp_ctx = 1;
5531 }
5532 }
5533 }
5534
5535 if (!mod_idx) {
5536 /* moveto_mod was specified, we are not going through the whole context */
5537 break;
5538 }
5539 /* next iteration */
5540 moveto_mod = NULL;
5541 }
5542
5543 /* skip nodes without children - leaves, leaflists, and anyxmls (ouput root will eval to true) */
5544 } else if (!(start_parent->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
5545 sub = NULL;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005546 while ((sub = lys_getnext(sub, start_parent, NULL, getnext_opts))) {
5547 if (!moveto_scnode_check(sub, set->root_type, name_dict, (moveto_mod ? moveto_mod : set->local_mod))) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005548 idx = lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005549 if ((idx < orig_used) && (idx > i)) {
5550 set->val.scnodes[idx].in_ctx = 2;
5551 temp_ctx = 1;
5552 }
5553 }
5554 }
5555 }
5556 }
5557 lydict_remove(set->ctx, name_dict);
5558
5559 /* correct temporary in_ctx values */
5560 if (temp_ctx) {
5561 for (i = 0; i < orig_used; ++i) {
5562 if (set->val.scnodes[i].in_ctx == 2) {
5563 set->val.scnodes[i].in_ctx = 1;
5564 }
5565 }
5566 }
5567
5568 return LY_SUCCESS;
5569}
5570
5571/**
5572 * @brief Move context @p set to a node and all its descendants. Handles '//' and '*', 'NAME',
5573 * 'PREFIX:*', or 'PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5574 * Context position aware.
5575 *
5576 * @param[in] set Set to use.
5577 * @param[in] qname Qualified node name to move to.
5578 * @param[in] qname_len Length of @p qname.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005579 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5580 */
5581static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005582moveto_node_alldesc(struct lyxp_set *set, const char *qname, uint16_t qname_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005583{
5584 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005585 const char *name_dict = NULL; /* optimization - so we can do (==) instead (!strncmp(...)) in moveto_node_check() */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005586 const struct lyd_node *next, *elem, *start;
5587 const struct lys_module *moveto_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005588 struct lyxp_set ret_set;
5589 LY_ERR rc;
5590
5591 if (!set || (set->type == LYXP_SET_EMPTY)) {
5592 return LY_SUCCESS;
5593 }
5594
5595 if (set->type != LYXP_SET_NODE_SET) {
5596 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5597 return LY_EVALID;
5598 }
5599
Michal Vasko6346ece2019-09-24 13:12:53 +02005600 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5601 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005602
5603 /* replace the original nodes (and throws away all text and attr nodes, root is replaced by a child) */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005604 rc = moveto_node(set, "*", 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005605 LY_CHECK_RET(rc);
5606
Michal Vasko6346ece2019-09-24 13:12:53 +02005607 /* name */
5608 name_dict = lydict_insert(set->ctx, qname, qname_len);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005609
Michal Vasko6346ece2019-09-24 13:12:53 +02005610 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005611 set_init(&ret_set, set);
5612 for (i = 0; i < set->used; ++i) {
5613
5614 /* TREE DFS */
5615 start = set->val.nodes[i].node;
5616 for (elem = next = start; elem; elem = next) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005617 rc = moveto_node_check(elem, set->root_type, name_dict, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005618 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005619 /* add matching node into result set */
5620 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5621 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5622 /* the node is a duplicate, we'll process it later in the set */
5623 goto skip_children;
5624 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005625 } else if (rc == LY_EINCOMPLETE) {
5626 lydict_remove(set->ctx, name_dict);
5627 return rc;
5628 } else if (rc == LY_EINVAL) {
5629 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005630 }
5631
5632 /* TREE DFS NEXT ELEM */
5633 /* select element for the next run - children first */
5634 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
5635 next = NULL;
5636 } else {
5637 next = lyd_node_children(elem);
5638 }
5639 if (!next) {
5640skip_children:
5641 /* no children, so try siblings, but only if it's not the start,
5642 * that is considered to be the root and it's siblings are not traversed */
5643 if (elem != start) {
5644 next = elem->next;
5645 } else {
5646 break;
5647 }
5648 }
5649 while (!next) {
5650 /* no siblings, go back through the parents */
5651 if ((struct lyd_node *)elem->parent == start) {
5652 /* we are done, no next element to process */
5653 break;
5654 }
5655 /* parent is already processed, go to its sibling */
5656 elem = (struct lyd_node *)elem->parent;
5657 next = elem->next;
5658 }
5659 }
5660 }
5661
5662 /* make the temporary set the current one */
5663 ret_set.ctx_pos = set->ctx_pos;
5664 ret_set.ctx_size = set->ctx_size;
5665 set_free_content(set);
5666 memcpy(set, &ret_set, sizeof *set);
5667
Michal Vasko6346ece2019-09-24 13:12:53 +02005668 lydict_remove(set->ctx, name_dict);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005669 return LY_SUCCESS;
5670}
5671
5672/**
5673 * @brief Move context @p set to a schema node and all its descendants. Handles '//' and '*', 'NAME',
5674 * 'PREFIX:*', or 'PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5675 *
5676 * @param[in] set Set to use.
5677 * @param[in] qname Qualified node name to move to.
5678 * @param[in] qname_len Length of @p qname.
5679 * @param[in] options XPath options.
5680 * @return LY_ERR
5681 */
5682static LY_ERR
5683moveto_scnode_alldesc(struct lyxp_set *set, const char *qname, uint16_t qname_len, int options)
5684{
Michal Vasko6346ece2019-09-24 13:12:53 +02005685 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005686 const struct lysc_node *next, *elem, *start;
5687 const struct lys_module *moveto_mod;
Michal Vasko6346ece2019-09-24 13:12:53 +02005688 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689
5690 if (!set || (set->type == LYXP_SET_EMPTY)) {
5691 return LY_SUCCESS;
5692 }
5693
5694 if (set->type != LYXP_SET_SCNODE_SET) {
5695 LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5696 return LY_EVALID;
5697 }
5698
Michal Vasko6346ece2019-09-24 13:12:53 +02005699 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5700 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701
5702 orig_used = set->used;
5703 for (i = 0; i < orig_used; ++i) {
5704 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005705 if (set->val.scnodes[i].in_ctx != -2) {
5706 continue;
5707 }
5708
5709 /* remember context node */
5710 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005711 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005712
5713 /* TREE DFS */
5714 start = set->val.scnodes[i].scnode;
5715 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005716 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5717 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719 }
5720
Michal Vaskocafad9d2019-11-07 15:20:03 +01005721 rc = moveto_scnode_check(elem, set->root_type, qname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005722 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005723 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724 set->val.scnodes[idx].in_ctx = 1;
5725 if (idx > i) {
5726 /* we will process it later in the set */
5727 goto skip_children;
5728 }
5729 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005730 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005731 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005732 } else if (rc == LY_EINVAL) {
5733 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 }
5735
5736next_iter:
5737 /* TREE DFS NEXT ELEM */
5738 /* select element for the next run - children first */
5739 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
5740 if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
5741 next = NULL;
5742 }
5743 if (!next) {
5744skip_children:
5745 /* no children, so try siblings, but only if it's not the start,
5746 * that is considered to be the root and it's siblings are not traversed */
5747 if (elem != start) {
5748 next = elem->next;
5749 } else {
5750 break;
5751 }
5752 }
5753 while (!next) {
5754 /* no siblings, go back through the parents */
5755 if (elem->parent == start) {
5756 /* we are done, no next element to process */
5757 break;
5758 }
5759 /* parent is already processed, go to its sibling */
5760 elem = elem->parent;
5761 next = elem->next;
5762 }
5763 }
5764 }
5765
5766 return LY_SUCCESS;
5767}
5768
5769/**
5770 * @brief Move context @p set to an attribute. Handles '/' and '@*', '@NAME', '@PREFIX:*',
5771 * or '@PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5772 * Indirectly context position aware.
5773 *
5774 * @param[in,out] set Set to use.
5775 * @param[in] qname Qualified node name to move to.
5776 * @param[in] qname_len Length of @p qname.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005777 * @return LY_ERR
5778 */
5779static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005780moveto_attr(struct lyxp_set *set, const char *qname, uint16_t qname_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005781{
5782 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005783 int replaced, all = 0;
5784 const struct lys_module *moveto_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005785 struct lyd_attr *sub;
Michal Vasko6346ece2019-09-24 13:12:53 +02005786 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787
5788 if (!set || (set->type == LYXP_SET_EMPTY)) {
5789 return LY_SUCCESS;
5790 }
5791
5792 if (set->type != LYXP_SET_NODE_SET) {
5793 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5794 return LY_EVALID;
5795 }
5796
Michal Vasko6346ece2019-09-24 13:12:53 +02005797 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5798 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005799
5800 if ((qname_len == 1) && (qname[0] == '*')) {
5801 all = 1;
5802 }
5803
5804 for (i = 0; i < set->used; ) {
5805 replaced = 0;
5806
5807 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5808 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005809 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005810 for (sub = set->val.nodes[i].node->attr; sub; sub = sub->next) {
5811
5812 /* check "namespace" */
5813 if (moveto_mod && (sub->annotation->module != moveto_mod)) {
5814 continue;
5815 }
5816
5817 if (all || (!strncmp(sub->name, qname, qname_len) && !sub->name[qname_len])) {
5818 /* match */
5819 if (!replaced) {
5820 set->val.attrs[i].attr = sub;
5821 set->val.attrs[i].type = LYXP_NODE_ATTR;
5822 /* pos does not change */
5823 replaced = 1;
5824 } else {
5825 set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_ATTR, i + 1);
5826 }
5827 ++i;
5828 }
5829 }
5830 }
5831
5832 if (!replaced) {
5833 /* no match */
5834 set_remove_node(set, i);
5835 }
5836 }
5837
5838 return LY_SUCCESS;
5839}
5840
5841/**
5842 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
5843 * Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware.
5844 *
5845 * @param[in,out] set1 Set to use for the result.
5846 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 * @return LY_ERR
5848 */
5849static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005850moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005851{
5852 LY_ERR rc;
5853
5854 if (((set1->type != LYXP_SET_NODE_SET) && (set1->type != LYXP_SET_EMPTY))
5855 || ((set2->type != LYXP_SET_NODE_SET) && (set2->type != LYXP_SET_EMPTY))) {
5856 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5857 return LY_EVALID;
5858 }
5859
5860 /* set2 is empty or both set1 and set2 */
5861 if (set2->type == LYXP_SET_EMPTY) {
5862 return LY_SUCCESS;
5863 }
5864
5865 if (set1->type == LYXP_SET_EMPTY) {
5866 memcpy(set1, set2, sizeof *set1);
5867 /* dynamic memory belongs to set1 now, do not free */
5868 set2->type = LYXP_SET_EMPTY;
5869 return LY_SUCCESS;
5870 }
5871
5872 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005873 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874
5875 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005876 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877 LY_CHECK_RET(rc);
5878
5879 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005880 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005881
5882 return LY_SUCCESS;
5883}
5884
5885/**
5886 * @brief Move context @p set to an attribute in any of the descendants. Handles '//' and '@*',
5887 * '@NAME', '@PREFIX:*', or '@PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5888 * Context position aware.
5889 *
5890 * @param[in,out] set Set to use.
5891 * @param[in] qname Qualified node name to move to.
5892 * @param[in] qname_len Length of @p qname.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005893 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5894 */
5895static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005896moveto_attr_alldesc(struct lyxp_set *set, const char *qname, uint16_t qname_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005897{
5898 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005899 int replaced, all = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005900 struct lyd_attr *sub;
Michal Vasko6346ece2019-09-24 13:12:53 +02005901 const struct lys_module *moveto_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902 struct lyxp_set *set_all_desc = NULL;
5903 LY_ERR rc;
5904
5905 if (!set || (set->type == LYXP_SET_EMPTY)) {
5906 return LY_SUCCESS;
5907 }
5908
5909 if (set->type != LYXP_SET_NODE_SET) {
5910 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5911 return LY_EVALID;
5912 }
5913
Michal Vasko6346ece2019-09-24 13:12:53 +02005914 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5915 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916
5917 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5918 * but it likely won't be used much, so it's a waste of time */
5919 /* copy the context */
5920 set_all_desc = set_copy(set);
5921 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005922 rc = moveto_node_alldesc(set_all_desc, "*", 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923 if (rc != LY_SUCCESS) {
5924 lyxp_set_free(set_all_desc);
5925 return rc;
5926 }
5927 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005928 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929 if (rc != LY_SUCCESS) {
5930 lyxp_set_free(set_all_desc);
5931 return rc;
5932 }
5933 lyxp_set_free(set_all_desc);
5934
5935 if ((qname_len == 1) && (qname[0] == '*')) {
5936 all = 1;
5937 }
5938
5939 for (i = 0; i < set->used; ) {
5940 replaced = 0;
5941
5942 /* only attributes of an elem can be in the result, skip all the rest,
5943 * we have all attributes qualified in lyd tree */
5944 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5945 for (sub = set->val.nodes[i].node->attr; sub; sub = sub->next) {
5946 /* check "namespace" */
5947 if (moveto_mod && (sub->annotation->module != moveto_mod)) {
5948 continue;
5949 }
5950
5951 if (all || (!strncmp(sub->name, qname, qname_len) && !sub->name[qname_len])) {
5952 /* match */
5953 if (!replaced) {
5954 set->val.attrs[i].attr = sub;
5955 set->val.attrs[i].type = LYXP_NODE_ATTR;
5956 /* pos does not change */
5957 replaced = 1;
5958 } else {
5959 set_insert_node(set, (struct lyd_node *)sub, set->val.attrs[i].pos, LYXP_NODE_ATTR, i + 1);
5960 }
5961 ++i;
5962 }
5963 }
5964 }
5965
5966 if (!replaced) {
5967 /* no match */
5968 set_remove_node(set, i);
5969 }
5970 }
5971
5972 return LY_SUCCESS;
5973}
5974
5975/**
5976 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
5977 * (or LYXP_SET_EMPTY). Context position aware.
5978 *
5979 * @param[in] parent Current parent.
5980 * @param[in] parent_pos Position of @p parent.
5981 * @param[in] parent_type Node type of @p parent.
5982 * @param[in,out] to_set Set to use.
5983 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 * @param[in] options XPath options.
5985 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5986 */
5987static LY_ERR
5988moveto_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 +01005989 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990{
5991 const struct lyd_node *sub;
5992 LY_ERR rc;
5993
5994 switch (parent_type) {
5995 case LYXP_NODE_ROOT:
5996 case LYXP_NODE_ROOT_CONFIG:
5997 /* add the same node but as an element */
5998 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_ELEM, -1)) {
5999 set_insert_node(to_set, parent, 0, LYXP_NODE_ELEM, to_set->used);
6000
6001 /* skip anydata/anyxml and dummy nodes */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006002 if (!(parent->schema->nodetype & LYS_ANYDATA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006003 /* also add all the children of this node, recursively */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006004 rc = moveto_self_add_children_r(parent, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006005 LY_CHECK_RET(rc);
6006 }
6007 }
6008 break;
6009 case LYXP_NODE_ELEM:
6010 /* add all the children ... */
6011 if (!(parent->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
6012 for (sub = lyd_node_children(parent); sub; sub = sub->next) {
6013 /* context check */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006014 if ((to_set->root_type == LYXP_NODE_ROOT_CONFIG) && (sub->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006015 continue;
6016 }
6017
Michal Vaskoa1424542019-11-14 16:08:52 +01006018 /* when check */
6019 if (moveto_when_check(sub)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006021 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022
6023 if (!set_dup_node_check(dup_check_set, sub, LYXP_NODE_ELEM, -1)) {
6024 set_insert_node(to_set, sub, 0, LYXP_NODE_ELEM, to_set->used);
6025
Michal Vasko5c4e5892019-11-14 12:31:38 +01006026 /* skip anydata/anyxml nodes */
6027 if (sub->schema->nodetype & LYS_ANYDATA) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028 continue;
6029 }
6030
6031 /* also add all the children of this node, recursively */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006032 rc = moveto_self_add_children_r(sub, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033 LY_CHECK_RET(rc);
6034 }
6035 }
6036
6037 /* ... or add their text node, ... */
6038 } else {
6039 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6040 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6041 }
6042 }
6043 break;
6044 default:
6045 LOGINT_RET(parent->schema->module->ctx);
6046 }
6047
6048 return LY_SUCCESS;
6049}
6050
6051/**
6052 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6053 * (or LYXP_SET_EMPTY). Context position aware.
6054 *
6055 * @param[in,out] set Set to use.
6056 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6057 * @param[in] options XPath options.
6058 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6059 */
6060static LY_ERR
6061moveto_self(struct lyxp_set *set, int all_desc, int options)
6062{
6063 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 struct lyxp_set ret_set;
6065 LY_ERR rc;
6066
6067 if (!set || (set->type == LYXP_SET_EMPTY)) {
6068 return LY_SUCCESS;
6069 }
6070
6071 if (set->type != LYXP_SET_NODE_SET) {
6072 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6073 return LY_EVALID;
6074 }
6075
6076 /* nothing to do */
6077 if (!all_desc) {
6078 return LY_SUCCESS;
6079 }
6080
Michal Vasko03ff5a72019-09-11 13:49:33 +02006081 /* add all the children, they get added recursively */
6082 set_init(&ret_set, set);
6083 for (i = 0; i < set->used; ++i) {
6084 /* copy the current node to tmp */
6085 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6086
6087 /* do not touch attributes and text nodes */
6088 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_ATTR)) {
6089 continue;
6090 }
6091
Michal Vasko5c4e5892019-11-14 12:31:38 +01006092 /* skip anydata/anyxml nodes */
6093 if (set->val.nodes[i].node->schema->nodetype & LYS_ANYDATA) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 continue;
6095 }
6096
6097 /* add all the children */
6098 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 +01006099 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100 if (rc != LY_SUCCESS) {
6101 set_free_content(&ret_set);
6102 return rc;
6103 }
6104 }
6105
6106 /* use the temporary set as the current one */
6107 ret_set.ctx_pos = set->ctx_pos;
6108 ret_set.ctx_size = set->ctx_size;
6109 set_free_content(set);
6110 memcpy(set, &ret_set, sizeof *set);
6111
6112 return LY_SUCCESS;
6113}
6114
6115/**
6116 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6117 * (or LYXP_SET_EMPTY).
6118 *
6119 * @param[in,out] set Set to use.
6120 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6121 * @param[in] options XPath options.
6122 * @return LY_ERR
6123 */
6124static LY_ERR
6125moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6126{
6127 const struct lysc_node *sub;
6128 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006129
6130 if (!set || (set->type == LYXP_SET_EMPTY)) {
6131 return LY_SUCCESS;
6132 }
6133
6134 if (set->type != LYXP_SET_SCNODE_SET) {
6135 LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6136 return LY_EVALID;
6137 }
6138
6139 /* nothing to do */
6140 if (!all_desc) {
6141 return LY_SUCCESS;
6142 }
6143
Michal Vasko03ff5a72019-09-11 13:49:33 +02006144 /* add all the children, they get added recursively */
6145 for (i = 0; i < set->used; ++i) {
6146 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006147 if (set->val.scnodes[i].in_ctx != -2) {
6148 continue;
6149 }
6150
6151 /* remember context node (it was traversed again so it changes to a normal node) */
6152 set->val.scnodes[i].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006153 }
6154
6155 /* add all the children */
6156 if (set->val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_CONTAINER)) {
6157 sub = NULL;
6158 while ((sub = lys_getnext(sub, set->val.scnodes[i].scnode, NULL, LYS_GETNEXT_NOSTATECHECK))) {
6159 /* RPC input/output check */
6160 if (options & LYXP_SCNODE_OUTPUT) {
6161 if (sub->parent->nodetype == LYS_INPUT) {
6162 continue;
6163 }
6164 } else {
6165 if (sub->parent->nodetype == LYS_OUTPUT) {
6166 continue;
6167 }
6168 }
6169
6170 /* context check */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006171 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (sub->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006172 continue;
6173 }
6174
Michal Vaskoecd62de2019-11-13 12:35:11 +01006175 lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006176 /* throw away the insert index, we want to consider that node again, recursively */
6177 }
6178 }
6179 }
6180
6181 return LY_SUCCESS;
6182}
6183
6184/**
6185 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6186 * (or LYXP_SET_EMPTY). Context position aware.
6187 *
6188 * @param[in] set Set to use.
6189 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6190 * @param[in] options XPath options.
6191 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6192 */
6193static LY_ERR
6194moveto_parent(struct lyxp_set *set, int all_desc, int options)
6195{
6196 LY_ERR rc;
6197 uint32_t i;
6198 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006199 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006200
6201 if (!set || (set->type == LYXP_SET_EMPTY)) {
6202 return LY_SUCCESS;
6203 }
6204
6205 if (set->type != LYXP_SET_NODE_SET) {
6206 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6207 return LY_EVALID;
6208 }
6209
6210 if (all_desc) {
6211 /* <path>//.. == <path>//./.. */
6212 rc = moveto_self(set, 1, options);
6213 LY_CHECK_RET(rc);
6214 }
6215
Michal Vasko57eab132019-09-24 11:46:26 +02006216 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006217 node = set->val.nodes[i].node;
6218
6219 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6220 new_node = (struct lyd_node *)node->parent;
6221 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6222 new_node = node;
6223 } else if (set->val.nodes[i].type == LYXP_NODE_ATTR) {
6224 new_node = set->val.attrs[i].attr->parent;
6225 if (!new_node) {
6226 LOGINT_RET(set->ctx);
6227 }
6228 } else {
6229 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006230 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006231 continue;
6232 }
6233
Michal Vaskoa1424542019-11-14 16:08:52 +01006234 /* when check */
6235 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006237 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238
6239 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006240 if (!new_node) {
6241 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006242
6243 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6244 } else {
6245 new_type = LYXP_NODE_ELEM;
6246 }
6247
Michal Vasko03ff5a72019-09-11 13:49:33 +02006248 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006249 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006250 } else {
6251 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006252 }
6253 }
6254
Michal Vasko2caefc12019-11-14 16:07:56 +01006255 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006256 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006257
6258 return LY_SUCCESS;
6259}
6260
6261/**
6262 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6263 * (or LYXP_SET_EMPTY).
6264 *
6265 * @param[in] set Set to use.
6266 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6267 * @param[in] options XPath options.
6268 * @return LY_ERR
6269 */
6270static LY_ERR
6271moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6272{
6273 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006274 const struct lysc_node *node, *new_node;
6275 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006276 LY_ERR rc;
6277
6278 if (!set || (set->type == LYXP_SET_EMPTY)) {
6279 return LY_SUCCESS;
6280 }
6281
6282 if (set->type != LYXP_SET_SCNODE_SET) {
6283 LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6284 return LY_EVALID;
6285 }
6286
6287 if (all_desc) {
6288 /* <path>//.. == <path>//./.. */
6289 rc = moveto_scnode_self(set, 1, options);
6290 LY_CHECK_RET(rc);
6291 }
6292
Michal Vasko03ff5a72019-09-11 13:49:33 +02006293 orig_used = set->used;
6294 for (i = 0; i < orig_used; ++i) {
6295 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006296 if (set->val.scnodes[i].in_ctx != -2) {
6297 continue;
6298 }
6299
6300 /* remember context node */
6301 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006302 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303
6304 node = set->val.scnodes[i].scnode;
6305
6306 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6307 for (new_node = node->parent;
6308 new_node && (new_node->nodetype & (LYS_CHOICE | LYS_CASE));
6309 new_node = new_node->parent);
6310 } else {
6311 /* root does not have a parent */
6312 continue;
6313 }
6314
Michal Vasko03ff5a72019-09-11 13:49:33 +02006315 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006316 if (!new_node) {
6317 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006318
6319 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6320 } else {
6321 new_type = LYXP_NODE_ELEM;
6322 }
6323
Michal Vaskoecd62de2019-11-13 12:35:11 +01006324 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006325 if ((idx < orig_used) && (idx > i)) {
6326 set->val.scnodes[idx].in_ctx = 2;
6327 temp_ctx = 1;
6328 }
6329 }
6330
6331 if (temp_ctx) {
6332 for (i = 0; i < orig_used; ++i) {
6333 if (set->val.scnodes[i].in_ctx == 2) {
6334 set->val.scnodes[i].in_ctx = 1;
6335 }
6336 }
6337 }
6338
6339 return LY_SUCCESS;
6340}
6341
6342/**
6343 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6344 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6345 *
6346 * @param[in,out] set1 Set to use for the result.
6347 * @param[in] set2 Set acting as the second operand for @p op.
6348 * @param[in] op Comparison operator to process.
6349 * @param[in] options XPath options.
6350 * @return LY_ERR
6351 */
6352static LY_ERR
6353moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6354{
6355 /*
6356 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6357 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6358 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6359 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6360 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6361 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6362 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6363 *
6364 * '=' or '!='
6365 * BOOLEAN + BOOLEAN
6366 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6367 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6368 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6369 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6370 * NUMBER + NUMBER
6371 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6372 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6373 * STRING + STRING
6374 *
6375 * '<=', '<', '>=', '>'
6376 * NUMBER + NUMBER
6377 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6378 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6379 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6380 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6381 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6382 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6383 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6384 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6385 */
6386 struct lyxp_set iter1, iter2;
6387 int result;
6388 int64_t i;
6389 LY_ERR rc;
6390
6391 iter1.type = LYXP_SET_EMPTY;
6392
6393 /* empty node-sets are always false */
6394 if ((set1->type == LYXP_SET_EMPTY) || (set2->type == LYXP_SET_EMPTY)) {
6395 set_fill_boolean(set1, 0);
6396 return LY_SUCCESS;
6397 }
6398
6399 /* iterative evaluation with node-sets */
6400 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6401 if (set1->type == LYXP_SET_NODE_SET) {
6402 for (i = 0; i < set1->used; ++i) {
6403 switch (set2->type) {
6404 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006405 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406 break;
6407 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006408 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006409 break;
6410 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006411 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412 break;
6413 }
6414 LY_CHECK_RET(rc);
6415
6416 rc = moveto_op_comp(&iter1, set2, op, options);
6417 if (rc != LY_SUCCESS) {
6418 set_free_content(&iter1);
6419 return rc;
6420 }
6421
6422 /* lazy evaluation until true */
6423 if (iter1.val.bool) {
6424 set_fill_boolean(set1, 1);
6425 return LY_SUCCESS;
6426 }
6427 }
6428 } else {
6429 for (i = 0; i < set2->used; ++i) {
6430 switch (set1->type) {
6431 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006432 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433 break;
6434 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006435 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006436 break;
6437 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006438 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006439 break;
6440 }
6441 LY_CHECK_RET(rc);
6442
6443 set_fill_set(&iter1, set1);
6444
6445 rc = moveto_op_comp(&iter1, &iter2, op, options);
6446 if (rc != LY_SUCCESS) {
6447 set_free_content(&iter1);
6448 set_free_content(&iter2);
6449 return rc;
6450 }
6451 set_free_content(&iter2);
6452
6453 /* lazy evaluation until true */
6454 if (iter1.val.bool) {
6455 set_fill_boolean(set1, 1);
6456 return LY_SUCCESS;
6457 }
6458 }
6459 }
6460
6461 /* false for all nodes */
6462 set_fill_boolean(set1, 0);
6463 return LY_SUCCESS;
6464 }
6465
6466 /* first convert properly */
6467 if ((op[0] == '=') || (op[0] == '!')) {
6468 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006469 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6470 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006471 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006472 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006473 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006474 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006475 LY_CHECK_RET(rc);
6476 } /* else we have 2 strings */
6477 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006478 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006479 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006480 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006481 LY_CHECK_RET(rc);
6482 }
6483
6484 assert(set1->type == set2->type);
6485
6486 /* compute result */
6487 if (op[0] == '=') {
6488 if (set1->type == LYXP_SET_BOOLEAN) {
6489 result = (set1->val.bool == set2->val.bool);
6490 } else if (set1->type == LYXP_SET_NUMBER) {
6491 result = (set1->val.num == set2->val.num);
6492 } else {
6493 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006494 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006495 }
6496 } else if (op[0] == '!') {
6497 if (set1->type == LYXP_SET_BOOLEAN) {
6498 result = (set1->val.bool != set2->val.bool);
6499 } else if (set1->type == LYXP_SET_NUMBER) {
6500 result = (set1->val.num != set2->val.num);
6501 } else {
6502 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006503 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006504 }
6505 } else {
6506 assert(set1->type == LYXP_SET_NUMBER);
6507 if (op[0] == '<') {
6508 if (op[1] == '=') {
6509 result = (set1->val.num <= set2->val.num);
6510 } else {
6511 result = (set1->val.num < set2->val.num);
6512 }
6513 } else {
6514 if (op[1] == '=') {
6515 result = (set1->val.num >= set2->val.num);
6516 } else {
6517 result = (set1->val.num > set2->val.num);
6518 }
6519 }
6520 }
6521
6522 /* assign result */
6523 if (result) {
6524 set_fill_boolean(set1, 1);
6525 } else {
6526 set_fill_boolean(set1, 0);
6527 }
6528
6529 return LY_SUCCESS;
6530}
6531
6532/**
6533 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6534 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6535 *
6536 * @param[in,out] set1 Set to use for the result.
6537 * @param[in] set2 Set acting as the second operand for @p op.
6538 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006539 * @return LY_ERR
6540 */
6541static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006542moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006543{
6544 LY_ERR rc;
6545
6546 /* unary '-' */
6547 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006548 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006549 LY_CHECK_RET(rc);
6550 set1->val.num *= -1;
6551 lyxp_set_free(set2);
6552 return LY_SUCCESS;
6553 }
6554
6555 assert(set1 && set2);
6556
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006557 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006558 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006559 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006560 LY_CHECK_RET(rc);
6561
6562 switch (op[0]) {
6563 /* '+' */
6564 case '+':
6565 set1->val.num += set2->val.num;
6566 break;
6567
6568 /* '-' */
6569 case '-':
6570 set1->val.num -= set2->val.num;
6571 break;
6572
6573 /* '*' */
6574 case '*':
6575 set1->val.num *= set2->val.num;
6576 break;
6577
6578 /* 'div' */
6579 case 'd':
6580 set1->val.num /= set2->val.num;
6581 break;
6582
6583 /* 'mod' */
6584 case 'm':
6585 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6586 break;
6587
6588 default:
6589 LOGINT_RET(set1->ctx);
6590 }
6591
6592 return LY_SUCCESS;
6593}
6594
6595/*
6596 * eval functions
6597 *
6598 * They execute a parsed XPath expression on some data subtree.
6599 */
6600
6601/**
6602 * @brief Evaluate Literal. Logs directly on error.
6603 *
6604 * @param[in] exp Parsed XPath expression.
6605 * @param[in] exp_idx Position in the expression @p exp.
6606 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6607 */
6608static void
6609eval_literal(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
6610{
6611 if (set) {
6612 if (exp->tok_len[*exp_idx] == 2) {
6613 set_fill_string(set, "", 0);
6614 } else {
6615 set_fill_string(set, &exp->expr[exp->tok_pos[*exp_idx] + 1], exp->tok_len[*exp_idx] - 2);
6616 }
6617 }
6618 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6619 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6620 ++(*exp_idx);
6621}
6622
6623/**
6624 * @brief Evaluate NodeTest. Logs directly on error.
6625 *
6626 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6627 *
6628 * @param[in] exp Parsed XPath expression.
6629 * @param[in] exp_idx Position in the expression @p exp.
6630 * @param[in] attr_axis Whether to search attributes or standard nodes.
6631 * @param[in] all_desc Whether to search all the descendants or children only.
6632 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6633 * @param[in] options XPath options.
6634 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6635 */
6636static int
6637eval_node_test(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc,
6638 struct lyxp_set *set, int options)
6639{
6640 int i;
6641 char *path;
6642 LY_ERR rc;
6643
6644 switch (exp->tokens[*exp_idx]) {
6645 case LYXP_TOKEN_NAMETEST:
6646 if (attr_axis) {
6647 if (set && (options & LYXP_SCNODE_ALL)) {
6648 set_scnode_clear_ctx(set);
6649 rc = LY_SUCCESS;
6650 } else {
6651 if (all_desc) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006652 rc = moveto_attr_alldesc(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006654 rc = moveto_attr(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 }
6656 }
6657 } else {
6658 if (all_desc) {
6659 if (set && (options & LYXP_SCNODE_ALL)) {
6660 rc = moveto_scnode_alldesc(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx], options);
6661 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006662 rc = moveto_node_alldesc(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006663 }
6664 } else {
6665 if (set && (options & LYXP_SCNODE_ALL)) {
6666 rc = moveto_scnode(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx], options);
6667 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006668 rc = moveto_node(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 }
6670 }
6671
6672 if ((rc == LY_SUCCESS) && set && (options & LYXP_SCNODE_ALL)) {
6673 for (i = set->used - 1; i > -1; --i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006674 if (set->val.scnodes[i].in_ctx > 0) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006675 break;
6676 }
6677 }
6678 if (i == -1) {
6679 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
6680 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
6681 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]],
6682 exp->tok_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
6683 free(path);
6684 }
6685 }
6686 }
6687 LY_CHECK_RET(rc);
6688
6689 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6690 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6691 ++(*exp_idx);
6692 break;
6693
6694 case LYXP_TOKEN_NODETYPE:
6695 if (set) {
6696 assert(exp->tok_len[*exp_idx] == 4);
6697 if (set->type == LYXP_SET_SCNODE_SET) {
6698 set_scnode_clear_ctx(set);
6699 /* just for the debug message underneath */
6700 set = NULL;
6701 } else {
6702 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "node", 4)) {
6703 rc = xpath_node(NULL, 0, set, options);
6704 LY_CHECK_RET(rc);
6705 } else {
6706 assert(!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "text", 4));
6707 rc = xpath_text(NULL, 0, set, options);
6708 LY_CHECK_RET(rc);
6709 }
6710 }
6711 }
6712 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6713 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6714 ++(*exp_idx);
6715
6716 /* '(' */
6717 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
6718 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6719 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6720 ++(*exp_idx);
6721
6722 /* ')' */
6723 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
6724 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6725 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6726 ++(*exp_idx);
6727 break;
6728
6729 default:
Michal Vasko02a77382019-09-12 11:47:35 +02006730 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 }
6732
6733 return LY_SUCCESS;
6734}
6735
6736/**
6737 * @brief Evaluate Predicate. Logs directly on error.
6738 *
6739 * [7] Predicate ::= '[' Expr ']'
6740 *
6741 * @param[in] exp Parsed XPath expression.
6742 * @param[in] exp_idx Position in the expression @p exp.
6743 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6744 * @param[in] options XPath options.
6745 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6746 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6747 */
6748static LY_ERR
6749eval_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options, int parent_pos_pred)
6750{
6751 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006752 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006753 uint32_t orig_pos, orig_size;
6754 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006755 struct lyxp_set set2;
6756 struct lyd_node *orig_parent;
6757
6758 /* '[' */
6759 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6760 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6761 ++(*exp_idx);
6762
6763 if (!set) {
6764only_parse:
6765 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
6766 LY_CHECK_RET(rc);
6767 } else if (set->type == LYXP_SET_NODE_SET) {
6768 /* 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 +01006769 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006770
6771 /* empty set, nothing to evaluate */
6772 if (!set->used) {
6773 goto only_parse;
6774 }
6775
6776 orig_exp = *exp_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777 orig_pos = 0;
6778 orig_size = set->used;
6779 orig_parent = NULL;
6780 for (i = 0; i < set->used; ) {
6781 set_init(&set2, set);
6782 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6783 /* remember the node context position for position() and context size for last(),
6784 * predicates should always be evaluated with respect to the child axis (since we do
6785 * not support explicit axes) so we assign positions based on their parents */
6786 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6787 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6788 orig_pos = 1;
6789 } else {
6790 ++orig_pos;
6791 }
6792
6793 set2.ctx_pos = orig_pos;
6794 set2.ctx_size = orig_size;
6795 *exp_idx = orig_exp;
6796
6797 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6798 if (rc != LY_SUCCESS) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006799 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800 return rc;
6801 }
6802
6803 /* number is a position */
6804 if (set2.type == LYXP_SET_NUMBER) {
6805 if ((long long)set2.val.num == orig_pos) {
6806 set2.val.num = 1;
6807 } else {
6808 set2.val.num = 0;
6809 }
6810 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006811 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006812
6813 /* predicate satisfied or not? */
Michal Vasko57eab132019-09-24 11:46:26 +02006814 if (!set2.val.bool) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006815 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006816 }
6817 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006818 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006819
6820 } else if (set->type == LYXP_SET_SCNODE_SET) {
6821 for (i = 0; i < set->used; ++i) {
6822 if (set->val.scnodes[i].in_ctx == 1) {
6823 /* there is a currently-valid node */
6824 break;
6825 }
6826 }
6827 /* empty set, nothing to evaluate */
6828 if (i == set->used) {
6829 goto only_parse;
6830 }
6831
6832 orig_exp = *exp_idx;
6833
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834 /* set special in_ctx to all the valid snodes */
6835 pred_in_ctx = set_scnode_new_in_ctx(set);
6836
6837 /* use the valid snodes one-by-one */
6838 for (i = 0; i < set->used; ++i) {
6839 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6840 continue;
6841 }
6842 set->val.scnodes[i].in_ctx = 1;
6843
6844 *exp_idx = orig_exp;
6845
6846 rc = eval_expr_select(exp, exp_idx, 0, set, options);
6847 LY_CHECK_RET(rc);
6848
6849 set->val.scnodes[i].in_ctx = pred_in_ctx;
6850 }
6851
6852 /* restore the state as it was before the predicate */
6853 for (i = 0; i < set->used; ++i) {
6854 if (set->val.scnodes[i].in_ctx == 1) {
6855 set->val.scnodes[i].in_ctx = 0;
6856 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6857 set->val.scnodes[i].in_ctx = 1;
6858 }
6859 }
6860
6861 } else {
6862 set2.type = LYXP_SET_EMPTY;
6863 set_fill_set(&set2, set);
6864
6865 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6866 if (rc != LY_SUCCESS) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006867 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006868 return rc;
6869 }
6870
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006871 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006872 if (!set2.val.bool) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006873 lyxp_set_cast(set, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006874 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006875 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006876 }
6877
6878 /* ']' */
6879 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK2);
6880 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6881 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6882 ++(*exp_idx);
6883
6884 return LY_SUCCESS;
6885}
6886
6887/**
6888 * @brief Evaluate RelativeLocationPath. Logs directly on error.
6889 *
6890 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
6891 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6892 *
6893 * @param[in] exp Parsed XPath expression.
6894 * @param[in] exp_idx Position in the expression @p exp.
6895 * @param[in] all_desc Whether to search all the descendants or children only.
6896 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6897 * @param[in] options XPath options.
6898 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
6899 */
6900static LY_ERR
6901eval_relative_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, int all_desc, struct lyxp_set *set, int options)
6902{
6903 int attr_axis;
6904 LY_ERR rc;
6905
6906 goto step;
6907 do {
6908 /* evaluate '/' or '//' */
6909 if (exp->tok_len[*exp_idx] == 1) {
6910 all_desc = 0;
6911 } else {
6912 assert(exp->tok_len[*exp_idx] == 2);
6913 all_desc = 1;
6914 }
6915 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6916 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6917 ++(*exp_idx);
6918
6919step:
6920 /* Step */
6921 attr_axis = 0;
6922 switch (exp->tokens[*exp_idx]) {
6923 case LYXP_TOKEN_DOT:
6924 /* evaluate '.' */
6925 if (set && (options & LYXP_SCNODE_ALL)) {
6926 rc = moveto_scnode_self(set, all_desc, options);
6927 } else {
6928 rc = moveto_self(set, all_desc, options);
6929 }
6930 LY_CHECK_RET(rc);
6931 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6932 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6933 ++(*exp_idx);
6934 break;
6935
6936 case LYXP_TOKEN_DDOT:
6937 /* evaluate '..' */
6938 if (set && (options & LYXP_SCNODE_ALL)) {
6939 rc = moveto_scnode_parent(set, all_desc, options);
6940 } else {
6941 rc = moveto_parent(set, all_desc, options);
6942 }
6943 LY_CHECK_RET(rc);
6944 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6945 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6946 ++(*exp_idx);
6947 break;
6948
6949 case LYXP_TOKEN_AT:
6950 /* evaluate '@' */
6951 attr_axis = 1;
6952 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6953 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6954 ++(*exp_idx);
6955
6956 /* fall through */
6957 case LYXP_TOKEN_NAMETEST:
6958 case LYXP_TOKEN_NODETYPE:
6959 rc = eval_node_test(exp, exp_idx, attr_axis, all_desc, set, options);
6960 LY_CHECK_RET(rc);
6961
6962 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
6963 rc = eval_predicate(exp, exp_idx, set, options, 1);
6964 LY_CHECK_RET(rc);
6965 }
6966 break;
6967
6968 default:
Michal Vasko02a77382019-09-12 11:47:35 +02006969 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006970 }
6971 } while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH));
6972
6973 return LY_SUCCESS;
6974}
6975
6976/**
6977 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
6978 *
6979 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
6980 *
6981 * @param[in] exp Parsed XPath expression.
6982 * @param[in] exp_idx Position in the expression @p exp.
6983 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6984 * @param[in] options XPath options.
6985 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6986 */
6987static LY_ERR
6988eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
6989{
6990 int all_desc;
6991 LY_ERR rc;
6992
6993 if (set) {
6994 /* no matter what tokens follow, we need to be at the root */
6995 moveto_root(set, options);
6996 }
6997
6998 /* '/' RelativeLocationPath? */
6999 if (exp->tok_len[*exp_idx] == 1) {
7000 /* evaluate '/' - deferred */
7001 all_desc = 0;
7002 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7003 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7004 ++(*exp_idx);
7005
Michal Vasko4b9e1052019-09-13 11:25:37 +02007006 if (exp_check_token(set ? set->ctx : NULL, exp, *exp_idx, LYXP_TOKEN_NONE, 0)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007007 return LY_SUCCESS;
7008 }
7009 switch (exp->tokens[*exp_idx]) {
7010 case LYXP_TOKEN_DOT:
7011 case LYXP_TOKEN_DDOT:
7012 case LYXP_TOKEN_AT:
7013 case LYXP_TOKEN_NAMETEST:
7014 case LYXP_TOKEN_NODETYPE:
7015 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7016 LY_CHECK_RET(rc);
7017 break;
7018 default:
7019 break;
7020 }
7021
7022 /* '//' RelativeLocationPath */
7023 } else {
7024 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7025 all_desc = 1;
7026 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7027 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7028 ++(*exp_idx);
7029
7030 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7031 LY_CHECK_RET(rc);
7032 }
7033
7034 return LY_SUCCESS;
7035}
7036
7037/**
7038 * @brief Evaluate FunctionCall. Logs directly on error.
7039 *
7040 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
7041 *
7042 * @param[in] exp Parsed XPath expression.
7043 * @param[in] exp_idx Position in the expression @p exp.
7044 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7045 * @param[in] options XPath options.
7046 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7047 */
7048static LY_ERR
7049eval_function_call(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7050{
7051 LY_ERR rc;
7052 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
7053 uint16_t arg_count = 0, i, func_exp = *exp_idx;
7054 struct lyxp_set **args = NULL, **args_aux;
7055
7056 if (set) {
7057 /* FunctionName */
7058 switch (exp->tok_len[*exp_idx]) {
7059 case 3:
7060 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
7061 xpath_func = &xpath_not;
7062 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
7063 xpath_func = &xpath_sum;
7064 }
7065 break;
7066 case 4:
7067 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
7068 xpath_func = &xpath_lang;
7069 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
7070 xpath_func = &xpath_last;
7071 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
7072 xpath_func = &xpath_name;
7073 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
7074 xpath_func = &xpath_true;
7075 }
7076 break;
7077 case 5:
7078 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
7079 xpath_func = &xpath_count;
7080 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
7081 xpath_func = &xpath_false;
7082 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
7083 xpath_func = &xpath_floor;
7084 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
7085 xpath_func = &xpath_round;
7086 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
7087 xpath_func = &xpath_deref;
7088 }
7089 break;
7090 case 6:
7091 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
7092 xpath_func = &xpath_concat;
7093 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
7094 xpath_func = &xpath_number;
7095 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
7096 xpath_func = &xpath_string;
7097 }
7098 break;
7099 case 7:
7100 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
7101 xpath_func = &xpath_boolean;
7102 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
7103 xpath_func = &xpath_ceiling;
7104 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
7105 xpath_func = &xpath_current;
7106 }
7107 break;
7108 case 8:
7109 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
7110 xpath_func = &xpath_contains;
7111 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
7112 xpath_func = &xpath_position;
7113 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
7114 xpath_func = &xpath_re_match;
7115 }
7116 break;
7117 case 9:
7118 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
7119 xpath_func = &xpath_substring;
7120 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
7121 xpath_func = &xpath_translate;
7122 }
7123 break;
7124 case 10:
7125 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
7126 xpath_func = &xpath_local_name;
7127 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
7128 xpath_func = &xpath_enum_value;
7129 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
7130 xpath_func = &xpath_bit_is_set;
7131 }
7132 break;
7133 case 11:
7134 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
7135 xpath_func = &xpath_starts_with;
7136 }
7137 break;
7138 case 12:
7139 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
7140 xpath_func = &xpath_derived_from;
7141 }
7142 break;
7143 case 13:
7144 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
7145 xpath_func = &xpath_namespace_uri;
7146 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
7147 xpath_func = &xpath_string_length;
7148 }
7149 break;
7150 case 15:
7151 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
7152 xpath_func = &xpath_normalize_space;
7153 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
7154 xpath_func = &xpath_substring_after;
7155 }
7156 break;
7157 case 16:
7158 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
7159 xpath_func = &xpath_substring_before;
7160 }
7161 break;
7162 case 20:
7163 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
7164 xpath_func = &xpath_derived_from_or_self;
7165 }
7166 break;
7167 }
7168
7169 if (!xpath_func) {
7170 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7171 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
7172 return LY_EVALID;
7173 }
7174 }
7175
7176 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7177 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7178 ++(*exp_idx);
7179
7180 /* '(' */
7181 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7182 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7183 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7184 ++(*exp_idx);
7185
7186 /* ( Expr ( ',' Expr )* )? */
7187 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
7188 if (set) {
7189 args = malloc(sizeof *args);
7190 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7191 arg_count = 1;
7192 args[0] = set_copy(set);
7193 if (!args[0]) {
7194 rc = LY_EMEM;
7195 goto cleanup;
7196 }
7197
7198 rc = eval_expr_select(exp, exp_idx, 0, args[0], options);
7199 LY_CHECK_GOTO(rc, cleanup);
7200 } else {
7201 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7202 LY_CHECK_GOTO(rc, cleanup);
7203 }
7204 }
7205 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_COMMA)) {
7206 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7207 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7208 ++(*exp_idx);
7209
7210 if (set) {
7211 ++arg_count;
7212 args_aux = realloc(args, arg_count * sizeof *args);
7213 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7214 args = args_aux;
7215 args[arg_count - 1] = set_copy(set);
7216 if (!args[arg_count - 1]) {
7217 rc = LY_EMEM;
7218 goto cleanup;
7219 }
7220
7221 rc = eval_expr_select(exp, exp_idx, 0, args[arg_count - 1], options);
7222 LY_CHECK_GOTO(rc, cleanup);
7223 } else {
7224 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7225 LY_CHECK_GOTO(rc, cleanup);
7226 }
7227 }
7228
7229 /* ')' */
7230 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7231 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7232 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7233 ++(*exp_idx);
7234
7235 if (set) {
7236 /* evaluate function */
7237 rc = xpath_func(args, arg_count, set, options);
7238
7239 if (options & LYXP_SCNODE_ALL) {
7240 if (rc == LY_EINVAL) {
7241 /* some validation warning TODO log everything immediately? */
7242 LOGWRN(set->ctx, "Previous warning generated by XPath function \"%.*s\".",
7243 (exp->tok_pos[*exp_idx - 1] - exp->tok_pos[func_exp]) + 1, &exp->expr[exp->tok_pos[func_exp]]);
7244 rc = LY_SUCCESS;
7245 }
7246
7247 /* merge all nodes from arg evaluations */
7248 for (i = 0; i < arg_count; ++i) {
7249 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007250 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007251 }
7252 }
7253 } else {
7254 rc = LY_SUCCESS;
7255 }
7256
7257cleanup:
7258 for (i = 0; i < arg_count; ++i) {
7259 lyxp_set_free(args[i]);
7260 }
7261 free(args);
7262
7263 return rc;
7264}
7265
7266/**
7267 * @brief Evaluate Number. Logs directly on error.
7268 *
7269 * @param[in] ctx Context for errors.
7270 * @param[in] exp Parsed XPath expression.
7271 * @param[in] exp_idx Position in the expression @p exp.
7272 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7273 * @return LY_ERR
7274 */
7275static LY_ERR
7276eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
7277{
7278 long double num;
7279 char *endptr;
7280
7281 if (set) {
7282 errno = 0;
7283 num = strtold(&exp->expr[exp->tok_pos[*exp_idx]], &endptr);
7284 if (errno) {
7285 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7286 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
7287 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]], strerror(errno));
7288 return LY_EVALID;
7289 } else if (endptr - &exp->expr[exp->tok_pos[*exp_idx]] != exp->tok_len[*exp_idx]) {
7290 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7291 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
7292 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
7293 return LY_EVALID;
7294 }
7295
7296 set_fill_number(set, num);
7297 }
7298
7299 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7300 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7301 ++(*exp_idx);
7302 return LY_SUCCESS;
7303}
7304
7305/**
7306 * @brief Evaluate PathExpr. Logs directly on error.
7307 *
7308 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
7309 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7310 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7311 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
7312 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
7313 *
7314 * @param[in] exp Parsed XPath expression.
7315 * @param[in] exp_idx Position in the expression @p exp.
7316 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7317 * @param[in] options XPath options.
7318 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7319 */
7320static LY_ERR
7321eval_path_expr(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7322{
7323 int all_desc, parent_pos_pred;
7324 LY_ERR rc;
7325
7326 switch (exp->tokens[*exp_idx]) {
7327 case LYXP_TOKEN_PAR1:
7328 /* '(' Expr ')' */
7329
7330 /* '(' */
7331 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7332 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7333 ++(*exp_idx);
7334
7335 /* Expr */
7336 rc = eval_expr_select(exp, exp_idx, 0, set, options);
7337 LY_CHECK_RET(rc);
7338
7339 /* ')' */
7340 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7341 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7342 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7343 ++(*exp_idx);
7344
7345 parent_pos_pred = 0;
7346 goto predicate;
7347
7348 case LYXP_TOKEN_DOT:
7349 case LYXP_TOKEN_DDOT:
7350 case LYXP_TOKEN_AT:
7351 case LYXP_TOKEN_NAMETEST:
7352 case LYXP_TOKEN_NODETYPE:
7353 /* RelativeLocationPath */
7354 rc = eval_relative_location_path(exp, exp_idx, 0, set, options);
7355 LY_CHECK_RET(rc);
7356 break;
7357
7358 case LYXP_TOKEN_FUNCNAME:
7359 /* FunctionCall */
7360 if (!set) {
7361 rc = eval_function_call(exp, exp_idx, NULL, options);
7362 } else {
7363 rc = eval_function_call(exp, exp_idx, set, options);
7364 }
7365 LY_CHECK_RET(rc);
7366
7367 parent_pos_pred = 1;
7368 goto predicate;
7369
7370 case LYXP_TOKEN_OPERATOR_PATH:
7371 /* AbsoluteLocationPath */
7372 rc = eval_absolute_location_path(exp, exp_idx, set, options);
7373 LY_CHECK_RET(rc);
7374 break;
7375
7376 case LYXP_TOKEN_LITERAL:
7377 /* Literal */
7378 if (!set || (options & LYXP_SCNODE_ALL)) {
7379 if (set) {
7380 set_scnode_clear_ctx(set);
7381 }
7382 eval_literal(exp, exp_idx, NULL);
7383 } else {
7384 eval_literal(exp, exp_idx, set);
7385 }
7386
7387 parent_pos_pred = 1;
7388 goto predicate;
7389
7390 case LYXP_TOKEN_NUMBER:
7391 /* Number */
7392 if (!set || (options & LYXP_SCNODE_ALL)) {
7393 if (set) {
7394 set_scnode_clear_ctx(set);
7395 }
Michal Vasko02a77382019-09-12 11:47:35 +02007396 rc = eval_number(NULL, exp, exp_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 } else {
7398 rc = eval_number(set->ctx, exp, exp_idx, set);
7399 }
7400 LY_CHECK_RET(rc);
7401
7402 parent_pos_pred = 1;
7403 goto predicate;
7404
7405 default:
7406 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, print_token(exp->tokens[*exp_idx]),
7407 &exp->expr[exp->tok_pos[*exp_idx]]);
7408 return LY_EVALID;
7409 }
7410
7411 return LY_SUCCESS;
7412
7413predicate:
7414 /* Predicate* */
7415 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
7416 rc = eval_predicate(exp, exp_idx, set, options, parent_pos_pred);
7417 LY_CHECK_RET(rc);
7418 }
7419
7420 /* ('/' or '//') RelativeLocationPath */
7421 if ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)) {
7422
7423 /* evaluate '/' or '//' */
7424 if (exp->tok_len[*exp_idx] == 1) {
7425 all_desc = 0;
7426 } else {
7427 assert(exp->tok_len[*exp_idx] == 2);
7428 all_desc = 1;
7429 }
7430
7431 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7432 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7433 ++(*exp_idx);
7434
7435 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7436 LY_CHECK_RET(rc);
7437 }
7438
7439 return LY_SUCCESS;
7440}
7441
7442/**
7443 * @brief Evaluate UnionExpr. Logs directly on error.
7444 *
7445 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
7446 *
7447 * @param[in] exp Parsed XPath expression.
7448 * @param[in] exp_idx Position in the expression @p exp.
7449 * @param[in] repeat How many times this expression is repeated.
7450 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7451 * @param[in] options XPath options.
7452 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7453 */
7454static LY_ERR
7455eval_union_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7456{
7457 LY_ERR rc = LY_SUCCESS;
7458 struct lyxp_set orig_set, set2;
7459 uint16_t i;
7460
7461 assert(repeat);
7462
7463 set_init(&orig_set, set);
7464 set_init(&set2, set);
7465
7466 set_fill_set(&orig_set, set);
7467
7468 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, set, options);
7469 LY_CHECK_GOTO(rc, cleanup);
7470
7471 /* ('|' PathExpr)* */
7472 for (i = 0; i < repeat; ++i) {
7473 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_UNI);
7474 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7475 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7476 ++(*exp_idx);
7477
7478 if (!set) {
7479 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, NULL, options);
7480 LY_CHECK_GOTO(rc, cleanup);
7481 continue;
7482 }
7483
7484 set_fill_set(&set2, &orig_set);
7485 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, &set2, options);
7486 LY_CHECK_GOTO(rc, cleanup);
7487
7488 /* eval */
7489 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007490 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007492 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 LY_CHECK_GOTO(rc, cleanup);
7494 }
7495 }
7496
7497cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007498 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7499 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 return rc;
7501}
7502
7503/**
7504 * @brief Evaluate UnaryExpr. Logs directly on error.
7505 *
7506 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
7507 *
7508 * @param[in] exp Parsed XPath expression.
7509 * @param[in] exp_idx Position in the expression @p exp.
7510 * @param[in] repeat How many times this expression is repeated.
7511 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7512 * @param[in] options XPath options.
7513 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7514 */
7515static LY_ERR
7516eval_unary_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7517{
7518 LY_ERR rc;
7519 uint16_t this_op, i;
7520
7521 assert(repeat);
7522
7523 /* ('-')+ */
7524 this_op = *exp_idx;
7525 for (i = 0; i < repeat; ++i) {
7526 assert(!exp_check_token(set->ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0) && (exp->expr[exp->tok_pos[*exp_idx]] == '-'));
7527
7528 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7529 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7530 ++(*exp_idx);
7531 }
7532
7533 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNARY, set, options);
7534 LY_CHECK_RET(rc);
7535
7536 if (set && (repeat % 2)) {
7537 if (options & LYXP_SCNODE_ALL) {
7538 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7539 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007540 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 LY_CHECK_RET(rc);
7542 }
7543 }
7544
7545 return LY_SUCCESS;
7546}
7547
7548/**
7549 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7550 *
7551 * [16] MultiplicativeExpr ::= UnaryExpr
7552 * | MultiplicativeExpr '*' UnaryExpr
7553 * | MultiplicativeExpr 'div' UnaryExpr
7554 * | MultiplicativeExpr 'mod' UnaryExpr
7555 *
7556 * @param[in] exp Parsed XPath expression.
7557 * @param[in] exp_idx Position in the expression @p exp.
7558 * @param[in] repeat How many times this expression is repeated.
7559 * @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
7564eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7565{
7566 LY_ERR rc;
7567 uint16_t this_op;
7568 struct lyxp_set orig_set, set2;
7569 uint16_t i;
7570
7571 assert(repeat);
7572
7573 set_init(&orig_set, set);
7574 set_init(&set2, set);
7575
7576 set_fill_set(&orig_set, set);
7577
7578 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
7579 LY_CHECK_GOTO(rc, cleanup);
7580
7581 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7582 for (i = 0; i < repeat; ++i) {
7583 this_op = *exp_idx;
7584
7585 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7586 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7587 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7588 ++(*exp_idx);
7589
7590 if (!set) {
7591 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
7592 LY_CHECK_GOTO(rc, cleanup);
7593 continue;
7594 }
7595
7596 set_fill_set(&set2, &orig_set);
7597 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
7598 LY_CHECK_GOTO(rc, cleanup);
7599
7600 /* eval */
7601 if (options & LYXP_SCNODE_ALL) {
7602 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007603 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 set_scnode_clear_ctx(set);
7605 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007606 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 LY_CHECK_GOTO(rc, cleanup);
7608 }
7609 }
7610
7611cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007612 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7613 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 return rc;
7615}
7616
7617/**
7618 * @brief Evaluate AdditiveExpr. Logs directly on error.
7619 *
7620 * [15] AdditiveExpr ::= MultiplicativeExpr
7621 * | AdditiveExpr '+' MultiplicativeExpr
7622 * | AdditiveExpr '-' MultiplicativeExpr
7623 *
7624 * @param[in] exp Parsed XPath expression.
7625 * @param[in] exp_idx Position in the expression @p exp.
7626 * @param[in] repeat How many times this expression is repeated.
7627 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7628 * @param[in] options XPath options.
7629 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7630 */
7631static LY_ERR
7632eval_additive_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7633{
7634 LY_ERR rc;
7635 uint16_t this_op;
7636 struct lyxp_set orig_set, set2;
7637 uint16_t i;
7638
7639 assert(repeat);
7640
7641 set_init(&orig_set, set);
7642 set_init(&set2, set);
7643
7644 set_fill_set(&orig_set, set);
7645
7646 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, set, options);
7647 LY_CHECK_GOTO(rc, cleanup);
7648
7649 /* ('+' / '-' MultiplicativeExpr)* */
7650 for (i = 0; i < repeat; ++i) {
7651 this_op = *exp_idx;
7652
7653 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7654 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7655 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7656 ++(*exp_idx);
7657
7658 if (!set) {
7659 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, NULL, options);
7660 LY_CHECK_GOTO(rc, cleanup);
7661 continue;
7662 }
7663
7664 set_fill_set(&set2, &orig_set);
7665 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, &set2, options);
7666 LY_CHECK_GOTO(rc, cleanup);
7667
7668 /* eval */
7669 if (options & LYXP_SCNODE_ALL) {
7670 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007671 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007672 set_scnode_clear_ctx(set);
7673 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007674 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 LY_CHECK_GOTO(rc, cleanup);
7676 }
7677 }
7678
7679cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007680 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7681 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007682 return rc;
7683}
7684
7685/**
7686 * @brief Evaluate RelationalExpr. Logs directly on error.
7687 *
7688 * [14] RelationalExpr ::= AdditiveExpr
7689 * | RelationalExpr '<' AdditiveExpr
7690 * | RelationalExpr '>' AdditiveExpr
7691 * | RelationalExpr '<=' AdditiveExpr
7692 * | RelationalExpr '>=' AdditiveExpr
7693 *
7694 * @param[in] exp Parsed XPath expression.
7695 * @param[in] exp_idx Position in the expression @p exp.
7696 * @param[in] repeat How many times this expression is repeated.
7697 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7698 * @param[in] options XPath options.
7699 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7700 */
7701static LY_ERR
7702eval_relational_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7703{
7704 LY_ERR rc;
7705 uint16_t this_op;
7706 struct lyxp_set orig_set, set2;
7707 uint16_t i;
7708
7709 assert(repeat);
7710
7711 set_init(&orig_set, set);
7712 set_init(&set2, set);
7713
7714 set_fill_set(&orig_set, set);
7715
7716 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, set, options);
7717 LY_CHECK_GOTO(rc, cleanup);
7718
7719 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7720 for (i = 0; i < repeat; ++i) {
7721 this_op = *exp_idx;
7722
7723 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7724 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7725 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7726 ++(*exp_idx);
7727
7728 if (!set) {
7729 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, NULL, options);
7730 LY_CHECK_GOTO(rc, cleanup);
7731 continue;
7732 }
7733
7734 set_fill_set(&set2, &orig_set);
7735 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, &set2, options);
7736 LY_CHECK_GOTO(rc, cleanup);
7737
7738 /* eval */
7739 if (options & LYXP_SCNODE_ALL) {
7740 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007741 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007742 set_scnode_clear_ctx(set);
7743 } else {
7744 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7745 LY_CHECK_GOTO(rc, cleanup);
7746 }
7747 }
7748
7749cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007750 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7751 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752 return rc;
7753}
7754
7755/**
7756 * @brief Evaluate EqualityExpr. Logs directly on error.
7757 *
7758 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
7759 * | EqualityExpr '!=' RelationalExpr
7760 *
7761 * @param[in] exp Parsed XPath expression.
7762 * @param[in] exp_idx Position in the expression @p exp.
7763 * @param[in] repeat How many times this expression is repeated.
7764 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7765 * @param[in] options XPath options.
7766 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7767 */
7768static LY_ERR
7769eval_equality_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7770{
7771 LY_ERR rc;
7772 uint16_t this_op;
7773 struct lyxp_set orig_set, set2;
7774 uint16_t i;
7775
7776 assert(repeat);
7777
7778 set_init(&orig_set, set);
7779 set_init(&set2, set);
7780
7781 set_fill_set(&orig_set, set);
7782
7783 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, set, options);
7784 LY_CHECK_GOTO(rc, cleanup);
7785
7786 /* ('=' / '!=' RelationalExpr)* */
7787 for (i = 0; i < repeat; ++i) {
7788 this_op = *exp_idx;
7789
7790 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7791 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7792 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7793 ++(*exp_idx);
7794
7795 if (!set) {
7796 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, NULL, options);
7797 LY_CHECK_GOTO(rc, cleanup);
7798 continue;
7799 }
7800
7801 set_fill_set(&set2, &orig_set);
7802 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, &set2, options);
7803 LY_CHECK_GOTO(rc, cleanup);
7804
7805 /* eval */
7806 if (options & LYXP_SCNODE_ALL) {
7807 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
7808 warn_equality_value(exp, set, *exp_idx - 1, this_op - 1, *exp_idx - 1);
7809 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *exp_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007810 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811 set_scnode_clear_ctx(set);
7812 } else {
7813 /* special handling of evaluations of identityref comparisons, always compare prefixed identites */
7814 if ((set->type == LYXP_SET_NODE_SET) && (set->val.nodes[0].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
7815 && (((struct lysc_node_leaf *)set->val.nodes[0].node->schema)->type->basetype == LY_TYPE_IDENT)) {
7816 /* left operand is identityref */
7817 if ((set2.type == LYXP_SET_STRING) && !strchr(set2.val.str, ':')) {
7818 /* missing prefix in the right operand */
7819 set2.val.str = ly_realloc(set2.val.str, strlen(set->local_mod->name) + 1 + strlen(set2.val.str) + 1);
7820 if (!set2.val.str) {
7821 goto cleanup;
7822 }
7823
7824 memmove(set2.val.str + strlen(set->local_mod->name) + 1, set2.val.str, strlen(set2.val.str) + 1);
7825 memcpy(set2.val.str, set->local_mod->name, strlen(set->local_mod->name));
7826 set2.val.str[strlen(set->local_mod->name)] = ':';
7827 }
7828 }
7829
7830 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7831 LY_CHECK_GOTO(rc, cleanup);
7832 }
7833 }
7834
7835cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007836 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7837 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007838 return rc;
7839}
7840
7841/**
7842 * @brief Evaluate AndExpr. Logs directly on error.
7843 *
7844 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
7845 *
7846 * @param[in] exp Parsed XPath expression.
7847 * @param[in] exp_idx Position in the expression @p exp.
7848 * @param[in] repeat How many times this expression is repeated.
7849 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7850 * @param[in] options XPath options.
7851 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7852 */
7853static LY_ERR
7854eval_and_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7855{
7856 LY_ERR rc;
7857 struct lyxp_set orig_set, set2;
7858 uint16_t i;
7859
7860 assert(repeat);
7861
7862 set_init(&orig_set, set);
7863 set_init(&set2, set);
7864
7865 set_fill_set(&orig_set, set);
7866
7867 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, set, options);
7868 LY_CHECK_GOTO(rc, cleanup);
7869
7870 /* cast to boolean, we know that will be the final result */
7871 if (set && (options & LYXP_SCNODE_ALL)) {
7872 set_scnode_clear_ctx(set);
7873 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007874 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007875 }
7876
7877 /* ('and' EqualityExpr)* */
7878 for (i = 0; i < repeat; ++i) {
7879 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
7880 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bool ? "skipped" : "parsed"),
7881 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7882 ++(*exp_idx);
7883
7884 /* lazy evaluation */
7885 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bool)) {
7886 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, NULL, options);
7887 LY_CHECK_GOTO(rc, cleanup);
7888 continue;
7889 }
7890
7891 set_fill_set(&set2, &orig_set);
7892 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, &set2, options);
7893 LY_CHECK_GOTO(rc, cleanup);
7894
7895 /* eval - just get boolean value actually */
7896 if (set->type == LYXP_SET_SCNODE_SET) {
7897 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007898 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007899 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007900 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007901 set_fill_set(set, &set2);
7902 }
7903 }
7904
7905cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007906 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7907 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007908 return rc;
7909}
7910
7911/**
7912 * @brief Evaluate OrExpr. Logs directly on error.
7913 *
7914 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
7915 *
7916 * @param[in] exp Parsed XPath expression.
7917 * @param[in] exp_idx Position in the expression @p exp.
7918 * @param[in] repeat How many times this expression is repeated.
7919 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7920 * @param[in] options XPath options.
7921 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7922 */
7923static LY_ERR
7924eval_or_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7925{
7926 LY_ERR rc;
7927 struct lyxp_set orig_set, set2;
7928 uint16_t i;
7929
7930 assert(repeat);
7931
7932 set_init(&orig_set, set);
7933 set_init(&set2, set);
7934
7935 set_fill_set(&orig_set, set);
7936
7937 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, set, options);
7938 LY_CHECK_GOTO(rc, cleanup);
7939
7940 /* cast to boolean, we know that will be the final result */
7941 if (set && (options & LYXP_SCNODE_ALL)) {
7942 set_scnode_clear_ctx(set);
7943 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007944 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007945 }
7946
7947 /* ('or' AndExpr)* */
7948 for (i = 0; i < repeat; ++i) {
7949 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
7950 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bool ? "skipped" : "parsed"),
7951 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7952 ++(*exp_idx);
7953
7954 /* lazy evaluation */
7955 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bool)) {
7956 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, NULL, options);
7957 LY_CHECK_GOTO(rc, cleanup);
7958 continue;
7959 }
7960
7961 set_fill_set(&set2, &orig_set);
7962 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
7963 * but it does not matter */
7964 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, &set2, options);
7965 LY_CHECK_GOTO(rc, cleanup);
7966
7967 /* eval - just get boolean value actually */
7968 if (set->type == LYXP_SET_SCNODE_SET) {
7969 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007970 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007972 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 set_fill_set(set, &set2);
7974 }
7975 }
7976
7977cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007978 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7979 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007980 return rc;
7981}
7982
7983/**
7984 * @brief Decide what expression is at the pointer @p exp_idx and evaluate it accordingly.
7985 *
7986 * @param[in] exp Parsed XPath expression.
7987 * @param[in] exp_idx Position in the expression @p exp.
7988 * @param[in] etype Expression type to evaluate.
7989 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7990 * @param[in] options XPath options.
7991 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7992 */
7993static LY_ERR
7994eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
7995{
7996 uint16_t i, count;
7997 enum lyxp_expr_type next_etype;
7998 LY_ERR rc;
7999
8000 /* process operator repeats */
8001 if (!exp->repeat[*exp_idx]) {
8002 next_etype = LYXP_EXPR_NONE;
8003 } else {
8004 /* find etype repeat */
8005 for (i = 0; exp->repeat[*exp_idx][i] > etype; ++i);
8006
8007 /* select one-priority lower because etype expression called us */
8008 if (i) {
8009 next_etype = exp->repeat[*exp_idx][i - 1];
8010 /* count repeats for that expression */
8011 for (count = 0; i && exp->repeat[*exp_idx][i - 1] == next_etype; ++count, --i);
8012 } else {
8013 next_etype = LYXP_EXPR_NONE;
8014 }
8015 }
8016
8017 /* decide what expression are we parsing based on the repeat */
8018 switch (next_etype) {
8019 case LYXP_EXPR_OR:
8020 rc = eval_or_expr(exp, exp_idx, count, set, options);
8021 break;
8022 case LYXP_EXPR_AND:
8023 rc = eval_and_expr(exp, exp_idx, count, set, options);
8024 break;
8025 case LYXP_EXPR_EQUALITY:
8026 rc = eval_equality_expr(exp, exp_idx, count, set, options);
8027 break;
8028 case LYXP_EXPR_RELATIONAL:
8029 rc = eval_relational_expr(exp, exp_idx, count, set, options);
8030 break;
8031 case LYXP_EXPR_ADDITIVE:
8032 rc = eval_additive_expr(exp, exp_idx, count, set, options);
8033 break;
8034 case LYXP_EXPR_MULTIPLICATIVE:
8035 rc = eval_multiplicative_expr(exp, exp_idx, count, set, options);
8036 break;
8037 case LYXP_EXPR_UNARY:
8038 rc = eval_unary_expr(exp, exp_idx, count, set, options);
8039 break;
8040 case LYXP_EXPR_UNION:
8041 rc = eval_union_expr(exp, exp_idx, count, set, options);
8042 break;
8043 case LYXP_EXPR_NONE:
8044 rc = eval_path_expr(exp, exp_idx, set, options);
8045 break;
8046 default:
8047 LOGINT_RET(set->ctx);
8048 }
8049
8050 return rc;
8051}
8052
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008053/**
8054 * @brief Get root type.
8055 *
8056 * @param[in] ctx_node Context node.
8057 * @param[in] ctx_scnode Schema context node.
8058 * @param[in] options XPath options.
8059 * @return Root type.
8060 */
8061static enum lyxp_node_type
8062lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8063{
8064 if (options & LYXP_SCNODE_ALL) {
8065 if (options & LYXP_SCNODE) {
8066 /* general root that can access everything */
8067 return LYXP_NODE_ROOT;
8068 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8069 /* root context node can access only config data (because we said so, it is unspecified) */
8070 return LYXP_NODE_ROOT_CONFIG;
8071 } else {
8072 return LYXP_NODE_ROOT;
8073 }
8074 }
8075
8076 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8077 /* root context node can access only config data (because we said so, it is unspecified) */
8078 return LYXP_NODE_ROOT_CONFIG;
8079 }
8080
8081 return LYXP_NODE_ROOT;
8082}
8083
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008085lyxp_eval(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node,
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086 enum lyxp_node_type ctx_node_type, const struct lyd_node **trees, struct lyxp_set *set, int options)
8087{
Michal Vasko03ff5a72019-09-11 13:49:33 +02008088 uint16_t exp_idx = 0;
8089 LY_ERR rc;
8090
Michal Vaskoecd62de2019-11-13 12:35:11 +01008091 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008092
8093 /* prepare set for evaluation */
8094 exp_idx = 0;
8095 memset(set, 0, sizeof *set);
8096 set->type = LYXP_SET_EMPTY;
8097 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node_type, options);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008098 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099 set->ctx_node = ctx_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008100 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 set->local_mod = local_mod;
8102 set->trees = trees;
8103 set->format = format;
8104
8105 /* evaluate */
8106 rc = eval_expr_select(exp, &exp_idx, 0, set, options);
8107 if (rc != LY_SUCCESS) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008108 lyxp_set_cast(set, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109 }
8110
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 return rc;
8112}
8113
8114#if 0
8115
8116/* full xml printing of set elements, not used currently */
8117
8118void
8119lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8120{
8121 uint32_t i;
8122 char *str_num;
8123 struct lyout out;
8124
8125 memset(&out, 0, sizeof out);
8126
8127 out.type = LYOUT_STREAM;
8128 out.method.f = f;
8129
8130 switch (set->type) {
8131 case LYXP_SET_EMPTY:
8132 ly_print(&out, "Empty XPath set\n\n");
8133 break;
8134 case LYXP_SET_BOOLEAN:
8135 ly_print(&out, "Boolean XPath set:\n");
8136 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8137 break;
8138 case LYXP_SET_STRING:
8139 ly_print(&out, "String XPath set:\n");
8140 ly_print(&out, "\"%s\"\n\n", set->value.str);
8141 break;
8142 case LYXP_SET_NUMBER:
8143 ly_print(&out, "Number XPath set:\n");
8144
8145 if (isnan(set->value.num)) {
8146 str_num = strdup("NaN");
8147 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8148 str_num = strdup("0");
8149 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8150 str_num = strdup("Infinity");
8151 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8152 str_num = strdup("-Infinity");
8153 } else if ((long long)set->value.num == set->value.num) {
8154 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8155 str_num = NULL;
8156 }
8157 } else {
8158 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8159 str_num = NULL;
8160 }
8161 }
8162 if (!str_num) {
8163 LOGMEM;
8164 return;
8165 }
8166 ly_print(&out, "%s\n\n", str_num);
8167 free(str_num);
8168 break;
8169 case LYXP_SET_NODE_SET:
8170 ly_print(&out, "Node XPath set:\n");
8171
8172 for (i = 0; i < set->used; ++i) {
8173 ly_print(&out, "%d. ", i + 1);
8174 switch (set->node_type[i]) {
8175 case LYXP_NODE_ROOT_ALL:
8176 ly_print(&out, "ROOT all\n\n");
8177 break;
8178 case LYXP_NODE_ROOT_CONFIG:
8179 ly_print(&out, "ROOT config\n\n");
8180 break;
8181 case LYXP_NODE_ROOT_STATE:
8182 ly_print(&out, "ROOT state\n\n");
8183 break;
8184 case LYXP_NODE_ROOT_NOTIF:
8185 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8186 break;
8187 case LYXP_NODE_ROOT_RPC:
8188 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8189 break;
8190 case LYXP_NODE_ROOT_OUTPUT:
8191 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8192 break;
8193 case LYXP_NODE_ELEM:
8194 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8195 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8196 ly_print(&out, "\n");
8197 break;
8198 case LYXP_NODE_TEXT:
8199 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8200 break;
8201 case LYXP_NODE_ATTR:
8202 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8203 break;
8204 }
8205 }
8206 break;
8207 }
8208}
8209
8210#endif
8211
8212LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008213lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008214{
8215 long double num;
8216 char *str;
8217 LY_ERR rc;
8218
8219 if (!set || (set->type == target)) {
8220 return LY_SUCCESS;
8221 }
8222
8223 /* it's not possible to convert anything into a node set */
8224 assert((target != LYXP_SET_NODE_SET) && ((set->type != LYXP_SET_SCNODE_SET) || (target == LYXP_SET_EMPTY)));
8225
8226 if (set->type == LYXP_SET_SCNODE_SET) {
8227 set_free_content(set);
8228 return LY_EINVAL;
8229 }
8230
8231 /* to STRING */
8232 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER)
8233 && ((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_EMPTY)))) {
8234 switch (set->type) {
8235 case LYXP_SET_NUMBER:
8236 if (isnan(set->val.num)) {
8237 set->val.str = strdup("NaN");
8238 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8239 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8240 set->val.str = strdup("0");
8241 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8242 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8243 set->val.str = strdup("Infinity");
8244 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8245 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8246 set->val.str = strdup("-Infinity");
8247 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8248 } else if ((long long)set->val.num == set->val.num) {
8249 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8250 LOGMEM_RET(set->ctx);
8251 }
8252 set->val.str = str;
8253 } else {
8254 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8255 LOGMEM_RET(set->ctx);
8256 }
8257 set->val.str = str;
8258 }
8259 break;
8260 case LYXP_SET_BOOLEAN:
8261 if (set->val.bool) {
8262 set->val.str = strdup("true");
8263 } else {
8264 set->val.str = strdup("false");
8265 }
8266 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8267 break;
8268 case LYXP_SET_NODE_SET:
8269 assert(set->used);
8270
8271 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008272 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008274 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 LY_CHECK_RET(rc);
8276 set_free_content(set);
8277 set->val.str = str;
8278 break;
8279 case LYXP_SET_EMPTY:
8280 set->val.str = strdup("");
8281 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8282 break;
8283 default:
8284 LOGINT_RET(set->ctx);
8285 }
8286 set->type = LYXP_SET_STRING;
8287 }
8288
8289 /* to NUMBER */
8290 if (target == LYXP_SET_NUMBER) {
8291 switch (set->type) {
8292 case LYXP_SET_STRING:
8293 num = cast_string_to_number(set->val.str);
8294 set_free_content(set);
8295 set->val.num = num;
8296 break;
8297 case LYXP_SET_BOOLEAN:
8298 if (set->val.bool) {
8299 set->val.num = 1;
8300 } else {
8301 set->val.num = 0;
8302 }
8303 break;
8304 default:
8305 LOGINT_RET(set->ctx);
8306 }
8307 set->type = LYXP_SET_NUMBER;
8308 }
8309
8310 /* to BOOLEAN */
8311 if (target == LYXP_SET_BOOLEAN) {
8312 switch (set->type) {
8313 case LYXP_SET_NUMBER:
8314 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
8315 set->val.bool = 0;
8316 } else {
8317 set->val.bool = 1;
8318 }
8319 break;
8320 case LYXP_SET_STRING:
8321 if (set->val.str[0]) {
8322 set_free_content(set);
8323 set->val.bool = 1;
8324 } else {
8325 set_free_content(set);
8326 set->val.bool = 0;
8327 }
8328 break;
8329 case LYXP_SET_NODE_SET:
8330 set_free_content(set);
8331
8332 assert(set->used);
8333 set->val.bool = 1;
8334 break;
8335 case LYXP_SET_EMPTY:
8336 set->val.bool = 0;
8337 break;
8338 default:
8339 LOGINT_RET(set->ctx);
8340 }
8341 set->type = LYXP_SET_BOOLEAN;
8342 }
8343
8344 /* to EMPTY */
8345 if (target == LYXP_SET_EMPTY) {
8346 set_free_content(set);
8347 set->type = LYXP_SET_EMPTY;
8348 }
8349
8350 return LY_SUCCESS;
8351}
8352
8353LY_ERR
8354lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8355 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8356{
8357 struct ly_ctx *ctx;
8358 uint16_t exp_idx = 0;
8359
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008360 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361
8362 ctx = local_mod->ctx;
8363
8364 /* prepare set for evaluation */
8365 exp_idx = 0;
8366 memset(set, 0, sizeof *set);
8367 set->type = LYXP_SET_SCNODE_SET;
Michal Vaskoecd62de2019-11-13 12:35:11 +01008368 lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008369 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008370 set->ctx = ctx;
8371 set->ctx_scnode = ctx_scnode;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008372 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 set->local_mod = local_mod;
8374 set->format = format;
8375
8376 /* evaluate */
8377 return eval_expr_select(exp, &exp_idx, 0, set, options);
8378}