blob: 7749d9fc3df9fe7a2614aa0066a7030b5c3ae568 [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
5235 if (!mod->implemented) {
5236 /* non-implemented module is not valid */
5237 mod = NULL;
5238 }
5239 if (!mod) {
5240 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5241 return LY_EVALID;
5242 }
5243 *qname += pref_len + 1;
5244 *qname_len -= pref_len + 1;
5245 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5246 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005247 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005248 } else {
5249 /* local module */
5250 mod = set->local_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 }
5252
Michal Vasko6346ece2019-09-24 13:12:53 +02005253 *moveto_mod = mod;
5254 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005255}
5256
5257/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005258 * @brief Move context @p set to the root. Handles absolute path.
5259 * Result is LYXP_SET_NODE_SET.
5260 *
5261 * @param[in,out] set Set to use.
5262 * @param[in] options Xpath options.
5263 */
5264static void
5265moveto_root(struct lyxp_set *set, int options)
5266{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267 if (!set) {
5268 return;
5269 }
5270
5271 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005272 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005273 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005274 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005275 lyxp_set_cast(set, LYXP_SET_EMPTY);
5276 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005277 }
5278}
5279
5280/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005281 * @brief Check whether a node has some unresolved "when".
5282 *
5283 * @param[in] node Node to check.
5284 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5285 */
5286static LY_ERR
5287moveto_when_check(const struct lyd_node *node)
5288{
5289 const struct lysc_node *schema;
5290
5291 if (!node) {
5292 return LY_SUCCESS;
5293 }
5294
5295 schema = node->schema;
5296 do {
5297 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5298 return LY_EINCOMPLETE;
5299 }
5300 schema = schema->parent;
5301 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5302
5303 return LY_SUCCESS;
5304}
5305
5306/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005307 * @brief Check @p node as a part of NameTest processing.
5308 *
5309 * @param[in] node Node to check.
5310 * @param[in] root_type XPath root node type.
5311 * @param[in] node_name Node name to move to. Must be in the dictionary!
5312 * @param[in] moveto_mod Expected module of the node.
Michal Vasko6346ece2019-09-24 13:12:53 +02005313 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5314 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315 */
5316static LY_ERR
5317moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5318 const struct lys_module *moveto_mod)
5319{
5320 /* module check */
5321 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005322 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323 }
5324
Michal Vasko5c4e5892019-11-14 12:31:38 +01005325 /* context check */
5326 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 return LY_EINVAL;
5328 }
5329
5330 /* name check */
Michal Vasko465a0e12019-11-07 11:11:58 +01005331 if (strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005332 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 }
5334
Michal Vaskoa1424542019-11-14 16:08:52 +01005335 /* when check */
5336 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005338 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339
5340 /* match */
5341 return LY_SUCCESS;
5342}
5343
5344/**
5345 * @brief Check @p node as a part of schema NameTest processing.
5346 *
5347 * @param[in] node Schema node to check.
5348 * @param[in] root_type XPath root node type.
5349 * @param[in] node_name Node name to move to. Must be in the dictionary!
5350 * @param[in] moveto_mod Expected module of the node.
Michal Vasko6346ece2019-09-24 13:12:53 +02005351 * @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 +02005352 */
5353static LY_ERR
5354moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005355 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005357 /* module check */
5358 if (strcmp(node_name, "*") && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 }
5361
5362 /* context check */
5363 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5364 return LY_EINVAL;
5365 }
5366
5367 /* name check */
Michal Vasko465a0e12019-11-07 11:11:58 +01005368 if (strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005369 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005370 }
5371
5372 /* match */
5373 return LY_SUCCESS;
5374}
5375
5376/**
5377 * @brief Move context @p set to a node. Handles '/' and '*', 'NAME', 'PREFIX:*', or 'PREFIX:NAME'.
5378 * Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware.
5379 *
5380 * @param[in,out] set Set to use.
5381 * @param[in] qname Qualified node name to move to.
5382 * @param[in] qname_len Length of @p qname.
5383 * @param[in] options XPath options.
5384 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5385 */
5386static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005387moveto_node(struct lyxp_set *set, const char *qname, uint16_t qname_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005388{
Michal Vasko79bebfd2019-11-14 16:09:19 +01005389 uint32_t i, j;
Michal Vasko6346ece2019-09-24 13:12:53 +02005390 int replaced;
5391 const char *name_dict = NULL; /* optimization - so we can do (==) instead (!strncmp(...)) in moveto_node_check() */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 const struct lys_module *moveto_mod;
5393 const struct lyd_node *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 LY_ERR rc;
5395
5396 if (!set || (set->type == LYXP_SET_EMPTY)) {
5397 return LY_SUCCESS;
5398 }
5399
5400 if (set->type != LYXP_SET_NODE_SET) {
5401 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5402 return LY_EVALID;
5403 }
5404
Michal Vasko6346ece2019-09-24 13:12:53 +02005405 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5406 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407
5408 /* name */
5409 name_dict = lydict_insert(set->ctx, qname, qname_len);
5410
5411 for (i = 0; i < set->used; ) {
5412 replaced = 0;
5413
5414 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 +01005415 assert(!set->val.nodes[i].node);
5416 /* search in all the trees */
Michal Vasko79bebfd2019-11-14 16:09:19 +01005417 LY_ARRAY_FOR(set->trees, j) {
5418 for (sub = set->trees[j]; sub; sub = sub->next) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005419 rc = moveto_node_check(sub, set->root_type, name_dict, moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005420 if (rc == LY_SUCCESS) {
5421 /* pos filled later */
5422 if (!replaced) {
5423 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5424 replaced = 1;
5425 } else {
5426 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5427 }
5428 ++i;
5429 } else if (rc == LY_EINCOMPLETE) {
5430 lydict_remove(set->ctx, name_dict);
5431 return rc;
5432 }
5433 }
5434 }
5435
Michal Vasko5c4e5892019-11-14 12:31:38 +01005436 /* skip nodes without children - leaves, leaflists, anyxmls (ouput root will eval to true) */
5437 } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005438
5439 for (sub = lyd_node_children(set->val.nodes[i].node); sub; sub = sub->next) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005440 rc = moveto_node_check(sub, set->root_type, name_dict, moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005441 if (rc == LY_SUCCESS) {
5442 if (!replaced) {
5443 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5444 replaced = 1;
5445 } else {
5446 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5447 }
5448 ++i;
5449 } else if (rc == LY_EINCOMPLETE) {
5450 lydict_remove(set->ctx, name_dict);
5451 return rc;
5452 }
5453 }
5454 }
5455
5456 if (!replaced) {
5457 /* no match */
5458 set_remove_node(set, i);
5459 }
5460 }
5461 lydict_remove(set->ctx, name_dict);
5462
5463 return LY_SUCCESS;
5464}
5465
5466/**
5467 * @brief Move context @p set to a schema node. Handles '/' and '*', 'NAME', 'PREFIX:*', or 'PREFIX:NAME'.
5468 * Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5469 *
5470 * @param[in,out] set Set to use.
5471 * @param[in] qname Qualified node name to move to.
5472 * @param[in] qname_len Length of @p qname.
5473 * @param[in] options XPath options.
5474 * @return LY_ERR
5475 */
5476static LY_ERR
5477moveto_scnode(struct lyxp_set *set, const char *qname, uint16_t qname_len, int options)
5478{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005479 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480 uint32_t mod_idx;
Michal Vasko6346ece2019-09-24 13:12:53 +02005481 const char *name_dict = NULL; /* optimization - so we can do (==) instead (!strncmp(...)) in moveto_node_check() */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005482 const struct lys_module *moveto_mod;
5483 const struct lysc_node *sub, *start_parent;
Michal Vasko6346ece2019-09-24 13:12:53 +02005484 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485
5486 if (!set || (set->type == LYXP_SET_EMPTY)) {
5487 return LY_SUCCESS;
5488 }
5489
5490 if (set->type != LYXP_SET_SCNODE_SET) {
5491 LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5492 return LY_EVALID;
5493 }
5494
Michal Vasko6346ece2019-09-24 13:12:53 +02005495 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5496 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005497
5498 /* name */
5499 name_dict = lydict_insert(set->ctx, qname, qname_len);
5500
Michal Vaskocafad9d2019-11-07 15:20:03 +01005501 /* getnext opts */
5502 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5503 if (options & LYXP_SCNODE_OUTPUT) {
5504 getnext_opts |= LYS_GETNEXT_OUTPUT;
5505 }
5506
Michal Vasko03ff5a72019-09-11 13:49:33 +02005507 orig_used = set->used;
5508 for (i = 0; i < orig_used; ++i) {
5509 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005510 if (set->val.scnodes[i].in_ctx != -2) {
5511 continue;
5512 }
5513
5514 /* remember context node */
5515 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005517
5518 start_parent = set->val.scnodes[i].scnode;
5519
5520 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
5521 /* it can actually be in any module, it's all <running>, but we know it's moveto_mod (if set),
5522 * so use it directly (root node itself is useless in this case) */
5523 mod_idx = 0;
5524 while (moveto_mod || (moveto_mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
5525 sub = NULL;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005526 while ((sub = lys_getnext(sub, NULL, moveto_mod->compiled, getnext_opts))) {
5527 if (!moveto_scnode_check(sub, set->root_type, name_dict, moveto_mod)) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005528 idx = lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005529 /* we need to prevent these nodes from being considered in this moveto */
5530 if ((idx < orig_used) && (idx > i)) {
5531 set->val.scnodes[idx].in_ctx = 2;
5532 temp_ctx = 1;
5533 }
5534 }
5535 }
5536
5537 if (!mod_idx) {
5538 /* moveto_mod was specified, we are not going through the whole context */
5539 break;
5540 }
5541 /* next iteration */
5542 moveto_mod = NULL;
5543 }
5544
5545 /* skip nodes without children - leaves, leaflists, and anyxmls (ouput root will eval to true) */
5546 } else if (!(start_parent->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
5547 sub = NULL;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005548 while ((sub = lys_getnext(sub, start_parent, NULL, getnext_opts))) {
5549 if (!moveto_scnode_check(sub, set->root_type, name_dict, (moveto_mod ? moveto_mod : set->local_mod))) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005550 idx = lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005551 if ((idx < orig_used) && (idx > i)) {
5552 set->val.scnodes[idx].in_ctx = 2;
5553 temp_ctx = 1;
5554 }
5555 }
5556 }
5557 }
5558 }
5559 lydict_remove(set->ctx, name_dict);
5560
5561 /* correct temporary in_ctx values */
5562 if (temp_ctx) {
5563 for (i = 0; i < orig_used; ++i) {
5564 if (set->val.scnodes[i].in_ctx == 2) {
5565 set->val.scnodes[i].in_ctx = 1;
5566 }
5567 }
5568 }
5569
5570 return LY_SUCCESS;
5571}
5572
5573/**
5574 * @brief Move context @p set to a node and all its descendants. Handles '//' and '*', 'NAME',
5575 * 'PREFIX:*', or 'PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5576 * Context position aware.
5577 *
5578 * @param[in] set Set to use.
5579 * @param[in] qname Qualified node name to move to.
5580 * @param[in] qname_len Length of @p qname.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5582 */
5583static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005584moveto_node_alldesc(struct lyxp_set *set, const char *qname, uint16_t qname_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005585{
5586 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005587 const char *name_dict = NULL; /* optimization - so we can do (==) instead (!strncmp(...)) in moveto_node_check() */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005588 const struct lyd_node *next, *elem, *start;
5589 const struct lys_module *moveto_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005590 struct lyxp_set ret_set;
5591 LY_ERR rc;
5592
5593 if (!set || (set->type == LYXP_SET_EMPTY)) {
5594 return LY_SUCCESS;
5595 }
5596
5597 if (set->type != LYXP_SET_NODE_SET) {
5598 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5599 return LY_EVALID;
5600 }
5601
Michal Vasko6346ece2019-09-24 13:12:53 +02005602 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5603 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005604
5605 /* 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 +01005606 rc = moveto_node(set, "*", 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005607 LY_CHECK_RET(rc);
5608
Michal Vasko6346ece2019-09-24 13:12:53 +02005609 /* name */
5610 name_dict = lydict_insert(set->ctx, qname, qname_len);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005611
Michal Vasko6346ece2019-09-24 13:12:53 +02005612 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005613 set_init(&ret_set, set);
5614 for (i = 0; i < set->used; ++i) {
5615
5616 /* TREE DFS */
5617 start = set->val.nodes[i].node;
5618 for (elem = next = start; elem; elem = next) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005619 rc = moveto_node_check(elem, set->root_type, name_dict, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005620 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005621 /* add matching node into result set */
5622 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5623 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5624 /* the node is a duplicate, we'll process it later in the set */
5625 goto skip_children;
5626 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005627 } else if (rc == LY_EINCOMPLETE) {
5628 lydict_remove(set->ctx, name_dict);
5629 return rc;
5630 } else if (rc == LY_EINVAL) {
5631 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005632 }
5633
5634 /* TREE DFS NEXT ELEM */
5635 /* select element for the next run - children first */
5636 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
5637 next = NULL;
5638 } else {
5639 next = lyd_node_children(elem);
5640 }
5641 if (!next) {
5642skip_children:
5643 /* no children, so try siblings, but only if it's not the start,
5644 * that is considered to be the root and it's siblings are not traversed */
5645 if (elem != start) {
5646 next = elem->next;
5647 } else {
5648 break;
5649 }
5650 }
5651 while (!next) {
5652 /* no siblings, go back through the parents */
5653 if ((struct lyd_node *)elem->parent == start) {
5654 /* we are done, no next element to process */
5655 break;
5656 }
5657 /* parent is already processed, go to its sibling */
5658 elem = (struct lyd_node *)elem->parent;
5659 next = elem->next;
5660 }
5661 }
5662 }
5663
5664 /* make the temporary set the current one */
5665 ret_set.ctx_pos = set->ctx_pos;
5666 ret_set.ctx_size = set->ctx_size;
5667 set_free_content(set);
5668 memcpy(set, &ret_set, sizeof *set);
5669
Michal Vasko6346ece2019-09-24 13:12:53 +02005670 lydict_remove(set->ctx, name_dict);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005671 return LY_SUCCESS;
5672}
5673
5674/**
5675 * @brief Move context @p set to a schema node and all its descendants. Handles '//' and '*', 'NAME',
5676 * 'PREFIX:*', or 'PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5677 *
5678 * @param[in] set Set to use.
5679 * @param[in] qname Qualified node name to move to.
5680 * @param[in] qname_len Length of @p qname.
5681 * @param[in] options XPath options.
5682 * @return LY_ERR
5683 */
5684static LY_ERR
5685moveto_scnode_alldesc(struct lyxp_set *set, const char *qname, uint16_t qname_len, int options)
5686{
Michal Vasko6346ece2019-09-24 13:12:53 +02005687 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 const struct lysc_node *next, *elem, *start;
5689 const struct lys_module *moveto_mod;
Michal Vasko6346ece2019-09-24 13:12:53 +02005690 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005691
5692 if (!set || (set->type == LYXP_SET_EMPTY)) {
5693 return LY_SUCCESS;
5694 }
5695
5696 if (set->type != LYXP_SET_SCNODE_SET) {
5697 LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5698 return LY_EVALID;
5699 }
5700
Michal Vasko6346ece2019-09-24 13:12:53 +02005701 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5702 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703
5704 orig_used = set->used;
5705 for (i = 0; i < orig_used; ++i) {
5706 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005707 if (set->val.scnodes[i].in_ctx != -2) {
5708 continue;
5709 }
5710
5711 /* remember context node */
5712 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714
5715 /* TREE DFS */
5716 start = set->val.scnodes[i].scnode;
5717 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005718 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5719 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721 }
5722
Michal Vaskocafad9d2019-11-07 15:20:03 +01005723 rc = moveto_scnode_check(elem, set->root_type, qname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005724 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005725 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005726 set->val.scnodes[idx].in_ctx = 1;
5727 if (idx > i) {
5728 /* we will process it later in the set */
5729 goto skip_children;
5730 }
5731 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005732 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005733 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005734 } else if (rc == LY_EINVAL) {
5735 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736 }
5737
5738next_iter:
5739 /* TREE DFS NEXT ELEM */
5740 /* select element for the next run - children first */
5741 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
5742 if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
5743 next = NULL;
5744 }
5745 if (!next) {
5746skip_children:
5747 /* no children, so try siblings, but only if it's not the start,
5748 * that is considered to be the root and it's siblings are not traversed */
5749 if (elem != start) {
5750 next = elem->next;
5751 } else {
5752 break;
5753 }
5754 }
5755 while (!next) {
5756 /* no siblings, go back through the parents */
5757 if (elem->parent == start) {
5758 /* we are done, no next element to process */
5759 break;
5760 }
5761 /* parent is already processed, go to its sibling */
5762 elem = elem->parent;
5763 next = elem->next;
5764 }
5765 }
5766 }
5767
5768 return LY_SUCCESS;
5769}
5770
5771/**
5772 * @brief Move context @p set to an attribute. Handles '/' and '@*', '@NAME', '@PREFIX:*',
5773 * or '@PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5774 * Indirectly context position aware.
5775 *
5776 * @param[in,out] set Set to use.
5777 * @param[in] qname Qualified node name to move to.
5778 * @param[in] qname_len Length of @p qname.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 * @return LY_ERR
5780 */
5781static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005782moveto_attr(struct lyxp_set *set, const char *qname, uint16_t qname_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005783{
5784 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005785 int replaced, all = 0;
5786 const struct lys_module *moveto_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787 struct lyd_attr *sub;
Michal Vasko6346ece2019-09-24 13:12:53 +02005788 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789
5790 if (!set || (set->type == LYXP_SET_EMPTY)) {
5791 return LY_SUCCESS;
5792 }
5793
5794 if (set->type != LYXP_SET_NODE_SET) {
5795 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5796 return LY_EVALID;
5797 }
5798
Michal Vasko6346ece2019-09-24 13:12:53 +02005799 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5800 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801
5802 if ((qname_len == 1) && (qname[0] == '*')) {
5803 all = 1;
5804 }
5805
5806 for (i = 0; i < set->used; ) {
5807 replaced = 0;
5808
5809 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5810 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005811 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 for (sub = set->val.nodes[i].node->attr; sub; sub = sub->next) {
5813
5814 /* check "namespace" */
5815 if (moveto_mod && (sub->annotation->module != moveto_mod)) {
5816 continue;
5817 }
5818
5819 if (all || (!strncmp(sub->name, qname, qname_len) && !sub->name[qname_len])) {
5820 /* match */
5821 if (!replaced) {
5822 set->val.attrs[i].attr = sub;
5823 set->val.attrs[i].type = LYXP_NODE_ATTR;
5824 /* pos does not change */
5825 replaced = 1;
5826 } else {
5827 set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_ATTR, i + 1);
5828 }
5829 ++i;
5830 }
5831 }
5832 }
5833
5834 if (!replaced) {
5835 /* no match */
5836 set_remove_node(set, i);
5837 }
5838 }
5839
5840 return LY_SUCCESS;
5841}
5842
5843/**
5844 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
5845 * Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware.
5846 *
5847 * @param[in,out] set1 Set to use for the result.
5848 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005849 * @return LY_ERR
5850 */
5851static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005852moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005853{
5854 LY_ERR rc;
5855
5856 if (((set1->type != LYXP_SET_NODE_SET) && (set1->type != LYXP_SET_EMPTY))
5857 || ((set2->type != LYXP_SET_NODE_SET) && (set2->type != LYXP_SET_EMPTY))) {
5858 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5859 return LY_EVALID;
5860 }
5861
5862 /* set2 is empty or both set1 and set2 */
5863 if (set2->type == LYXP_SET_EMPTY) {
5864 return LY_SUCCESS;
5865 }
5866
5867 if (set1->type == LYXP_SET_EMPTY) {
5868 memcpy(set1, set2, sizeof *set1);
5869 /* dynamic memory belongs to set1 now, do not free */
5870 set2->type = LYXP_SET_EMPTY;
5871 return LY_SUCCESS;
5872 }
5873
5874 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005875 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005876
5877 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005878 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 LY_CHECK_RET(rc);
5880
5881 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005882 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005883
5884 return LY_SUCCESS;
5885}
5886
5887/**
5888 * @brief Move context @p set to an attribute in any of the descendants. Handles '//' and '@*',
5889 * '@NAME', '@PREFIX:*', or '@PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5890 * Context position aware.
5891 *
5892 * @param[in,out] set Set to use.
5893 * @param[in] qname Qualified node name to move to.
5894 * @param[in] qname_len Length of @p qname.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5896 */
5897static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005898moveto_attr_alldesc(struct lyxp_set *set, const char *qname, uint16_t qname_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899{
5900 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005901 int replaced, all = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902 struct lyd_attr *sub;
Michal Vasko6346ece2019-09-24 13:12:53 +02005903 const struct lys_module *moveto_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005904 struct lyxp_set *set_all_desc = NULL;
5905 LY_ERR rc;
5906
5907 if (!set || (set->type == LYXP_SET_EMPTY)) {
5908 return LY_SUCCESS;
5909 }
5910
5911 if (set->type != LYXP_SET_NODE_SET) {
5912 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5913 return LY_EVALID;
5914 }
5915
Michal Vasko6346ece2019-09-24 13:12:53 +02005916 rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod);
5917 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005918
5919 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5920 * but it likely won't be used much, so it's a waste of time */
5921 /* copy the context */
5922 set_all_desc = set_copy(set);
5923 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005924 rc = moveto_node_alldesc(set_all_desc, "*", 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925 if (rc != LY_SUCCESS) {
5926 lyxp_set_free(set_all_desc);
5927 return rc;
5928 }
5929 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005930 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005931 if (rc != LY_SUCCESS) {
5932 lyxp_set_free(set_all_desc);
5933 return rc;
5934 }
5935 lyxp_set_free(set_all_desc);
5936
5937 if ((qname_len == 1) && (qname[0] == '*')) {
5938 all = 1;
5939 }
5940
5941 for (i = 0; i < set->used; ) {
5942 replaced = 0;
5943
5944 /* only attributes of an elem can be in the result, skip all the rest,
5945 * we have all attributes qualified in lyd tree */
5946 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5947 for (sub = set->val.nodes[i].node->attr; sub; sub = sub->next) {
5948 /* check "namespace" */
5949 if (moveto_mod && (sub->annotation->module != moveto_mod)) {
5950 continue;
5951 }
5952
5953 if (all || (!strncmp(sub->name, qname, qname_len) && !sub->name[qname_len])) {
5954 /* match */
5955 if (!replaced) {
5956 set->val.attrs[i].attr = sub;
5957 set->val.attrs[i].type = LYXP_NODE_ATTR;
5958 /* pos does not change */
5959 replaced = 1;
5960 } else {
5961 set_insert_node(set, (struct lyd_node *)sub, set->val.attrs[i].pos, LYXP_NODE_ATTR, i + 1);
5962 }
5963 ++i;
5964 }
5965 }
5966 }
5967
5968 if (!replaced) {
5969 /* no match */
5970 set_remove_node(set, i);
5971 }
5972 }
5973
5974 return LY_SUCCESS;
5975}
5976
5977/**
5978 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
5979 * (or LYXP_SET_EMPTY). Context position aware.
5980 *
5981 * @param[in] parent Current parent.
5982 * @param[in] parent_pos Position of @p parent.
5983 * @param[in] parent_type Node type of @p parent.
5984 * @param[in,out] to_set Set to use.
5985 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 * @param[in] options XPath options.
5987 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5988 */
5989static LY_ERR
5990moveto_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 +01005991 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992{
5993 const struct lyd_node *sub;
5994 LY_ERR rc;
5995
5996 switch (parent_type) {
5997 case LYXP_NODE_ROOT:
5998 case LYXP_NODE_ROOT_CONFIG:
5999 /* add the same node but as an element */
6000 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_ELEM, -1)) {
6001 set_insert_node(to_set, parent, 0, LYXP_NODE_ELEM, to_set->used);
6002
6003 /* skip anydata/anyxml and dummy nodes */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006004 if (!(parent->schema->nodetype & LYS_ANYDATA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006005 /* also add all the children of this node, recursively */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006006 rc = moveto_self_add_children_r(parent, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007 LY_CHECK_RET(rc);
6008 }
6009 }
6010 break;
6011 case LYXP_NODE_ELEM:
6012 /* add all the children ... */
6013 if (!(parent->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
6014 for (sub = lyd_node_children(parent); sub; sub = sub->next) {
6015 /* context check */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006016 if ((to_set->root_type == LYXP_NODE_ROOT_CONFIG) && (sub->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017 continue;
6018 }
6019
Michal Vaskoa1424542019-11-14 16:08:52 +01006020 /* when check */
6021 if (moveto_when_check(sub)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006023 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024
6025 if (!set_dup_node_check(dup_check_set, sub, LYXP_NODE_ELEM, -1)) {
6026 set_insert_node(to_set, sub, 0, LYXP_NODE_ELEM, to_set->used);
6027
Michal Vasko5c4e5892019-11-14 12:31:38 +01006028 /* skip anydata/anyxml nodes */
6029 if (sub->schema->nodetype & LYS_ANYDATA) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030 continue;
6031 }
6032
6033 /* also add all the children of this node, recursively */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006034 rc = moveto_self_add_children_r(sub, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 LY_CHECK_RET(rc);
6036 }
6037 }
6038
6039 /* ... or add their text node, ... */
6040 } else {
6041 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6042 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6043 }
6044 }
6045 break;
6046 default:
6047 LOGINT_RET(parent->schema->module->ctx);
6048 }
6049
6050 return LY_SUCCESS;
6051}
6052
6053/**
6054 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6055 * (or LYXP_SET_EMPTY). Context position aware.
6056 *
6057 * @param[in,out] set Set to use.
6058 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6059 * @param[in] options XPath options.
6060 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6061 */
6062static LY_ERR
6063moveto_self(struct lyxp_set *set, int all_desc, int options)
6064{
6065 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066 struct lyxp_set ret_set;
6067 LY_ERR rc;
6068
6069 if (!set || (set->type == LYXP_SET_EMPTY)) {
6070 return LY_SUCCESS;
6071 }
6072
6073 if (set->type != LYXP_SET_NODE_SET) {
6074 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6075 return LY_EVALID;
6076 }
6077
6078 /* nothing to do */
6079 if (!all_desc) {
6080 return LY_SUCCESS;
6081 }
6082
Michal Vasko03ff5a72019-09-11 13:49:33 +02006083 /* add all the children, they get added recursively */
6084 set_init(&ret_set, set);
6085 for (i = 0; i < set->used; ++i) {
6086 /* copy the current node to tmp */
6087 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6088
6089 /* do not touch attributes and text nodes */
6090 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_ATTR)) {
6091 continue;
6092 }
6093
Michal Vasko5c4e5892019-11-14 12:31:38 +01006094 /* skip anydata/anyxml nodes */
6095 if (set->val.nodes[i].node->schema->nodetype & LYS_ANYDATA) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006096 continue;
6097 }
6098
6099 /* add all the children */
6100 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 +01006101 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102 if (rc != LY_SUCCESS) {
6103 set_free_content(&ret_set);
6104 return rc;
6105 }
6106 }
6107
6108 /* use the temporary set as the current one */
6109 ret_set.ctx_pos = set->ctx_pos;
6110 ret_set.ctx_size = set->ctx_size;
6111 set_free_content(set);
6112 memcpy(set, &ret_set, sizeof *set);
6113
6114 return LY_SUCCESS;
6115}
6116
6117/**
6118 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6119 * (or LYXP_SET_EMPTY).
6120 *
6121 * @param[in,out] set Set to use.
6122 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6123 * @param[in] options XPath options.
6124 * @return LY_ERR
6125 */
6126static LY_ERR
6127moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6128{
6129 const struct lysc_node *sub;
6130 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006131
6132 if (!set || (set->type == LYXP_SET_EMPTY)) {
6133 return LY_SUCCESS;
6134 }
6135
6136 if (set->type != LYXP_SET_SCNODE_SET) {
6137 LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6138 return LY_EVALID;
6139 }
6140
6141 /* nothing to do */
6142 if (!all_desc) {
6143 return LY_SUCCESS;
6144 }
6145
Michal Vasko03ff5a72019-09-11 13:49:33 +02006146 /* add all the children, they get added recursively */
6147 for (i = 0; i < set->used; ++i) {
6148 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006149 if (set->val.scnodes[i].in_ctx != -2) {
6150 continue;
6151 }
6152
6153 /* remember context node (it was traversed again so it changes to a normal node) */
6154 set->val.scnodes[i].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155 }
6156
6157 /* add all the children */
6158 if (set->val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_CONTAINER)) {
6159 sub = NULL;
6160 while ((sub = lys_getnext(sub, set->val.scnodes[i].scnode, NULL, LYS_GETNEXT_NOSTATECHECK))) {
6161 /* RPC input/output check */
6162 if (options & LYXP_SCNODE_OUTPUT) {
6163 if (sub->parent->nodetype == LYS_INPUT) {
6164 continue;
6165 }
6166 } else {
6167 if (sub->parent->nodetype == LYS_OUTPUT) {
6168 continue;
6169 }
6170 }
6171
6172 /* context check */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006173 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (sub->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 continue;
6175 }
6176
Michal Vaskoecd62de2019-11-13 12:35:11 +01006177 lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 /* throw away the insert index, we want to consider that node again, recursively */
6179 }
6180 }
6181 }
6182
6183 return LY_SUCCESS;
6184}
6185
6186/**
6187 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6188 * (or LYXP_SET_EMPTY). Context position aware.
6189 *
6190 * @param[in] set Set to use.
6191 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6192 * @param[in] options XPath options.
6193 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6194 */
6195static LY_ERR
6196moveto_parent(struct lyxp_set *set, int all_desc, int options)
6197{
6198 LY_ERR rc;
6199 uint32_t i;
6200 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006201 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006202
6203 if (!set || (set->type == LYXP_SET_EMPTY)) {
6204 return LY_SUCCESS;
6205 }
6206
6207 if (set->type != LYXP_SET_NODE_SET) {
6208 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6209 return LY_EVALID;
6210 }
6211
6212 if (all_desc) {
6213 /* <path>//.. == <path>//./.. */
6214 rc = moveto_self(set, 1, options);
6215 LY_CHECK_RET(rc);
6216 }
6217
Michal Vasko57eab132019-09-24 11:46:26 +02006218 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006219 node = set->val.nodes[i].node;
6220
6221 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6222 new_node = (struct lyd_node *)node->parent;
6223 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6224 new_node = node;
6225 } else if (set->val.nodes[i].type == LYXP_NODE_ATTR) {
6226 new_node = set->val.attrs[i].attr->parent;
6227 if (!new_node) {
6228 LOGINT_RET(set->ctx);
6229 }
6230 } else {
6231 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006232 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006233 continue;
6234 }
6235
Michal Vaskoa1424542019-11-14 16:08:52 +01006236 /* when check */
6237 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006239 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006240
6241 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006242 if (!new_node) {
6243 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244
6245 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6246 } else {
6247 new_type = LYXP_NODE_ELEM;
6248 }
6249
Michal Vasko03ff5a72019-09-11 13:49:33 +02006250 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006251 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006252 } else {
6253 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006254 }
6255 }
6256
Michal Vasko2caefc12019-11-14 16:07:56 +01006257 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006258 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006259
6260 return LY_SUCCESS;
6261}
6262
6263/**
6264 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6265 * (or LYXP_SET_EMPTY).
6266 *
6267 * @param[in] set Set to use.
6268 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6269 * @param[in] options XPath options.
6270 * @return LY_ERR
6271 */
6272static LY_ERR
6273moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6274{
6275 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006276 const struct lysc_node *node, *new_node;
6277 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006278 LY_ERR rc;
6279
6280 if (!set || (set->type == LYXP_SET_EMPTY)) {
6281 return LY_SUCCESS;
6282 }
6283
6284 if (set->type != LYXP_SET_SCNODE_SET) {
6285 LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6286 return LY_EVALID;
6287 }
6288
6289 if (all_desc) {
6290 /* <path>//.. == <path>//./.. */
6291 rc = moveto_scnode_self(set, 1, options);
6292 LY_CHECK_RET(rc);
6293 }
6294
Michal Vasko03ff5a72019-09-11 13:49:33 +02006295 orig_used = set->used;
6296 for (i = 0; i < orig_used; ++i) {
6297 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006298 if (set->val.scnodes[i].in_ctx != -2) {
6299 continue;
6300 }
6301
6302 /* remember context node */
6303 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006304 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006305
6306 node = set->val.scnodes[i].scnode;
6307
6308 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6309 for (new_node = node->parent;
6310 new_node && (new_node->nodetype & (LYS_CHOICE | LYS_CASE));
6311 new_node = new_node->parent);
6312 } else {
6313 /* root does not have a parent */
6314 continue;
6315 }
6316
Michal Vasko03ff5a72019-09-11 13:49:33 +02006317 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006318 if (!new_node) {
6319 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320
6321 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6322 } else {
6323 new_type = LYXP_NODE_ELEM;
6324 }
6325
Michal Vaskoecd62de2019-11-13 12:35:11 +01006326 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006327 if ((idx < orig_used) && (idx > i)) {
6328 set->val.scnodes[idx].in_ctx = 2;
6329 temp_ctx = 1;
6330 }
6331 }
6332
6333 if (temp_ctx) {
6334 for (i = 0; i < orig_used; ++i) {
6335 if (set->val.scnodes[i].in_ctx == 2) {
6336 set->val.scnodes[i].in_ctx = 1;
6337 }
6338 }
6339 }
6340
6341 return LY_SUCCESS;
6342}
6343
6344/**
6345 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6346 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6347 *
6348 * @param[in,out] set1 Set to use for the result.
6349 * @param[in] set2 Set acting as the second operand for @p op.
6350 * @param[in] op Comparison operator to process.
6351 * @param[in] options XPath options.
6352 * @return LY_ERR
6353 */
6354static LY_ERR
6355moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6356{
6357 /*
6358 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6359 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6360 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6361 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6362 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6363 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6364 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6365 *
6366 * '=' or '!='
6367 * BOOLEAN + BOOLEAN
6368 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6369 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6370 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6371 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6372 * NUMBER + NUMBER
6373 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6374 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6375 * STRING + STRING
6376 *
6377 * '<=', '<', '>=', '>'
6378 * NUMBER + NUMBER
6379 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6380 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6381 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6382 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6383 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6384 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6385 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6386 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6387 */
6388 struct lyxp_set iter1, iter2;
6389 int result;
6390 int64_t i;
6391 LY_ERR rc;
6392
6393 iter1.type = LYXP_SET_EMPTY;
6394
6395 /* empty node-sets are always false */
6396 if ((set1->type == LYXP_SET_EMPTY) || (set2->type == LYXP_SET_EMPTY)) {
6397 set_fill_boolean(set1, 0);
6398 return LY_SUCCESS;
6399 }
6400
6401 /* iterative evaluation with node-sets */
6402 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6403 if (set1->type == LYXP_SET_NODE_SET) {
6404 for (i = 0; i < set1->used; ++i) {
6405 switch (set2->type) {
6406 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006407 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408 break;
6409 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006410 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006411 break;
6412 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006413 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006414 break;
6415 }
6416 LY_CHECK_RET(rc);
6417
6418 rc = moveto_op_comp(&iter1, set2, op, options);
6419 if (rc != LY_SUCCESS) {
6420 set_free_content(&iter1);
6421 return rc;
6422 }
6423
6424 /* lazy evaluation until true */
6425 if (iter1.val.bool) {
6426 set_fill_boolean(set1, 1);
6427 return LY_SUCCESS;
6428 }
6429 }
6430 } else {
6431 for (i = 0; i < set2->used; ++i) {
6432 switch (set1->type) {
6433 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006434 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006435 break;
6436 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006437 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438 break;
6439 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006440 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006441 break;
6442 }
6443 LY_CHECK_RET(rc);
6444
6445 set_fill_set(&iter1, set1);
6446
6447 rc = moveto_op_comp(&iter1, &iter2, op, options);
6448 if (rc != LY_SUCCESS) {
6449 set_free_content(&iter1);
6450 set_free_content(&iter2);
6451 return rc;
6452 }
6453 set_free_content(&iter2);
6454
6455 /* lazy evaluation until true */
6456 if (iter1.val.bool) {
6457 set_fill_boolean(set1, 1);
6458 return LY_SUCCESS;
6459 }
6460 }
6461 }
6462
6463 /* false for all nodes */
6464 set_fill_boolean(set1, 0);
6465 return LY_SUCCESS;
6466 }
6467
6468 /* first convert properly */
6469 if ((op[0] == '=') || (op[0] == '!')) {
6470 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006471 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6472 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006473 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006474 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006475 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006476 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006477 LY_CHECK_RET(rc);
6478 } /* else we have 2 strings */
6479 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006480 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006481 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006482 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006483 LY_CHECK_RET(rc);
6484 }
6485
6486 assert(set1->type == set2->type);
6487
6488 /* compute result */
6489 if (op[0] == '=') {
6490 if (set1->type == LYXP_SET_BOOLEAN) {
6491 result = (set1->val.bool == set2->val.bool);
6492 } else if (set1->type == LYXP_SET_NUMBER) {
6493 result = (set1->val.num == set2->val.num);
6494 } else {
6495 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006496 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497 }
6498 } else if (op[0] == '!') {
6499 if (set1->type == LYXP_SET_BOOLEAN) {
6500 result = (set1->val.bool != set2->val.bool);
6501 } else if (set1->type == LYXP_SET_NUMBER) {
6502 result = (set1->val.num != set2->val.num);
6503 } else {
6504 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006505 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006506 }
6507 } else {
6508 assert(set1->type == LYXP_SET_NUMBER);
6509 if (op[0] == '<') {
6510 if (op[1] == '=') {
6511 result = (set1->val.num <= set2->val.num);
6512 } else {
6513 result = (set1->val.num < set2->val.num);
6514 }
6515 } else {
6516 if (op[1] == '=') {
6517 result = (set1->val.num >= set2->val.num);
6518 } else {
6519 result = (set1->val.num > set2->val.num);
6520 }
6521 }
6522 }
6523
6524 /* assign result */
6525 if (result) {
6526 set_fill_boolean(set1, 1);
6527 } else {
6528 set_fill_boolean(set1, 0);
6529 }
6530
6531 return LY_SUCCESS;
6532}
6533
6534/**
6535 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6536 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6537 *
6538 * @param[in,out] set1 Set to use for the result.
6539 * @param[in] set2 Set acting as the second operand for @p op.
6540 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541 * @return LY_ERR
6542 */
6543static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006544moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006545{
6546 LY_ERR rc;
6547
6548 /* unary '-' */
6549 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006550 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006551 LY_CHECK_RET(rc);
6552 set1->val.num *= -1;
6553 lyxp_set_free(set2);
6554 return LY_SUCCESS;
6555 }
6556
6557 assert(set1 && set2);
6558
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006559 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006560 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006561 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006562 LY_CHECK_RET(rc);
6563
6564 switch (op[0]) {
6565 /* '+' */
6566 case '+':
6567 set1->val.num += set2->val.num;
6568 break;
6569
6570 /* '-' */
6571 case '-':
6572 set1->val.num -= set2->val.num;
6573 break;
6574
6575 /* '*' */
6576 case '*':
6577 set1->val.num *= set2->val.num;
6578 break;
6579
6580 /* 'div' */
6581 case 'd':
6582 set1->val.num /= set2->val.num;
6583 break;
6584
6585 /* 'mod' */
6586 case 'm':
6587 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6588 break;
6589
6590 default:
6591 LOGINT_RET(set1->ctx);
6592 }
6593
6594 return LY_SUCCESS;
6595}
6596
6597/*
6598 * eval functions
6599 *
6600 * They execute a parsed XPath expression on some data subtree.
6601 */
6602
6603/**
6604 * @brief Evaluate Literal. Logs directly on error.
6605 *
6606 * @param[in] exp Parsed XPath expression.
6607 * @param[in] exp_idx Position in the expression @p exp.
6608 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6609 */
6610static void
6611eval_literal(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
6612{
6613 if (set) {
6614 if (exp->tok_len[*exp_idx] == 2) {
6615 set_fill_string(set, "", 0);
6616 } else {
6617 set_fill_string(set, &exp->expr[exp->tok_pos[*exp_idx] + 1], exp->tok_len[*exp_idx] - 2);
6618 }
6619 }
6620 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6621 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6622 ++(*exp_idx);
6623}
6624
6625/**
6626 * @brief Evaluate NodeTest. Logs directly on error.
6627 *
6628 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6629 *
6630 * @param[in] exp Parsed XPath expression.
6631 * @param[in] exp_idx Position in the expression @p exp.
6632 * @param[in] attr_axis Whether to search attributes or standard nodes.
6633 * @param[in] all_desc Whether to search all the descendants or children only.
6634 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6635 * @param[in] options XPath options.
6636 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6637 */
6638static int
6639eval_node_test(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc,
6640 struct lyxp_set *set, int options)
6641{
6642 int i;
6643 char *path;
6644 LY_ERR rc;
6645
6646 switch (exp->tokens[*exp_idx]) {
6647 case LYXP_TOKEN_NAMETEST:
6648 if (attr_axis) {
6649 if (set && (options & LYXP_SCNODE_ALL)) {
6650 set_scnode_clear_ctx(set);
6651 rc = LY_SUCCESS;
6652 } else {
6653 if (all_desc) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006654 rc = moveto_attr_alldesc(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006656 rc = moveto_attr(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 }
6658 }
6659 } else {
6660 if (all_desc) {
6661 if (set && (options & LYXP_SCNODE_ALL)) {
6662 rc = moveto_scnode_alldesc(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx], options);
6663 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006664 rc = moveto_node_alldesc(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006665 }
6666 } else {
6667 if (set && (options & LYXP_SCNODE_ALL)) {
6668 rc = moveto_scnode(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx], options);
6669 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006670 rc = moveto_node(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 }
6672 }
6673
6674 if ((rc == LY_SUCCESS) && set && (options & LYXP_SCNODE_ALL)) {
6675 for (i = set->used - 1; i > -1; --i) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006676 if (set->val.scnodes[i].in_ctx > 0) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006677 break;
6678 }
6679 }
6680 if (i == -1) {
6681 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
6682 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
6683 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]],
6684 exp->tok_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
6685 free(path);
6686 }
6687 }
6688 }
6689 LY_CHECK_RET(rc);
6690
6691 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6692 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6693 ++(*exp_idx);
6694 break;
6695
6696 case LYXP_TOKEN_NODETYPE:
6697 if (set) {
6698 assert(exp->tok_len[*exp_idx] == 4);
6699 if (set->type == LYXP_SET_SCNODE_SET) {
6700 set_scnode_clear_ctx(set);
6701 /* just for the debug message underneath */
6702 set = NULL;
6703 } else {
6704 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "node", 4)) {
6705 rc = xpath_node(NULL, 0, set, options);
6706 LY_CHECK_RET(rc);
6707 } else {
6708 assert(!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "text", 4));
6709 rc = xpath_text(NULL, 0, set, options);
6710 LY_CHECK_RET(rc);
6711 }
6712 }
6713 }
6714 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6715 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6716 ++(*exp_idx);
6717
6718 /* '(' */
6719 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
6720 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6721 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6722 ++(*exp_idx);
6723
6724 /* ')' */
6725 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
6726 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6727 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6728 ++(*exp_idx);
6729 break;
6730
6731 default:
Michal Vasko02a77382019-09-12 11:47:35 +02006732 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006733 }
6734
6735 return LY_SUCCESS;
6736}
6737
6738/**
6739 * @brief Evaluate Predicate. Logs directly on error.
6740 *
6741 * [7] Predicate ::= '[' Expr ']'
6742 *
6743 * @param[in] exp Parsed XPath expression.
6744 * @param[in] exp_idx Position in the expression @p exp.
6745 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6746 * @param[in] options XPath options.
6747 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6748 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6749 */
6750static LY_ERR
6751eval_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options, int parent_pos_pred)
6752{
6753 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006754 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006755 uint32_t orig_pos, orig_size;
6756 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006757 struct lyxp_set set2;
6758 struct lyd_node *orig_parent;
6759
6760 /* '[' */
6761 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6762 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6763 ++(*exp_idx);
6764
6765 if (!set) {
6766only_parse:
6767 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
6768 LY_CHECK_RET(rc);
6769 } else if (set->type == LYXP_SET_NODE_SET) {
6770 /* 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 +01006771 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772
6773 /* empty set, nothing to evaluate */
6774 if (!set->used) {
6775 goto only_parse;
6776 }
6777
6778 orig_exp = *exp_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 orig_pos = 0;
6780 orig_size = set->used;
6781 orig_parent = NULL;
6782 for (i = 0; i < set->used; ) {
6783 set_init(&set2, set);
6784 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6785 /* remember the node context position for position() and context size for last(),
6786 * predicates should always be evaluated with respect to the child axis (since we do
6787 * not support explicit axes) so we assign positions based on their parents */
6788 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6789 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6790 orig_pos = 1;
6791 } else {
6792 ++orig_pos;
6793 }
6794
6795 set2.ctx_pos = orig_pos;
6796 set2.ctx_size = orig_size;
6797 *exp_idx = orig_exp;
6798
6799 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6800 if (rc != LY_SUCCESS) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006801 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006802 return rc;
6803 }
6804
6805 /* number is a position */
6806 if (set2.type == LYXP_SET_NUMBER) {
6807 if ((long long)set2.val.num == orig_pos) {
6808 set2.val.num = 1;
6809 } else {
6810 set2.val.num = 0;
6811 }
6812 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006813 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006814
6815 /* predicate satisfied or not? */
Michal Vasko57eab132019-09-24 11:46:26 +02006816 if (!set2.val.bool) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006817 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818 }
6819 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006820 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821
6822 } else if (set->type == LYXP_SET_SCNODE_SET) {
6823 for (i = 0; i < set->used; ++i) {
6824 if (set->val.scnodes[i].in_ctx == 1) {
6825 /* there is a currently-valid node */
6826 break;
6827 }
6828 }
6829 /* empty set, nothing to evaluate */
6830 if (i == set->used) {
6831 goto only_parse;
6832 }
6833
6834 orig_exp = *exp_idx;
6835
Michal Vasko03ff5a72019-09-11 13:49:33 +02006836 /* set special in_ctx to all the valid snodes */
6837 pred_in_ctx = set_scnode_new_in_ctx(set);
6838
6839 /* use the valid snodes one-by-one */
6840 for (i = 0; i < set->used; ++i) {
6841 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6842 continue;
6843 }
6844 set->val.scnodes[i].in_ctx = 1;
6845
6846 *exp_idx = orig_exp;
6847
6848 rc = eval_expr_select(exp, exp_idx, 0, set, options);
6849 LY_CHECK_RET(rc);
6850
6851 set->val.scnodes[i].in_ctx = pred_in_ctx;
6852 }
6853
6854 /* restore the state as it was before the predicate */
6855 for (i = 0; i < set->used; ++i) {
6856 if (set->val.scnodes[i].in_ctx == 1) {
6857 set->val.scnodes[i].in_ctx = 0;
6858 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6859 set->val.scnodes[i].in_ctx = 1;
6860 }
6861 }
6862
6863 } else {
6864 set2.type = LYXP_SET_EMPTY;
6865 set_fill_set(&set2, set);
6866
6867 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6868 if (rc != LY_SUCCESS) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006869 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870 return rc;
6871 }
6872
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006873 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006874 if (!set2.val.bool) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006875 lyxp_set_cast(set, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006876 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006877 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006878 }
6879
6880 /* ']' */
6881 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK2);
6882 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6883 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6884 ++(*exp_idx);
6885
6886 return LY_SUCCESS;
6887}
6888
6889/**
6890 * @brief Evaluate RelativeLocationPath. Logs directly on error.
6891 *
6892 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
6893 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6894 *
6895 * @param[in] exp Parsed XPath expression.
6896 * @param[in] exp_idx Position in the expression @p exp.
6897 * @param[in] all_desc Whether to search all the descendants or children only.
6898 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6899 * @param[in] options XPath options.
6900 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
6901 */
6902static LY_ERR
6903eval_relative_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, int all_desc, struct lyxp_set *set, int options)
6904{
6905 int attr_axis;
6906 LY_ERR rc;
6907
6908 goto step;
6909 do {
6910 /* evaluate '/' or '//' */
6911 if (exp->tok_len[*exp_idx] == 1) {
6912 all_desc = 0;
6913 } else {
6914 assert(exp->tok_len[*exp_idx] == 2);
6915 all_desc = 1;
6916 }
6917 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6918 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6919 ++(*exp_idx);
6920
6921step:
6922 /* Step */
6923 attr_axis = 0;
6924 switch (exp->tokens[*exp_idx]) {
6925 case LYXP_TOKEN_DOT:
6926 /* evaluate '.' */
6927 if (set && (options & LYXP_SCNODE_ALL)) {
6928 rc = moveto_scnode_self(set, all_desc, options);
6929 } else {
6930 rc = moveto_self(set, all_desc, options);
6931 }
6932 LY_CHECK_RET(rc);
6933 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6934 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6935 ++(*exp_idx);
6936 break;
6937
6938 case LYXP_TOKEN_DDOT:
6939 /* evaluate '..' */
6940 if (set && (options & LYXP_SCNODE_ALL)) {
6941 rc = moveto_scnode_parent(set, all_desc, options);
6942 } else {
6943 rc = moveto_parent(set, all_desc, options);
6944 }
6945 LY_CHECK_RET(rc);
6946 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6947 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6948 ++(*exp_idx);
6949 break;
6950
6951 case LYXP_TOKEN_AT:
6952 /* evaluate '@' */
6953 attr_axis = 1;
6954 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6955 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6956 ++(*exp_idx);
6957
6958 /* fall through */
6959 case LYXP_TOKEN_NAMETEST:
6960 case LYXP_TOKEN_NODETYPE:
6961 rc = eval_node_test(exp, exp_idx, attr_axis, all_desc, set, options);
6962 LY_CHECK_RET(rc);
6963
6964 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
6965 rc = eval_predicate(exp, exp_idx, set, options, 1);
6966 LY_CHECK_RET(rc);
6967 }
6968 break;
6969
6970 default:
Michal Vasko02a77382019-09-12 11:47:35 +02006971 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006972 }
6973 } while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH));
6974
6975 return LY_SUCCESS;
6976}
6977
6978/**
6979 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
6980 *
6981 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
6982 *
6983 * @param[in] exp Parsed XPath expression.
6984 * @param[in] exp_idx Position in the expression @p exp.
6985 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6986 * @param[in] options XPath options.
6987 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6988 */
6989static LY_ERR
6990eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
6991{
6992 int all_desc;
6993 LY_ERR rc;
6994
6995 if (set) {
6996 /* no matter what tokens follow, we need to be at the root */
6997 moveto_root(set, options);
6998 }
6999
7000 /* '/' RelativeLocationPath? */
7001 if (exp->tok_len[*exp_idx] == 1) {
7002 /* evaluate '/' - deferred */
7003 all_desc = 0;
7004 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7005 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7006 ++(*exp_idx);
7007
Michal Vasko4b9e1052019-09-13 11:25:37 +02007008 if (exp_check_token(set ? set->ctx : NULL, exp, *exp_idx, LYXP_TOKEN_NONE, 0)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007009 return LY_SUCCESS;
7010 }
7011 switch (exp->tokens[*exp_idx]) {
7012 case LYXP_TOKEN_DOT:
7013 case LYXP_TOKEN_DDOT:
7014 case LYXP_TOKEN_AT:
7015 case LYXP_TOKEN_NAMETEST:
7016 case LYXP_TOKEN_NODETYPE:
7017 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7018 LY_CHECK_RET(rc);
7019 break;
7020 default:
7021 break;
7022 }
7023
7024 /* '//' RelativeLocationPath */
7025 } else {
7026 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7027 all_desc = 1;
7028 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7029 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7030 ++(*exp_idx);
7031
7032 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7033 LY_CHECK_RET(rc);
7034 }
7035
7036 return LY_SUCCESS;
7037}
7038
7039/**
7040 * @brief Evaluate FunctionCall. Logs directly on error.
7041 *
7042 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
7043 *
7044 * @param[in] exp Parsed XPath expression.
7045 * @param[in] exp_idx Position in the expression @p exp.
7046 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7047 * @param[in] options XPath options.
7048 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7049 */
7050static LY_ERR
7051eval_function_call(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7052{
7053 LY_ERR rc;
7054 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
7055 uint16_t arg_count = 0, i, func_exp = *exp_idx;
7056 struct lyxp_set **args = NULL, **args_aux;
7057
7058 if (set) {
7059 /* FunctionName */
7060 switch (exp->tok_len[*exp_idx]) {
7061 case 3:
7062 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
7063 xpath_func = &xpath_not;
7064 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
7065 xpath_func = &xpath_sum;
7066 }
7067 break;
7068 case 4:
7069 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
7070 xpath_func = &xpath_lang;
7071 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
7072 xpath_func = &xpath_last;
7073 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
7074 xpath_func = &xpath_name;
7075 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
7076 xpath_func = &xpath_true;
7077 }
7078 break;
7079 case 5:
7080 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
7081 xpath_func = &xpath_count;
7082 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
7083 xpath_func = &xpath_false;
7084 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
7085 xpath_func = &xpath_floor;
7086 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
7087 xpath_func = &xpath_round;
7088 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
7089 xpath_func = &xpath_deref;
7090 }
7091 break;
7092 case 6:
7093 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
7094 xpath_func = &xpath_concat;
7095 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
7096 xpath_func = &xpath_number;
7097 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
7098 xpath_func = &xpath_string;
7099 }
7100 break;
7101 case 7:
7102 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
7103 xpath_func = &xpath_boolean;
7104 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
7105 xpath_func = &xpath_ceiling;
7106 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
7107 xpath_func = &xpath_current;
7108 }
7109 break;
7110 case 8:
7111 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
7112 xpath_func = &xpath_contains;
7113 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
7114 xpath_func = &xpath_position;
7115 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
7116 xpath_func = &xpath_re_match;
7117 }
7118 break;
7119 case 9:
7120 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
7121 xpath_func = &xpath_substring;
7122 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
7123 xpath_func = &xpath_translate;
7124 }
7125 break;
7126 case 10:
7127 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
7128 xpath_func = &xpath_local_name;
7129 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
7130 xpath_func = &xpath_enum_value;
7131 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
7132 xpath_func = &xpath_bit_is_set;
7133 }
7134 break;
7135 case 11:
7136 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
7137 xpath_func = &xpath_starts_with;
7138 }
7139 break;
7140 case 12:
7141 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
7142 xpath_func = &xpath_derived_from;
7143 }
7144 break;
7145 case 13:
7146 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
7147 xpath_func = &xpath_namespace_uri;
7148 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
7149 xpath_func = &xpath_string_length;
7150 }
7151 break;
7152 case 15:
7153 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
7154 xpath_func = &xpath_normalize_space;
7155 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
7156 xpath_func = &xpath_substring_after;
7157 }
7158 break;
7159 case 16:
7160 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
7161 xpath_func = &xpath_substring_before;
7162 }
7163 break;
7164 case 20:
7165 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
7166 xpath_func = &xpath_derived_from_or_self;
7167 }
7168 break;
7169 }
7170
7171 if (!xpath_func) {
7172 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7173 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]]);
7174 return LY_EVALID;
7175 }
7176 }
7177
7178 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7179 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7180 ++(*exp_idx);
7181
7182 /* '(' */
7183 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7184 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7185 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7186 ++(*exp_idx);
7187
7188 /* ( Expr ( ',' Expr )* )? */
7189 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
7190 if (set) {
7191 args = malloc(sizeof *args);
7192 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7193 arg_count = 1;
7194 args[0] = set_copy(set);
7195 if (!args[0]) {
7196 rc = LY_EMEM;
7197 goto cleanup;
7198 }
7199
7200 rc = eval_expr_select(exp, exp_idx, 0, args[0], options);
7201 LY_CHECK_GOTO(rc, cleanup);
7202 } else {
7203 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7204 LY_CHECK_GOTO(rc, cleanup);
7205 }
7206 }
7207 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_COMMA)) {
7208 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7209 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7210 ++(*exp_idx);
7211
7212 if (set) {
7213 ++arg_count;
7214 args_aux = realloc(args, arg_count * sizeof *args);
7215 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7216 args = args_aux;
7217 args[arg_count - 1] = set_copy(set);
7218 if (!args[arg_count - 1]) {
7219 rc = LY_EMEM;
7220 goto cleanup;
7221 }
7222
7223 rc = eval_expr_select(exp, exp_idx, 0, args[arg_count - 1], options);
7224 LY_CHECK_GOTO(rc, cleanup);
7225 } else {
7226 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7227 LY_CHECK_GOTO(rc, cleanup);
7228 }
7229 }
7230
7231 /* ')' */
7232 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7233 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7234 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7235 ++(*exp_idx);
7236
7237 if (set) {
7238 /* evaluate function */
7239 rc = xpath_func(args, arg_count, set, options);
7240
7241 if (options & LYXP_SCNODE_ALL) {
7242 if (rc == LY_EINVAL) {
7243 /* some validation warning TODO log everything immediately? */
7244 LOGWRN(set->ctx, "Previous warning generated by XPath function \"%.*s\".",
7245 (exp->tok_pos[*exp_idx - 1] - exp->tok_pos[func_exp]) + 1, &exp->expr[exp->tok_pos[func_exp]]);
7246 rc = LY_SUCCESS;
7247 }
7248
7249 /* merge all nodes from arg evaluations */
7250 for (i = 0; i < arg_count; ++i) {
7251 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007252 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007253 }
7254 }
7255 } else {
7256 rc = LY_SUCCESS;
7257 }
7258
7259cleanup:
7260 for (i = 0; i < arg_count; ++i) {
7261 lyxp_set_free(args[i]);
7262 }
7263 free(args);
7264
7265 return rc;
7266}
7267
7268/**
7269 * @brief Evaluate Number. Logs directly on error.
7270 *
7271 * @param[in] ctx Context for errors.
7272 * @param[in] exp Parsed XPath expression.
7273 * @param[in] exp_idx Position in the expression @p exp.
7274 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7275 * @return LY_ERR
7276 */
7277static LY_ERR
7278eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
7279{
7280 long double num;
7281 char *endptr;
7282
7283 if (set) {
7284 errno = 0;
7285 num = strtold(&exp->expr[exp->tok_pos[*exp_idx]], &endptr);
7286 if (errno) {
7287 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7288 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
7289 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]], strerror(errno));
7290 return LY_EVALID;
7291 } else if (endptr - &exp->expr[exp->tok_pos[*exp_idx]] != exp->tok_len[*exp_idx]) {
7292 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7293 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
7294 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
7295 return LY_EVALID;
7296 }
7297
7298 set_fill_number(set, num);
7299 }
7300
7301 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7302 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7303 ++(*exp_idx);
7304 return LY_SUCCESS;
7305}
7306
7307/**
7308 * @brief Evaluate PathExpr. Logs directly on error.
7309 *
7310 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
7311 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7312 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7313 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
7314 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
7315 *
7316 * @param[in] exp Parsed XPath expression.
7317 * @param[in] exp_idx Position in the expression @p exp.
7318 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7319 * @param[in] options XPath options.
7320 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7321 */
7322static LY_ERR
7323eval_path_expr(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7324{
7325 int all_desc, parent_pos_pred;
7326 LY_ERR rc;
7327
7328 switch (exp->tokens[*exp_idx]) {
7329 case LYXP_TOKEN_PAR1:
7330 /* '(' Expr ')' */
7331
7332 /* '(' */
7333 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7334 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7335 ++(*exp_idx);
7336
7337 /* Expr */
7338 rc = eval_expr_select(exp, exp_idx, 0, set, options);
7339 LY_CHECK_RET(rc);
7340
7341 /* ')' */
7342 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7343 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7344 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7345 ++(*exp_idx);
7346
7347 parent_pos_pred = 0;
7348 goto predicate;
7349
7350 case LYXP_TOKEN_DOT:
7351 case LYXP_TOKEN_DDOT:
7352 case LYXP_TOKEN_AT:
7353 case LYXP_TOKEN_NAMETEST:
7354 case LYXP_TOKEN_NODETYPE:
7355 /* RelativeLocationPath */
7356 rc = eval_relative_location_path(exp, exp_idx, 0, set, options);
7357 LY_CHECK_RET(rc);
7358 break;
7359
7360 case LYXP_TOKEN_FUNCNAME:
7361 /* FunctionCall */
7362 if (!set) {
7363 rc = eval_function_call(exp, exp_idx, NULL, options);
7364 } else {
7365 rc = eval_function_call(exp, exp_idx, set, options);
7366 }
7367 LY_CHECK_RET(rc);
7368
7369 parent_pos_pred = 1;
7370 goto predicate;
7371
7372 case LYXP_TOKEN_OPERATOR_PATH:
7373 /* AbsoluteLocationPath */
7374 rc = eval_absolute_location_path(exp, exp_idx, set, options);
7375 LY_CHECK_RET(rc);
7376 break;
7377
7378 case LYXP_TOKEN_LITERAL:
7379 /* Literal */
7380 if (!set || (options & LYXP_SCNODE_ALL)) {
7381 if (set) {
7382 set_scnode_clear_ctx(set);
7383 }
7384 eval_literal(exp, exp_idx, NULL);
7385 } else {
7386 eval_literal(exp, exp_idx, set);
7387 }
7388
7389 parent_pos_pred = 1;
7390 goto predicate;
7391
7392 case LYXP_TOKEN_NUMBER:
7393 /* Number */
7394 if (!set || (options & LYXP_SCNODE_ALL)) {
7395 if (set) {
7396 set_scnode_clear_ctx(set);
7397 }
Michal Vasko02a77382019-09-12 11:47:35 +02007398 rc = eval_number(NULL, exp, exp_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399 } else {
7400 rc = eval_number(set->ctx, exp, exp_idx, set);
7401 }
7402 LY_CHECK_RET(rc);
7403
7404 parent_pos_pred = 1;
7405 goto predicate;
7406
7407 default:
7408 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, print_token(exp->tokens[*exp_idx]),
7409 &exp->expr[exp->tok_pos[*exp_idx]]);
7410 return LY_EVALID;
7411 }
7412
7413 return LY_SUCCESS;
7414
7415predicate:
7416 /* Predicate* */
7417 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
7418 rc = eval_predicate(exp, exp_idx, set, options, parent_pos_pred);
7419 LY_CHECK_RET(rc);
7420 }
7421
7422 /* ('/' or '//') RelativeLocationPath */
7423 if ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)) {
7424
7425 /* evaluate '/' or '//' */
7426 if (exp->tok_len[*exp_idx] == 1) {
7427 all_desc = 0;
7428 } else {
7429 assert(exp->tok_len[*exp_idx] == 2);
7430 all_desc = 1;
7431 }
7432
7433 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7434 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7435 ++(*exp_idx);
7436
7437 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7438 LY_CHECK_RET(rc);
7439 }
7440
7441 return LY_SUCCESS;
7442}
7443
7444/**
7445 * @brief Evaluate UnionExpr. Logs directly on error.
7446 *
7447 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
7448 *
7449 * @param[in] exp Parsed XPath expression.
7450 * @param[in] exp_idx Position in the expression @p exp.
7451 * @param[in] repeat How many times this expression is repeated.
7452 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7453 * @param[in] options XPath options.
7454 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7455 */
7456static LY_ERR
7457eval_union_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7458{
7459 LY_ERR rc = LY_SUCCESS;
7460 struct lyxp_set orig_set, set2;
7461 uint16_t i;
7462
7463 assert(repeat);
7464
7465 set_init(&orig_set, set);
7466 set_init(&set2, set);
7467
7468 set_fill_set(&orig_set, set);
7469
7470 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, set, options);
7471 LY_CHECK_GOTO(rc, cleanup);
7472
7473 /* ('|' PathExpr)* */
7474 for (i = 0; i < repeat; ++i) {
7475 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_UNI);
7476 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7477 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7478 ++(*exp_idx);
7479
7480 if (!set) {
7481 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, NULL, options);
7482 LY_CHECK_GOTO(rc, cleanup);
7483 continue;
7484 }
7485
7486 set_fill_set(&set2, &orig_set);
7487 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, &set2, options);
7488 LY_CHECK_GOTO(rc, cleanup);
7489
7490 /* eval */
7491 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007492 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007494 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 LY_CHECK_GOTO(rc, cleanup);
7496 }
7497 }
7498
7499cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007500 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7501 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 return rc;
7503}
7504
7505/**
7506 * @brief Evaluate UnaryExpr. Logs directly on error.
7507 *
7508 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
7509 *
7510 * @param[in] exp Parsed XPath expression.
7511 * @param[in] exp_idx Position in the expression @p exp.
7512 * @param[in] repeat How many times this expression is repeated.
7513 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7514 * @param[in] options XPath options.
7515 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7516 */
7517static LY_ERR
7518eval_unary_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7519{
7520 LY_ERR rc;
7521 uint16_t this_op, i;
7522
7523 assert(repeat);
7524
7525 /* ('-')+ */
7526 this_op = *exp_idx;
7527 for (i = 0; i < repeat; ++i) {
7528 assert(!exp_check_token(set->ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0) && (exp->expr[exp->tok_pos[*exp_idx]] == '-'));
7529
7530 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7531 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7532 ++(*exp_idx);
7533 }
7534
7535 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNARY, set, options);
7536 LY_CHECK_RET(rc);
7537
7538 if (set && (repeat % 2)) {
7539 if (options & LYXP_SCNODE_ALL) {
7540 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7541 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007542 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 LY_CHECK_RET(rc);
7544 }
7545 }
7546
7547 return LY_SUCCESS;
7548}
7549
7550/**
7551 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7552 *
7553 * [16] MultiplicativeExpr ::= UnaryExpr
7554 * | MultiplicativeExpr '*' UnaryExpr
7555 * | MultiplicativeExpr 'div' UnaryExpr
7556 * | MultiplicativeExpr 'mod' UnaryExpr
7557 *
7558 * @param[in] exp Parsed XPath expression.
7559 * @param[in] exp_idx Position in the expression @p exp.
7560 * @param[in] repeat How many times this expression is repeated.
7561 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7562 * @param[in] options XPath options.
7563 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7564 */
7565static LY_ERR
7566eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7567{
7568 LY_ERR rc;
7569 uint16_t this_op;
7570 struct lyxp_set orig_set, set2;
7571 uint16_t i;
7572
7573 assert(repeat);
7574
7575 set_init(&orig_set, set);
7576 set_init(&set2, set);
7577
7578 set_fill_set(&orig_set, set);
7579
7580 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
7581 LY_CHECK_GOTO(rc, cleanup);
7582
7583 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7584 for (i = 0; i < repeat; ++i) {
7585 this_op = *exp_idx;
7586
7587 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7588 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7589 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7590 ++(*exp_idx);
7591
7592 if (!set) {
7593 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
7594 LY_CHECK_GOTO(rc, cleanup);
7595 continue;
7596 }
7597
7598 set_fill_set(&set2, &orig_set);
7599 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
7600 LY_CHECK_GOTO(rc, cleanup);
7601
7602 /* eval */
7603 if (options & LYXP_SCNODE_ALL) {
7604 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007605 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606 set_scnode_clear_ctx(set);
7607 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007608 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007609 LY_CHECK_GOTO(rc, cleanup);
7610 }
7611 }
7612
7613cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007614 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7615 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 return rc;
7617}
7618
7619/**
7620 * @brief Evaluate AdditiveExpr. Logs directly on error.
7621 *
7622 * [15] AdditiveExpr ::= MultiplicativeExpr
7623 * | AdditiveExpr '+' MultiplicativeExpr
7624 * | AdditiveExpr '-' MultiplicativeExpr
7625 *
7626 * @param[in] exp Parsed XPath expression.
7627 * @param[in] exp_idx Position in the expression @p exp.
7628 * @param[in] repeat How many times this expression is repeated.
7629 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7630 * @param[in] options XPath options.
7631 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7632 */
7633static LY_ERR
7634eval_additive_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7635{
7636 LY_ERR rc;
7637 uint16_t this_op;
7638 struct lyxp_set orig_set, set2;
7639 uint16_t i;
7640
7641 assert(repeat);
7642
7643 set_init(&orig_set, set);
7644 set_init(&set2, set);
7645
7646 set_fill_set(&orig_set, set);
7647
7648 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, set, options);
7649 LY_CHECK_GOTO(rc, cleanup);
7650
7651 /* ('+' / '-' MultiplicativeExpr)* */
7652 for (i = 0; i < repeat; ++i) {
7653 this_op = *exp_idx;
7654
7655 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7656 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7657 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7658 ++(*exp_idx);
7659
7660 if (!set) {
7661 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, NULL, options);
7662 LY_CHECK_GOTO(rc, cleanup);
7663 continue;
7664 }
7665
7666 set_fill_set(&set2, &orig_set);
7667 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, &set2, options);
7668 LY_CHECK_GOTO(rc, cleanup);
7669
7670 /* eval */
7671 if (options & LYXP_SCNODE_ALL) {
7672 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007673 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 set_scnode_clear_ctx(set);
7675 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007676 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 LY_CHECK_GOTO(rc, cleanup);
7678 }
7679 }
7680
7681cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007682 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7683 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684 return rc;
7685}
7686
7687/**
7688 * @brief Evaluate RelationalExpr. Logs directly on error.
7689 *
7690 * [14] RelationalExpr ::= AdditiveExpr
7691 * | RelationalExpr '<' AdditiveExpr
7692 * | RelationalExpr '>' AdditiveExpr
7693 * | RelationalExpr '<=' AdditiveExpr
7694 * | RelationalExpr '>=' AdditiveExpr
7695 *
7696 * @param[in] exp Parsed XPath expression.
7697 * @param[in] exp_idx Position in the expression @p exp.
7698 * @param[in] repeat How many times this expression is repeated.
7699 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7700 * @param[in] options XPath options.
7701 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7702 */
7703static LY_ERR
7704eval_relational_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7705{
7706 LY_ERR rc;
7707 uint16_t this_op;
7708 struct lyxp_set orig_set, set2;
7709 uint16_t i;
7710
7711 assert(repeat);
7712
7713 set_init(&orig_set, set);
7714 set_init(&set2, set);
7715
7716 set_fill_set(&orig_set, set);
7717
7718 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, set, options);
7719 LY_CHECK_GOTO(rc, cleanup);
7720
7721 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7722 for (i = 0; i < repeat; ++i) {
7723 this_op = *exp_idx;
7724
7725 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7726 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7727 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7728 ++(*exp_idx);
7729
7730 if (!set) {
7731 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, NULL, options);
7732 LY_CHECK_GOTO(rc, cleanup);
7733 continue;
7734 }
7735
7736 set_fill_set(&set2, &orig_set);
7737 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, &set2, options);
7738 LY_CHECK_GOTO(rc, cleanup);
7739
7740 /* eval */
7741 if (options & LYXP_SCNODE_ALL) {
7742 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007743 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007744 set_scnode_clear_ctx(set);
7745 } else {
7746 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7747 LY_CHECK_GOTO(rc, cleanup);
7748 }
7749 }
7750
7751cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007752 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7753 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007754 return rc;
7755}
7756
7757/**
7758 * @brief Evaluate EqualityExpr. Logs directly on error.
7759 *
7760 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
7761 * | EqualityExpr '!=' RelationalExpr
7762 *
7763 * @param[in] exp Parsed XPath expression.
7764 * @param[in] exp_idx Position in the expression @p exp.
7765 * @param[in] repeat How many times this expression is repeated.
7766 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7767 * @param[in] options XPath options.
7768 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7769 */
7770static LY_ERR
7771eval_equality_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7772{
7773 LY_ERR rc;
7774 uint16_t this_op;
7775 struct lyxp_set orig_set, set2;
7776 uint16_t i;
7777
7778 assert(repeat);
7779
7780 set_init(&orig_set, set);
7781 set_init(&set2, set);
7782
7783 set_fill_set(&orig_set, set);
7784
7785 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, set, options);
7786 LY_CHECK_GOTO(rc, cleanup);
7787
7788 /* ('=' / '!=' RelationalExpr)* */
7789 for (i = 0; i < repeat; ++i) {
7790 this_op = *exp_idx;
7791
7792 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7793 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7794 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7795 ++(*exp_idx);
7796
7797 if (!set) {
7798 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, NULL, options);
7799 LY_CHECK_GOTO(rc, cleanup);
7800 continue;
7801 }
7802
7803 set_fill_set(&set2, &orig_set);
7804 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, &set2, options);
7805 LY_CHECK_GOTO(rc, cleanup);
7806
7807 /* eval */
7808 if (options & LYXP_SCNODE_ALL) {
7809 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
7810 warn_equality_value(exp, set, *exp_idx - 1, this_op - 1, *exp_idx - 1);
7811 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *exp_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007812 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813 set_scnode_clear_ctx(set);
7814 } else {
7815 /* special handling of evaluations of identityref comparisons, always compare prefixed identites */
7816 if ((set->type == LYXP_SET_NODE_SET) && (set->val.nodes[0].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
7817 && (((struct lysc_node_leaf *)set->val.nodes[0].node->schema)->type->basetype == LY_TYPE_IDENT)) {
7818 /* left operand is identityref */
7819 if ((set2.type == LYXP_SET_STRING) && !strchr(set2.val.str, ':')) {
7820 /* missing prefix in the right operand */
7821 set2.val.str = ly_realloc(set2.val.str, strlen(set->local_mod->name) + 1 + strlen(set2.val.str) + 1);
7822 if (!set2.val.str) {
7823 goto cleanup;
7824 }
7825
7826 memmove(set2.val.str + strlen(set->local_mod->name) + 1, set2.val.str, strlen(set2.val.str) + 1);
7827 memcpy(set2.val.str, set->local_mod->name, strlen(set->local_mod->name));
7828 set2.val.str[strlen(set->local_mod->name)] = ':';
7829 }
7830 }
7831
7832 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7833 LY_CHECK_GOTO(rc, cleanup);
7834 }
7835 }
7836
7837cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007838 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7839 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840 return rc;
7841}
7842
7843/**
7844 * @brief Evaluate AndExpr. Logs directly on error.
7845 *
7846 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
7847 *
7848 * @param[in] exp Parsed XPath expression.
7849 * @param[in] exp_idx Position in the expression @p exp.
7850 * @param[in] repeat How many times this expression is repeated.
7851 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7852 * @param[in] options XPath options.
7853 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7854 */
7855static LY_ERR
7856eval_and_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7857{
7858 LY_ERR rc;
7859 struct lyxp_set orig_set, set2;
7860 uint16_t i;
7861
7862 assert(repeat);
7863
7864 set_init(&orig_set, set);
7865 set_init(&set2, set);
7866
7867 set_fill_set(&orig_set, set);
7868
7869 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, set, options);
7870 LY_CHECK_GOTO(rc, cleanup);
7871
7872 /* cast to boolean, we know that will be the final result */
7873 if (set && (options & LYXP_SCNODE_ALL)) {
7874 set_scnode_clear_ctx(set);
7875 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007876 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007877 }
7878
7879 /* ('and' EqualityExpr)* */
7880 for (i = 0; i < repeat; ++i) {
7881 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
7882 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bool ? "skipped" : "parsed"),
7883 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7884 ++(*exp_idx);
7885
7886 /* lazy evaluation */
7887 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bool)) {
7888 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, NULL, options);
7889 LY_CHECK_GOTO(rc, cleanup);
7890 continue;
7891 }
7892
7893 set_fill_set(&set2, &orig_set);
7894 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, &set2, options);
7895 LY_CHECK_GOTO(rc, cleanup);
7896
7897 /* eval - just get boolean value actually */
7898 if (set->type == LYXP_SET_SCNODE_SET) {
7899 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007900 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007901 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007902 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007903 set_fill_set(set, &set2);
7904 }
7905 }
7906
7907cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007908 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7909 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007910 return rc;
7911}
7912
7913/**
7914 * @brief Evaluate OrExpr. Logs directly on error.
7915 *
7916 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
7917 *
7918 * @param[in] exp Parsed XPath expression.
7919 * @param[in] exp_idx Position in the expression @p exp.
7920 * @param[in] repeat How many times this expression is repeated.
7921 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7922 * @param[in] options XPath options.
7923 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7924 */
7925static LY_ERR
7926eval_or_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7927{
7928 LY_ERR rc;
7929 struct lyxp_set orig_set, set2;
7930 uint16_t i;
7931
7932 assert(repeat);
7933
7934 set_init(&orig_set, set);
7935 set_init(&set2, set);
7936
7937 set_fill_set(&orig_set, set);
7938
7939 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, set, options);
7940 LY_CHECK_GOTO(rc, cleanup);
7941
7942 /* cast to boolean, we know that will be the final result */
7943 if (set && (options & LYXP_SCNODE_ALL)) {
7944 set_scnode_clear_ctx(set);
7945 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007946 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007947 }
7948
7949 /* ('or' AndExpr)* */
7950 for (i = 0; i < repeat; ++i) {
7951 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
7952 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bool ? "skipped" : "parsed"),
7953 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7954 ++(*exp_idx);
7955
7956 /* lazy evaluation */
7957 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bool)) {
7958 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, NULL, options);
7959 LY_CHECK_GOTO(rc, cleanup);
7960 continue;
7961 }
7962
7963 set_fill_set(&set2, &orig_set);
7964 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
7965 * but it does not matter */
7966 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, &set2, options);
7967 LY_CHECK_GOTO(rc, cleanup);
7968
7969 /* eval - just get boolean value actually */
7970 if (set->type == LYXP_SET_SCNODE_SET) {
7971 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007972 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007974 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007975 set_fill_set(set, &set2);
7976 }
7977 }
7978
7979cleanup:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007980 lyxp_set_cast(&orig_set, LYXP_SET_EMPTY);
7981 lyxp_set_cast(&set2, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007982 return rc;
7983}
7984
7985/**
7986 * @brief Decide what expression is at the pointer @p exp_idx and evaluate it accordingly.
7987 *
7988 * @param[in] exp Parsed XPath expression.
7989 * @param[in] exp_idx Position in the expression @p exp.
7990 * @param[in] etype Expression type to evaluate.
7991 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7992 * @param[in] options XPath options.
7993 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7994 */
7995static LY_ERR
7996eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
7997{
7998 uint16_t i, count;
7999 enum lyxp_expr_type next_etype;
8000 LY_ERR rc;
8001
8002 /* process operator repeats */
8003 if (!exp->repeat[*exp_idx]) {
8004 next_etype = LYXP_EXPR_NONE;
8005 } else {
8006 /* find etype repeat */
8007 for (i = 0; exp->repeat[*exp_idx][i] > etype; ++i);
8008
8009 /* select one-priority lower because etype expression called us */
8010 if (i) {
8011 next_etype = exp->repeat[*exp_idx][i - 1];
8012 /* count repeats for that expression */
8013 for (count = 0; i && exp->repeat[*exp_idx][i - 1] == next_etype; ++count, --i);
8014 } else {
8015 next_etype = LYXP_EXPR_NONE;
8016 }
8017 }
8018
8019 /* decide what expression are we parsing based on the repeat */
8020 switch (next_etype) {
8021 case LYXP_EXPR_OR:
8022 rc = eval_or_expr(exp, exp_idx, count, set, options);
8023 break;
8024 case LYXP_EXPR_AND:
8025 rc = eval_and_expr(exp, exp_idx, count, set, options);
8026 break;
8027 case LYXP_EXPR_EQUALITY:
8028 rc = eval_equality_expr(exp, exp_idx, count, set, options);
8029 break;
8030 case LYXP_EXPR_RELATIONAL:
8031 rc = eval_relational_expr(exp, exp_idx, count, set, options);
8032 break;
8033 case LYXP_EXPR_ADDITIVE:
8034 rc = eval_additive_expr(exp, exp_idx, count, set, options);
8035 break;
8036 case LYXP_EXPR_MULTIPLICATIVE:
8037 rc = eval_multiplicative_expr(exp, exp_idx, count, set, options);
8038 break;
8039 case LYXP_EXPR_UNARY:
8040 rc = eval_unary_expr(exp, exp_idx, count, set, options);
8041 break;
8042 case LYXP_EXPR_UNION:
8043 rc = eval_union_expr(exp, exp_idx, count, set, options);
8044 break;
8045 case LYXP_EXPR_NONE:
8046 rc = eval_path_expr(exp, exp_idx, set, options);
8047 break;
8048 default:
8049 LOGINT_RET(set->ctx);
8050 }
8051
8052 return rc;
8053}
8054
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008055/**
8056 * @brief Get root type.
8057 *
8058 * @param[in] ctx_node Context node.
8059 * @param[in] ctx_scnode Schema context node.
8060 * @param[in] options XPath options.
8061 * @return Root type.
8062 */
8063static enum lyxp_node_type
8064lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8065{
8066 if (options & LYXP_SCNODE_ALL) {
8067 if (options & LYXP_SCNODE) {
8068 /* general root that can access everything */
8069 return LYXP_NODE_ROOT;
8070 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8071 /* root context node can access only config data (because we said so, it is unspecified) */
8072 return LYXP_NODE_ROOT_CONFIG;
8073 } else {
8074 return LYXP_NODE_ROOT;
8075 }
8076 }
8077
8078 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8079 /* root context node can access only config data (because we said so, it is unspecified) */
8080 return LYXP_NODE_ROOT_CONFIG;
8081 }
8082
8083 return LYXP_NODE_ROOT;
8084}
8085
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008087lyxp_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 +02008088 enum lyxp_node_type ctx_node_type, const struct lyd_node **trees, struct lyxp_set *set, int options)
8089{
Michal Vasko03ff5a72019-09-11 13:49:33 +02008090 uint16_t exp_idx = 0;
8091 LY_ERR rc;
8092
Michal Vaskoecd62de2019-11-13 12:35:11 +01008093 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094
8095 /* prepare set for evaluation */
8096 exp_idx = 0;
8097 memset(set, 0, sizeof *set);
8098 set->type = LYXP_SET_EMPTY;
8099 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node_type, options);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008100 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101 set->ctx_node = ctx_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008102 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008103 set->local_mod = local_mod;
8104 set->trees = trees;
8105 set->format = format;
8106
8107 /* evaluate */
8108 rc = eval_expr_select(exp, &exp_idx, 0, set, options);
8109 if (rc != LY_SUCCESS) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008110 lyxp_set_cast(set, LYXP_SET_EMPTY);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 }
8112
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113 return rc;
8114}
8115
8116#if 0
8117
8118/* full xml printing of set elements, not used currently */
8119
8120void
8121lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8122{
8123 uint32_t i;
8124 char *str_num;
8125 struct lyout out;
8126
8127 memset(&out, 0, sizeof out);
8128
8129 out.type = LYOUT_STREAM;
8130 out.method.f = f;
8131
8132 switch (set->type) {
8133 case LYXP_SET_EMPTY:
8134 ly_print(&out, "Empty XPath set\n\n");
8135 break;
8136 case LYXP_SET_BOOLEAN:
8137 ly_print(&out, "Boolean XPath set:\n");
8138 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8139 break;
8140 case LYXP_SET_STRING:
8141 ly_print(&out, "String XPath set:\n");
8142 ly_print(&out, "\"%s\"\n\n", set->value.str);
8143 break;
8144 case LYXP_SET_NUMBER:
8145 ly_print(&out, "Number XPath set:\n");
8146
8147 if (isnan(set->value.num)) {
8148 str_num = strdup("NaN");
8149 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8150 str_num = strdup("0");
8151 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8152 str_num = strdup("Infinity");
8153 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8154 str_num = strdup("-Infinity");
8155 } else if ((long long)set->value.num == set->value.num) {
8156 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8157 str_num = NULL;
8158 }
8159 } else {
8160 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8161 str_num = NULL;
8162 }
8163 }
8164 if (!str_num) {
8165 LOGMEM;
8166 return;
8167 }
8168 ly_print(&out, "%s\n\n", str_num);
8169 free(str_num);
8170 break;
8171 case LYXP_SET_NODE_SET:
8172 ly_print(&out, "Node XPath set:\n");
8173
8174 for (i = 0; i < set->used; ++i) {
8175 ly_print(&out, "%d. ", i + 1);
8176 switch (set->node_type[i]) {
8177 case LYXP_NODE_ROOT_ALL:
8178 ly_print(&out, "ROOT all\n\n");
8179 break;
8180 case LYXP_NODE_ROOT_CONFIG:
8181 ly_print(&out, "ROOT config\n\n");
8182 break;
8183 case LYXP_NODE_ROOT_STATE:
8184 ly_print(&out, "ROOT state\n\n");
8185 break;
8186 case LYXP_NODE_ROOT_NOTIF:
8187 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8188 break;
8189 case LYXP_NODE_ROOT_RPC:
8190 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8191 break;
8192 case LYXP_NODE_ROOT_OUTPUT:
8193 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8194 break;
8195 case LYXP_NODE_ELEM:
8196 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8197 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8198 ly_print(&out, "\n");
8199 break;
8200 case LYXP_NODE_TEXT:
8201 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8202 break;
8203 case LYXP_NODE_ATTR:
8204 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8205 break;
8206 }
8207 }
8208 break;
8209 }
8210}
8211
8212#endif
8213
8214LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008215lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008216{
8217 long double num;
8218 char *str;
8219 LY_ERR rc;
8220
8221 if (!set || (set->type == target)) {
8222 return LY_SUCCESS;
8223 }
8224
8225 /* it's not possible to convert anything into a node set */
8226 assert((target != LYXP_SET_NODE_SET) && ((set->type != LYXP_SET_SCNODE_SET) || (target == LYXP_SET_EMPTY)));
8227
8228 if (set->type == LYXP_SET_SCNODE_SET) {
8229 set_free_content(set);
8230 return LY_EINVAL;
8231 }
8232
8233 /* to STRING */
8234 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER)
8235 && ((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_EMPTY)))) {
8236 switch (set->type) {
8237 case LYXP_SET_NUMBER:
8238 if (isnan(set->val.num)) {
8239 set->val.str = strdup("NaN");
8240 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8241 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8242 set->val.str = strdup("0");
8243 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8244 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8245 set->val.str = strdup("Infinity");
8246 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8247 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8248 set->val.str = strdup("-Infinity");
8249 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8250 } else if ((long long)set->val.num == set->val.num) {
8251 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8252 LOGMEM_RET(set->ctx);
8253 }
8254 set->val.str = str;
8255 } else {
8256 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8257 LOGMEM_RET(set->ctx);
8258 }
8259 set->val.str = str;
8260 }
8261 break;
8262 case LYXP_SET_BOOLEAN:
8263 if (set->val.bool) {
8264 set->val.str = strdup("true");
8265 } else {
8266 set->val.str = strdup("false");
8267 }
8268 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8269 break;
8270 case LYXP_SET_NODE_SET:
8271 assert(set->used);
8272
8273 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008274 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008276 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 LY_CHECK_RET(rc);
8278 set_free_content(set);
8279 set->val.str = str;
8280 break;
8281 case LYXP_SET_EMPTY:
8282 set->val.str = strdup("");
8283 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8284 break;
8285 default:
8286 LOGINT_RET(set->ctx);
8287 }
8288 set->type = LYXP_SET_STRING;
8289 }
8290
8291 /* to NUMBER */
8292 if (target == LYXP_SET_NUMBER) {
8293 switch (set->type) {
8294 case LYXP_SET_STRING:
8295 num = cast_string_to_number(set->val.str);
8296 set_free_content(set);
8297 set->val.num = num;
8298 break;
8299 case LYXP_SET_BOOLEAN:
8300 if (set->val.bool) {
8301 set->val.num = 1;
8302 } else {
8303 set->val.num = 0;
8304 }
8305 break;
8306 default:
8307 LOGINT_RET(set->ctx);
8308 }
8309 set->type = LYXP_SET_NUMBER;
8310 }
8311
8312 /* to BOOLEAN */
8313 if (target == LYXP_SET_BOOLEAN) {
8314 switch (set->type) {
8315 case LYXP_SET_NUMBER:
8316 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
8317 set->val.bool = 0;
8318 } else {
8319 set->val.bool = 1;
8320 }
8321 break;
8322 case LYXP_SET_STRING:
8323 if (set->val.str[0]) {
8324 set_free_content(set);
8325 set->val.bool = 1;
8326 } else {
8327 set_free_content(set);
8328 set->val.bool = 0;
8329 }
8330 break;
8331 case LYXP_SET_NODE_SET:
8332 set_free_content(set);
8333
8334 assert(set->used);
8335 set->val.bool = 1;
8336 break;
8337 case LYXP_SET_EMPTY:
8338 set->val.bool = 0;
8339 break;
8340 default:
8341 LOGINT_RET(set->ctx);
8342 }
8343 set->type = LYXP_SET_BOOLEAN;
8344 }
8345
8346 /* to EMPTY */
8347 if (target == LYXP_SET_EMPTY) {
8348 set_free_content(set);
8349 set->type = LYXP_SET_EMPTY;
8350 }
8351
8352 return LY_SUCCESS;
8353}
8354
8355LY_ERR
8356lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8357 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8358{
8359 struct ly_ctx *ctx;
8360 uint16_t exp_idx = 0;
8361
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008362 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363
8364 ctx = local_mod->ctx;
8365
8366 /* prepare set for evaluation */
8367 exp_idx = 0;
8368 memset(set, 0, sizeof *set);
8369 set->type = LYXP_SET_SCNODE_SET;
Michal Vaskoecd62de2019-11-13 12:35:11 +01008370 lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008371 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008372 set->ctx = ctx;
8373 set->ctx_scnode = ctx_scnode;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008374 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 set->local_mod = local_mod;
8376 set->format = format;
8377
8378 /* evaluate */
8379 return eval_expr_select(exp, &exp_idx, 0, set, options);
8380}