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