Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file xpath.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG XPath evaluation functions |
| 5 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 7 | * |
| 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 | */ |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 17 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 19 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include <errno.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 25 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 26 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 28 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 32 | #include "out.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 33 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 34 | #include "path.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 35 | #include "plugins_exts/metadata.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 36 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 37 | #include "printer_data.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 38 | #include "schema_compile_node.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 39 | #include "tree.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 40 | #include "tree_data.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 42 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 43 | #include "tree_schema_internal.h" |
| 44 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 45 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 46 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth); |
| 47 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 48 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 49 | static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set, |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 50 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 51 | static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, |
| 52 | const struct lyd_node *node, enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 53 | static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 54 | enum lyxp_axis axis, uint32_t options); |
| 55 | static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 56 | enum lyxp_axis axis, uint32_t options); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 57 | static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 58 | |
aPiecek | 96dc1e3 | 2021-10-08 15:45:59 +0200 | [diff] [blame] | 59 | /* Functions are divided into the following basic classes: |
| 60 | * |
| 61 | * (re)parse functions: |
| 62 | * Parse functions parse the expression into |
| 63 | * tokens (syntactic analysis). |
| 64 | * Reparse functions perform semantic analysis |
| 65 | * (do not save the result, just a check) of |
| 66 | * the expression and fill repeat indices. |
| 67 | * |
| 68 | * warn functions: |
| 69 | * Warn functions check specific reasonable conditions for schema XPath |
| 70 | * and print a warning if they are not satisfied. |
| 71 | * |
| 72 | * moveto functions: |
| 73 | * They and only they actually change the context (set). |
| 74 | * |
| 75 | * eval functions: |
| 76 | * They execute a parsed XPath expression on some data subtree. |
| 77 | */ |
| 78 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 79 | /** |
| 80 | * @brief Print the type of an XPath \p set. |
| 81 | * |
| 82 | * @param[in] set Set to use. |
| 83 | * @return Set type string. |
| 84 | */ |
| 85 | static const char * |
| 86 | print_set_type(struct lyxp_set *set) |
| 87 | { |
| 88 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 89 | case LYXP_SET_NODE_SET: |
| 90 | return "node set"; |
| 91 | case LYXP_SET_SCNODE_SET: |
| 92 | return "schema node set"; |
| 93 | case LYXP_SET_BOOLEAN: |
| 94 | return "boolean"; |
| 95 | case LYXP_SET_NUMBER: |
| 96 | return "number"; |
| 97 | case LYXP_SET_STRING: |
| 98 | return "string"; |
| 99 | } |
| 100 | |
| 101 | return NULL; |
| 102 | } |
| 103 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 104 | const char * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 105 | lyxp_token2str(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 106 | { |
| 107 | switch (tok) { |
| 108 | case LYXP_TOKEN_PAR1: |
| 109 | return "("; |
| 110 | case LYXP_TOKEN_PAR2: |
| 111 | return ")"; |
| 112 | case LYXP_TOKEN_BRACK1: |
| 113 | return "["; |
| 114 | case LYXP_TOKEN_BRACK2: |
| 115 | return "]"; |
| 116 | case LYXP_TOKEN_DOT: |
| 117 | return "."; |
| 118 | case LYXP_TOKEN_DDOT: |
| 119 | return ".."; |
| 120 | case LYXP_TOKEN_AT: |
| 121 | return "@"; |
| 122 | case LYXP_TOKEN_COMMA: |
| 123 | return ","; |
Michal Vasko | e0a8ce7 | 2023-04-04 08:30:44 +0200 | [diff] [blame] | 124 | case LYXP_TOKEN_DCOLON: |
| 125 | return "::"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 126 | case LYXP_TOKEN_NAMETEST: |
| 127 | return "NameTest"; |
| 128 | case LYXP_TOKEN_NODETYPE: |
| 129 | return "NodeType"; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 130 | case LYXP_TOKEN_VARREF: |
| 131 | return "VariableReference"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 132 | case LYXP_TOKEN_FUNCNAME: |
| 133 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 134 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 135 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 136 | case LYXP_TOKEN_OPER_EQUAL: |
| 137 | return "Operator(Equal)"; |
| 138 | case LYXP_TOKEN_OPER_NEQUAL: |
| 139 | return "Operator(Non-equal)"; |
| 140 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 141 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 142 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 143 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 144 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 145 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 146 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 147 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 148 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 149 | return "Operator(Recursive Path)"; |
Michal Vasko | e0a8ce7 | 2023-04-04 08:30:44 +0200 | [diff] [blame] | 150 | case LYXP_TOKEN_AXISNAME: |
| 151 | return "AxisName"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 152 | case LYXP_TOKEN_LITERAL: |
| 153 | return "Literal"; |
| 154 | case LYXP_TOKEN_NUMBER: |
| 155 | return "Number"; |
| 156 | default: |
| 157 | LOGINT(NULL); |
| 158 | return ""; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 163 | * @brief Transform string into an axis. |
| 164 | * |
| 165 | * @param[in] str String to transform. |
| 166 | * @param[in] str_len Length of @p str. |
| 167 | * @return Transformed axis. |
| 168 | */ |
| 169 | static enum lyxp_axis |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 170 | str2axis(const char *str, uint32_t str_len) |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 171 | { |
| 172 | switch (str_len) { |
| 173 | case 4: |
| 174 | assert(!strncmp("self", str, str_len)); |
| 175 | return LYXP_AXIS_SELF; |
| 176 | case 5: |
| 177 | assert(!strncmp("child", str, str_len)); |
| 178 | return LYXP_AXIS_CHILD; |
| 179 | case 6: |
| 180 | assert(!strncmp("parent", str, str_len)); |
| 181 | return LYXP_AXIS_PARENT; |
| 182 | case 8: |
| 183 | assert(!strncmp("ancestor", str, str_len)); |
| 184 | return LYXP_AXIS_ANCESTOR; |
| 185 | case 9: |
| 186 | if (str[0] == 'a') { |
| 187 | assert(!strncmp("attribute", str, str_len)); |
| 188 | return LYXP_AXIS_ATTRIBUTE; |
| 189 | } else if (str[0] == 'f') { |
| 190 | assert(!strncmp("following", str, str_len)); |
| 191 | return LYXP_AXIS_FOLLOWING; |
| 192 | } else { |
| 193 | assert(!strncmp("preceding", str, str_len)); |
| 194 | return LYXP_AXIS_PRECEDING; |
| 195 | } |
| 196 | break; |
| 197 | case 10: |
| 198 | assert(!strncmp("descendant", str, str_len)); |
| 199 | return LYXP_AXIS_DESCENDANT; |
| 200 | case 16: |
| 201 | assert(!strncmp("ancestor-or-self", str, str_len)); |
| 202 | return LYXP_AXIS_ANCESTOR_OR_SELF; |
| 203 | case 17: |
| 204 | if (str[0] == 'f') { |
| 205 | assert(!strncmp("following-sibling", str, str_len)); |
| 206 | return LYXP_AXIS_FOLLOWING_SIBLING; |
| 207 | } else { |
| 208 | assert(!strncmp("preceding-sibling", str, str_len)); |
| 209 | return LYXP_AXIS_PRECEDING_SIBLING; |
| 210 | } |
| 211 | break; |
| 212 | case 18: |
| 213 | assert(!strncmp("descendant-or-self", str, str_len)); |
| 214 | return LYXP_AXIS_DESCENDANT_OR_SELF; |
| 215 | } |
| 216 | |
| 217 | LOGINT(NULL); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 222 | * @brief Print the whole expression \p exp to debug output. |
| 223 | * |
| 224 | * @param[in] exp Expression to use. |
| 225 | */ |
| 226 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 227 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 228 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 229 | #define MSG_BUFFER_SIZE 128 |
| 230 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 231 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 232 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 233 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | |
| 237 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 238 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 239 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 240 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 241 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 242 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 243 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 244 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 245 | } |
| 246 | strcat(tmp, ")"); |
| 247 | } |
| 248 | LOGDBG(LY_LDGXPATH, tmp); |
| 249 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 250 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | #ifndef NDEBUG |
| 254 | |
| 255 | /** |
| 256 | * @brief Print XPath set content to debug output. |
| 257 | * |
| 258 | * @param[in] set Set to print. |
| 259 | */ |
| 260 | static void |
| 261 | print_set_debug(struct lyxp_set *set) |
| 262 | { |
| 263 | uint32_t i; |
| 264 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 265 | struct lyxp_set_node *item; |
| 266 | struct lyxp_set_scnode *sitem; |
| 267 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 268 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
| 272 | switch (set->type) { |
| 273 | case LYXP_SET_NODE_SET: |
| 274 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 275 | for (i = 0; i < set->used; ++i) { |
| 276 | item = &set->val.nodes[i]; |
| 277 | |
| 278 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 279 | case LYXP_NODE_NONE: |
| 280 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 281 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 282 | case LYXP_NODE_ROOT: |
| 283 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 284 | break; |
| 285 | case LYXP_NODE_ROOT_CONFIG: |
| 286 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 287 | break; |
| 288 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 289 | if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 290 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 291 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 292 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 293 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 294 | item->node->schema->name, lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 295 | } else { |
| 296 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 297 | } |
| 298 | break; |
| 299 | case LYXP_NODE_TEXT: |
| 300 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 301 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 302 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 303 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 304 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 305 | } |
| 306 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 307 | case LYXP_NODE_META: |
| 308 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 309 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | } |
| 313 | break; |
| 314 | |
| 315 | case LYXP_SET_SCNODE_SET: |
| 316 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 317 | for (i = 0; i < set->used; ++i) { |
| 318 | sitem = &set->val.scnodes[i]; |
| 319 | |
| 320 | switch (sitem->type) { |
| 321 | case LYXP_NODE_ROOT: |
| 322 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 323 | break; |
| 324 | case LYXP_NODE_ROOT_CONFIG: |
| 325 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 326 | break; |
| 327 | case LYXP_NODE_ELEM: |
| 328 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 329 | break; |
| 330 | default: |
| 331 | LOGINT(NULL); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | break; |
| 336 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 337 | case LYXP_SET_BOOLEAN: |
| 338 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 339 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 340 | break; |
| 341 | |
| 342 | case LYXP_SET_STRING: |
| 343 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 344 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 345 | break; |
| 346 | |
| 347 | case LYXP_SET_NUMBER: |
| 348 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 349 | |
| 350 | if (isnan(set->val.num)) { |
| 351 | str = strdup("NaN"); |
| 352 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 353 | str = strdup("0"); |
| 354 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 355 | str = strdup("Infinity"); |
| 356 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 357 | str = strdup("-Infinity"); |
| 358 | } else if ((long long)set->val.num == set->val.num) { |
| 359 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 360 | str = NULL; |
| 361 | } |
| 362 | } else { |
| 363 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 364 | str = NULL; |
| 365 | } |
| 366 | } |
| 367 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 368 | |
| 369 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 370 | free(str); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | #endif |
| 375 | |
| 376 | /** |
| 377 | * @brief Realloc the string \p str. |
| 378 | * |
| 379 | * @param[in] ctx libyang context for logging. |
| 380 | * @param[in] needed How much free space is required. |
| 381 | * @param[in,out] str Pointer to the string to use. |
| 382 | * @param[in,out] used Used bytes in \p str. |
| 383 | * @param[in,out] size Allocated bytes in \p str. |
| 384 | * @return LY_ERR |
| 385 | */ |
| 386 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 387 | cast_string_realloc(const struct ly_ctx *ctx, uint64_t needed, char **str, uint32_t *used, uint32_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 388 | { |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 389 | if (*size - (unsigned)*used < needed) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 390 | do { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 391 | if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 392 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 393 | return LY_EINVAL; |
| 394 | } |
| 395 | *size += LYXP_STRING_CAST_SIZE_STEP; |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 396 | } while (*size - (unsigned)*used < needed); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 397 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 398 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 399 | } |
| 400 | |
| 401 | return LY_SUCCESS; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * @brief Cast nodes recursively to one string @p str. |
| 406 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 407 | * @param[in] node Node to cast, NULL if root. |
| 408 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 409 | * @param[in] indent Current indent. |
| 410 | * @param[in,out] str Resulting string. |
| 411 | * @param[in,out] used Used bytes in @p str. |
| 412 | * @param[in,out] size Allocated bytes in @p str. |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 413 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 414 | */ |
| 415 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 416 | cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used, |
| 417 | uint32_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 419 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 420 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 421 | const struct lyd_node *child; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 422 | enum lyxp_node_type child_type; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 423 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 424 | struct lyd_node_any *any; |
| 425 | LY_ERR rc; |
| 426 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 427 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 428 | return LY_SUCCESS; |
| 429 | } |
| 430 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 431 | if (!node) { |
| 432 | /* fake container */ |
| 433 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 434 | strcpy(*str + (*used - 1), "\n"); |
| 435 | ++(*used); |
| 436 | |
| 437 | ++indent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 438 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 439 | /* print all the top-level nodes */ |
| 440 | child = NULL; |
| 441 | child_type = 0; |
| 442 | while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) { |
| 443 | LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 446 | /* end fake container */ |
| 447 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 448 | strcpy(*str + (*used - 1), "\n"); |
| 449 | ++(*used); |
| 450 | |
| 451 | --indent; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 452 | } else { |
| 453 | switch (node->schema->nodetype) { |
| 454 | case LYS_CONTAINER: |
| 455 | case LYS_LIST: |
| 456 | case LYS_RPC: |
| 457 | case LYS_NOTIF: |
| 458 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
| 459 | strcpy(*str + (*used - 1), "\n"); |
| 460 | ++(*used); |
| 461 | |
| 462 | for (child = lyd_child(node); child; child = child->next) { |
| 463 | LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size)); |
| 464 | } |
| 465 | |
| 466 | break; |
| 467 | |
| 468 | case LYS_LEAF: |
| 469 | case LYS_LEAFLIST: |
| 470 | value_str = lyd_get_value(node); |
| 471 | |
| 472 | /* print indent */ |
| 473 | LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size)); |
| 474 | memset(*str + (*used - 1), ' ', indent * 2); |
| 475 | *used += indent * 2; |
| 476 | |
| 477 | /* print value */ |
| 478 | if (*used == 1) { |
| 479 | sprintf(*str + (*used - 1), "%s", value_str); |
| 480 | *used += strlen(value_str); |
| 481 | } else { |
| 482 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 483 | *used += strlen(value_str) + 1; |
| 484 | } |
| 485 | |
| 486 | break; |
| 487 | |
| 488 | case LYS_ANYXML: |
| 489 | case LYS_ANYDATA: |
| 490 | any = (struct lyd_node_any *)node; |
| 491 | if (!(void *)any->value.tree) { |
| 492 | /* no content */ |
| 493 | buf = strdup(""); |
| 494 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 495 | } else { |
| 496 | struct ly_out *out; |
| 497 | |
| 498 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 499 | /* try to parse it into a data tree */ |
| 500 | if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB, |
| 501 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
| 502 | /* successfully parsed */ |
| 503 | free(any->value.mem); |
| 504 | any->value.tree = tree; |
| 505 | any->value_type = LYD_ANYDATA_DATATREE; |
| 506 | } |
| 507 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
| 508 | } |
| 509 | |
| 510 | switch (any->value_type) { |
| 511 | case LYD_ANYDATA_STRING: |
| 512 | case LYD_ANYDATA_XML: |
| 513 | case LYD_ANYDATA_JSON: |
| 514 | buf = strdup(any->value.json); |
| 515 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 516 | break; |
| 517 | case LYD_ANYDATA_DATATREE: |
| 518 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
| 519 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
| 520 | ly_out_free(out, NULL, 0); |
| 521 | LY_CHECK_RET(rc < 0, -rc); |
| 522 | break; |
| 523 | case LYD_ANYDATA_LYB: |
| 524 | LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 525 | return LY_EINVAL; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | line = strtok_r(buf, "\n", &ptr); |
| 530 | do { |
| 531 | rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size); |
| 532 | if (rc != LY_SUCCESS) { |
| 533 | free(buf); |
| 534 | return rc; |
| 535 | } |
| 536 | memset(*str + (*used - 1), ' ', indent * 2); |
| 537 | *used += indent * 2; |
| 538 | |
| 539 | strcpy(*str + (*used - 1), line); |
| 540 | *used += strlen(line); |
| 541 | |
| 542 | strcpy(*str + (*used - 1), "\n"); |
| 543 | *used += 1; |
| 544 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 545 | |
| 546 | free(buf); |
| 547 | break; |
| 548 | |
| 549 | default: |
| 550 | LOGINT_RET(set->ctx); |
| 551 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | return LY_SUCCESS; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @brief Cast an element into a string. |
| 559 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 560 | * @param[in] node Node to cast, NULL if root. |
| 561 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 562 | * @param[out] str Element cast to dynamically-allocated string. |
| 563 | * @return LY_ERR |
| 564 | */ |
| 565 | static LY_ERR |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 566 | cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 567 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 568 | uint32_t used, size; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 569 | LY_ERR rc; |
| 570 | |
| 571 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 572 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 573 | (*str)[0] = '\0'; |
| 574 | used = 1; |
| 575 | size = LYXP_STRING_CAST_SIZE_START; |
| 576 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 577 | rc = cast_string_recursive(node, set, 0, str, &used, &size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 578 | if (rc != LY_SUCCESS) { |
| 579 | free(*str); |
| 580 | return rc; |
| 581 | } |
| 582 | |
| 583 | if (size > used) { |
| 584 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 585 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 586 | } |
| 587 | return LY_SUCCESS; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 592 | * Context position aware. |
| 593 | * |
| 594 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 595 | * @param[out] str Cast dynamically-allocated string. |
| 596 | * @return LY_ERR |
| 597 | */ |
| 598 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 599 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 600 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 601 | if (!set->used) { |
| 602 | *str = strdup(""); |
| 603 | if (!*str) { |
| 604 | LOGMEM_RET(set->ctx); |
| 605 | } |
| 606 | return LY_SUCCESS; |
| 607 | } |
| 608 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 609 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 610 | case LYXP_NODE_NONE: |
| 611 | /* invalid */ |
| 612 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 613 | case LYXP_NODE_ROOT: |
| 614 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 615 | case LYXP_NODE_ELEM: |
| 616 | case LYXP_NODE_TEXT: |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 617 | return cast_string_elem(set->val.nodes[0].node, set, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 618 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 619 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 620 | if (!*str) { |
| 621 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 622 | } |
| 623 | return LY_SUCCESS; |
| 624 | } |
| 625 | |
| 626 | LOGINT_RET(set->ctx); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * @brief Cast a string into an XPath number. |
| 631 | * |
| 632 | * @param[in] str String to use. |
| 633 | * @return Cast number. |
| 634 | */ |
| 635 | static long double |
| 636 | cast_string_to_number(const char *str) |
| 637 | { |
| 638 | long double num; |
| 639 | char *ptr; |
| 640 | |
| 641 | errno = 0; |
| 642 | num = strtold(str, &ptr); |
| 643 | if (errno || *ptr) { |
| 644 | num = NAN; |
| 645 | } |
| 646 | return num; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * @brief Callback for checking value equality. |
| 651 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 652 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 653 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 654 | * @param[in] val1_p First value. |
| 655 | * @param[in] val2_p Second value. |
| 656 | * @param[in] mod Whether hash table is being modified. |
| 657 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 658 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 659 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 660 | static ly_bool |
| 661 | set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 662 | { |
| 663 | struct lyxp_set_hash_node *val1, *val2; |
| 664 | |
| 665 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 666 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 667 | |
| 668 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 669 | return 1; |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * @brief Insert node and its hash into set. |
| 677 | * |
| 678 | * @param[in] set et to insert to. |
| 679 | * @param[in] node Node with hash. |
| 680 | * @param[in] type Node type. |
| 681 | */ |
| 682 | static void |
| 683 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 684 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 685 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 686 | uint32_t i, hash; |
| 687 | struct lyxp_set_hash_node hnode; |
| 688 | |
| 689 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 690 | /* create hash table and add all the nodes */ |
| 691 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 692 | for (i = 0; i < set->used; ++i) { |
| 693 | hnode.node = set->val.nodes[i].node; |
| 694 | hnode.type = set->val.nodes[i].type; |
| 695 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 696 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 697 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 698 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 699 | |
| 700 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 701 | assert(!r); |
| 702 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 703 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 704 | if (hnode.node == node) { |
| 705 | /* it was just added, do not add it twice */ |
| 706 | node = NULL; |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 712 | /* add the new node into hash table */ |
| 713 | hnode.node = node; |
| 714 | hnode.type = type; |
| 715 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 716 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 717 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 718 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 719 | |
| 720 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 721 | assert(!r); |
| 722 | (void)r; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * @brief Remove node and its hash from set. |
| 728 | * |
| 729 | * @param[in] set Set to remove from. |
| 730 | * @param[in] node Node to remove. |
| 731 | * @param[in] type Node type. |
| 732 | */ |
| 733 | static void |
| 734 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 735 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 736 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 737 | struct lyxp_set_hash_node hnode; |
| 738 | uint32_t hash; |
| 739 | |
| 740 | if (set->ht) { |
| 741 | hnode.node = node; |
| 742 | hnode.type = type; |
| 743 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 744 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 745 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 746 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 747 | |
| 748 | r = lyht_remove(set->ht, &hnode, hash); |
| 749 | assert(!r); |
| 750 | (void)r; |
| 751 | |
| 752 | if (!set->ht->used) { |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 753 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 754 | set->ht = NULL; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * @brief Check whether node is in set based on its hash. |
| 761 | * |
| 762 | * @param[in] set Set to search in. |
| 763 | * @param[in] node Node to search for. |
| 764 | * @param[in] type Node type. |
| 765 | * @param[in] skip_idx Index in @p set to skip. |
| 766 | * @return LY_ERR |
| 767 | */ |
| 768 | static LY_ERR |
| 769 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 770 | { |
| 771 | struct lyxp_set_hash_node hnode, *match_p; |
| 772 | uint32_t hash; |
| 773 | |
| 774 | hnode.node = node; |
| 775 | hnode.type = type; |
| 776 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 777 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 778 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 779 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 780 | |
| 781 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 782 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 783 | /* we found it on the index that should be skipped, find another */ |
| 784 | hnode = *match_p; |
| 785 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 786 | /* none other found */ |
| 787 | return LY_SUCCESS; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | return LY_EEXIST; |
| 792 | } |
| 793 | |
| 794 | /* not found */ |
| 795 | return LY_SUCCESS; |
| 796 | } |
| 797 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 798 | void |
| 799 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 800 | { |
| 801 | if (!set) { |
| 802 | return; |
| 803 | } |
| 804 | |
| 805 | if (set->type == LYXP_SET_NODE_SET) { |
| 806 | free(set->val.nodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 807 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 808 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 809 | free(set->val.scnodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 810 | lyht_free(set->ht, NULL); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 811 | } else { |
| 812 | if (set->type == LYXP_SET_STRING) { |
| 813 | free(set->val.str); |
| 814 | } |
| 815 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 816 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 817 | |
| 818 | set->val.nodes = NULL; |
| 819 | set->used = 0; |
| 820 | set->size = 0; |
| 821 | set->ht = NULL; |
| 822 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 823 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 826 | /** |
| 827 | * @brief Free dynamically-allocated set. |
| 828 | * |
| 829 | * @param[in] set Set to free. |
| 830 | */ |
| 831 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 832 | lyxp_set_free(struct lyxp_set *set) |
| 833 | { |
| 834 | if (!set) { |
| 835 | return; |
| 836 | } |
| 837 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 838 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 839 | free(set); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * @brief Initialize set context. |
| 844 | * |
| 845 | * @param[in] new Set to initialize. |
| 846 | * @param[in] set Arbitrary initialized set. |
| 847 | */ |
| 848 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 849 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 850 | { |
| 851 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 852 | if (set) { |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 853 | new->non_child_axis = set->non_child_axis; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 854 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 855 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 856 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 857 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 858 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 859 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 860 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 861 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 862 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 863 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /** |
| 867 | * @brief Create a deep copy of a set. |
| 868 | * |
| 869 | * @param[in] set Set to copy. |
| 870 | * @return Copy of @p set. |
| 871 | */ |
| 872 | static struct lyxp_set * |
| 873 | set_copy(struct lyxp_set *set) |
| 874 | { |
| 875 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 876 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 877 | |
| 878 | if (!set) { |
| 879 | return NULL; |
| 880 | } |
| 881 | |
| 882 | ret = malloc(sizeof *ret); |
| 883 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 884 | set_init(ret, set); |
| 885 | |
| 886 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 887 | ret->type = set->type; |
| 888 | |
| 889 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 890 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 891 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 892 | uint32_t idx; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 893 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 894 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 895 | set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 896 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 897 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 898 | lyxp_set_free(ret); |
| 899 | return NULL; |
| 900 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 901 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 905 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 906 | if (set->used) { |
| 907 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 908 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 909 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 910 | } else { |
| 911 | ret->val.nodes = NULL; |
| 912 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 913 | |
| 914 | ret->used = ret->size = set->used; |
| 915 | ret->ctx_pos = set->ctx_pos; |
| 916 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 917 | if (set->ht) { |
| 918 | ret->ht = lyht_dup(set->ht); |
| 919 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 920 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 921 | memcpy(ret, set, sizeof *ret); |
| 922 | if (set->type == LYXP_SET_STRING) { |
| 923 | ret->val.str = strdup(set->val.str); |
| 924 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 925 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | return ret; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 933 | * |
| 934 | * @param[in] set Set to fill. |
| 935 | * @param[in] string String to fill into \p set. |
| 936 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 937 | */ |
| 938 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 939 | set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 940 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 941 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 942 | |
| 943 | set->type = LYXP_SET_STRING; |
| 944 | if ((str_len == 0) && (string[0] != '\0')) { |
| 945 | string = ""; |
| 946 | } |
| 947 | set->val.str = strndup(string, str_len); |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 952 | * |
| 953 | * @param[in] set Set to fill. |
| 954 | * @param[in] number Number to fill into \p set. |
| 955 | */ |
| 956 | static void |
| 957 | set_fill_number(struct lyxp_set *set, long double number) |
| 958 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 959 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 960 | |
| 961 | set->type = LYXP_SET_NUMBER; |
| 962 | set->val.num = number; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 967 | * |
| 968 | * @param[in] set Set to fill. |
| 969 | * @param[in] boolean Boolean to fill into \p set. |
| 970 | */ |
| 971 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 972 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 973 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 974 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 975 | |
| 976 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 977 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /** |
| 981 | * @brief Fill XPath set with the value from another set (deep assign). |
| 982 | * Any current data are disposed of. |
| 983 | * |
| 984 | * @param[in] trg Set to fill. |
| 985 | * @param[in] src Source set to copy into \p trg. |
| 986 | */ |
| 987 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 988 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 989 | { |
| 990 | if (!trg || !src) { |
| 991 | return; |
| 992 | } |
| 993 | |
| 994 | if (trg->type == LYXP_SET_NODE_SET) { |
| 995 | free(trg->val.nodes); |
| 996 | } else if (trg->type == LYXP_SET_STRING) { |
| 997 | free(trg->val.str); |
| 998 | } |
| 999 | set_init(trg, src); |
| 1000 | |
| 1001 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 1002 | trg->type = LYXP_SET_SCNODE_SET; |
| 1003 | trg->used = src->used; |
| 1004 | trg->size = src->used; |
| 1005 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1006 | if (trg->size) { |
| 1007 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 1008 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1009 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 1010 | } else { |
| 1011 | trg->val.scnodes = NULL; |
| 1012 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1013 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1014 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1015 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1016 | set_fill_number(trg, src->val.num); |
| 1017 | } else if (src->type == LYXP_SET_STRING) { |
| 1018 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 1019 | } else { |
| 1020 | if (trg->type == LYXP_SET_NODE_SET) { |
| 1021 | free(trg->val.nodes); |
| 1022 | } else if (trg->type == LYXP_SET_STRING) { |
| 1023 | free(trg->val.str); |
| 1024 | } |
| 1025 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1026 | assert(src->type == LYXP_SET_NODE_SET); |
| 1027 | |
| 1028 | trg->type = LYXP_SET_NODE_SET; |
| 1029 | trg->used = src->used; |
| 1030 | trg->size = src->used; |
| 1031 | trg->ctx_pos = src->ctx_pos; |
| 1032 | trg->ctx_size = src->ctx_size; |
| 1033 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1034 | if (trg->size) { |
| 1035 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 1036 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1037 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 1038 | } else { |
| 1039 | trg->val.nodes = NULL; |
| 1040 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1041 | if (src->ht) { |
| 1042 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1043 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1044 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * @brief Clear context of all schema nodes. |
| 1051 | * |
| 1052 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1053 | * @param[in] new_ctx New context state for all the nodes currently in the context. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1054 | */ |
| 1055 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1056 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1057 | { |
| 1058 | uint32_t i; |
| 1059 | |
| 1060 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1061 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1062 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1063 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 1064 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * @brief Remove a node from a set. Removing last node changes |
| 1071 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1072 | * |
| 1073 | * @param[in] set Set to use. |
| 1074 | * @param[in] idx Index from @p set of the node to be removed. |
| 1075 | */ |
| 1076 | static void |
| 1077 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1078 | { |
| 1079 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1080 | assert(idx < set->used); |
| 1081 | |
| 1082 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1083 | |
| 1084 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1085 | if (idx < set->used) { |
| 1086 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1087 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1088 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1093 | * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE. |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1094 | * |
| 1095 | * @param[in] set Set to use. |
| 1096 | * @param[in] idx Index from @p set of the node to be removed. |
| 1097 | */ |
| 1098 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1099 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1100 | { |
| 1101 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1102 | assert(idx < set->used); |
| 1103 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1104 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1105 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1106 | } |
| 1107 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1111 | * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1112 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1113 | * |
| 1114 | * @param[in] set Set to consolidate. |
| 1115 | */ |
| 1116 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1117 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1118 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1119 | uint32_t i, orig_used, end = 0; |
| 1120 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1121 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1122 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1123 | |
| 1124 | orig_used = set->used; |
| 1125 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1126 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1127 | start = -1; |
| 1128 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1129 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1130 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1131 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1132 | end = i; |
| 1133 | ++i; |
| 1134 | break; |
| 1135 | } |
| 1136 | |
| 1137 | ++i; |
| 1138 | if (i == orig_used) { |
| 1139 | end = i; |
| 1140 | } |
| 1141 | } while (i < orig_used); |
| 1142 | |
| 1143 | if (start > -1) { |
| 1144 | /* move the whole chunk of valid nodes together */ |
| 1145 | if (set->used != (unsigned)start) { |
| 1146 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1147 | } |
| 1148 | set->used += end - start; |
| 1149 | } |
| 1150 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1154 | * @brief Check for duplicates in a node set. |
| 1155 | * |
| 1156 | * @param[in] set Set to check. |
| 1157 | * @param[in] node Node to look for in @p set. |
| 1158 | * @param[in] node_type Type of @p node. |
| 1159 | * @param[in] skip_idx Index from @p set to skip. |
| 1160 | * @return LY_ERR |
| 1161 | */ |
| 1162 | static LY_ERR |
| 1163 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1164 | { |
| 1165 | uint32_t i; |
| 1166 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1167 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1168 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1169 | } |
| 1170 | |
| 1171 | for (i = 0; i < set->used; ++i) { |
| 1172 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1173 | continue; |
| 1174 | } |
| 1175 | |
| 1176 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1177 | return LY_EEXIST; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | return LY_SUCCESS; |
| 1182 | } |
| 1183 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1184 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1185 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1186 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1187 | { |
| 1188 | uint32_t i; |
| 1189 | |
| 1190 | for (i = 0; i < set->used; ++i) { |
| 1191 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1192 | continue; |
| 1193 | } |
| 1194 | |
| 1195 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1196 | if (index_p) { |
| 1197 | *index_p = i; |
| 1198 | } |
| 1199 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1203 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1206 | void |
| 1207 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1208 | { |
| 1209 | uint32_t orig_used, i, j; |
| 1210 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1211 | assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1212 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1213 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1214 | return; |
| 1215 | } |
| 1216 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1217 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1218 | /* release hidden allocated data (lyxp_set.size) */ |
| 1219 | lyxp_set_free_content(set1); |
| 1220 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1221 | memcpy(set1, set2, sizeof *set1); |
| 1222 | return; |
| 1223 | } |
| 1224 | |
| 1225 | if (set1->used + set2->used > set1->size) { |
| 1226 | set1->size = set1->used + set2->used; |
| 1227 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1228 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1229 | } |
| 1230 | |
| 1231 | orig_used = set1->used; |
| 1232 | |
| 1233 | for (i = 0; i < set2->used; ++i) { |
| 1234 | for (j = 0; j < orig_used; ++j) { |
| 1235 | /* detect duplicities */ |
| 1236 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1237 | break; |
| 1238 | } |
| 1239 | } |
| 1240 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1241 | if (j < orig_used) { |
| 1242 | /* node is there, but update its status if needed */ |
| 1243 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1244 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1245 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1246 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1247 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1248 | } |
| 1249 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1250 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1251 | ++set1->used; |
| 1252 | } |
| 1253 | } |
| 1254 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1255 | lyxp_set_free_content(set2); |
| 1256 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | /** |
| 1260 | * @brief Insert a node into a set. Context position aware. |
| 1261 | * |
| 1262 | * @param[in] set Set to use. |
| 1263 | * @param[in] node Node to insert to @p set. |
| 1264 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1265 | * @param[in] node_type Node type of @p node. |
| 1266 | * @param[in] idx Index in @p set to insert into. |
| 1267 | */ |
| 1268 | static void |
| 1269 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1270 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1271 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1272 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1273 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1274 | /* first item */ |
| 1275 | if (idx) { |
| 1276 | /* no real harm done, but it is a bug */ |
| 1277 | LOGINT(set->ctx); |
| 1278 | idx = 0; |
| 1279 | } |
| 1280 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1281 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1282 | set->type = LYXP_SET_NODE_SET; |
| 1283 | set->used = 0; |
| 1284 | set->size = LYXP_SET_SIZE_START; |
| 1285 | set->ctx_pos = 1; |
| 1286 | set->ctx_size = 1; |
| 1287 | set->ht = NULL; |
| 1288 | } else { |
| 1289 | /* not an empty set */ |
| 1290 | if (set->used == set->size) { |
| 1291 | |
| 1292 | /* set is full */ |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1293 | set->val.nodes = ly_realloc(set->val.nodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1294 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1295 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | if (idx > set->used) { |
| 1299 | LOGINT(set->ctx); |
| 1300 | idx = set->used; |
| 1301 | } |
| 1302 | |
| 1303 | /* make space for the new node */ |
| 1304 | if (idx < set->used) { |
| 1305 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | /* finally assign the value */ |
| 1310 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1311 | set->val.nodes[idx].type = node_type; |
| 1312 | set->val.nodes[idx].pos = pos; |
| 1313 | ++set->used; |
| 1314 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1315 | /* add into hash table */ |
| 1316 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 1319 | LY_ERR |
| 1320 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1321 | enum lyxp_axis axis, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1322 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1323 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1324 | |
| 1325 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1326 | |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1327 | if (!set->size) { |
| 1328 | /* first item */ |
| 1329 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
| 1330 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
| 1331 | set->type = LYXP_SET_SCNODE_SET; |
| 1332 | set->used = 0; |
| 1333 | set->size = LYXP_SET_SIZE_START; |
| 1334 | set->ctx_pos = 1; |
| 1335 | set->ctx_size = 1; |
| 1336 | set->ht = NULL; |
| 1337 | } |
| 1338 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1339 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 1340 | /* BUG if axes differ, this new one is thrown away */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1341 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1342 | } else { |
| 1343 | if (set->used == set->size) { |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1344 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size * LYXP_SET_SIZE_MUL_STEP) * sizeof *set->val.scnodes); |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1345 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1346 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1349 | index = set->used; |
| 1350 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1351 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1352 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1353 | set->val.scnodes[index].axis = axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1354 | ++set->used; |
| 1355 | } |
| 1356 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1357 | if (index_p) { |
| 1358 | *index_p = index; |
| 1359 | } |
| 1360 | |
| 1361 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1365 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1366 | * |
| 1367 | * @param[in] set Set to modify. |
| 1368 | * @return New context value. |
| 1369 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1370 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1371 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1372 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1373 | uint32_t i; |
| 1374 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1375 | |
| 1376 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1377 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1378 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1379 | retry: |
| 1380 | for (i = 0; i < set->used; ++i) { |
| 1381 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1382 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1383 | goto retry; |
| 1384 | } |
| 1385 | } |
| 1386 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1387 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1388 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | return ret_ctx; |
| 1393 | } |
| 1394 | |
| 1395 | /** |
| 1396 | * @brief Get unique @p node position in the data. |
| 1397 | * |
| 1398 | * @param[in] node Node to find. |
| 1399 | * @param[in] node_type Node type of @p node. |
| 1400 | * @param[in] root Root node. |
| 1401 | * @param[in] root_type Type of the XPath @p root node. |
| 1402 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1403 | * be used to increase efficiency and start the DFS from this node. |
| 1404 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1405 | * @return Node position. |
| 1406 | */ |
| 1407 | static uint32_t |
| 1408 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1409 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1410 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1411 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1412 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1413 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1414 | |
| 1415 | assert(prev && prev_pos && !root->prev->next); |
| 1416 | |
| 1417 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1418 | return 0; |
| 1419 | } |
| 1420 | |
| 1421 | if (*prev) { |
| 1422 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1423 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1424 | for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1425 | goto dfs_search; |
| 1426 | } |
| 1427 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1428 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1429 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1430 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1431 | LYD_TREE_DFS_continue = 0; |
| 1432 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1433 | if (*prev && !elem) { |
| 1434 | /* resume previous DFS */ |
| 1435 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1436 | LYD_TREE_DFS_continue = 0; |
| 1437 | } |
| 1438 | |
Michal Vasko | 482a682 | 2022-05-06 08:56:12 +0200 | [diff] [blame] | 1439 | if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1440 | /* skip */ |
| 1441 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1442 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1443 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1444 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1445 | break; |
| 1446 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1447 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1448 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1449 | |
| 1450 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1454 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1455 | break; |
| 1456 | } |
| 1457 | } |
| 1458 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1459 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1460 | if (!(*prev)) { |
| 1461 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1462 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1463 | return 0; |
| 1464 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1465 | /* start the search again from the beginning */ |
| 1466 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1467 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1468 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1469 | pos = 1; |
| 1470 | goto dfs_search; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | /* remember the last found node for next time */ |
| 1475 | *prev = node; |
| 1476 | *prev_pos = pos; |
| 1477 | |
| 1478 | return pos; |
| 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * @brief Assign (fill) missing node positions. |
| 1483 | * |
| 1484 | * @param[in] set Set to fill positions in. |
| 1485 | * @param[in] root Context root node. |
| 1486 | * @param[in] root_type Context root type. |
| 1487 | * @return LY_ERR |
| 1488 | */ |
| 1489 | static LY_ERR |
| 1490 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1491 | { |
| 1492 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1493 | uint32_t i, tmp_pos = 0; |
| 1494 | |
| 1495 | for (i = 0; i < set->used; ++i) { |
| 1496 | if (!set->val.nodes[i].pos) { |
| 1497 | tmp_node = NULL; |
| 1498 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1499 | case LYXP_NODE_META: |
| 1500 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1501 | if (!tmp_node) { |
| 1502 | LOGINT_RET(root->schema->module->ctx); |
| 1503 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1504 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1505 | case LYXP_NODE_ELEM: |
| 1506 | case LYXP_NODE_TEXT: |
| 1507 | if (!tmp_node) { |
| 1508 | tmp_node = set->val.nodes[i].node; |
| 1509 | } |
| 1510 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1511 | break; |
| 1512 | default: |
| 1513 | /* all roots have position 0 */ |
| 1514 | break; |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | return LY_SUCCESS; |
| 1520 | } |
| 1521 | |
| 1522 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1523 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1524 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1525 | * @param[in] meta Metadata to use. |
| 1526 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1527 | */ |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1528 | static uint32_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1529 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1530 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1531 | uint32_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1532 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1533 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1534 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1535 | ++pos; |
| 1536 | } |
| 1537 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1538 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1539 | return pos; |
| 1540 | } |
| 1541 | |
| 1542 | /** |
| 1543 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1544 | * |
| 1545 | * @param[in] item1 1st node. |
| 1546 | * @param[in] item2 2nd node. |
| 1547 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1548 | */ |
| 1549 | static int |
| 1550 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1551 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1552 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1553 | |
| 1554 | if (item1->pos < item2->pos) { |
| 1555 | return -1; |
| 1556 | } |
| 1557 | |
| 1558 | if (item1->pos > item2->pos) { |
| 1559 | return 1; |
| 1560 | } |
| 1561 | |
| 1562 | /* node positions are equal, the fun case */ |
| 1563 | |
| 1564 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1565 | /* special case since text nodes are actually saved as their parents */ |
| 1566 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1567 | if (item1->type == LYXP_NODE_ELEM) { |
| 1568 | assert(item2->type == LYXP_NODE_TEXT); |
| 1569 | return -1; |
| 1570 | } else { |
| 1571 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1572 | return 1; |
| 1573 | } |
| 1574 | } |
| 1575 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1576 | /* we need meta positions now */ |
| 1577 | if (item1->type == LYXP_NODE_META) { |
| 1578 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1579 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1580 | if (item2->type == LYXP_NODE_META) { |
| 1581 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1584 | /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1585 | /* check for duplicates */ |
| 1586 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1587 | assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1588 | return 0; |
| 1589 | } |
| 1590 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1591 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1592 | /* elem is always first, 2nd node is after it */ |
| 1593 | if (item1->type == LYXP_NODE_ELEM) { |
| 1594 | assert(item2->type != LYXP_NODE_ELEM); |
| 1595 | return -1; |
| 1596 | } |
| 1597 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1598 | /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1599 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1600 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1601 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1602 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1603 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1604 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1605 | return 1; |
| 1606 | } |
| 1607 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1608 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1609 | /* 2nd is after 1st */ |
| 1610 | return -1; |
| 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * @brief Set cast for comparisons. |
| 1615 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1616 | * @param[in,out] trg Target set to cast source into. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1617 | * @param[in] src Source set. |
| 1618 | * @param[in] type Target set type. |
| 1619 | * @param[in] src_idx Source set node index. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1620 | * @return LY_SUCCESS on success. |
| 1621 | * @return LY_ERR value on error. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1622 | */ |
| 1623 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1624 | set_comp_cast(struct lyxp_set *trg, const struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1625 | { |
| 1626 | assert(src->type == LYXP_SET_NODE_SET); |
| 1627 | |
| 1628 | set_init(trg, src); |
| 1629 | |
| 1630 | /* insert node into target set */ |
| 1631 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1632 | |
| 1633 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1634 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1635 | } |
| 1636 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1637 | /** |
| 1638 | * @brief Set content canonization for comparisons. |
| 1639 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1640 | * @param[in,out] set Set to canonize. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1641 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1642 | * @return LY_SUCCESS on success. |
| 1643 | * @return LY_ERR value on error. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1644 | */ |
| 1645 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1646 | set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node) |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1647 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1648 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1649 | struct lyd_value val; |
| 1650 | struct ly_err_item *err = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1651 | LY_ERR rc; |
| 1652 | |
| 1653 | /* is there anything to canonize even? */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1654 | if (set->type == LYXP_SET_STRING) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1655 | /* do we have a type to use for canonization? */ |
| 1656 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1657 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1658 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1659 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1660 | } |
| 1661 | } |
| 1662 | if (!type) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1663 | /* no canonization needed/possible */ |
| 1664 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1665 | } |
| 1666 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1667 | /* check for built-in types without required canonization */ |
| 1668 | if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) { |
| 1669 | /* string */ |
| 1670 | return LY_SUCCESS; |
| 1671 | } |
| 1672 | if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) { |
| 1673 | /* boolean */ |
| 1674 | return LY_SUCCESS; |
| 1675 | } |
| 1676 | if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) { |
| 1677 | /* enumeration */ |
| 1678 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1679 | } |
| 1680 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1681 | /* print canonized string, ignore errors, the value may not satisfy schema constraints */ |
| 1682 | rc = type->plugin->store(set->ctx, type, set->val.str, strlen(set->val.str), 0, set->format, set->prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1683 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1684 | ly_err_free(err); |
| 1685 | if (rc) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1686 | /* invalid value, function store automaticaly dealloc value when fail */ |
| 1687 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1688 | } |
| 1689 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1690 | /* use the canonized string value */ |
| 1691 | free(set->val.str); |
| 1692 | set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val)); |
| 1693 | type->plugin->free(set->ctx, &val); |
| 1694 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1695 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1696 | return LY_SUCCESS; |
| 1697 | } |
| 1698 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1699 | /** |
| 1700 | * @brief Bubble sort @p set into XPath document order. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1701 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1702 | * |
| 1703 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1704 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1705 | */ |
| 1706 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1707 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1708 | { |
| 1709 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1710 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1711 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1712 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1713 | struct lyxp_set_node item; |
| 1714 | struct lyxp_set_hash_node hnode; |
| 1715 | uint64_t hash; |
| 1716 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1717 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1718 | return 0; |
| 1719 | } |
| 1720 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1721 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1722 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1723 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1724 | |
| 1725 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1726 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1727 | return -1; |
| 1728 | } |
| 1729 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1730 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1731 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1732 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1733 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1734 | |
| 1735 | for (i = 0; i < set->used; ++i) { |
| 1736 | inverted = 0; |
| 1737 | change = 0; |
| 1738 | |
| 1739 | for (j = 1; j < set->used - i; ++j) { |
| 1740 | /* compare node positions */ |
| 1741 | if (inverted) { |
| 1742 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1743 | } else { |
| 1744 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1745 | } |
| 1746 | |
| 1747 | /* swap if needed */ |
| 1748 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1749 | change = 1; |
| 1750 | |
| 1751 | item = set->val.nodes[j - 1]; |
| 1752 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1753 | set->val.nodes[j] = item; |
| 1754 | } else { |
| 1755 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1756 | inverted = !inverted; |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | ++ret; |
| 1761 | |
| 1762 | if (!change) { |
| 1763 | break; |
| 1764 | } |
| 1765 | } |
| 1766 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1767 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1768 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1769 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1770 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1771 | |
| 1772 | /* check node hashes */ |
| 1773 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1774 | assert(set->ht); |
| 1775 | for (i = 0; i < set->used; ++i) { |
| 1776 | hnode.node = set->val.nodes[i].node; |
| 1777 | hnode.type = set->val.nodes[i].type; |
| 1778 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 1779 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1780 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1781 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1782 | |
| 1783 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | return ret - 1; |
| 1788 | } |
| 1789 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1790 | /** |
| 1791 | * @brief Merge 2 sorted sets into one. |
| 1792 | * |
| 1793 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1794 | * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1795 | * @return LY_ERR |
| 1796 | */ |
| 1797 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1798 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1799 | { |
| 1800 | uint32_t i, j, k, count, dup_count; |
| 1801 | int cmp; |
| 1802 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1803 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1804 | if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1805 | return LY_EINVAL; |
| 1806 | } |
| 1807 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1808 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1809 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1810 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1811 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1812 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1813 | return LY_SUCCESS; |
| 1814 | } |
| 1815 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1816 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0143b68 | 2021-12-13 17:13:09 +0100 | [diff] [blame] | 1817 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1818 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1819 | |
| 1820 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1821 | if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1822 | return LY_EINT; |
| 1823 | } |
| 1824 | |
| 1825 | #ifndef NDEBUG |
| 1826 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1827 | print_set_debug(trg); |
| 1828 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1829 | print_set_debug(src); |
| 1830 | #endif |
| 1831 | |
| 1832 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1833 | * will likely be wasted on them, too bad) */ |
| 1834 | if (trg->size - trg->used < src->used) { |
| 1835 | trg->size = trg->used + src->used; |
| 1836 | |
| 1837 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1838 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1839 | } |
| 1840 | |
| 1841 | i = 0; |
| 1842 | j = 0; |
| 1843 | count = 0; |
| 1844 | dup_count = 0; |
| 1845 | do { |
| 1846 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1847 | if (!cmp) { |
| 1848 | if (!count) { |
| 1849 | /* duplicate, just skip it */ |
| 1850 | ++i; |
| 1851 | ++j; |
| 1852 | } else { |
| 1853 | /* we are copying something already, so let's copy the duplicate too, |
| 1854 | * we are hoping that afterwards there are some more nodes to |
| 1855 | * copy and this way we can copy them all together */ |
| 1856 | ++count; |
| 1857 | ++dup_count; |
| 1858 | ++i; |
| 1859 | ++j; |
| 1860 | } |
| 1861 | } else if (cmp < 0) { |
| 1862 | /* inserting src node into trg, just remember it for now */ |
| 1863 | ++count; |
| 1864 | ++i; |
| 1865 | |
| 1866 | /* insert the hash now */ |
| 1867 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1868 | } else if (count) { |
| 1869 | copy_nodes: |
| 1870 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1871 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1872 | &trg->val.nodes[j], |
| 1873 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1874 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1875 | |
| 1876 | trg->used += count - dup_count; |
| 1877 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1878 | j += count - dup_count; |
| 1879 | |
| 1880 | count = 0; |
| 1881 | dup_count = 0; |
| 1882 | } else { |
| 1883 | ++j; |
| 1884 | } |
| 1885 | } while ((i < src->used) && (j < trg->used)); |
| 1886 | |
| 1887 | if ((i < src->used) || count) { |
| 1888 | /* insert all the hashes first */ |
| 1889 | for (k = i; k < src->used; ++k) { |
| 1890 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1891 | } |
| 1892 | |
| 1893 | /* loop ended, but we need to copy something at trg end */ |
| 1894 | count += src->used - i; |
| 1895 | i = src->used; |
| 1896 | goto copy_nodes; |
| 1897 | } |
| 1898 | |
| 1899 | /* we are inserting hashes before the actual node insert, which causes |
| 1900 | * situations when there were initially not enough items for a hash table, |
| 1901 | * but even after some were inserted, hash table was not created (during |
| 1902 | * insertion the number of items is not updated yet) */ |
| 1903 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1904 | set_insert_node_hash(trg, NULL, 0); |
| 1905 | } |
| 1906 | |
| 1907 | #ifndef NDEBUG |
| 1908 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1909 | print_set_debug(trg); |
| 1910 | #endif |
| 1911 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1912 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1913 | return LY_SUCCESS; |
| 1914 | } |
| 1915 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1916 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1917 | lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1918 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1919 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1920 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1921 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1922 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1923 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1924 | } |
| 1925 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1926 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1927 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1928 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]), |
| 1929 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1930 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1931 | return LY_ENOT; |
| 1932 | } |
| 1933 | |
| 1934 | return LY_SUCCESS; |
| 1935 | } |
| 1936 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1937 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1938 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1939 | { |
| 1940 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1941 | |
| 1942 | /* skip the token */ |
| 1943 | ++(*tok_idx); |
| 1944 | |
| 1945 | return LY_SUCCESS; |
| 1946 | } |
| 1947 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1948 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1949 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1950 | exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok1, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1951 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1952 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1953 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1954 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1955 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1956 | } |
| 1957 | return LY_EINCOMPLETE; |
| 1958 | } |
| 1959 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1960 | if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1961 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1962 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1963 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1964 | } |
| 1965 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | return LY_SUCCESS; |
| 1969 | } |
| 1970 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 1971 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1972 | lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok1, |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 1973 | enum lyxp_token want_tok2) |
| 1974 | { |
| 1975 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 1976 | |
| 1977 | /* skip the token */ |
| 1978 | ++(*tok_idx); |
| 1979 | |
| 1980 | return LY_SUCCESS; |
| 1981 | } |
| 1982 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1983 | /** |
| 1984 | * @brief Stack operation push on the repeat array. |
| 1985 | * |
| 1986 | * @param[in] exp Expression to use. |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 1987 | * @param[in] tok_idx Position in the expresion @p exp. |
| 1988 | * @param[in] repeat_expr_type Repeated expression type, this value is pushed. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | */ |
| 1990 | static void |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 1991 | exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_expr_type repeat_expr_type) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1992 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1993 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1994 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1995 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1996 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1997 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1998 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 1999 | exp->repeat[tok_idx][i] = repeat_expr_type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2000 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2001 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2002 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 2003 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2004 | exp->repeat[tok_idx][0] = repeat_expr_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | /** |
| 2009 | * @brief Reparse Predicate. Logs directly on error. |
| 2010 | * |
| 2011 | * [7] Predicate ::= '[' Expr ']' |
| 2012 | * |
| 2013 | * @param[in] ctx Context for logging. |
| 2014 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2015 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2016 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2017 | * @return LY_ERR |
| 2018 | */ |
| 2019 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2020 | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2021 | { |
| 2022 | LY_ERR rc; |
| 2023 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2024 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2025 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2026 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2027 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2028 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2029 | LY_CHECK_RET(rc); |
| 2030 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2031 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2032 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2033 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2034 | |
| 2035 | return LY_SUCCESS; |
| 2036 | } |
| 2037 | |
| 2038 | /** |
| 2039 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 2040 | * |
| 2041 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 2042 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 2043 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 2044 | * |
| 2045 | * @param[in] ctx Context for logging. |
| 2046 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2047 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2048 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2049 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 2050 | */ |
| 2051 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2052 | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2053 | { |
| 2054 | LY_ERR rc; |
| 2055 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2056 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2057 | LY_CHECK_RET(rc); |
| 2058 | |
| 2059 | goto step; |
| 2060 | do { |
| 2061 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2062 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2064 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2065 | LY_CHECK_RET(rc); |
| 2066 | step: |
| 2067 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2068 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2070 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | break; |
| 2072 | |
| 2073 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2074 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2075 | break; |
| 2076 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2077 | case LYXP_TOKEN_AXISNAME: |
| 2078 | ++(*tok_idx); |
| 2079 | |
| 2080 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON); |
| 2081 | LY_CHECK_RET(rc); |
| 2082 | |
| 2083 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2084 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2085 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2086 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2087 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2088 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2089 | if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2090 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2091 | return LY_EVALID; |
| 2092 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2093 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) { |
| 2094 | goto reparse_nodetype; |
| 2095 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2096 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2097 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2100 | |
| 2101 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2102 | reparse_nodetype: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2103 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2104 | |
| 2105 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2107 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2108 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2109 | |
| 2110 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2111 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2112 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | |
| 2115 | reparse_predicate: |
| 2116 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2117 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2118 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2119 | LY_CHECK_RET(rc); |
| 2120 | } |
| 2121 | break; |
| 2122 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2123 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2124 | return LY_EVALID; |
| 2125 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2126 | } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2127 | |
| 2128 | return LY_SUCCESS; |
| 2129 | } |
| 2130 | |
| 2131 | /** |
| 2132 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2133 | * |
| 2134 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2135 | * |
| 2136 | * @param[in] ctx Context for logging. |
| 2137 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2138 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2139 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2140 | * @return LY_ERR |
| 2141 | */ |
| 2142 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2143 | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2144 | { |
| 2145 | LY_ERR rc; |
| 2146 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2147 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2148 | |
| 2149 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2150 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2151 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2152 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | return LY_SUCCESS; |
| 2156 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | case LYXP_TOKEN_DOT: |
| 2159 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2160 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | case LYXP_TOKEN_AT: |
| 2162 | case LYXP_TOKEN_NAMETEST: |
| 2163 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2164 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2165 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2166 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | default: |
| 2168 | break; |
| 2169 | } |
| 2170 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2171 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2172 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2173 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2174 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2175 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2176 | LY_CHECK_RET(rc); |
| 2177 | } |
| 2178 | |
| 2179 | return LY_SUCCESS; |
| 2180 | } |
| 2181 | |
| 2182 | /** |
| 2183 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2184 | * |
| 2185 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2186 | * |
| 2187 | * @param[in] ctx Context for logging. |
| 2188 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2189 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2190 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2191 | * @return LY_ERR |
| 2192 | */ |
| 2193 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2194 | reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2195 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2196 | int8_t min_arg_count = -1; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2197 | uint32_t arg_count, max_arg_count = 0, func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2198 | LY_ERR rc; |
| 2199 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2200 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2201 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | func_tok_idx = *tok_idx; |
| 2203 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2204 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2205 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2206 | min_arg_count = 1; |
| 2207 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2208 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2209 | min_arg_count = 1; |
| 2210 | max_arg_count = 1; |
| 2211 | } |
| 2212 | break; |
| 2213 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2215 | min_arg_count = 1; |
| 2216 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | min_arg_count = 0; |
| 2219 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2220 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2221 | min_arg_count = 0; |
| 2222 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | min_arg_count = 0; |
| 2225 | max_arg_count = 0; |
| 2226 | } |
| 2227 | break; |
| 2228 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | min_arg_count = 1; |
| 2231 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | min_arg_count = 0; |
| 2234 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2235 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2236 | min_arg_count = 1; |
| 2237 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2239 | min_arg_count = 1; |
| 2240 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2242 | min_arg_count = 1; |
| 2243 | max_arg_count = 1; |
| 2244 | } |
| 2245 | break; |
| 2246 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2248 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2249 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2250 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2251 | min_arg_count = 0; |
| 2252 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2253 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2254 | min_arg_count = 0; |
| 2255 | max_arg_count = 1; |
| 2256 | } |
| 2257 | break; |
| 2258 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | min_arg_count = 1; |
| 2261 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | min_arg_count = 1; |
| 2264 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2265 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2266 | min_arg_count = 0; |
| 2267 | max_arg_count = 0; |
| 2268 | } |
| 2269 | break; |
| 2270 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | min_arg_count = 2; |
| 2273 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2274 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2275 | min_arg_count = 0; |
| 2276 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2277 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2278 | min_arg_count = 2; |
| 2279 | max_arg_count = 2; |
| 2280 | } |
| 2281 | break; |
| 2282 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2283 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2284 | min_arg_count = 2; |
| 2285 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2286 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2287 | min_arg_count = 3; |
| 2288 | max_arg_count = 3; |
| 2289 | } |
| 2290 | break; |
| 2291 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2292 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2293 | min_arg_count = 0; |
| 2294 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2295 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | min_arg_count = 1; |
| 2297 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2298 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2299 | min_arg_count = 2; |
| 2300 | max_arg_count = 2; |
| 2301 | } |
| 2302 | break; |
| 2303 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2304 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2305 | min_arg_count = 2; |
| 2306 | max_arg_count = 2; |
| 2307 | } |
| 2308 | break; |
| 2309 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2310 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2311 | min_arg_count = 2; |
| 2312 | max_arg_count = 2; |
| 2313 | } |
| 2314 | break; |
| 2315 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2316 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2317 | min_arg_count = 0; |
| 2318 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2320 | min_arg_count = 0; |
| 2321 | max_arg_count = 1; |
| 2322 | } |
| 2323 | break; |
| 2324 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2325 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2326 | min_arg_count = 0; |
| 2327 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | min_arg_count = 2; |
| 2330 | max_arg_count = 2; |
| 2331 | } |
| 2332 | break; |
| 2333 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2334 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2335 | min_arg_count = 2; |
| 2336 | max_arg_count = 2; |
| 2337 | } |
| 2338 | break; |
| 2339 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2340 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2341 | min_arg_count = 2; |
| 2342 | max_arg_count = 2; |
| 2343 | } |
| 2344 | break; |
| 2345 | } |
| 2346 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2347 | LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2348 | return LY_EINVAL; |
| 2349 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2350 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2351 | |
| 2352 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2353 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2354 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2355 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2356 | |
| 2357 | /* ( Expr ( ',' Expr )* )? */ |
| 2358 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2359 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2360 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2361 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2362 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2363 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2364 | LY_CHECK_RET(rc); |
| 2365 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2366 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2367 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2368 | |
| 2369 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2370 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2371 | LY_CHECK_RET(rc); |
| 2372 | } |
| 2373 | |
| 2374 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2375 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2376 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2377 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2378 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2379 | if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2380 | LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2381 | return LY_EVALID; |
| 2382 | } |
| 2383 | |
| 2384 | return LY_SUCCESS; |
| 2385 | } |
| 2386 | |
| 2387 | /** |
| 2388 | * @brief Reparse PathExpr. Logs directly on error. |
| 2389 | * |
| 2390 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2391 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2392 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2393 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2394 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2395 | * |
| 2396 | * @param[in] ctx Context for logging. |
| 2397 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2398 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2399 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2400 | * @return LY_ERR |
| 2401 | */ |
| 2402 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2403 | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2404 | { |
| 2405 | LY_ERR rc; |
| 2406 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2407 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2408 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2409 | } |
| 2410 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2411 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2412 | case LYXP_TOKEN_PAR1: |
| 2413 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2414 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2415 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2416 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | LY_CHECK_RET(rc); |
| 2418 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2419 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2420 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2421 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2422 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2423 | case LYXP_TOKEN_DOT: |
| 2424 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2425 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2426 | case LYXP_TOKEN_AT: |
| 2427 | case LYXP_TOKEN_NAMETEST: |
| 2428 | case LYXP_TOKEN_NODETYPE: |
| 2429 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2430 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2431 | LY_CHECK_RET(rc); |
| 2432 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2433 | case LYXP_TOKEN_VARREF: |
| 2434 | /* VariableReference */ |
| 2435 | ++(*tok_idx); |
| 2436 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2437 | case LYXP_TOKEN_FUNCNAME: |
| 2438 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2439 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2440 | LY_CHECK_RET(rc); |
| 2441 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2442 | case LYXP_TOKEN_OPER_PATH: |
| 2443 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2444 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2445 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2446 | LY_CHECK_RET(rc); |
| 2447 | break; |
| 2448 | case LYXP_TOKEN_LITERAL: |
| 2449 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2450 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2451 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2452 | case LYXP_TOKEN_NUMBER: |
| 2453 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2454 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2455 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2456 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2457 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2458 | return LY_EVALID; |
| 2459 | } |
| 2460 | |
| 2461 | return LY_SUCCESS; |
| 2462 | |
| 2463 | predicate: |
| 2464 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2465 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2466 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2467 | LY_CHECK_RET(rc); |
| 2468 | } |
| 2469 | |
| 2470 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2471 | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2472 | |
| 2473 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2474 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2475 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2476 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2477 | LY_CHECK_RET(rc); |
| 2478 | } |
| 2479 | |
| 2480 | return LY_SUCCESS; |
| 2481 | } |
| 2482 | |
| 2483 | /** |
| 2484 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2485 | * |
| 2486 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2487 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2488 | * |
| 2489 | * @param[in] ctx Context for logging. |
| 2490 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2491 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2492 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2493 | * @return LY_ERR |
| 2494 | */ |
| 2495 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2496 | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2497 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2498 | uint32_t prev_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2499 | LY_ERR rc; |
| 2500 | |
| 2501 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2502 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2503 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2504 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2505 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2506 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2510 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2511 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2512 | LY_CHECK_RET(rc); |
| 2513 | |
| 2514 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2516 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2517 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2518 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2519 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2520 | LY_CHECK_RET(rc); |
| 2521 | } |
| 2522 | |
| 2523 | return LY_SUCCESS; |
| 2524 | } |
| 2525 | |
| 2526 | /** |
| 2527 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2528 | * |
| 2529 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2530 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2531 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2532 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2533 | * | MultiplicativeExpr '*' UnaryExpr |
| 2534 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2535 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2536 | * |
| 2537 | * @param[in] ctx Context for logging. |
| 2538 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2539 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2540 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2541 | * @return LY_ERR |
| 2542 | */ |
| 2543 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2544 | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2545 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2546 | uint32_t prev_add_exp, prev_mul_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2547 | LY_ERR rc; |
| 2548 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2549 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2550 | goto reparse_multiplicative_expr; |
| 2551 | |
| 2552 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2553 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2554 | ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2555 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2556 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2557 | |
| 2558 | reparse_multiplicative_expr: |
| 2559 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2560 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2561 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2562 | LY_CHECK_RET(rc); |
| 2563 | |
| 2564 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2565 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2566 | ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2567 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2568 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2569 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2570 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2571 | LY_CHECK_RET(rc); |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | return LY_SUCCESS; |
| 2576 | } |
| 2577 | |
| 2578 | /** |
| 2579 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2580 | * |
| 2581 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2582 | * | EqualityExpr '!=' RelationalExpr |
| 2583 | * [14] RelationalExpr ::= AdditiveExpr |
| 2584 | * | RelationalExpr '<' AdditiveExpr |
| 2585 | * | RelationalExpr '>' AdditiveExpr |
| 2586 | * | RelationalExpr '<=' AdditiveExpr |
| 2587 | * | RelationalExpr '>=' AdditiveExpr |
| 2588 | * |
| 2589 | * @param[in] ctx Context for logging. |
| 2590 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2591 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2592 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2593 | * @return LY_ERR |
| 2594 | */ |
| 2595 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2596 | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2597 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2598 | uint32_t prev_eq_exp, prev_rel_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2599 | LY_ERR rc; |
| 2600 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2601 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2602 | goto reparse_additive_expr; |
| 2603 | |
| 2604 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2605 | while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2606 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2607 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2608 | |
| 2609 | reparse_additive_expr: |
| 2610 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2611 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2612 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2613 | LY_CHECK_RET(rc); |
| 2614 | |
| 2615 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2616 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2618 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2619 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2620 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2621 | LY_CHECK_RET(rc); |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | return LY_SUCCESS; |
| 2626 | } |
| 2627 | |
| 2628 | /** |
| 2629 | * @brief Reparse OrExpr. Logs directly on error. |
| 2630 | * |
| 2631 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2632 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2633 | * |
| 2634 | * @param[in] ctx Context for logging. |
| 2635 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2636 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2637 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2638 | * @return LY_ERR |
| 2639 | */ |
| 2640 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2641 | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2642 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2643 | uint32_t prev_or_exp, prev_and_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2644 | LY_ERR rc; |
| 2645 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2646 | ++depth; |
| 2647 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2648 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2649 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2650 | goto reparse_equality_expr; |
| 2651 | |
| 2652 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2653 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2654 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2655 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2656 | |
| 2657 | reparse_equality_expr: |
| 2658 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2659 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2660 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2661 | LY_CHECK_RET(rc); |
| 2662 | |
| 2663 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2664 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2665 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2666 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2667 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2668 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2669 | LY_CHECK_RET(rc); |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | return LY_SUCCESS; |
| 2674 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2675 | |
| 2676 | /** |
| 2677 | * @brief Parse NCName. |
| 2678 | * |
| 2679 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2680 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2681 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2682 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2683 | parse_ncname(const char *ncname) |
| 2684 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2685 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2686 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2687 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2688 | |
| 2689 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2690 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2691 | return len; |
| 2692 | } |
| 2693 | |
| 2694 | do { |
| 2695 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2696 | if (!*ncname) { |
| 2697 | break; |
| 2698 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2699 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2700 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2701 | |
| 2702 | return len; |
| 2703 | } |
| 2704 | |
| 2705 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2706 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2707 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2708 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2709 | * @param[in] exp Expression to use. |
| 2710 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2711 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2712 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2713 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2714 | */ |
| 2715 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2716 | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint32_t tok_pos, uint32_t tok_len) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2717 | { |
| 2718 | uint32_t prev; |
| 2719 | |
| 2720 | if (exp->used == exp->size) { |
| 2721 | prev = exp->size; |
| 2722 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2723 | if (prev > exp->size) { |
| 2724 | LOGINT(ctx); |
| 2725 | return LY_EINT; |
| 2726 | } |
| 2727 | |
| 2728 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2729 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2730 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2731 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2732 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2733 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2734 | } |
| 2735 | |
| 2736 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2737 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2738 | exp->tok_len[exp->used] = tok_len; |
| 2739 | ++exp->used; |
| 2740 | return LY_SUCCESS; |
| 2741 | } |
| 2742 | |
| 2743 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2744 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2745 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2746 | uint32_t i; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2747 | |
| 2748 | if (!expr) { |
| 2749 | return; |
| 2750 | } |
| 2751 | |
| 2752 | lydict_remove(ctx, expr->expr); |
| 2753 | free(expr->tokens); |
| 2754 | free(expr->tok_pos); |
| 2755 | free(expr->tok_len); |
| 2756 | if (expr->repeat) { |
| 2757 | for (i = 0; i < expr->used; ++i) { |
| 2758 | free(expr->repeat[i]); |
| 2759 | } |
| 2760 | } |
| 2761 | free(expr->repeat); |
| 2762 | free(expr); |
| 2763 | } |
| 2764 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2765 | /** |
| 2766 | * @brief Parse Axis name. |
| 2767 | * |
| 2768 | * @param[in] str String to parse. |
| 2769 | * @param[in] str_len Length of @p str. |
| 2770 | * @return LY_SUCCESS if an axis. |
| 2771 | * @return LY_ENOT otherwise. |
| 2772 | */ |
| 2773 | static LY_ERR |
| 2774 | expr_parse_axis(const char *str, size_t str_len) |
| 2775 | { |
| 2776 | switch (str_len) { |
| 2777 | case 4: |
| 2778 | if (!strncmp("self", str, str_len)) { |
| 2779 | return LY_SUCCESS; |
| 2780 | } |
| 2781 | break; |
| 2782 | case 5: |
| 2783 | if (!strncmp("child", str, str_len)) { |
| 2784 | return LY_SUCCESS; |
| 2785 | } |
| 2786 | break; |
| 2787 | case 6: |
| 2788 | if (!strncmp("parent", str, str_len)) { |
| 2789 | return LY_SUCCESS; |
| 2790 | } |
| 2791 | break; |
| 2792 | case 8: |
| 2793 | if (!strncmp("ancestor", str, str_len)) { |
| 2794 | return LY_SUCCESS; |
| 2795 | } |
| 2796 | break; |
| 2797 | case 9: |
| 2798 | if (!strncmp("attribute", str, str_len)) { |
| 2799 | return LY_SUCCESS; |
| 2800 | } else if (!strncmp("following", str, str_len)) { |
| 2801 | return LY_SUCCESS; |
| 2802 | } else if (!strncmp("namespace", str, str_len)) { |
| 2803 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2804 | return LY_ENOT; |
| 2805 | } else if (!strncmp("preceding", str, str_len)) { |
| 2806 | return LY_SUCCESS; |
| 2807 | } |
| 2808 | break; |
| 2809 | case 10: |
| 2810 | if (!strncmp("descendant", str, str_len)) { |
| 2811 | return LY_SUCCESS; |
| 2812 | } |
| 2813 | break; |
| 2814 | case 16: |
| 2815 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2816 | return LY_SUCCESS; |
| 2817 | } |
| 2818 | break; |
| 2819 | case 17: |
| 2820 | if (!strncmp("following-sibling", str, str_len)) { |
| 2821 | return LY_SUCCESS; |
| 2822 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2823 | return LY_SUCCESS; |
| 2824 | } |
| 2825 | break; |
| 2826 | case 18: |
| 2827 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2828 | return LY_SUCCESS; |
| 2829 | } |
| 2830 | break; |
| 2831 | } |
| 2832 | |
| 2833 | return LY_ENOT; |
| 2834 | } |
| 2835 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2836 | LY_ERR |
| 2837 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2838 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2839 | LY_ERR ret = LY_SUCCESS; |
| 2840 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2841 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2842 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2843 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2844 | uint32_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2845 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2846 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2847 | assert(expr_p); |
| 2848 | |
| 2849 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2850 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2851 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2855 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2856 | } |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2857 | if (expr_len > UINT32_MAX) { |
| 2858 | LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2859 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2863 | expr = calloc(1, sizeof *expr); |
| 2864 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2865 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2866 | expr->used = 0; |
| 2867 | expr->size = LYXP_EXPR_SIZE_START; |
| 2868 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2869 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2870 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2871 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2872 | LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2873 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2874 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2875 | LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2876 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2877 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2878 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2879 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2880 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2881 | ++parsed; |
| 2882 | } |
| 2883 | |
| 2884 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2885 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2886 | |
| 2887 | /* '(' */ |
| 2888 | tok_len = 1; |
| 2889 | tok_type = LYXP_TOKEN_PAR1; |
| 2890 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2891 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2892 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2893 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2894 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2895 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2896 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2897 | /* it is NodeType after all */ |
| 2898 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2899 | |
| 2900 | prev_ntype_check = 0; |
| 2901 | prev_func_check = 0; |
| 2902 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2903 | /* it is FunctionName after all */ |
| 2904 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2905 | |
| 2906 | prev_ntype_check = 0; |
| 2907 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2908 | } |
| 2909 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2910 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2911 | |
| 2912 | /* ')' */ |
| 2913 | tok_len = 1; |
| 2914 | tok_type = LYXP_TOKEN_PAR2; |
| 2915 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2916 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2917 | |
| 2918 | /* '[' */ |
| 2919 | tok_len = 1; |
| 2920 | tok_type = LYXP_TOKEN_BRACK1; |
| 2921 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2922 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2923 | |
| 2924 | /* ']' */ |
| 2925 | tok_len = 1; |
| 2926 | tok_type = LYXP_TOKEN_BRACK2; |
| 2927 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2928 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2929 | |
| 2930 | /* '..' */ |
| 2931 | tok_len = 2; |
| 2932 | tok_type = LYXP_TOKEN_DDOT; |
| 2933 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2934 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2935 | |
| 2936 | /* '.' */ |
| 2937 | tok_len = 1; |
| 2938 | tok_type = LYXP_TOKEN_DOT; |
| 2939 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2940 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2941 | |
| 2942 | /* '@' */ |
| 2943 | tok_len = 1; |
| 2944 | tok_type = LYXP_TOKEN_AT; |
| 2945 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2946 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2947 | |
| 2948 | /* ',' */ |
| 2949 | tok_len = 1; |
| 2950 | tok_type = LYXP_TOKEN_COMMA; |
| 2951 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2952 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2953 | |
| 2954 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2955 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2956 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2957 | LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2958 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2959 | ++tok_len; |
| 2960 | tok_type = LYXP_TOKEN_LITERAL; |
| 2961 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2962 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2963 | |
| 2964 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2965 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2966 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2967 | LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2968 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2969 | ++tok_len; |
| 2970 | tok_type = LYXP_TOKEN_LITERAL; |
| 2971 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2972 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2973 | |
| 2974 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2975 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2976 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2977 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2978 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2979 | } |
| 2980 | tok_type = LYXP_TOKEN_NUMBER; |
| 2981 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2982 | } else if (expr_str[parsed] == '$') { |
| 2983 | |
| 2984 | /* VariableReference */ |
| 2985 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2986 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2987 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 2988 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 2989 | tok_len = ncname_len; |
| 2990 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 2991 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 2992 | error); |
| 2993 | tok_type = LYXP_TOKEN_VARREF; |
| 2994 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2995 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2996 | |
| 2997 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2998 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2999 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3000 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3001 | } else { |
| 3002 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3003 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3004 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3005 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3006 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3007 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3008 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3009 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3010 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 3011 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3012 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3013 | |
| 3014 | /* Operator '<=', '>=' */ |
| 3015 | tok_len = 2; |
| 3016 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3017 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3018 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3019 | |
| 3020 | /* Operator '|' */ |
| 3021 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3022 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3023 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3024 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3025 | |
| 3026 | /* Operator '+', '-' */ |
| 3027 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3028 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3029 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3030 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3031 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3032 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3033 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3034 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3035 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3036 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3037 | |
| 3038 | /* Operator '<', '>' */ |
| 3039 | tok_len = 1; |
| 3040 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3041 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3042 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3043 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3044 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3045 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3046 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3047 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3048 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3049 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3050 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3051 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3052 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3053 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3054 | |
| 3055 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3056 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3057 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3058 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3059 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3060 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3061 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3062 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3063 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3064 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3065 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3066 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3067 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3068 | } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3069 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3070 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3071 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3072 | } else if (prev_ntype_check || prev_func_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3073 | LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3074 | expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3075 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3076 | goto error; |
| 3077 | } else { |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3078 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3079 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3080 | goto error; |
| 3081 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3082 | } else { |
| 3083 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3084 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3085 | if (expr_str[parsed] == '*') { |
| 3086 | ncname_len = 1; |
| 3087 | } else { |
| 3088 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3089 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3090 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3091 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3092 | tok_len = ncname_len; |
| 3093 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3094 | has_axis = 0; |
| 3095 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3096 | /* axis */ |
| 3097 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
| 3098 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error); |
| 3099 | tok_type = LYXP_TOKEN_AXISNAME; |
| 3100 | |
| 3101 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3102 | parsed += tok_len; |
| 3103 | |
| 3104 | /* '::' */ |
| 3105 | tok_len = 2; |
| 3106 | tok_type = LYXP_TOKEN_DCOLON; |
| 3107 | |
| 3108 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3109 | parsed += tok_len; |
| 3110 | |
| 3111 | if (expr_str[parsed] == '*') { |
| 3112 | ncname_len = 1; |
| 3113 | } else { |
| 3114 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3115 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3116 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3117 | } |
| 3118 | tok_len = ncname_len; |
| 3119 | |
| 3120 | has_axis = 1; |
| 3121 | } |
| 3122 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3123 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3124 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3125 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3126 | ++tok_len; |
| 3127 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3128 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 3129 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3130 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3131 | tok_len += ncname_len; |
| 3132 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3133 | /* remove old flags to prevent ambiguities */ |
| 3134 | prev_ntype_check = 0; |
| 3135 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3136 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3137 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3138 | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 3139 | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
| 3140 | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3141 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3146 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3147 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3148 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3149 | ++parsed; |
| 3150 | } |
| 3151 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3152 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3153 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3154 | if (reparse) { |
| 3155 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3156 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 3157 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3158 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3159 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 3160 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3161 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3162 | LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3163 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3164 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3165 | goto error; |
| 3166 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3167 | } |
| 3168 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3169 | print_expr_struct_debug(expr); |
| 3170 | *expr_p = expr; |
| 3171 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3172 | |
| 3173 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3174 | lyxp_expr_free(ctx, expr); |
| 3175 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3176 | } |
| 3177 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3178 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3179 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t start_idx, uint32_t end_idx, |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3180 | struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3181 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3182 | LY_ERR ret = LY_SUCCESS; |
| 3183 | struct lyxp_expr *dup = NULL; |
Michal Vasko | d59039d | 2022-09-09 09:07:30 +0200 | [diff] [blame] | 3184 | uint32_t used = 0, i, j, expr_len; |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3185 | const char *expr_start; |
| 3186 | |
| 3187 | assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx))); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3188 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3189 | if (!exp) { |
| 3190 | goto cleanup; |
| 3191 | } |
| 3192 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3193 | if (!start_idx && !end_idx) { |
| 3194 | end_idx = exp->used - 1; |
| 3195 | } |
| 3196 | |
| 3197 | expr_start = exp->expr + exp->tok_pos[start_idx]; |
| 3198 | expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx]; |
| 3199 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3200 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3201 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3202 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3203 | if (exp->used) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3204 | used = (end_idx - start_idx) + 1; |
| 3205 | |
| 3206 | dup->tokens = malloc(used * sizeof *dup->tokens); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3207 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3208 | memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3209 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3210 | dup->tok_pos = malloc(used * sizeof *dup->tok_pos); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3211 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3212 | memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3213 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3214 | if (start_idx) { |
| 3215 | /* fix the indices in the expression */ |
| 3216 | for (i = 0; i < used; ++i) { |
| 3217 | dup->tok_pos[i] -= expr_start - exp->expr; |
| 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | dup->tok_len = malloc(used * sizeof *dup->tok_len); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3222 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3223 | memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3224 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3225 | if (exp->repeat) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3226 | dup->repeat = malloc(used * sizeof *dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3227 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3228 | for (i = start_idx; i <= end_idx; ++i) { |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3229 | if (!exp->repeat[i]) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3230 | dup->repeat[i - start_idx] = NULL; |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3231 | } else { |
| 3232 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3233 | /* the ending 0 as well */ |
| 3234 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3235 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3236 | dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat); |
| 3237 | LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3238 | memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3239 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3240 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3241 | } |
| 3242 | } |
| 3243 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3244 | dup->used = used; |
| 3245 | dup->size = used; |
| 3246 | |
| 3247 | /* copy only subexpression */ |
| 3248 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3249 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3250 | cleanup: |
| 3251 | if (ret) { |
| 3252 | lyxp_expr_free(ctx, dup); |
| 3253 | } else { |
| 3254 | *dup_p = dup; |
| 3255 | } |
| 3256 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3257 | } |
| 3258 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3259 | /** |
| 3260 | * @brief Get the last-added schema node that is currently in the context. |
| 3261 | * |
| 3262 | * @param[in] set Set to search in. |
| 3263 | * @return Last-added schema context node, NULL if no node is in context. |
| 3264 | */ |
| 3265 | static struct lysc_node * |
| 3266 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3267 | { |
| 3268 | uint32_t i; |
| 3269 | |
| 3270 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3271 | return NULL; |
| 3272 | } |
| 3273 | |
| 3274 | i = set->used; |
| 3275 | do { |
| 3276 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3277 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3278 | /* if there are more, simply return the first found (last added) */ |
| 3279 | return set->val.scnodes[i].scnode; |
| 3280 | } |
| 3281 | } while (i); |
| 3282 | |
| 3283 | return NULL; |
| 3284 | } |
| 3285 | |
| 3286 | /** |
| 3287 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3288 | * |
| 3289 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3290 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3291 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3292 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3293 | warn_is_numeric_type(struct lysc_type *type) |
| 3294 | { |
| 3295 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3296 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3297 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3298 | |
| 3299 | switch (type->basetype) { |
| 3300 | case LY_TYPE_DEC64: |
| 3301 | case LY_TYPE_INT8: |
| 3302 | case LY_TYPE_UINT8: |
| 3303 | case LY_TYPE_INT16: |
| 3304 | case LY_TYPE_UINT16: |
| 3305 | case LY_TYPE_INT32: |
| 3306 | case LY_TYPE_UINT32: |
| 3307 | case LY_TYPE_INT64: |
| 3308 | case LY_TYPE_UINT64: |
| 3309 | return 1; |
| 3310 | case LY_TYPE_UNION: |
| 3311 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3312 | LY_ARRAY_FOR(uni->types, u) { |
| 3313 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3314 | if (ret) { |
| 3315 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3316 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3317 | } |
| 3318 | } |
| 3319 | /* did not find any suitable type */ |
| 3320 | return 0; |
| 3321 | case LY_TYPE_LEAFREF: |
| 3322 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3323 | default: |
| 3324 | return 0; |
| 3325 | } |
| 3326 | } |
| 3327 | |
| 3328 | /** |
| 3329 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3330 | * |
| 3331 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3332 | * @return Boolean value whether @p type's basetype is string type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3333 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3334 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3335 | warn_is_string_type(struct lysc_type *type) |
| 3336 | { |
| 3337 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3338 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3339 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3340 | |
| 3341 | switch (type->basetype) { |
| 3342 | case LY_TYPE_BITS: |
| 3343 | case LY_TYPE_ENUM: |
| 3344 | case LY_TYPE_IDENT: |
| 3345 | case LY_TYPE_INST: |
| 3346 | case LY_TYPE_STRING: |
| 3347 | return 1; |
| 3348 | case LY_TYPE_UNION: |
| 3349 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3350 | LY_ARRAY_FOR(uni->types, u) { |
| 3351 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3352 | if (ret) { |
| 3353 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3354 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3355 | } |
| 3356 | } |
| 3357 | /* did not find any suitable type */ |
| 3358 | return 0; |
| 3359 | case LY_TYPE_LEAFREF: |
| 3360 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3361 | default: |
| 3362 | return 0; |
| 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | /** |
| 3367 | * @brief Test whether a type is one specific type. |
| 3368 | * |
| 3369 | * @param[in] type Type to test. |
| 3370 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3371 | * @return Boolean value whether the given @p type is of the specific basetype @p base. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3372 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3373 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3374 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3375 | { |
| 3376 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3377 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3378 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3379 | |
| 3380 | if (type->basetype == base) { |
| 3381 | return 1; |
| 3382 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3383 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3384 | LY_ARRAY_FOR(uni->types, u) { |
| 3385 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3386 | if (ret) { |
| 3387 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3388 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3389 | } |
| 3390 | } |
| 3391 | /* did not find any suitable type */ |
| 3392 | return 0; |
| 3393 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3394 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3395 | } |
| 3396 | |
| 3397 | return 0; |
| 3398 | } |
| 3399 | |
| 3400 | /** |
| 3401 | * @brief Get next type of a (union) type. |
| 3402 | * |
| 3403 | * @param[in] type Base type. |
| 3404 | * @param[in] prev_type Previously returned type. |
| 3405 | * @return Next type or NULL. |
| 3406 | */ |
| 3407 | static struct lysc_type * |
| 3408 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3409 | { |
| 3410 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3411 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3412 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3413 | |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3414 | if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3415 | uni = (struct lysc_type_union *)type; |
| 3416 | if (!prev_type) { |
| 3417 | return uni->types[0]; |
| 3418 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3419 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3420 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3421 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3422 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3423 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3424 | found = 1; |
| 3425 | } |
| 3426 | } |
| 3427 | return NULL; |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3428 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3429 | if (prev_type) { |
| 3430 | assert(type == prev_type); |
| 3431 | return NULL; |
| 3432 | } else { |
| 3433 | return type; |
| 3434 | } |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | /** |
| 3439 | * @brief Test whether 2 types have a common type. |
| 3440 | * |
| 3441 | * @param[in] type1 First type. |
| 3442 | * @param[in] type2 Second type. |
| 3443 | * @return 1 if they do, 0 otherwise. |
| 3444 | */ |
| 3445 | static int |
| 3446 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3447 | { |
| 3448 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3449 | |
| 3450 | t1 = NULL; |
| 3451 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3452 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3453 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3454 | } else { |
| 3455 | rt1 = t1; |
| 3456 | } |
| 3457 | |
| 3458 | t2 = NULL; |
| 3459 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3460 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3461 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3462 | } else { |
| 3463 | rt2 = t2; |
| 3464 | } |
| 3465 | |
| 3466 | if (rt2->basetype == rt1->basetype) { |
| 3467 | /* match found */ |
| 3468 | return 1; |
| 3469 | } |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | return 0; |
| 3474 | } |
| 3475 | |
| 3476 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3477 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3478 | * |
| 3479 | * @param[in] ctx Context for logging. |
| 3480 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3481 | * @param[in] subexpr Subexpression start. |
| 3482 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3483 | * @param[in] cur_scnode Expression context node. |
| 3484 | */ |
| 3485 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3486 | warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len, |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3487 | const struct lysc_node *cur_scnode) |
| 3488 | { |
| 3489 | char *path; |
| 3490 | |
| 3491 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3492 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".", |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3493 | tok_pos, subexpr_len, subexpr, path); |
| 3494 | free(path); |
| 3495 | } |
| 3496 | |
| 3497 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3498 | * @brief Check both operands of comparison operators. |
| 3499 | * |
| 3500 | * @param[in] ctx Context for errors. |
| 3501 | * @param[in] set1 First operand set. |
| 3502 | * @param[in] set2 Second operand set. |
| 3503 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3504 | * @param[in] expr Start of the expression to print with the warning. |
| 3505 | * @param[in] tok_pos Token position. |
| 3506 | */ |
| 3507 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3508 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3509 | uint32_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3510 | { |
| 3511 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3512 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3513 | |
| 3514 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3515 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3516 | |
| 3517 | if (!node1 && !node2) { |
| 3518 | /* no node-sets involved, nothing to do */ |
| 3519 | return; |
| 3520 | } |
| 3521 | |
| 3522 | if (node1) { |
| 3523 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3524 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3525 | warning = 1; |
| 3526 | leaves = 0; |
| 3527 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3528 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3529 | warning = 1; |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | if (node2) { |
| 3534 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3535 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3536 | warning = 1; |
| 3537 | leaves = 0; |
| 3538 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3539 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3540 | warning = 1; |
| 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3545 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3546 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3547 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3548 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3549 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3550 | warning = 1; |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3555 | warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | /** |
| 3560 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3561 | * |
| 3562 | * @param[in] exp Parsed XPath expression. |
| 3563 | * @param[in] set Set with the leaf/leaf-list. |
| 3564 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3565 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3566 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3567 | */ |
| 3568 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3569 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp, |
| 3570 | uint32_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3571 | { |
| 3572 | struct lysc_node *scnode; |
| 3573 | struct lysc_type *type; |
| 3574 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3575 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3576 | LY_ERR rc; |
| 3577 | struct ly_err_item *err = NULL; |
| 3578 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3579 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3580 | ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3581 | /* check that the node can have the specified value */ |
| 3582 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3583 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3584 | } else { |
| 3585 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3586 | } |
| 3587 | if (!value) { |
| 3588 | LOGMEM(set->ctx); |
| 3589 | return; |
| 3590 | } |
| 3591 | |
| 3592 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3593 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3594 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3595 | warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp], |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3596 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3597 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3601 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3602 | rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 3603 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3604 | if (rc == LY_EINCOMPLETE) { |
| 3605 | rc = LY_SUCCESS; |
| 3606 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3607 | |
| 3608 | if (err) { |
| 3609 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3610 | ly_err_free(err); |
| 3611 | } else if (rc != LY_SUCCESS) { |
| 3612 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3613 | } |
| 3614 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3615 | warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp], |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3616 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3617 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3618 | } else { |
| 3619 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3620 | } |
| 3621 | } |
| 3622 | free(value); |
| 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | /* |
| 3627 | * XPath functions |
| 3628 | */ |
| 3629 | |
| 3630 | /** |
| 3631 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3632 | * depending on whether the first node bit value from the second argument is set. |
| 3633 | * |
| 3634 | * @param[in] args Array of arguments. |
| 3635 | * @param[in] arg_count Count of elements in @p args. |
| 3636 | * @param[in,out] set Context and result set at the same time. |
| 3637 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3638 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3639 | */ |
| 3640 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3641 | xpath_bit_is_set(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3642 | { |
| 3643 | struct lyd_node_term *leaf; |
| 3644 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3645 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3646 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3647 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3648 | |
| 3649 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3650 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3651 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3652 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3653 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3654 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3655 | sleaf->name); |
| 3656 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3657 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3658 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3662 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3663 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3664 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3665 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3666 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3667 | } |
| 3668 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3669 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3670 | return rc; |
| 3671 | } |
| 3672 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3673 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3674 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3675 | return LY_EVALID; |
| 3676 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3677 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3678 | LY_CHECK_RET(rc); |
| 3679 | |
| 3680 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3681 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3682 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3683 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3684 | LYD_VALUE_GET(&leaf->value, bits); |
| 3685 | LY_ARRAY_FOR(bits->items, u) { |
| 3686 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3687 | set_fill_boolean(set, 1); |
| 3688 | break; |
| 3689 | } |
| 3690 | } |
| 3691 | } |
| 3692 | } |
| 3693 | |
| 3694 | return LY_SUCCESS; |
| 3695 | } |
| 3696 | |
| 3697 | /** |
| 3698 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3699 | * with the argument converted to boolean. |
| 3700 | * |
| 3701 | * @param[in] args Array of arguments. |
| 3702 | * @param[in] arg_count Count of elements in @p args. |
| 3703 | * @param[in,out] set Context and result set at the same time. |
| 3704 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3705 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3706 | */ |
| 3707 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3708 | xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3709 | { |
| 3710 | LY_ERR rc; |
| 3711 | |
| 3712 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3713 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3714 | return LY_SUCCESS; |
| 3715 | } |
| 3716 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3717 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3718 | LY_CHECK_RET(rc); |
| 3719 | set_fill_set(set, args[0]); |
| 3720 | |
| 3721 | return LY_SUCCESS; |
| 3722 | } |
| 3723 | |
| 3724 | /** |
| 3725 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3726 | * with the first argument rounded up to the nearest integer. |
| 3727 | * |
| 3728 | * @param[in] args Array of arguments. |
| 3729 | * @param[in] arg_count Count of elements in @p args. |
| 3730 | * @param[in,out] set Context and result set at the same time. |
| 3731 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3732 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3733 | */ |
| 3734 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3735 | xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3736 | { |
| 3737 | struct lysc_node_leaf *sleaf; |
| 3738 | LY_ERR rc = LY_SUCCESS; |
| 3739 | |
| 3740 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3741 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3742 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3743 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3744 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3745 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3746 | sleaf->name); |
| 3747 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3748 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3749 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3750 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3751 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3752 | return rc; |
| 3753 | } |
| 3754 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3755 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3756 | LY_CHECK_RET(rc); |
| 3757 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3758 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3759 | } else { |
| 3760 | set_fill_number(set, args[0]->val.num); |
| 3761 | } |
| 3762 | |
| 3763 | return LY_SUCCESS; |
| 3764 | } |
| 3765 | |
| 3766 | /** |
| 3767 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3768 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3769 | * |
| 3770 | * @param[in] args Array of arguments. |
| 3771 | * @param[in] arg_count Count of elements in @p args. |
| 3772 | * @param[in,out] set Context and result set at the same time. |
| 3773 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3774 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3775 | */ |
| 3776 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3777 | xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3778 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3779 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3780 | char *str = NULL; |
| 3781 | size_t used = 1; |
| 3782 | LY_ERR rc = LY_SUCCESS; |
| 3783 | struct lysc_node_leaf *sleaf; |
| 3784 | |
| 3785 | if (options & LYXP_SCNODE_ALL) { |
| 3786 | for (i = 0; i < arg_count; ++i) { |
| 3787 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3788 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3789 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3790 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3791 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3792 | LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3793 | } |
| 3794 | } |
| 3795 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3796 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3797 | return rc; |
| 3798 | } |
| 3799 | |
| 3800 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3801 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3802 | if (rc != LY_SUCCESS) { |
| 3803 | free(str); |
| 3804 | return rc; |
| 3805 | } |
| 3806 | |
| 3807 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3808 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3809 | strcpy(str + used - 1, args[i]->val.str); |
| 3810 | used += strlen(args[i]->val.str); |
| 3811 | } |
| 3812 | |
| 3813 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3814 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3815 | set->type = LYXP_SET_STRING; |
| 3816 | set->val.str = str; |
| 3817 | |
| 3818 | return LY_SUCCESS; |
| 3819 | } |
| 3820 | |
| 3821 | /** |
| 3822 | * @brief Execute the XPath contains(string, string) function. |
| 3823 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3824 | * be found in the first or not. |
| 3825 | * |
| 3826 | * @param[in] args Array of arguments. |
| 3827 | * @param[in] arg_count Count of elements in @p args. |
| 3828 | * @param[in,out] set Context and result set at the same time. |
| 3829 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3830 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3831 | */ |
| 3832 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3833 | xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3834 | { |
| 3835 | struct lysc_node_leaf *sleaf; |
| 3836 | LY_ERR rc = LY_SUCCESS; |
| 3837 | |
| 3838 | if (options & LYXP_SCNODE_ALL) { |
| 3839 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3840 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3841 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3842 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3843 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3844 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3849 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3850 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3851 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3852 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3853 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3854 | } |
| 3855 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3856 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3857 | return rc; |
| 3858 | } |
| 3859 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3860 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3861 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3862 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3863 | LY_CHECK_RET(rc); |
| 3864 | |
| 3865 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3866 | set_fill_boolean(set, 1); |
| 3867 | } else { |
| 3868 | set_fill_boolean(set, 0); |
| 3869 | } |
| 3870 | |
| 3871 | return LY_SUCCESS; |
| 3872 | } |
| 3873 | |
| 3874 | /** |
| 3875 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3876 | * with the size of the node-set from the argument. |
| 3877 | * |
| 3878 | * @param[in] args Array of arguments. |
| 3879 | * @param[in] arg_count Count of elements in @p args. |
| 3880 | * @param[in,out] set Context and result set at the same time. |
| 3881 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3882 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3883 | */ |
| 3884 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3885 | xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3886 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3887 | LY_ERR rc = LY_SUCCESS; |
| 3888 | |
| 3889 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3890 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3891 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3892 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3893 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3894 | return rc; |
| 3895 | } |
| 3896 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3897 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3898 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3899 | return LY_EVALID; |
| 3900 | } |
| 3901 | |
| 3902 | set_fill_number(set, args[0]->used); |
| 3903 | return LY_SUCCESS; |
| 3904 | } |
| 3905 | |
| 3906 | /** |
| 3907 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3908 | * with the context with the intial node. |
| 3909 | * |
| 3910 | * @param[in] args Array of arguments. |
| 3911 | * @param[in] arg_count Count of elements in @p args. |
| 3912 | * @param[in,out] set Context and result set at the same time. |
| 3913 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3914 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3915 | */ |
| 3916 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3917 | xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3918 | { |
| 3919 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3920 | LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3921 | return LY_EVALID; |
| 3922 | } |
| 3923 | |
| 3924 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3925 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3926 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3927 | if (set->cur_scnode) { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 3928 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3929 | } else { |
| 3930 | /* root node */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 3931 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3932 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3933 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3934 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3935 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3936 | if (set->cur_node) { |
| 3937 | /* position is filled later */ |
| 3938 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3939 | } else { |
| 3940 | /* root node */ |
| 3941 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3942 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | return LY_SUCCESS; |
| 3946 | } |
| 3947 | |
| 3948 | /** |
| 3949 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3950 | * leafref or instance-identifier target node(s). |
| 3951 | * |
| 3952 | * @param[in] args Array of arguments. |
| 3953 | * @param[in] arg_count Count of elements in @p args. |
| 3954 | * @param[in,out] set Context and result set at the same time. |
| 3955 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3956 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3957 | */ |
| 3958 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3959 | xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3960 | { |
| 3961 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3962 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3963 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3964 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3965 | struct ly_path *p; |
| 3966 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3967 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3968 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3969 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3970 | |
| 3971 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3972 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3973 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3974 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 3975 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3976 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3977 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3978 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3979 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3980 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3981 | __func__, sleaf->name); |
| 3982 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3983 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3984 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 3985 | if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3986 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 3987 | oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3988 | |
| 3989 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3990 | r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY, |
Michal Vasko | 24fc4d1 | 2021-07-12 14:41:20 +0200 | [diff] [blame] | 3991 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3992 | if (!r) { |
| 3993 | /* get the target node */ |
| 3994 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 3995 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3996 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 3997 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3998 | } /* else the target was found before but is disabled so it was removed */ |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3999 | } |
| 4000 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4001 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4002 | } |
| 4003 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4004 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4005 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4006 | return LY_EVALID; |
| 4007 | } |
| 4008 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4009 | lyxp_set_free_content(set); |
| 4010 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4011 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4012 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4013 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4014 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 4015 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4016 | if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree, |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4017 | &node, &errmsg)) { |
Michal Vasko | 1a2b8ee | 2022-12-05 10:41:55 +0100 | [diff] [blame] | 4018 | LOGERR(set->ctx, LY_EVALID, "%s", errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4019 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4020 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4021 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4022 | } else { |
| 4023 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 4024 | if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4025 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4026 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4027 | return LY_EVALID; |
| 4028 | } |
| 4029 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4030 | |
| 4031 | /* insert it */ |
| 4032 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4033 | } |
| 4034 | } |
| 4035 | |
| 4036 | return LY_SUCCESS; |
| 4037 | } |
| 4038 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4039 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4040 | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func) |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4041 | { |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 4042 | uint32_t i, id_len; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4043 | LY_ARRAY_COUNT_TYPE u; |
| 4044 | struct lyd_node_term *leaf; |
| 4045 | struct lysc_node_leaf *sleaf; |
| 4046 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4047 | struct lyd_value *val; |
| 4048 | const struct lys_module *mod; |
| 4049 | const char *id_name; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4050 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4051 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4052 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4053 | |
| 4054 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4055 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4056 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4057 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4058 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4059 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4060 | sleaf->name); |
| 4061 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4062 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4063 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4067 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4068 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4069 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4070 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4071 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4072 | } |
| 4073 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4074 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4075 | return rc; |
| 4076 | } |
| 4077 | |
| 4078 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4079 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)"); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4080 | return LY_EVALID; |
| 4081 | } |
| 4082 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4083 | LY_CHECK_RET(rc); |
| 4084 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4085 | /* parse the identity */ |
| 4086 | id_name = args[1]->val.str; |
| 4087 | id_len = strlen(id_name); |
| 4088 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4089 | LY_CHECK_RET(rc); |
| 4090 | if (!mod) { |
| 4091 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4092 | return LY_EVALID; |
| 4093 | } |
| 4094 | |
| 4095 | /* find the identity */ |
| 4096 | found = 0; |
| 4097 | LY_ARRAY_FOR(mod->identities, u) { |
| 4098 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4099 | /* we have match */ |
| 4100 | found = 1; |
| 4101 | break; |
| 4102 | } |
| 4103 | } |
| 4104 | if (!found) { |
| 4105 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4106 | return LY_EVALID; |
| 4107 | } |
| 4108 | id = &mod->identities[u]; |
| 4109 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4110 | set_fill_boolean(set, 0); |
| 4111 | found = 0; |
| 4112 | for (i = 0; i < args[0]->used; ++i) { |
| 4113 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4114 | continue; |
| 4115 | } |
| 4116 | |
| 4117 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4118 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4119 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4120 | val = &leaf->value; |
| 4121 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 4122 | /* uninteresting */ |
| 4123 | continue; |
| 4124 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4125 | } else { |
| 4126 | meta = args[0]->val.meta[i].meta; |
| 4127 | val = &meta->value; |
| 4128 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4129 | /* uninteresting */ |
| 4130 | continue; |
| 4131 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4132 | } |
| 4133 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4134 | /* check the identity itself */ |
| 4135 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4136 | set_fill_boolean(set, 1); |
| 4137 | found = 1; |
| 4138 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4139 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4140 | set_fill_boolean(set, 1); |
| 4141 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4142 | } |
| 4143 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4144 | if (found) { |
| 4145 | break; |
| 4146 | } |
| 4147 | } |
| 4148 | |
| 4149 | return LY_SUCCESS; |
| 4150 | } |
| 4151 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4152 | /** |
| 4153 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4154 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4155 | * argument identity. |
| 4156 | * |
| 4157 | * @param[in] args Array of arguments. |
| 4158 | * @param[in] arg_count Count of elements in @p args. |
| 4159 | * @param[in,out] set Context and result set at the same time. |
| 4160 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4161 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4162 | */ |
| 4163 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4164 | xpath_derived_from(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4165 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4166 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | /** |
| 4170 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4171 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4172 | * the second argument identity. |
| 4173 | * |
| 4174 | * @param[in] args Array of arguments. |
| 4175 | * @param[in] arg_count Count of elements in @p args. |
| 4176 | * @param[in,out] set Context and result set at the same time. |
| 4177 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4178 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4179 | */ |
| 4180 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4181 | xpath_derived_from_or_self(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4182 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4183 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | /** |
| 4187 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4188 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4189 | * |
| 4190 | * @param[in] args Array of arguments. |
| 4191 | * @param[in] arg_count Count of elements in @p args. |
| 4192 | * @param[in,out] set Context and result set at the same time. |
| 4193 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4194 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4195 | */ |
| 4196 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4197 | xpath_enum_value(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4198 | { |
| 4199 | struct lyd_node_term *leaf; |
| 4200 | struct lysc_node_leaf *sleaf; |
| 4201 | LY_ERR rc = LY_SUCCESS; |
| 4202 | |
| 4203 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4204 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4205 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4206 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4207 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4208 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4209 | sleaf->name); |
| 4210 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4211 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4212 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4213 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4214 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4215 | return rc; |
| 4216 | } |
| 4217 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4218 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4219 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4220 | return LY_EVALID; |
| 4221 | } |
| 4222 | |
| 4223 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4224 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4225 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4226 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4227 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4228 | set_fill_number(set, leaf->value.enum_item->value); |
| 4229 | } |
| 4230 | } |
| 4231 | |
| 4232 | return LY_SUCCESS; |
| 4233 | } |
| 4234 | |
| 4235 | /** |
| 4236 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4237 | * with false value. |
| 4238 | * |
| 4239 | * @param[in] args Array of arguments. |
| 4240 | * @param[in] arg_count Count of elements in @p args. |
| 4241 | * @param[in,out] set Context and result set at the same time. |
| 4242 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4243 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4244 | */ |
| 4245 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4246 | xpath_false(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4247 | { |
| 4248 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4249 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4250 | return LY_SUCCESS; |
| 4251 | } |
| 4252 | |
| 4253 | set_fill_boolean(set, 0); |
| 4254 | return LY_SUCCESS; |
| 4255 | } |
| 4256 | |
| 4257 | /** |
| 4258 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4259 | * with the first argument floored (truncated). |
| 4260 | * |
| 4261 | * @param[in] args Array of arguments. |
| 4262 | * @param[in] arg_count Count of elements in @p args. |
| 4263 | * @param[in,out] set Context and result set at the same time. |
| 4264 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4265 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4266 | */ |
| 4267 | static LY_ERR |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4268 | xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4269 | { |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4270 | struct lysc_node_leaf *sleaf; |
| 4271 | LY_ERR rc = LY_SUCCESS; |
| 4272 | |
| 4273 | if (options & LYXP_SCNODE_ALL) { |
| 4274 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
| 4275 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 4276 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4277 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4278 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4279 | sleaf->name); |
| 4280 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4281 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4282 | } |
| 4283 | } |
| 4284 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 4285 | return rc; |
| 4286 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4287 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4288 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4289 | LY_CHECK_RET(rc); |
| 4290 | if (isfinite(args[0]->val.num)) { |
| 4291 | set_fill_number(set, (long long)args[0]->val.num); |
| 4292 | } |
| 4293 | |
| 4294 | return LY_SUCCESS; |
| 4295 | } |
| 4296 | |
| 4297 | /** |
| 4298 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4299 | * whether the language of the text matches the one from the argument. |
| 4300 | * |
| 4301 | * @param[in] args Array of arguments. |
| 4302 | * @param[in] arg_count Count of elements in @p args. |
| 4303 | * @param[in,out] set Context and result set at the same time. |
| 4304 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4305 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4306 | */ |
| 4307 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4308 | xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4309 | { |
| 4310 | const struct lyd_node *node; |
| 4311 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4312 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4313 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4314 | LY_ERR rc = LY_SUCCESS; |
| 4315 | |
| 4316 | if (options & LYXP_SCNODE_ALL) { |
| 4317 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4318 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4319 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4320 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4321 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4322 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4323 | } |
| 4324 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4325 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4326 | return rc; |
| 4327 | } |
| 4328 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4329 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4330 | LY_CHECK_RET(rc); |
| 4331 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4332 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4333 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4334 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4335 | } else if (!set->used) { |
| 4336 | set_fill_boolean(set, 0); |
| 4337 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | switch (set->val.nodes[0].type) { |
| 4341 | case LYXP_NODE_ELEM: |
| 4342 | case LYXP_NODE_TEXT: |
| 4343 | node = set->val.nodes[0].node; |
| 4344 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4345 | case LYXP_NODE_META: |
| 4346 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4347 | break; |
| 4348 | default: |
| 4349 | /* nothing to do with roots */ |
| 4350 | set_fill_boolean(set, 0); |
| 4351 | return LY_SUCCESS; |
| 4352 | } |
| 4353 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4354 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4355 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4356 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4357 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4358 | if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4359 | break; |
| 4360 | } |
| 4361 | } |
| 4362 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4363 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4364 | break; |
| 4365 | } |
| 4366 | } |
| 4367 | |
| 4368 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4369 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4370 | set_fill_boolean(set, 0); |
| 4371 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4372 | uint64_t i; |
| 4373 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4374 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4375 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4376 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4377 | set_fill_boolean(set, 0); |
| 4378 | break; |
| 4379 | } |
| 4380 | } |
| 4381 | if (!args[0]->val.str[i]) { |
| 4382 | if (!val[i] || (val[i] == '-')) { |
| 4383 | set_fill_boolean(set, 1); |
| 4384 | } else { |
| 4385 | set_fill_boolean(set, 0); |
| 4386 | } |
| 4387 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | return LY_SUCCESS; |
| 4391 | } |
| 4392 | |
| 4393 | /** |
| 4394 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4395 | * with the context size. |
| 4396 | * |
| 4397 | * @param[in] args Array of arguments. |
| 4398 | * @param[in] arg_count Count of elements in @p args. |
| 4399 | * @param[in,out] set Context and result set at the same time. |
| 4400 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4401 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4402 | */ |
| 4403 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4404 | xpath_last(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4405 | { |
| 4406 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4407 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4408 | return LY_SUCCESS; |
| 4409 | } |
| 4410 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4411 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4412 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4413 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4414 | } else if (!set->used) { |
| 4415 | set_fill_number(set, 0); |
| 4416 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | set_fill_number(set, set->ctx_size); |
| 4420 | return LY_SUCCESS; |
| 4421 | } |
| 4422 | |
| 4423 | /** |
| 4424 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4425 | * with the node name without namespace from the argument or the context. |
| 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. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4431 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4432 | */ |
| 4433 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4434 | xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4435 | { |
| 4436 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4437 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4438 | /* suppress unused variable warning */ |
| 4439 | (void)options; |
| 4440 | |
| 4441 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4442 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4443 | return LY_SUCCESS; |
| 4444 | } |
| 4445 | |
| 4446 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4447 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4448 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4449 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4450 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4451 | } else if (!args[0]->used) { |
| 4452 | set_fill_string(set, "", 0); |
| 4453 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4457 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4458 | |
| 4459 | item = &args[0]->val.nodes[0]; |
| 4460 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4461 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4462 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4463 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4464 | } else if (!set->used) { |
| 4465 | set_fill_string(set, "", 0); |
| 4466 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4470 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4471 | |
| 4472 | item = &set->val.nodes[0]; |
| 4473 | } |
| 4474 | |
| 4475 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4476 | case LYXP_NODE_NONE: |
| 4477 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4478 | case LYXP_NODE_ROOT: |
| 4479 | case LYXP_NODE_ROOT_CONFIG: |
| 4480 | case LYXP_NODE_TEXT: |
| 4481 | set_fill_string(set, "", 0); |
| 4482 | break; |
| 4483 | case LYXP_NODE_ELEM: |
| 4484 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4485 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4486 | case LYXP_NODE_META: |
| 4487 | set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4488 | break; |
| 4489 | } |
| 4490 | |
| 4491 | return LY_SUCCESS; |
| 4492 | } |
| 4493 | |
| 4494 | /** |
| 4495 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4496 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4497 | * |
| 4498 | * @param[in] args Array of arguments. |
| 4499 | * @param[in] arg_count Count of elements in @p args. |
| 4500 | * @param[in,out] set Context and result set at the same time. |
| 4501 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4502 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4503 | */ |
| 4504 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4505 | xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4506 | { |
| 4507 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4508 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4509 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4510 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4511 | |
| 4512 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4513 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4514 | return LY_SUCCESS; |
| 4515 | } |
| 4516 | |
| 4517 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4518 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4519 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4520 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4521 | } else if (!args[0]->used) { |
| 4522 | set_fill_string(set, "", 0); |
| 4523 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4524 | } |
| 4525 | |
| 4526 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4527 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4528 | |
| 4529 | item = &args[0]->val.nodes[0]; |
| 4530 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4531 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4532 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4533 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4534 | } else if (!set->used) { |
| 4535 | set_fill_string(set, "", 0); |
| 4536 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4540 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4541 | |
| 4542 | item = &set->val.nodes[0]; |
| 4543 | } |
| 4544 | |
| 4545 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4546 | case LYXP_NODE_NONE: |
| 4547 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4548 | case LYXP_NODE_ROOT: |
| 4549 | case LYXP_NODE_ROOT_CONFIG: |
| 4550 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4551 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4552 | break; |
| 4553 | case LYXP_NODE_ELEM: |
| 4554 | mod = item->node->schema->module; |
| 4555 | name = item->node->schema->name; |
| 4556 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4557 | case LYXP_NODE_META: |
| 4558 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4559 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4560 | break; |
| 4561 | } |
| 4562 | |
| 4563 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4564 | int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 4565 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4566 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4567 | set_fill_string(set, str, strlen(str)); |
| 4568 | free(str); |
| 4569 | } else { |
| 4570 | set_fill_string(set, "", 0); |
| 4571 | } |
| 4572 | |
| 4573 | return LY_SUCCESS; |
| 4574 | } |
| 4575 | |
| 4576 | /** |
| 4577 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4578 | * with the namespace of the node from the argument or the context. |
| 4579 | * |
| 4580 | * @param[in] args Array of arguments. |
| 4581 | * @param[in] arg_count Count of elements in @p args. |
| 4582 | * @param[in,out] set Context and result set at the same time. |
| 4583 | * @param[in] options XPath options. |
| 4584 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4585 | */ |
| 4586 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4587 | xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4588 | { |
| 4589 | struct lyxp_set_node *item; |
| 4590 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4591 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4592 | /* suppress unused variable warning */ |
| 4593 | (void)options; |
| 4594 | |
| 4595 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4596 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4597 | return LY_SUCCESS; |
| 4598 | } |
| 4599 | |
| 4600 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4601 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4602 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4603 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4604 | return LY_EVALID; |
| 4605 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4606 | set_fill_string(set, "", 0); |
| 4607 | return LY_SUCCESS; |
| 4608 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4609 | |
| 4610 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4611 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4612 | |
| 4613 | item = &args[0]->val.nodes[0]; |
| 4614 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4615 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4616 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4617 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4618 | } else if (!set->used) { |
| 4619 | set_fill_string(set, "", 0); |
| 4620 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4624 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4625 | |
| 4626 | item = &set->val.nodes[0]; |
| 4627 | } |
| 4628 | |
| 4629 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4630 | case LYXP_NODE_NONE: |
| 4631 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4632 | case LYXP_NODE_ROOT: |
| 4633 | case LYXP_NODE_ROOT_CONFIG: |
| 4634 | case LYXP_NODE_TEXT: |
| 4635 | set_fill_string(set, "", 0); |
| 4636 | break; |
| 4637 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4638 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4639 | if (item->type == LYXP_NODE_ELEM) { |
| 4640 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4641 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4642 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4643 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4647 | break; |
| 4648 | } |
| 4649 | |
| 4650 | return LY_SUCCESS; |
| 4651 | } |
| 4652 | |
| 4653 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4654 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4655 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4656 | * from the argument or the context. |
| 4657 | * |
| 4658 | * @param[in] args Array of arguments. |
| 4659 | * @param[in] arg_count Count of elements in @p args. |
| 4660 | * @param[in,out] set Context and result set at the same time. |
| 4661 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4662 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4663 | */ |
| 4664 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4665 | xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4666 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4667 | uint32_t i, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4668 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4669 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4670 | struct lysc_node_leaf *sleaf; |
| 4671 | LY_ERR rc = LY_SUCCESS; |
| 4672 | |
| 4673 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4674 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4675 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4676 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4677 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4678 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4679 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4680 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4681 | } |
| 4682 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4683 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4684 | return rc; |
| 4685 | } |
| 4686 | |
| 4687 | if (arg_count) { |
| 4688 | set_fill_set(set, args[0]); |
| 4689 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4690 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4691 | LY_CHECK_RET(rc); |
| 4692 | |
| 4693 | /* is there any normalization necessary? */ |
| 4694 | for (i = 0; set->val.str[i]; ++i) { |
| 4695 | if (is_xmlws(set->val.str[i])) { |
| 4696 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4697 | have_spaces = 1; |
| 4698 | break; |
| 4699 | } |
| 4700 | space_before = 1; |
| 4701 | } else { |
| 4702 | space_before = 0; |
| 4703 | } |
| 4704 | } |
| 4705 | |
| 4706 | /* yep, there is */ |
| 4707 | if (have_spaces) { |
| 4708 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4709 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4710 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4711 | new_used = 0; |
| 4712 | |
| 4713 | space_before = 0; |
| 4714 | for (i = 0; set->val.str[i]; ++i) { |
| 4715 | if (is_xmlws(set->val.str[i])) { |
| 4716 | if ((i == 0) || space_before) { |
| 4717 | space_before = 1; |
| 4718 | continue; |
| 4719 | } else { |
| 4720 | space_before = 1; |
| 4721 | } |
| 4722 | } else { |
| 4723 | space_before = 0; |
| 4724 | } |
| 4725 | |
| 4726 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4727 | ++new_used; |
| 4728 | } |
| 4729 | |
| 4730 | /* at worst there is one trailing space now */ |
| 4731 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4732 | --new_used; |
| 4733 | } |
| 4734 | |
| 4735 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4736 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4737 | new[new_used] = '\0'; |
| 4738 | |
| 4739 | free(set->val.str); |
| 4740 | set->val.str = new; |
| 4741 | } |
| 4742 | |
| 4743 | return LY_SUCCESS; |
| 4744 | } |
| 4745 | |
| 4746 | /** |
| 4747 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4748 | * with the argument converted to boolean and logically inverted. |
| 4749 | * |
| 4750 | * @param[in] args Array of arguments. |
| 4751 | * @param[in] arg_count Count of elements in @p args. |
| 4752 | * @param[in,out] set Context and result set at the same time. |
| 4753 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4754 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4755 | */ |
| 4756 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4757 | xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4758 | { |
| 4759 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4760 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4761 | return LY_SUCCESS; |
| 4762 | } |
| 4763 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4764 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4765 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4766 | set_fill_boolean(set, 0); |
| 4767 | } else { |
| 4768 | set_fill_boolean(set, 1); |
| 4769 | } |
| 4770 | |
| 4771 | return LY_SUCCESS; |
| 4772 | } |
| 4773 | |
| 4774 | /** |
| 4775 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4776 | * with the number representation of either the argument or the context. |
| 4777 | * |
| 4778 | * @param[in] args Array of arguments. |
| 4779 | * @param[in] arg_count Count of elements in @p args. |
| 4780 | * @param[in,out] set Context and result set at the same time. |
| 4781 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4782 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4783 | */ |
| 4784 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4785 | xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4786 | { |
| 4787 | LY_ERR rc; |
| 4788 | |
| 4789 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4790 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4791 | return LY_SUCCESS; |
| 4792 | } |
| 4793 | |
| 4794 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4795 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4796 | LY_CHECK_RET(rc); |
| 4797 | set_fill_set(set, args[0]); |
| 4798 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4799 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4800 | LY_CHECK_RET(rc); |
| 4801 | } |
| 4802 | |
| 4803 | return LY_SUCCESS; |
| 4804 | } |
| 4805 | |
| 4806 | /** |
| 4807 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4808 | * with the context position. |
| 4809 | * |
| 4810 | * @param[in] args Array of arguments. |
| 4811 | * @param[in] arg_count Count of elements in @p args. |
| 4812 | * @param[in,out] set Context and result set at the same time. |
| 4813 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4814 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4815 | */ |
| 4816 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4817 | xpath_position(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4818 | { |
| 4819 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4820 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4821 | return LY_SUCCESS; |
| 4822 | } |
| 4823 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4824 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4825 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4826 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4827 | } else if (!set->used) { |
| 4828 | set_fill_number(set, 0); |
| 4829 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | set_fill_number(set, set->ctx_pos); |
| 4833 | |
| 4834 | /* UNUSED in 'Release' build type */ |
| 4835 | (void)options; |
| 4836 | return LY_SUCCESS; |
| 4837 | } |
| 4838 | |
| 4839 | /** |
| 4840 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4841 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4842 | * YANG 1.1 RFC section 10.2.1. |
| 4843 | * |
| 4844 | * @param[in] args Array of arguments. |
| 4845 | * @param[in] arg_count Count of elements in @p args. |
| 4846 | * @param[in,out] set Context and result set at the same time. |
| 4847 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4848 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4849 | */ |
| 4850 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4851 | xpath_re_match(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4852 | { |
| 4853 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4854 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4855 | LY_ERR rc = LY_SUCCESS; |
| 4856 | struct ly_err_item *err; |
| 4857 | |
| 4858 | if (options & LYXP_SCNODE_ALL) { |
| 4859 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4860 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4861 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4862 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4863 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4864 | } |
| 4865 | } |
| 4866 | |
| 4867 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4868 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4869 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4870 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4871 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4872 | } |
| 4873 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4874 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4875 | return rc; |
| 4876 | } |
| 4877 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4878 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4879 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4880 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4881 | LY_CHECK_RET(rc); |
| 4882 | |
| 4883 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4884 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4885 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4886 | rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code); |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 4887 | if (set->cur_node) { |
| 4888 | LOG_LOCBACK(0, 1, 0, 0); |
| 4889 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4890 | if (rc != LY_SUCCESS) { |
| 4891 | LY_ARRAY_FREE(patterns); |
| 4892 | return rc; |
| 4893 | } |
| 4894 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4895 | rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4896 | pcre2_code_free((*pattern)->code); |
| 4897 | free(*pattern); |
| 4898 | LY_ARRAY_FREE(patterns); |
| 4899 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4900 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4901 | ly_err_free(err); |
| 4902 | return rc; |
| 4903 | } |
| 4904 | |
| 4905 | if (rc == LY_EVALID) { |
| 4906 | ly_err_free(err); |
| 4907 | set_fill_boolean(set, 0); |
| 4908 | } else { |
| 4909 | set_fill_boolean(set, 1); |
| 4910 | } |
| 4911 | |
| 4912 | return LY_SUCCESS; |
| 4913 | } |
| 4914 | |
| 4915 | /** |
| 4916 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4917 | * with the rounded first argument. For details refer to |
| 4918 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4919 | * |
| 4920 | * @param[in] args Array of arguments. |
| 4921 | * @param[in] arg_count Count of elements in @p args. |
| 4922 | * @param[in,out] set Context and result set at the same time. |
| 4923 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4924 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4925 | */ |
| 4926 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4927 | xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4928 | { |
| 4929 | struct lysc_node_leaf *sleaf; |
| 4930 | LY_ERR rc = LY_SUCCESS; |
| 4931 | |
| 4932 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4933 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4934 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4935 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4936 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4937 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4938 | sleaf->name); |
| 4939 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4940 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4941 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4942 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4943 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4944 | return rc; |
| 4945 | } |
| 4946 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4947 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4948 | LY_CHECK_RET(rc); |
| 4949 | |
| 4950 | /* cover only the cases where floor can't be used */ |
| 4951 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4952 | set_fill_number(set, -0.0f); |
| 4953 | } else { |
| 4954 | args[0]->val.num += 0.5; |
| 4955 | rc = xpath_floor(args, 1, args[0], options); |
| 4956 | LY_CHECK_RET(rc); |
| 4957 | set_fill_number(set, args[0]->val.num); |
| 4958 | } |
| 4959 | |
| 4960 | return LY_SUCCESS; |
| 4961 | } |
| 4962 | |
| 4963 | /** |
| 4964 | * @brief Execute the XPath starts-with(string, string) function. |
| 4965 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4966 | * the prefix of the first or not. |
| 4967 | * |
| 4968 | * @param[in] args Array of arguments. |
| 4969 | * @param[in] arg_count Count of elements in @p args. |
| 4970 | * @param[in,out] set Context and result set at the same time. |
| 4971 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4972 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4973 | */ |
| 4974 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4975 | xpath_starts_with(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4976 | { |
| 4977 | struct lysc_node_leaf *sleaf; |
| 4978 | LY_ERR rc = LY_SUCCESS; |
| 4979 | |
| 4980 | if (options & LYXP_SCNODE_ALL) { |
| 4981 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4982 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4983 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4984 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4985 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4986 | } |
| 4987 | } |
| 4988 | |
| 4989 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4990 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4991 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4992 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4993 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4994 | } |
| 4995 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4996 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4997 | return rc; |
| 4998 | } |
| 4999 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5000 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5001 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5002 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5003 | LY_CHECK_RET(rc); |
| 5004 | |
| 5005 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 5006 | set_fill_boolean(set, 0); |
| 5007 | } else { |
| 5008 | set_fill_boolean(set, 1); |
| 5009 | } |
| 5010 | |
| 5011 | return LY_SUCCESS; |
| 5012 | } |
| 5013 | |
| 5014 | /** |
| 5015 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 5016 | * with the string representation of either the argument or 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. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5022 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5023 | */ |
| 5024 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5025 | xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5026 | { |
| 5027 | LY_ERR rc; |
| 5028 | |
| 5029 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5030 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5031 | return LY_SUCCESS; |
| 5032 | } |
| 5033 | |
| 5034 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5035 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5036 | LY_CHECK_RET(rc); |
| 5037 | set_fill_set(set, args[0]); |
| 5038 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5039 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5040 | LY_CHECK_RET(rc); |
| 5041 | } |
| 5042 | |
| 5043 | return LY_SUCCESS; |
| 5044 | } |
| 5045 | |
| 5046 | /** |
| 5047 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5048 | * with the length of the string in either the argument or the context. |
| 5049 | * |
| 5050 | * @param[in] args Array of arguments. |
| 5051 | * @param[in] arg_count Count of elements in @p args. |
| 5052 | * @param[in,out] set Context and result set at the same time. |
| 5053 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5054 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5055 | */ |
| 5056 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5057 | xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5058 | { |
| 5059 | struct lysc_node_leaf *sleaf; |
| 5060 | LY_ERR rc = LY_SUCCESS; |
| 5061 | |
| 5062 | if (options & LYXP_SCNODE_ALL) { |
| 5063 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5064 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5065 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5066 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5067 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5068 | } |
| 5069 | } |
| 5070 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5071 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5072 | LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5073 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5074 | LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5075 | } |
| 5076 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5077 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5078 | return rc; |
| 5079 | } |
| 5080 | |
| 5081 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5082 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5083 | LY_CHECK_RET(rc); |
| 5084 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5085 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5086 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5087 | LY_CHECK_RET(rc); |
| 5088 | set_fill_number(set, strlen(set->val.str)); |
| 5089 | } |
| 5090 | |
| 5091 | return LY_SUCCESS; |
| 5092 | } |
| 5093 | |
| 5094 | /** |
| 5095 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5096 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5097 | * on the second argument index ending on the third argument index, |
| 5098 | * indexed from 1. For exact definition refer to |
| 5099 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5100 | * |
| 5101 | * @param[in] args Array of arguments. |
| 5102 | * @param[in] arg_count Count of elements in @p args. |
| 5103 | * @param[in,out] set Context and result set at the same time. |
| 5104 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5105 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5106 | */ |
| 5107 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5108 | xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5109 | { |
Michal Vasko | 6db996e | 2022-07-28 10:28:04 +0200 | [diff] [blame] | 5110 | int64_t start; |
| 5111 | int32_t len; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5112 | uint32_t str_start, str_len, pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5113 | struct lysc_node_leaf *sleaf; |
| 5114 | LY_ERR rc = LY_SUCCESS; |
| 5115 | |
| 5116 | if (options & LYXP_SCNODE_ALL) { |
| 5117 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5118 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5119 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5120 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5121 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5126 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5127 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5128 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5129 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5133 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5134 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5135 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5136 | LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5137 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5138 | LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5139 | } |
| 5140 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5141 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5142 | return rc; |
| 5143 | } |
| 5144 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5145 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5146 | LY_CHECK_RET(rc); |
| 5147 | |
| 5148 | /* start */ |
| 5149 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5150 | return -1; |
| 5151 | } |
| 5152 | if (isfinite(args[1]->val.num)) { |
| 5153 | start = args[1]->val.num - 1; |
| 5154 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5155 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5156 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5157 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5158 | } |
| 5159 | |
| 5160 | /* len */ |
| 5161 | if (arg_count == 3) { |
| 5162 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5163 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5164 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5165 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5166 | } else if (isfinite(args[2]->val.num)) { |
| 5167 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5168 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5169 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5170 | } |
| 5171 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5172 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | /* find matching character positions */ |
| 5176 | str_start = 0; |
| 5177 | str_len = 0; |
| 5178 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5179 | if (pos < start) { |
| 5180 | ++str_start; |
| 5181 | } else if (pos < start + len) { |
| 5182 | ++str_len; |
| 5183 | } else { |
| 5184 | break; |
| 5185 | } |
| 5186 | } |
| 5187 | |
| 5188 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5189 | return LY_SUCCESS; |
| 5190 | } |
| 5191 | |
| 5192 | /** |
| 5193 | * @brief Execute the XPath substring-after(string, string) function. |
| 5194 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5195 | * of the second argument in the first or an empty string. |
| 5196 | * |
| 5197 | * @param[in] args Array of arguments. |
| 5198 | * @param[in] arg_count Count of elements in @p args. |
| 5199 | * @param[in,out] set Context and result set at the same time. |
| 5200 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5201 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5202 | */ |
| 5203 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5204 | xpath_substring_after(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5205 | { |
| 5206 | char *ptr; |
| 5207 | struct lysc_node_leaf *sleaf; |
| 5208 | LY_ERR rc = LY_SUCCESS; |
| 5209 | |
| 5210 | if (options & LYXP_SCNODE_ALL) { |
| 5211 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5212 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5213 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5214 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5215 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5216 | } |
| 5217 | } |
| 5218 | |
| 5219 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5220 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5221 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5222 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5223 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5224 | } |
| 5225 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5226 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5227 | return rc; |
| 5228 | } |
| 5229 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5230 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5231 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5232 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5233 | LY_CHECK_RET(rc); |
| 5234 | |
| 5235 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5236 | if (ptr) { |
| 5237 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5238 | } else { |
| 5239 | set_fill_string(set, "", 0); |
| 5240 | } |
| 5241 | |
| 5242 | return LY_SUCCESS; |
| 5243 | } |
| 5244 | |
| 5245 | /** |
| 5246 | * @brief Execute the XPath substring-before(string, string) function. |
| 5247 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5248 | * of the second argument in the first or an empty string. |
| 5249 | * |
| 5250 | * @param[in] args Array of arguments. |
| 5251 | * @param[in] arg_count Count of elements in @p args. |
| 5252 | * @param[in,out] set Context and result set at the same time. |
| 5253 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5254 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5255 | */ |
| 5256 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5257 | xpath_substring_before(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5258 | { |
| 5259 | char *ptr; |
| 5260 | struct lysc_node_leaf *sleaf; |
| 5261 | LY_ERR rc = LY_SUCCESS; |
| 5262 | |
| 5263 | if (options & LYXP_SCNODE_ALL) { |
| 5264 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5265 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5266 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5267 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5268 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5269 | } |
| 5270 | } |
| 5271 | |
| 5272 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5273 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5274 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5275 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5276 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5277 | } |
| 5278 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5279 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5280 | return rc; |
| 5281 | } |
| 5282 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5283 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5284 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5285 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5286 | LY_CHECK_RET(rc); |
| 5287 | |
| 5288 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5289 | if (ptr) { |
| 5290 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5291 | } else { |
| 5292 | set_fill_string(set, "", 0); |
| 5293 | } |
| 5294 | |
| 5295 | return LY_SUCCESS; |
| 5296 | } |
| 5297 | |
| 5298 | /** |
| 5299 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5300 | * with the sum of all the nodes in the context. |
| 5301 | * |
| 5302 | * @param[in] args Array of arguments. |
| 5303 | * @param[in] arg_count Count of elements in @p args. |
| 5304 | * @param[in,out] set Context and result set at the same time. |
| 5305 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5306 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5307 | */ |
| 5308 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5309 | xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5310 | { |
| 5311 | long double num; |
| 5312 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5313 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5314 | struct lyxp_set set_item; |
| 5315 | struct lysc_node_leaf *sleaf; |
| 5316 | LY_ERR rc = LY_SUCCESS; |
| 5317 | |
| 5318 | if (options & LYXP_SCNODE_ALL) { |
| 5319 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5320 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5321 | if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5322 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5323 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5324 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5325 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5326 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5327 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5328 | } |
| 5329 | } |
| 5330 | } |
| 5331 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5332 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5333 | return rc; |
| 5334 | } |
| 5335 | |
| 5336 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5337 | |
| 5338 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5339 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5340 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5341 | } else if (!args[0]->used) { |
| 5342 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5343 | } |
| 5344 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5345 | set_init(&set_item, set); |
| 5346 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5347 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5348 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5349 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5350 | |
| 5351 | set_item.used = 1; |
| 5352 | set_item.size = 1; |
| 5353 | |
| 5354 | for (i = 0; i < args[0]->used; ++i) { |
| 5355 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5356 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5357 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5358 | LY_CHECK_RET(rc); |
| 5359 | num = cast_string_to_number(str); |
| 5360 | free(str); |
| 5361 | set->val.num += num; |
| 5362 | } |
| 5363 | |
| 5364 | free(set_item.val.nodes); |
| 5365 | |
| 5366 | return LY_SUCCESS; |
| 5367 | } |
| 5368 | |
| 5369 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5370 | * @brief Execute the XPath translate(string, string, string) function. |
| 5371 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5372 | * from the second argument replaced by those on the corresponding |
| 5373 | * positions in the third argument. |
| 5374 | * |
| 5375 | * @param[in] args Array of arguments. |
| 5376 | * @param[in] arg_count Count of elements in @p args. |
| 5377 | * @param[in,out] set Context and result set at the same time. |
| 5378 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5379 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5380 | */ |
| 5381 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5382 | xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5383 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5384 | uint32_t i, j, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5385 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5386 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5387 | struct lysc_node_leaf *sleaf; |
| 5388 | LY_ERR rc = LY_SUCCESS; |
| 5389 | |
| 5390 | if (options & LYXP_SCNODE_ALL) { |
| 5391 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5392 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5393 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5394 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5395 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5396 | } |
| 5397 | } |
| 5398 | |
| 5399 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5400 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5401 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5402 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5403 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5404 | } |
| 5405 | } |
| 5406 | |
| 5407 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5408 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5409 | LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5410 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5411 | LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5412 | } |
| 5413 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5414 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5415 | return rc; |
| 5416 | } |
| 5417 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5418 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5419 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5420 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5421 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5422 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5423 | LY_CHECK_RET(rc); |
| 5424 | |
| 5425 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5426 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5427 | new_used = 0; |
| 5428 | |
| 5429 | have_removed = 0; |
| 5430 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5431 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5432 | |
| 5433 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5434 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5435 | /* removing this char */ |
| 5436 | if (j >= strlen(args[2]->val.str)) { |
| 5437 | have_removed = 1; |
| 5438 | found = 1; |
| 5439 | break; |
| 5440 | } |
| 5441 | /* replacing this char */ |
| 5442 | new[new_used] = args[2]->val.str[j]; |
| 5443 | ++new_used; |
| 5444 | found = 1; |
| 5445 | break; |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | /* copying this char */ |
| 5450 | if (!found) { |
| 5451 | new[new_used] = args[0]->val.str[i]; |
| 5452 | ++new_used; |
| 5453 | } |
| 5454 | } |
| 5455 | |
| 5456 | if (have_removed) { |
| 5457 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5458 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5459 | } |
| 5460 | new[new_used] = '\0'; |
| 5461 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5462 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5463 | set->type = LYXP_SET_STRING; |
| 5464 | set->val.str = new; |
| 5465 | |
| 5466 | return LY_SUCCESS; |
| 5467 | } |
| 5468 | |
| 5469 | /** |
| 5470 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5471 | * with true value. |
| 5472 | * |
| 5473 | * @param[in] args Array of arguments. |
| 5474 | * @param[in] arg_count Count of elements in @p args. |
| 5475 | * @param[in,out] set Context and result set at the same time. |
| 5476 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5477 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5478 | */ |
| 5479 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5480 | xpath_true(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5481 | { |
| 5482 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5483 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5484 | return LY_SUCCESS; |
| 5485 | } |
| 5486 | |
| 5487 | set_fill_boolean(set, 1); |
| 5488 | return LY_SUCCESS; |
| 5489 | } |
| 5490 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5491 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5492 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5493 | * with only nodes from the context. |
| 5494 | * |
| 5495 | * @param[in,out] set Context and result set at the same time. |
| 5496 | * @param[in] axis Axis to search on. |
| 5497 | * @param[in] options XPath options. |
| 5498 | * @return LY_ERR |
| 5499 | */ |
| 5500 | static LY_ERR |
| 5501 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5502 | { |
| 5503 | if (options & LYXP_SCNODE_ALL) { |
| 5504 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5505 | } |
| 5506 | |
| 5507 | if (set->type != LYXP_SET_NODE_SET) { |
| 5508 | lyxp_set_free_content(set); |
| 5509 | return LY_SUCCESS; |
| 5510 | } |
| 5511 | |
| 5512 | /* just like moving to a node with no restrictions */ |
| 5513 | return moveto_node(set, NULL, NULL, axis, options); |
| 5514 | } |
| 5515 | |
| 5516 | /** |
| 5517 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5518 | * with the text content of the nodes in the context. |
| 5519 | * |
| 5520 | * @param[in,out] set Context and result set at the same time. |
| 5521 | * @param[in] axis Axis to search on. |
| 5522 | * @param[in] options XPath options. |
| 5523 | * @return LY_ERR |
| 5524 | */ |
| 5525 | static LY_ERR |
| 5526 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5527 | { |
| 5528 | uint32_t i; |
| 5529 | |
| 5530 | if (options & LYXP_SCNODE_ALL) { |
| 5531 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5532 | return LY_SUCCESS; |
| 5533 | } |
| 5534 | |
| 5535 | if (set->type != LYXP_SET_NODE_SET) { |
| 5536 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5537 | return LY_EVALID; |
| 5538 | } |
| 5539 | |
| 5540 | if (axis != LYXP_AXIS_CHILD) { |
| 5541 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5542 | lyxp_set_free_content(set); |
| 5543 | return LY_SUCCESS; |
| 5544 | } |
| 5545 | |
| 5546 | for (i = 0; i < set->used; ++i) { |
| 5547 | switch (set->val.nodes[i].type) { |
| 5548 | case LYXP_NODE_NONE: |
| 5549 | LOGINT_RET(set->ctx); |
| 5550 | case LYXP_NODE_ELEM: |
| 5551 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5552 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5553 | break; |
| 5554 | } |
| 5555 | /* fall through */ |
| 5556 | case LYXP_NODE_ROOT: |
| 5557 | case LYXP_NODE_ROOT_CONFIG: |
| 5558 | case LYXP_NODE_TEXT: |
| 5559 | case LYXP_NODE_META: |
| 5560 | set_remove_node_none(set, i); |
| 5561 | break; |
| 5562 | } |
| 5563 | } |
| 5564 | set_remove_nodes_none(set); |
| 5565 | |
| 5566 | return LY_SUCCESS; |
| 5567 | } |
| 5568 | |
| 5569 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5570 | * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5571 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5572 | * XPath @p set is expected to be a (sc)node set! |
| 5573 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5574 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5575 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5576 | * @param[in] set Set with general XPath context. |
| 5577 | * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5578 | * @param[out] moveto_mod Expected module of a matching node. |
| 5579 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5580 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5581 | static LY_ERR |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 5582 | moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5583 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5584 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5585 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5586 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5587 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5588 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5589 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5590 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5591 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5592 | /* specific module */ |
| 5593 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5594 | mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5595 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5596 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5597 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5598 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5599 | return LY_EVALID; |
| 5600 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5601 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5602 | *qname += pref_len + 1; |
| 5603 | *qname_len -= pref_len + 1; |
| 5604 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5605 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5606 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5607 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5608 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5609 | case LY_VALUE_SCHEMA: |
| 5610 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5611 | /* current module */ |
| 5612 | mod = set->cur_mod; |
| 5613 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5614 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5615 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5616 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5617 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5618 | /* inherit parent (context node) module */ |
| 5619 | if (ctx_scnode) { |
| 5620 | mod = ctx_scnode->module; |
| 5621 | } else { |
| 5622 | mod = NULL; |
| 5623 | } |
| 5624 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5625 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5626 | /* all nodes need to be prefixed */ |
| 5627 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5628 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5629 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5630 | } |
| 5631 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5632 | *moveto_mod = mod; |
| 5633 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5634 | } |
| 5635 | |
| 5636 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5637 | * @brief Move context @p set to the root. Handles absolute path. |
| 5638 | * Result is LYXP_SET_NODE_SET. |
| 5639 | * |
| 5640 | * @param[in,out] set Set to use. |
| 5641 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5642 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5643 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5644 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5645 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5646 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5647 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5648 | |
| 5649 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5650 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 5651 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5652 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5653 | set->type = LYXP_SET_NODE_SET; |
| 5654 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5655 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 5656 | set->non_child_axis = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5657 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5658 | |
| 5659 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5660 | } |
| 5661 | |
| 5662 | /** |
| 5663 | * @brief Check @p node as a part of NameTest processing. |
| 5664 | * |
| 5665 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5666 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5667 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5668 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5669 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5670 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5671 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
Michal Vasko | cd2c88a | 2022-06-07 10:54:34 +0200 | [diff] [blame] | 5672 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5673 | */ |
| 5674 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5675 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5676 | const char *node_name, const struct lys_module *moveto_mod, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5677 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5678 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5679 | assert(node_type == set->root_type); |
| 5680 | |
| 5681 | if (node_name || moveto_mod) { |
| 5682 | /* root will not match a specific node */ |
| 5683 | return LY_ENOT; |
| 5684 | } |
| 5685 | return LY_SUCCESS; |
| 5686 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5687 | /* other types will not match */ |
| 5688 | return LY_ENOT; |
| 5689 | } |
| 5690 | |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5691 | if (!node->schema) { |
| 5692 | /* opaque node never matches */ |
| 5693 | return LY_ENOT; |
| 5694 | } |
| 5695 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5696 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5697 | if (moveto_mod) { |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5698 | if ((set->ctx == LYD_CTX(node)) && (node->schema->module != moveto_mod)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5699 | return LY_ENOT; |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5700 | } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->module->name, moveto_mod->name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5701 | return LY_ENOT; |
| 5702 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5703 | } |
| 5704 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5705 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5706 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5707 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5708 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5709 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5710 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5714 | if (node_name) { |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5715 | if ((set->ctx == LYD_CTX(node)) && (node->schema->name != node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5716 | return LY_ENOT; |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5717 | } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->name, node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5718 | return LY_ENOT; |
| 5719 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5720 | } |
| 5721 | |
Michal Vasko | 6d65712 | 2022-10-19 14:38:35 +0200 | [diff] [blame] | 5722 | /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */ |
| 5723 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE) && |
| 5724 | (node != set->cur_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5725 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5726 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5727 | |
| 5728 | /* match */ |
| 5729 | return LY_SUCCESS; |
| 5730 | } |
| 5731 | |
| 5732 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5733 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5734 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5735 | * @param[in] iter Last returned node. |
| 5736 | * @param[in] stop Node to stop the search on and not return. |
| 5737 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5738 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5739 | static const struct lyd_node * |
| 5740 | moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5741 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5742 | const struct lyd_node *next = NULL; |
| 5743 | |
| 5744 | /* 1) child */ |
| 5745 | next = lyd_child(iter); |
| 5746 | if (!next) { |
| 5747 | if (iter == stop) { |
| 5748 | /* reached stop, no more descendants */ |
| 5749 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5750 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5751 | /* 2) child next sibling */ |
| 5752 | next = iter->next; |
| 5753 | } |
| 5754 | while (!next) { |
| 5755 | iter = lyd_parent(iter); |
| 5756 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5757 | return NULL; |
| 5758 | } |
| 5759 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5760 | } |
| 5761 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5762 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5766 | * @brief Get the next node in a backward DFS. |
| 5767 | * |
| 5768 | * @param[in] iter Last returned node. |
| 5769 | * @param[in] stop Node to stop the search on and not return. |
| 5770 | * @return Next node, NULL if there are no more. |
| 5771 | */ |
| 5772 | static const struct lyd_node * |
| 5773 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5774 | { |
| 5775 | const struct lyd_node *next = NULL; |
| 5776 | |
| 5777 | /* 1) previous sibling innermost last child */ |
| 5778 | next = iter->prev->next ? iter->prev : NULL; |
| 5779 | while (next && lyd_child(next)) { |
| 5780 | next = lyd_child(next); |
| 5781 | next = next->prev; |
| 5782 | } |
| 5783 | |
| 5784 | if (!next) { |
| 5785 | /* 2) parent */ |
| 5786 | iter = lyd_parent(iter); |
| 5787 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5788 | return NULL; |
| 5789 | } |
| 5790 | next = iter; |
| 5791 | } |
| 5792 | |
| 5793 | return next; |
| 5794 | } |
| 5795 | |
| 5796 | /** |
| 5797 | * @brief Get the first node on an axis for a context node. |
| 5798 | * |
| 5799 | * @param[in,out] iter NULL, updated to the next node. |
| 5800 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5801 | * @param[in] node Context node. |
| 5802 | * @param[in] node_type Type of @p node. |
| 5803 | * @param[in] axis Axis to use. |
| 5804 | * @param[in] set XPath set with the general context. |
| 5805 | * @return LY_SUCCESS on success. |
| 5806 | * @return LY_ENOTFOUND if no next node found. |
| 5807 | */ |
| 5808 | static LY_ERR |
| 5809 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5810 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5811 | { |
| 5812 | const struct lyd_node *next = NULL; |
| 5813 | enum lyxp_node_type next_type = 0; |
| 5814 | |
| 5815 | assert(!*iter); |
| 5816 | assert(!*iter_type); |
| 5817 | |
| 5818 | switch (axis) { |
| 5819 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5820 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5821 | case LYXP_AXIS_SELF: |
| 5822 | /* return the context node */ |
| 5823 | next = node; |
| 5824 | next_type = node_type; |
| 5825 | break; |
| 5826 | |
| 5827 | case LYXP_AXIS_ANCESTOR: |
| 5828 | case LYXP_AXIS_PARENT: |
| 5829 | if (node_type == LYXP_NODE_ELEM) { |
| 5830 | next = lyd_parent(node); |
| 5831 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5832 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5833 | next = node; |
| 5834 | next_type = LYXP_NODE_ELEM; |
| 5835 | } else if (node_type == LYXP_NODE_META) { |
| 5836 | next = ((struct lyd_meta *)node)->parent; |
| 5837 | next_type = LYXP_NODE_ELEM; |
| 5838 | } /* else root does not have a parent */ |
| 5839 | break; |
| 5840 | |
| 5841 | case LYXP_AXIS_CHILD: |
| 5842 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5843 | assert(!node); |
| 5844 | |
| 5845 | /* search in all the trees */ |
| 5846 | next = set->tree; |
| 5847 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5848 | } else { |
| 5849 | /* search in children */ |
| 5850 | next = lyd_child(node); |
| 5851 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5852 | } |
| 5853 | break; |
| 5854 | |
| 5855 | case LYXP_AXIS_DESCENDANT: |
| 5856 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5857 | /* top-level nodes */ |
| 5858 | next = set->tree; |
| 5859 | next_type = LYXP_NODE_ELEM; |
| 5860 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5861 | /* start from the context node */ |
| 5862 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5863 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5864 | } /* else no children */ |
| 5865 | break; |
| 5866 | |
| 5867 | case LYXP_AXIS_FOLLOWING: |
| 5868 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5869 | if (node_type == LYXP_NODE_ELEM) { |
| 5870 | /* first next sibling */ |
| 5871 | next = node->next; |
| 5872 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5873 | } /* else no sibling */ |
| 5874 | break; |
| 5875 | |
| 5876 | case LYXP_AXIS_PRECEDING: |
| 5877 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5878 | /* skip ancestors */ |
| 5879 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5880 | assert(next); |
| 5881 | next_type = LYXP_NODE_ELEM; |
| 5882 | } /* else no sibling */ |
| 5883 | break; |
| 5884 | |
| 5885 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5886 | if (node_type == LYXP_NODE_ELEM) { |
| 5887 | /* first previous sibling */ |
| 5888 | next = node->prev->next ? node->prev : NULL; |
| 5889 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5890 | } /* else no sibling */ |
| 5891 | break; |
| 5892 | |
| 5893 | case LYXP_AXIS_ATTRIBUTE: |
| 5894 | /* handled specially */ |
| 5895 | assert(0); |
| 5896 | LOGINT(set->ctx); |
| 5897 | break; |
| 5898 | } |
| 5899 | |
| 5900 | *iter = next; |
| 5901 | *iter_type = next_type; |
| 5902 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5903 | } |
| 5904 | |
| 5905 | /** |
| 5906 | * @brief Iterate over all nodes on an axis for a context node. |
| 5907 | * |
| 5908 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5909 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5910 | * @param[in] node Context node. |
| 5911 | * @param[in] node_type Type of @p node. |
| 5912 | * @param[in] axis Axis to use. |
| 5913 | * @param[in] set XPath set with the general context. |
| 5914 | * @return LY_SUCCESS on success. |
| 5915 | * @return LY_ENOTFOUND if no next node found. |
| 5916 | */ |
| 5917 | static LY_ERR |
| 5918 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5919 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5920 | { |
| 5921 | const struct lyd_node *next = NULL; |
| 5922 | enum lyxp_node_type next_type = 0; |
| 5923 | |
| 5924 | if (!*iter_type) { |
| 5925 | /* first returned node */ |
| 5926 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5927 | } |
| 5928 | |
| 5929 | switch (axis) { |
| 5930 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5931 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5932 | /* fake first ancestor, we returned self before */ |
| 5933 | *iter = NULL; |
| 5934 | *iter_type = 0; |
| 5935 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 5936 | } /* else continue ancestor */ |
| 5937 | |
| 5938 | /* fallthrough */ |
| 5939 | case LYXP_AXIS_ANCESTOR: |
| 5940 | if (*iter_type == LYXP_NODE_ELEM) { |
| 5941 | /* iter parent */ |
| 5942 | next = lyd_parent(*iter); |
| 5943 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5944 | } /* else root, no ancestors */ |
| 5945 | break; |
| 5946 | |
| 5947 | case LYXP_AXIS_CHILD: |
| 5948 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5949 | |
| 5950 | /* next sibling (child) */ |
| 5951 | next = (*iter)->next; |
| 5952 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5953 | break; |
| 5954 | |
| 5955 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5956 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5957 | /* fake first descendant, we returned self before */ |
| 5958 | *iter = NULL; |
| 5959 | *iter_type = 0; |
| 5960 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 5961 | } /* else continue descendant */ |
| 5962 | |
| 5963 | /* fallthrough */ |
| 5964 | case LYXP_AXIS_DESCENDANT: |
| 5965 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5966 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 5967 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5968 | break; |
| 5969 | |
| 5970 | case LYXP_AXIS_FOLLOWING: |
| 5971 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5972 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 5973 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5974 | break; |
| 5975 | |
| 5976 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5977 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5978 | |
| 5979 | /* next sibling */ |
| 5980 | next = (*iter)->next; |
| 5981 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5982 | break; |
| 5983 | |
| 5984 | case LYXP_AXIS_PARENT: |
| 5985 | case LYXP_AXIS_SELF: |
| 5986 | /* parent/self was returned before */ |
| 5987 | break; |
| 5988 | |
| 5989 | case LYXP_AXIS_PRECEDING: |
| 5990 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5991 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 5992 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5993 | break; |
| 5994 | |
| 5995 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5996 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5997 | |
| 5998 | /* previous sibling */ |
| 5999 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 6000 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6001 | break; |
| 6002 | |
| 6003 | case LYXP_AXIS_ATTRIBUTE: |
| 6004 | /* handled specially */ |
| 6005 | assert(0); |
| 6006 | LOGINT(set->ctx); |
| 6007 | break; |
| 6008 | } |
| 6009 | |
| 6010 | *iter = next; |
| 6011 | *iter_type = next_type; |
| 6012 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6013 | } |
| 6014 | |
| 6015 | /** |
| 6016 | * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6017 | * |
| 6018 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6019 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6020 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6021 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6022 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6023 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6024 | */ |
| 6025 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6026 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6027 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6028 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6029 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6030 | const struct lyd_node *iter; |
| 6031 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6032 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6033 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6034 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6035 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6036 | return LY_SUCCESS; |
| 6037 | } |
| 6038 | |
| 6039 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6040 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6041 | return LY_EVALID; |
| 6042 | } |
| 6043 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6044 | /* init result set */ |
| 6045 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6047 | for (i = 0; i < set->used; ++i) { |
| 6048 | /* iterate over all the nodes on the axis of the node */ |
| 6049 | iter = NULL; |
| 6050 | iter_type = 0; |
| 6051 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6052 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6053 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6054 | rc = r; |
| 6055 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6056 | } else if (r) { |
| 6057 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6058 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6059 | |
| 6060 | /* check for duplicates if they are possible */ |
| 6061 | switch (axis) { |
| 6062 | case LYXP_AXIS_ANCESTOR: |
| 6063 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6064 | case LYXP_AXIS_DESCENDANT: |
| 6065 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6066 | case LYXP_AXIS_FOLLOWING: |
| 6067 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6068 | case LYXP_AXIS_PARENT: |
| 6069 | case LYXP_AXIS_PRECEDING: |
| 6070 | case LYXP_AXIS_PRECEDING_SIBLING: |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6071 | result.non_child_axis = 1; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6072 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6073 | continue; |
| 6074 | } |
| 6075 | break; |
| 6076 | case LYXP_AXIS_CHILD: |
| 6077 | case LYXP_AXIS_SELF: |
| 6078 | break; |
| 6079 | case LYXP_AXIS_ATTRIBUTE: |
| 6080 | /* handled specially */ |
| 6081 | assert(0); |
| 6082 | LOGINT(set->ctx); |
| 6083 | break; |
| 6084 | } |
| 6085 | |
| 6086 | /* matching node */ |
| 6087 | set_insert_node(&result, iter, 0, iter_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6088 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6089 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6090 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6091 | /* move result to the set */ |
| 6092 | lyxp_set_free_content(set); |
| 6093 | *set = result; |
| 6094 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6095 | |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6096 | /* sort the final set if the document order could have been broken */ |
| 6097 | if (set->non_child_axis) { |
| 6098 | set_sort(set); |
| 6099 | } else { |
| 6100 | assert(!set_sort(set)); |
| 6101 | } |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6102 | |
| 6103 | cleanup: |
| 6104 | lyxp_set_free_content(&result); |
| 6105 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6106 | } |
| 6107 | |
| 6108 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6109 | * @brief Move context @p set to child nodes using hashes. Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6110 | * |
| 6111 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6112 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6113 | * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6114 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6115 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6116 | */ |
| 6117 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6118 | moveto_node_hash_child(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates, |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6119 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6120 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6121 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6122 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6123 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6124 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6125 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6126 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6127 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6128 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6129 | /* init result set */ |
| 6130 | set_init(&result, set); |
| 6131 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6132 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6133 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6134 | } |
| 6135 | |
| 6136 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6137 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6138 | ret = LY_EVALID; |
| 6139 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | /* context check for all the nodes since we have the schema node */ |
| 6143 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6144 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6145 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6146 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6147 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6148 | lyxp_set_free_content(set); |
| 6149 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6150 | } |
| 6151 | |
| 6152 | /* create specific data instance if needed */ |
| 6153 | if (scnode->nodetype == LYS_LIST) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 6154 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6155 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6156 | LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6157 | } |
| 6158 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6159 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6160 | siblings = NULL; |
| 6161 | |
| 6162 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6163 | assert(!set->val.nodes[i].node); |
| 6164 | |
| 6165 | /* search in all the trees */ |
| 6166 | siblings = set->tree; |
| 6167 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6168 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6169 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6170 | } |
| 6171 | |
| 6172 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6173 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6174 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6175 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6176 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6177 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6178 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6179 | |
| 6180 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6181 | if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6182 | ret = LY_EINCOMPLETE; |
| 6183 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | if (sub) { |
| 6187 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6188 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6189 | } |
| 6190 | } |
| 6191 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6192 | /* move result to the set */ |
| 6193 | lyxp_set_free_content(set); |
| 6194 | *set = result; |
| 6195 | result.type = LYXP_SET_NUMBER; |
| 6196 | assert(!set_sort(set)); |
| 6197 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6198 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6199 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6200 | lyd_free_tree(inst); |
| 6201 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6202 | } |
| 6203 | |
| 6204 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6205 | * @brief Check @p node as a part of schema NameTest processing. |
| 6206 | * |
| 6207 | * @param[in] node Schema node to check. |
| 6208 | * @param[in] ctx_scnode Context node. |
| 6209 | * @param[in] set Set to read general context from. |
| 6210 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6211 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6212 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6213 | */ |
| 6214 | static LY_ERR |
| 6215 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6216 | const char *node_name, const struct lys_module *moveto_mod) |
| 6217 | { |
| 6218 | if (!moveto_mod && node_name) { |
| 6219 | switch (set->format) { |
| 6220 | case LY_VALUE_SCHEMA: |
| 6221 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6222 | /* use current module */ |
| 6223 | moveto_mod = set->cur_mod; |
| 6224 | break; |
| 6225 | case LY_VALUE_JSON: |
| 6226 | case LY_VALUE_LYB: |
| 6227 | case LY_VALUE_STR_NS: |
| 6228 | /* inherit module of the context node, if any */ |
| 6229 | if (ctx_scnode) { |
| 6230 | moveto_mod = ctx_scnode->module; |
| 6231 | } |
| 6232 | break; |
| 6233 | case LY_VALUE_CANON: |
| 6234 | case LY_VALUE_XML: |
| 6235 | /* not defined */ |
| 6236 | LOGINT(set->ctx); |
| 6237 | return LY_EINVAL; |
| 6238 | } |
| 6239 | } |
| 6240 | |
| 6241 | if (!node) { |
| 6242 | /* root will not match a specific node */ |
| 6243 | if (node_name || moveto_mod) { |
| 6244 | return LY_ENOT; |
| 6245 | } |
| 6246 | return LY_SUCCESS; |
| 6247 | } |
| 6248 | |
| 6249 | /* module check */ |
| 6250 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6251 | return LY_ENOT; |
| 6252 | } |
| 6253 | |
| 6254 | /* context check */ |
| 6255 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6256 | return LY_EINVAL; |
| 6257 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6258 | return LY_EINVAL; |
| 6259 | } |
| 6260 | |
| 6261 | /* name check */ |
| 6262 | if (node_name && (node->name != node_name)) { |
| 6263 | return LY_ENOT; |
| 6264 | } |
| 6265 | |
| 6266 | /* match */ |
| 6267 | return LY_SUCCESS; |
| 6268 | } |
| 6269 | |
| 6270 | /** |
| 6271 | * @brief Get the next node in a forward schema node DFS. |
| 6272 | * |
| 6273 | * @param[in] iter Last returned node. |
| 6274 | * @param[in] stop Node to stop the search on and not return. |
| 6275 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6276 | * @return Next node, NULL if there are no more. |
| 6277 | */ |
| 6278 | static const struct lysc_node * |
| 6279 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6280 | { |
| 6281 | const struct lysc_node *next = NULL; |
| 6282 | |
| 6283 | next = lysc_node_child(iter); |
| 6284 | if (!next) { |
| 6285 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6286 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6287 | /* we are done, no next element to process */ |
| 6288 | return NULL; |
| 6289 | } |
| 6290 | |
| 6291 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6292 | } |
| 6293 | while (!next && iter) { |
| 6294 | /* parent is already processed, go to its sibling */ |
| 6295 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6296 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6297 | /* we are done, no next element to process */ |
| 6298 | return NULL; |
| 6299 | } |
| 6300 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6301 | } |
| 6302 | |
| 6303 | return next; |
| 6304 | } |
| 6305 | |
| 6306 | /** |
| 6307 | * @brief Consider schema node based on its in_ctx enum value. |
| 6308 | * |
| 6309 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6310 | * @param[in] axis Axis to use. |
| 6311 | * @return LY_SUCCESS on success. |
| 6312 | * @return LY_ENOT if the node should not be returned. |
| 6313 | */ |
| 6314 | static LY_ERR |
| 6315 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6316 | { |
| 6317 | switch (axis) { |
| 6318 | case LYXP_AXIS_SELF: |
| 6319 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6320 | /* additionally put the start node into context */ |
| 6321 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6322 | return LY_SUCCESS; |
| 6323 | } |
| 6324 | break; |
| 6325 | case LYXP_AXIS_PARENT: |
| 6326 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6327 | case LYXP_AXIS_ANCESTOR: |
| 6328 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6329 | case LYXP_AXIS_DESCENDANT: |
| 6330 | case LYXP_AXIS_FOLLOWING: |
| 6331 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6332 | case LYXP_AXIS_PRECEDING: |
| 6333 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6334 | case LYXP_AXIS_CHILD: |
| 6335 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6336 | /* remember that context node was used */ |
| 6337 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6338 | return LY_SUCCESS; |
| 6339 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6340 | /* traversed */ |
| 6341 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6342 | return LY_SUCCESS; |
| 6343 | } |
| 6344 | break; |
| 6345 | case LYXP_AXIS_ATTRIBUTE: |
| 6346 | /* unreachable */ |
| 6347 | assert(0); |
| 6348 | LOGINT(NULL); |
| 6349 | break; |
| 6350 | } |
| 6351 | |
| 6352 | return LY_ENOT; |
| 6353 | } |
| 6354 | |
| 6355 | /** |
| 6356 | * @brief Get previous sibling for a schema node. |
| 6357 | * |
| 6358 | * @param[in] scnode Schema node. |
| 6359 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6360 | * @return Previous sibling, NULL if none. |
| 6361 | */ |
| 6362 | static const struct lysc_node * |
| 6363 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6364 | { |
| 6365 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6366 | |
| 6367 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6368 | if (next == scnode) { |
| 6369 | break; |
| 6370 | } |
| 6371 | |
| 6372 | prev = next; |
| 6373 | } |
| 6374 | |
| 6375 | return prev; |
| 6376 | } |
| 6377 | |
| 6378 | /** |
| 6379 | * @brief Get the first schema node on an axis for a context node. |
| 6380 | * |
| 6381 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6382 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6383 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6384 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6385 | * @param[in] scnode Context node. |
| 6386 | * @param[in] node_type Type of @p scnode. |
| 6387 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6388 | * @param[in] axis Axis to use. |
| 6389 | * @param[in] set XPath set with the general context. |
| 6390 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6391 | * @return LY_SUCCESS on success. |
| 6392 | * @return LY_ENOTFOUND if no next node found. |
| 6393 | */ |
| 6394 | static LY_ERR |
| 6395 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6396 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6397 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6398 | { |
| 6399 | const struct lysc_node *next = NULL; |
| 6400 | enum lyxp_node_type next_type = 0; |
| 6401 | |
| 6402 | assert(!*iter); |
| 6403 | assert(!*iter_type); |
| 6404 | |
| 6405 | *iter_mod = NULL; |
| 6406 | *iter_mod_idx = 0; |
| 6407 | |
| 6408 | switch (axis) { |
| 6409 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6410 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6411 | case LYXP_AXIS_SELF: |
| 6412 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6413 | /* just return the node */ |
| 6414 | next = scnode; |
| 6415 | next_type = node_type; |
| 6416 | } |
| 6417 | break; |
| 6418 | |
| 6419 | case LYXP_AXIS_ANCESTOR: |
| 6420 | case LYXP_AXIS_PARENT: |
| 6421 | if (node_type == LYXP_NODE_ELEM) { |
| 6422 | next = lysc_data_parent(scnode); |
| 6423 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6424 | } /* else no parent */ |
| 6425 | break; |
| 6426 | |
| 6427 | case LYXP_AXIS_DESCENDANT: |
| 6428 | case LYXP_AXIS_CHILD: |
| 6429 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6430 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6431 | * it can be in a top-level augment */ |
| 6432 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6433 | /* module may not be implemented or not compiled yet */ |
| 6434 | if (!(*iter_mod)->compiled) { |
| 6435 | continue; |
| 6436 | } |
| 6437 | |
| 6438 | /* get next node */ |
| 6439 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6440 | next_type = LYXP_NODE_ELEM; |
| 6441 | break; |
| 6442 | } |
| 6443 | } |
| 6444 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6445 | /* get next node */ |
| 6446 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6447 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6448 | } |
| 6449 | break; |
| 6450 | |
| 6451 | case LYXP_AXIS_FOLLOWING: |
| 6452 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6453 | if (node_type == LYXP_NODE_ELEM) { |
| 6454 | /* first next sibling */ |
| 6455 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6456 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6457 | } /* else no sibling */ |
| 6458 | break; |
| 6459 | |
| 6460 | case LYXP_AXIS_PRECEDING: |
| 6461 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6462 | if (node_type == LYXP_NODE_ELEM) { |
| 6463 | /* first parent sibling */ |
| 6464 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6465 | if (next == scnode) { |
| 6466 | /* no preceding sibling */ |
| 6467 | next = NULL; |
| 6468 | } |
| 6469 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6470 | } /* else no sibling */ |
| 6471 | break; |
| 6472 | |
| 6473 | case LYXP_AXIS_ATTRIBUTE: |
| 6474 | /* unreachable */ |
| 6475 | assert(0); |
| 6476 | LOGINT(set->ctx); |
| 6477 | break; |
| 6478 | } |
| 6479 | |
| 6480 | *iter = next; |
| 6481 | *iter_type = next_type; |
| 6482 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6483 | } |
| 6484 | |
| 6485 | /** |
| 6486 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6487 | * |
| 6488 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6489 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6490 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6491 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6492 | * @param[in] scnode Context node. |
| 6493 | * @param[in] node_type Type of @p scnode. |
| 6494 | * @param[in] axis Axis to use. |
| 6495 | * @param[in] set XPath set with the general context. |
| 6496 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6497 | * @return LY_SUCCESS on success. |
| 6498 | * @return LY_ENOTFOUND if no next node found. |
| 6499 | */ |
| 6500 | static LY_ERR |
| 6501 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6502 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6503 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6504 | { |
| 6505 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6506 | enum lyxp_node_type next_type = 0; |
| 6507 | |
| 6508 | if (!*iter_type) { |
| 6509 | /* first returned node */ |
| 6510 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6511 | getnext_opts); |
| 6512 | } |
| 6513 | |
| 6514 | switch (axis) { |
| 6515 | case LYXP_AXIS_PARENT: |
| 6516 | case LYXP_AXIS_SELF: |
| 6517 | /* parent/self was returned before */ |
| 6518 | break; |
| 6519 | |
| 6520 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6521 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6522 | /* fake first ancestor, we returned self before */ |
| 6523 | *iter = NULL; |
| 6524 | *iter_type = 0; |
| 6525 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6526 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
| 6527 | } /* else continue ancestor */ |
| 6528 | |
| 6529 | /* fallthrough */ |
| 6530 | case LYXP_AXIS_ANCESTOR: |
| 6531 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6532 | next = lysc_data_parent(*iter); |
| 6533 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6534 | } /* else no ancestor */ |
| 6535 | break; |
| 6536 | |
| 6537 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6538 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6539 | /* fake first descendant, we returned self before */ |
| 6540 | *iter = NULL; |
| 6541 | *iter_type = 0; |
| 6542 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6543 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6544 | } /* else DFS until context node */ |
| 6545 | dfs_stop = scnode; |
| 6546 | |
| 6547 | /* fallthrough */ |
| 6548 | case LYXP_AXIS_DESCENDANT: |
| 6549 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6550 | /* DFS until the context node */ |
| 6551 | dfs_stop = scnode; |
| 6552 | } |
| 6553 | |
| 6554 | /* fallthrough */ |
| 6555 | case LYXP_AXIS_PRECEDING: |
| 6556 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6557 | /* DFS until the previous sibling */ |
| 6558 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6559 | assert(dfs_stop); |
| 6560 | |
| 6561 | if (*iter == dfs_stop) { |
| 6562 | /* we are done */ |
| 6563 | break; |
| 6564 | } |
| 6565 | } |
| 6566 | |
| 6567 | /* fallthrough */ |
| 6568 | case LYXP_AXIS_FOLLOWING: |
| 6569 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6570 | /* DFS through the whole module */ |
| 6571 | dfs_stop = NULL; |
| 6572 | } |
| 6573 | |
| 6574 | /* nested nodes */ |
| 6575 | assert(*iter); |
| 6576 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6577 | if (next) { |
| 6578 | next_type = LYXP_NODE_ELEM; |
| 6579 | break; |
| 6580 | } /* else get next top-level node just like a child */ |
| 6581 | |
| 6582 | /* fallthrough */ |
| 6583 | case LYXP_AXIS_CHILD: |
| 6584 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6585 | if (!*iter_mod) { |
| 6586 | /* nodes from a single module */ |
| 6587 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6588 | next_type = LYXP_NODE_ELEM; |
| 6589 | break; |
| 6590 | } |
| 6591 | |
| 6592 | assert(scnode); |
Michal Vasko | a353cce | 2022-11-14 10:09:55 +0100 | [diff] [blame] | 6593 | if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6594 | /* iterating over top-level nodes, find next */ |
| 6595 | while (lysc_data_parent(*iter)) { |
| 6596 | *iter = lysc_data_parent(*iter); |
| 6597 | } |
| 6598 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6599 | next_type = LYXP_NODE_ELEM; |
| 6600 | break; |
| 6601 | } |
| 6602 | } |
| 6603 | } |
| 6604 | |
| 6605 | while (*iter_mod) { |
| 6606 | /* module top-level nodes */ |
| 6607 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6608 | next_type = LYXP_NODE_ELEM; |
| 6609 | break; |
| 6610 | } |
| 6611 | |
| 6612 | /* get next module */ |
| 6613 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6614 | /* module may not be implemented or not compiled yet */ |
| 6615 | if ((*iter_mod)->compiled) { |
| 6616 | break; |
| 6617 | } |
| 6618 | } |
| 6619 | |
| 6620 | /* new module, start over */ |
| 6621 | *iter = NULL; |
| 6622 | } |
| 6623 | break; |
| 6624 | |
| 6625 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6626 | assert(*iter); |
| 6627 | |
| 6628 | /* next parent sibling until scnode */ |
| 6629 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6630 | if (next == scnode) { |
| 6631 | /* no previous sibling */ |
| 6632 | next = NULL; |
| 6633 | } |
| 6634 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6635 | break; |
| 6636 | |
| 6637 | case LYXP_AXIS_ATTRIBUTE: |
| 6638 | /* unreachable */ |
| 6639 | assert(0); |
| 6640 | LOGINT(set->ctx); |
| 6641 | break; |
| 6642 | } |
| 6643 | |
| 6644 | *iter = next; |
| 6645 | *iter_type = next_type; |
| 6646 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6647 | } |
| 6648 | |
| 6649 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6650 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6651 | * |
| 6652 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6653 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6654 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6655 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6656 | * @param[in] options XPath options. |
| 6657 | * @return LY_ERR |
| 6658 | */ |
| 6659 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6660 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6661 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6662 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6663 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6664 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6665 | const struct lys_module *mod; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6666 | const struct lysc_node *iter; |
| 6667 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6668 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6669 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6670 | return LY_SUCCESS; |
| 6671 | } |
| 6672 | |
| 6673 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6674 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6675 | return LY_EVALID; |
| 6676 | } |
| 6677 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6678 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6679 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6680 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6681 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6682 | } |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 6683 | if (options & LYXP_SCNODE_SCHEMAMOUNT) { |
| 6684 | getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT; |
| 6685 | } |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6686 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6687 | orig_used = set->used; |
| 6688 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6689 | /* update in_ctx first */ |
| 6690 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6691 | /* not usable, skip */ |
| 6692 | continue; |
| 6693 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6694 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6695 | iter = NULL; |
| 6696 | iter_type = 0; |
| 6697 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6698 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6699 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6700 | continue; |
| 6701 | } |
| 6702 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6703 | /* insert */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6704 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, axis, &idx)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6705 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6706 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6707 | if ((idx < orig_used) && (idx > i)) { |
| 6708 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6709 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6710 | } |
| 6711 | } |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 6712 | |
| 6713 | if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) && |
| 6714 | (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode, |
| 6715 | moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) { |
| 6716 | /* there is a matching node from an extension, use it */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6717 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx)); |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 6718 | if ((idx < orig_used) && (idx > i)) { |
| 6719 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6720 | temp_ctx = 1; |
| 6721 | } |
| 6722 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6723 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6724 | |
| 6725 | /* correct temporary in_ctx values */ |
| 6726 | if (temp_ctx) { |
| 6727 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6728 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6729 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6730 | } |
| 6731 | } |
| 6732 | } |
| 6733 | |
| 6734 | return LY_SUCCESS; |
| 6735 | } |
| 6736 | |
| 6737 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6738 | * @brief Move context @p set to a child node and all its descendants. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6739 | * Context position aware. |
| 6740 | * |
| 6741 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6742 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6743 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6744 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6745 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6746 | */ |
| 6747 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6748 | moveto_node_alldesc_child(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6749 | { |
| 6750 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6751 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6752 | struct lyxp_set ret_set; |
| 6753 | LY_ERR rc; |
| 6754 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6755 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6756 | return LY_SUCCESS; |
| 6757 | } |
| 6758 | |
| 6759 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6760 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6761 | return LY_EVALID; |
| 6762 | } |
| 6763 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6764 | /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6765 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6766 | LY_CHECK_RET(rc); |
| 6767 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6768 | /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6769 | set_init(&ret_set, set); |
| 6770 | for (i = 0; i < set->used; ++i) { |
| 6771 | |
| 6772 | /* TREE DFS */ |
| 6773 | start = set->val.nodes[i].node; |
| 6774 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6775 | rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6776 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6777 | /* add matching node into result set */ |
| 6778 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6779 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6780 | /* the node is a duplicate, we'll process it later in the set */ |
| 6781 | goto skip_children; |
| 6782 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6783 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6784 | return rc; |
| 6785 | } else if (rc == LY_EINVAL) { |
| 6786 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6787 | } |
| 6788 | |
| 6789 | /* TREE DFS NEXT ELEM */ |
| 6790 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6791 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6792 | if (!next) { |
| 6793 | skip_children: |
| 6794 | /* no children, so try siblings, but only if it's not the start, |
| 6795 | * that is considered to be the root and it's siblings are not traversed */ |
| 6796 | if (elem != start) { |
| 6797 | next = elem->next; |
| 6798 | } else { |
| 6799 | break; |
| 6800 | } |
| 6801 | } |
| 6802 | while (!next) { |
| 6803 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6804 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6805 | /* we are done, no next element to process */ |
| 6806 | break; |
| 6807 | } |
| 6808 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6809 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6810 | next = elem->next; |
| 6811 | } |
| 6812 | } |
| 6813 | } |
| 6814 | |
| 6815 | /* make the temporary set the current one */ |
| 6816 | ret_set.ctx_pos = set->ctx_pos; |
| 6817 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6818 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6819 | memcpy(set, &ret_set, sizeof *set); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6820 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6821 | |
| 6822 | return LY_SUCCESS; |
| 6823 | } |
| 6824 | |
| 6825 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6826 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6827 | * Result is LYXP_SET_NODE_SET. |
| 6828 | * |
| 6829 | * @param[in] set Set to use. |
| 6830 | * @param[in] start Start node whose subtree to add. |
| 6831 | * @param[in] start_idx Index of @p start in @p set. |
| 6832 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6833 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6834 | * @param[in] options XPath options. |
| 6835 | * @return LY_ERR value. |
| 6836 | */ |
| 6837 | static LY_ERR |
| 6838 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6839 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6840 | { |
| 6841 | const struct lysc_node *next, *elem; |
| 6842 | uint32_t idx; |
| 6843 | LY_ERR rc; |
| 6844 | |
| 6845 | /* TREE DFS */ |
| 6846 | for (elem = next = start; elem; elem = next) { |
| 6847 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6848 | /* schema-only nodes, skip root */ |
| 6849 | goto next_iter; |
| 6850 | } |
| 6851 | |
| 6852 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6853 | if (!rc) { |
| 6854 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6855 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6856 | if (idx > start_idx) { |
| 6857 | /* we will process it later in the set */ |
| 6858 | goto skip_children; |
| 6859 | } |
| 6860 | } else { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6861 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL)); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6862 | } |
| 6863 | } else if (rc == LY_EINVAL) { |
| 6864 | goto skip_children; |
| 6865 | } |
| 6866 | |
| 6867 | next_iter: |
| 6868 | /* TREE DFS NEXT ELEM */ |
| 6869 | /* select element for the next run - children first */ |
| 6870 | next = lysc_node_child(elem); |
| 6871 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6872 | next = next->next; |
| 6873 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6874 | next = next->next; |
| 6875 | } |
| 6876 | if (!next) { |
| 6877 | skip_children: |
| 6878 | /* no children, so try siblings, but only if it's not the start, |
| 6879 | * that is considered to be the root and it's siblings are not traversed */ |
| 6880 | if (elem != start) { |
| 6881 | next = elem->next; |
| 6882 | } else { |
| 6883 | break; |
| 6884 | } |
| 6885 | } |
| 6886 | while (!next) { |
| 6887 | /* no siblings, go back through the parents */ |
| 6888 | if (elem->parent == start) { |
| 6889 | /* we are done, no next element to process */ |
| 6890 | break; |
| 6891 | } |
| 6892 | /* parent is already processed, go to its sibling */ |
| 6893 | elem = elem->parent; |
| 6894 | next = elem->next; |
| 6895 | } |
| 6896 | } |
| 6897 | |
| 6898 | return LY_SUCCESS; |
| 6899 | } |
| 6900 | |
| 6901 | /** |
| 6902 | * @brief Move context @p set to a child schema node and all its descendants. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6903 | * |
| 6904 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6905 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6906 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6907 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6908 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6909 | */ |
| 6910 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6911 | moveto_scnode_alldesc_child(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6912 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6913 | uint32_t i, orig_used, mod_idx; |
| 6914 | const struct lys_module *mod; |
| 6915 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6916 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6917 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6918 | return LY_SUCCESS; |
| 6919 | } |
| 6920 | |
| 6921 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6922 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6923 | return LY_EVALID; |
| 6924 | } |
| 6925 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6926 | orig_used = set->used; |
| 6927 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6928 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6929 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6930 | continue; |
| 6931 | } |
| 6932 | |
| 6933 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6934 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6935 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6936 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6937 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6938 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6939 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6940 | /* traverse all top-level nodes in all the modules */ |
| 6941 | mod_idx = 0; |
| 6942 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6943 | /* module may not be implemented or not compiled yet */ |
| 6944 | if (!mod->compiled) { |
| 6945 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6946 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6947 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6948 | root = NULL; |
| 6949 | /* no getnext opts needed */ |
| 6950 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 6951 | LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6952 | } |
| 6953 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6954 | |
| 6955 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6956 | /* add all the descendants recursively */ |
| 6957 | LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6958 | } |
| 6959 | } |
| 6960 | |
| 6961 | return LY_SUCCESS; |
| 6962 | } |
| 6963 | |
| 6964 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6965 | * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6966 | * Indirectly context position aware. |
| 6967 | * |
| 6968 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6969 | * @param[in] mod Matching metadata module, NULL for any. |
| 6970 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6971 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6972 | * @return LY_ERR |
| 6973 | */ |
| 6974 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6975 | moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6976 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6977 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6978 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6979 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6980 | return LY_SUCCESS; |
| 6981 | } |
| 6982 | |
| 6983 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6984 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6985 | return LY_EVALID; |
| 6986 | } |
| 6987 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6988 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6989 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6990 | |
| 6991 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 6992 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6993 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6994 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6995 | |
| 6996 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6997 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6998 | continue; |
| 6999 | } |
| 7000 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7001 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7002 | /* match */ |
| 7003 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7004 | set->val.meta[i].meta = sub; |
| 7005 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7006 | /* pos does not change */ |
| 7007 | replaced = 1; |
| 7008 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7009 | set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_META, i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7010 | } |
| 7011 | ++i; |
| 7012 | } |
| 7013 | } |
| 7014 | } |
| 7015 | |
| 7016 | if (!replaced) { |
| 7017 | /* no match */ |
| 7018 | set_remove_node(set, i); |
| 7019 | } |
| 7020 | } |
| 7021 | |
| 7022 | return LY_SUCCESS; |
| 7023 | } |
| 7024 | |
| 7025 | /** |
| 7026 | * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards. |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7027 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7028 | * |
| 7029 | * @param[in,out] set1 Set to use for the result. |
| 7030 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7031 | * @return LY_ERR |
| 7032 | */ |
| 7033 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7034 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7035 | { |
| 7036 | LY_ERR rc; |
| 7037 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7038 | if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7039 | LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7040 | return LY_EVALID; |
| 7041 | } |
| 7042 | |
| 7043 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7044 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7045 | return LY_SUCCESS; |
| 7046 | } |
| 7047 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7048 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 7049 | /* release hidden allocated data (lyxp_set.size) */ |
| 7050 | lyxp_set_free_content(set1); |
| 7051 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7052 | memcpy(set1, set2, sizeof *set1); |
| 7053 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7054 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7055 | return LY_SUCCESS; |
| 7056 | } |
| 7057 | |
| 7058 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7059 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7060 | |
| 7061 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7062 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7063 | LY_CHECK_RET(rc); |
| 7064 | |
| 7065 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7066 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7067 | |
| 7068 | return LY_SUCCESS; |
| 7069 | } |
| 7070 | |
| 7071 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7072 | * @brief Move context @p set to an attribute in any of the descendants. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7073 | * Context position aware. |
| 7074 | * |
| 7075 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7076 | * @param[in] mod Matching metadata module, NULL for any. |
| 7077 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7078 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7079 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7080 | */ |
| 7081 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7082 | moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7083 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7084 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7085 | struct lyxp_set *set_all_desc = NULL; |
| 7086 | LY_ERR rc; |
| 7087 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7088 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7089 | return LY_SUCCESS; |
| 7090 | } |
| 7091 | |
| 7092 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7093 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7094 | return LY_EVALID; |
| 7095 | } |
| 7096 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7097 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7098 | * but it likely won't be used much, so it's a waste of time */ |
| 7099 | /* copy the context */ |
| 7100 | set_all_desc = set_copy(set); |
| 7101 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7102 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7103 | if (rc != LY_SUCCESS) { |
| 7104 | lyxp_set_free(set_all_desc); |
| 7105 | return rc; |
| 7106 | } |
| 7107 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7108 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7109 | if (rc != LY_SUCCESS) { |
| 7110 | lyxp_set_free(set_all_desc); |
| 7111 | return rc; |
| 7112 | } |
| 7113 | lyxp_set_free(set_all_desc); |
| 7114 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7115 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7116 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7117 | |
| 7118 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7119 | * we have all attributes qualified in lyd tree */ |
| 7120 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7121 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7122 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7123 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7124 | continue; |
| 7125 | } |
| 7126 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7127 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7128 | /* match */ |
| 7129 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7130 | set->val.meta[i].meta = sub; |
| 7131 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7132 | /* pos does not change */ |
| 7133 | replaced = 1; |
| 7134 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7135 | set_insert_node(set, (struct lyd_node *)sub, set->val.meta[i].pos, LYXP_NODE_META, i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7136 | } |
| 7137 | ++i; |
| 7138 | } |
| 7139 | } |
| 7140 | } |
| 7141 | |
| 7142 | if (!replaced) { |
| 7143 | /* no match */ |
| 7144 | set_remove_node(set, i); |
| 7145 | } |
| 7146 | } |
| 7147 | |
| 7148 | return LY_SUCCESS; |
| 7149 | } |
| 7150 | |
| 7151 | /** |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7152 | * @brief Move context @p set1 single item to the result of a comparison. |
| 7153 | * |
| 7154 | * @param[in] set1 First set with the item to compare. |
| 7155 | * @param[in] idx1 Index of the item in @p set1. |
| 7156 | * @param[in] set2 Second set. |
| 7157 | * @param[in] op Comparison operator to process. |
| 7158 | * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`. |
| 7159 | * @param[out] result Result of the comparison. |
| 7160 | * @return LY_ERR value. |
| 7161 | */ |
| 7162 | static LY_ERR |
| 7163 | moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op, |
| 7164 | ly_bool switch_operands, ly_bool *result) |
| 7165 | { |
| 7166 | struct lyxp_set tmp1 = {0}; |
| 7167 | LY_ERR rc = LY_SUCCESS; |
| 7168 | |
| 7169 | assert(set1->type == LYXP_SET_NODE_SET); |
| 7170 | |
| 7171 | /* cast set1 */ |
| 7172 | switch (set2->type) { |
| 7173 | case LYXP_SET_NUMBER: |
| 7174 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); |
| 7175 | break; |
| 7176 | case LYXP_SET_BOOLEAN: |
| 7177 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); |
| 7178 | break; |
| 7179 | default: |
| 7180 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); |
| 7181 | break; |
| 7182 | } |
| 7183 | LY_CHECK_GOTO(rc, cleanup); |
| 7184 | |
| 7185 | /* canonize set2 */ |
| 7186 | LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup); |
| 7187 | |
| 7188 | /* compare recursively and store the result */ |
| 7189 | if (switch_operands) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7190 | LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7191 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7192 | LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7193 | } |
| 7194 | |
| 7195 | cleanup: |
| 7196 | lyxp_set_free_content(&tmp1); |
| 7197 | return rc; |
| 7198 | } |
| 7199 | |
| 7200 | /** |
| 7201 | * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7202 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7203 | * |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7204 | * @param[in] set1 Set acting as the first operand for @p op. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7205 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7206 | * @param[in] op Comparison operator to process. |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7207 | * @param[out] result Result of the comparison. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7208 | * @return LY_ERR |
| 7209 | */ |
| 7210 | static LY_ERR |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7211 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7212 | { |
| 7213 | /* |
| 7214 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7215 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7216 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7217 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7218 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7219 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7220 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7221 | * |
| 7222 | * '=' or '!=' |
| 7223 | * BOOLEAN + BOOLEAN |
| 7224 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7225 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7226 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7227 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7228 | * NUMBER + NUMBER |
| 7229 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7230 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7231 | * STRING + STRING |
| 7232 | * |
| 7233 | * '<=', '<', '>=', '>' |
| 7234 | * NUMBER + NUMBER |
| 7235 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7236 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7237 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7238 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7239 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7240 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7241 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7242 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7243 | */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7244 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7245 | LY_ERR rc; |
| 7246 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7247 | /* iterative evaluation with node-sets */ |
| 7248 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7249 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7250 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7251 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7252 | LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7253 | |
| 7254 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7255 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7256 | return LY_SUCCESS; |
| 7257 | } |
| 7258 | } |
| 7259 | } else { |
| 7260 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7261 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7262 | LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7263 | |
| 7264 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7265 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7266 | return LY_SUCCESS; |
| 7267 | } |
| 7268 | } |
| 7269 | } |
| 7270 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7271 | /* false for all the nodes */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7272 | *result = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7273 | return LY_SUCCESS; |
| 7274 | } |
| 7275 | |
| 7276 | /* first convert properly */ |
| 7277 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7278 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7279 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7280 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7281 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7282 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7283 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7284 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7285 | LY_CHECK_RET(rc); |
| 7286 | } /* else we have 2 strings */ |
| 7287 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7288 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7289 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7290 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7291 | LY_CHECK_RET(rc); |
| 7292 | } |
| 7293 | |
| 7294 | assert(set1->type == set2->type); |
| 7295 | |
| 7296 | /* compute result */ |
| 7297 | if (op[0] == '=') { |
| 7298 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7299 | *result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7300 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7301 | *result = (set1->val.num == set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7302 | } else { |
| 7303 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7304 | *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7305 | } |
| 7306 | } else if (op[0] == '!') { |
| 7307 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7308 | *result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7309 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7310 | *result = (set1->val.num != set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7311 | } else { |
| 7312 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7313 | *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7314 | } |
| 7315 | } else { |
| 7316 | assert(set1->type == LYXP_SET_NUMBER); |
| 7317 | if (op[0] == '<') { |
| 7318 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7319 | *result = (set1->val.num <= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7320 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7321 | *result = (set1->val.num < set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7322 | } |
| 7323 | } else { |
| 7324 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7325 | *result = (set1->val.num >= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7326 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7327 | *result = (set1->val.num > set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7328 | } |
| 7329 | } |
| 7330 | } |
| 7331 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7332 | return LY_SUCCESS; |
| 7333 | } |
| 7334 | |
| 7335 | /** |
| 7336 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7337 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7338 | * |
| 7339 | * @param[in,out] set1 Set to use for the result. |
| 7340 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7341 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7342 | * @return LY_ERR |
| 7343 | */ |
| 7344 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7345 | moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7346 | { |
| 7347 | LY_ERR rc; |
| 7348 | |
| 7349 | /* unary '-' */ |
| 7350 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7351 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | LY_CHECK_RET(rc); |
| 7353 | set1->val.num *= -1; |
| 7354 | lyxp_set_free(set2); |
| 7355 | return LY_SUCCESS; |
| 7356 | } |
| 7357 | |
| 7358 | assert(set1 && set2); |
| 7359 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7360 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7362 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7363 | LY_CHECK_RET(rc); |
| 7364 | |
| 7365 | switch (op[0]) { |
| 7366 | /* '+' */ |
| 7367 | case '+': |
| 7368 | set1->val.num += set2->val.num; |
| 7369 | break; |
| 7370 | |
| 7371 | /* '-' */ |
| 7372 | case '-': |
| 7373 | set1->val.num -= set2->val.num; |
| 7374 | break; |
| 7375 | |
| 7376 | /* '*' */ |
| 7377 | case '*': |
| 7378 | set1->val.num *= set2->val.num; |
| 7379 | break; |
| 7380 | |
| 7381 | /* 'div' */ |
| 7382 | case 'd': |
| 7383 | set1->val.num /= set2->val.num; |
| 7384 | break; |
| 7385 | |
| 7386 | /* 'mod' */ |
| 7387 | case 'm': |
| 7388 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7389 | break; |
| 7390 | |
| 7391 | default: |
| 7392 | LOGINT_RET(set1->ctx); |
| 7393 | } |
| 7394 | |
| 7395 | return LY_SUCCESS; |
| 7396 | } |
| 7397 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7398 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7399 | * @brief Evaluate Predicate. Logs directly on error. |
| 7400 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7401 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7402 | * |
| 7403 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7404 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7405 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7406 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7407 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7408 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7409 | */ |
| 7410 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7411 | eval_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options, enum lyxp_axis axis) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7412 | { |
| 7413 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7414 | uint32_t i, orig_exp, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7415 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7416 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7417 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7418 | |
| 7419 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7420 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7421 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7422 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7423 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7424 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7425 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7426 | rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7427 | LY_CHECK_RET(rc); |
| 7428 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7429 | /* we (possibly) need the set sorted, it can affect the result (if the predicate result is a number) */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7430 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7431 | |
| 7432 | /* empty set, nothing to evaluate */ |
| 7433 | if (!set->used) { |
| 7434 | goto only_parse; |
| 7435 | } |
| 7436 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7437 | /* decide forward or reverse axis */ |
| 7438 | switch (axis) { |
| 7439 | case LYXP_AXIS_ANCESTOR: |
| 7440 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7441 | case LYXP_AXIS_PRECEDING: |
| 7442 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7443 | reverse_axis = 1; |
| 7444 | break; |
| 7445 | case LYXP_AXIS_DESCENDANT: |
| 7446 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7447 | case LYXP_AXIS_FOLLOWING: |
| 7448 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7449 | case LYXP_AXIS_PARENT: |
| 7450 | case LYXP_AXIS_CHILD: |
| 7451 | case LYXP_AXIS_SELF: |
| 7452 | case LYXP_AXIS_ATTRIBUTE: |
| 7453 | reverse_axis = 0; |
| 7454 | break; |
| 7455 | } |
| 7456 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7458 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7459 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7460 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7461 | set_init(&set2, set); |
| 7462 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7463 | |
| 7464 | /* remember the node context position for position() and context size for last() */ |
| 7465 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7466 | |
| 7467 | set2.ctx_pos = orig_pos; |
| 7468 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7470 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7471 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7472 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7473 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7474 | return rc; |
| 7475 | } |
| 7476 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7477 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7478 | if (set2.type == LYXP_SET_NUMBER) { |
| 7479 | if ((long long)set2.val.num == orig_pos) { |
| 7480 | set2.val.num = 1; |
| 7481 | } else { |
| 7482 | set2.val.num = 0; |
| 7483 | } |
| 7484 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7485 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7486 | |
| 7487 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7488 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7489 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7490 | } |
| 7491 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7492 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7493 | |
| 7494 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7495 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7496 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7497 | /* there is a currently-valid node */ |
| 7498 | break; |
| 7499 | } |
| 7500 | } |
| 7501 | /* empty set, nothing to evaluate */ |
| 7502 | if (i == set->used) { |
| 7503 | goto only_parse; |
| 7504 | } |
| 7505 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7506 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7507 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7508 | /* set special in_ctx to all the valid snodes */ |
| 7509 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7510 | |
| 7511 | /* use the valid snodes one-by-one */ |
| 7512 | for (i = 0; i < set->used; ++i) { |
| 7513 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7514 | continue; |
| 7515 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7516 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7517 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7518 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7519 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7520 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7521 | LY_CHECK_RET(rc); |
| 7522 | |
| 7523 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7524 | } |
| 7525 | |
| 7526 | /* restore the state as it was before the predicate */ |
| 7527 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7528 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7529 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7530 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7531 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7532 | } |
| 7533 | } |
| 7534 | |
| 7535 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7536 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7537 | set_fill_set(&set2, set); |
| 7538 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7539 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7540 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7541 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7542 | return rc; |
| 7543 | } |
| 7544 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7545 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7546 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7547 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7548 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7549 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7550 | } |
| 7551 | |
| 7552 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7553 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7554 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7555 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7556 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7557 | |
| 7558 | return LY_SUCCESS; |
| 7559 | } |
| 7560 | |
| 7561 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7562 | * @brief Evaluate Literal. Logs directly on error. |
| 7563 | * |
| 7564 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7565 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7566 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7567 | */ |
| 7568 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7569 | eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7570 | { |
| 7571 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7572 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7573 | set_fill_string(set, "", 0); |
| 7574 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7575 | set_fill_string(set, &exp->expr[exp->tok_pos[*tok_idx] + 1], exp->tok_len[*tok_idx] - 2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7576 | } |
| 7577 | } |
| 7578 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7579 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7580 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7581 | } |
| 7582 | |
| 7583 | /** |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7584 | * @brief Check that a nametest in a predicate matches a key node. |
| 7585 | * |
| 7586 | * @param[in] nametest Nametest to check. |
| 7587 | * @param[in] len Length of @p nametest. |
| 7588 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7589 | * @param[in] set Context set. |
| 7590 | * @param[in] key Expected key node. |
| 7591 | * @return LY_SUCCESS on success, |
| 7592 | * @return LY_ENOT if a predicate could not be compiled. |
| 7593 | * @return LY_ERR on any error. |
| 7594 | */ |
| 7595 | static LY_ERR |
| 7596 | eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode, |
| 7597 | const struct lyxp_set *set, const struct lysc_node *key) |
| 7598 | { |
| 7599 | const struct lys_module *mod; |
| 7600 | |
| 7601 | /* prefix (module) */ |
| 7602 | LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod)); |
| 7603 | if (mod != key->module) { |
| 7604 | return LY_ENOT; |
| 7605 | } |
| 7606 | |
| 7607 | /* node name */ |
| 7608 | if (ly_strncmp(key->name, nametest, len)) { |
| 7609 | return LY_ENOT; |
| 7610 | } |
| 7611 | |
| 7612 | return LY_SUCCESS; |
| 7613 | } |
| 7614 | |
| 7615 | /** |
| 7616 | * @brief Append a simple predicate for the node. |
| 7617 | * |
| 7618 | * @param[in] exp Full parsed XPath expression. |
| 7619 | * @param[in] tok_idx Predicate start index in @p exp. |
| 7620 | * @param[in] end_tok_idx Predicate end index in @p exp. |
| 7621 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7622 | * @param[in] set Context set. |
| 7623 | * @param[in] pred_node Node with the value referenced in the predicate. |
| 7624 | * @param[in,out] pred Predicate to append to. |
| 7625 | * @param[in,out] pred_len Length of @p pred, is updated. |
| 7626 | * @return LY_SUCCESS on success, |
| 7627 | * @return LY_ENOT if a predicate could not be compiled. |
| 7628 | * @return LY_ERR on any error. |
| 7629 | */ |
| 7630 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7631 | eval_name_test_try_compile_predicate_append(const struct lyxp_expr *exp, uint32_t tok_idx, uint32_t end_tok_idx, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7632 | const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred, |
| 7633 | uint32_t *pred_len) |
| 7634 | { |
| 7635 | LY_ERR rc = LY_SUCCESS; |
| 7636 | uint32_t i; |
| 7637 | const struct lyd_node *siblings; |
| 7638 | struct lyd_node *ctx_node; |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7639 | const struct lysc_node *sparent, *cur_scnode; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7640 | struct lyxp_expr *val_exp = NULL; |
| 7641 | struct lyxp_set set2 = {0}; |
| 7642 | char quot; |
| 7643 | |
| 7644 | /* duplicate the value expression */ |
| 7645 | LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup); |
| 7646 | |
| 7647 | /* get its atoms */ |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7648 | cur_scnode = set->cur_node ? set->cur_node->schema : NULL; |
| 7649 | LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode, |
| 7650 | ctx_scnode, &set2, LYXP_SCNODE), cleanup); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7651 | |
| 7652 | /* check whether we can compile a single predicate (evaluation result value is always the same) */ |
| 7653 | for (i = 0; i < set2.used; ++i) { |
| 7654 | if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) { |
| 7655 | /* skip root and context node */ |
| 7656 | continue; |
| 7657 | } |
| 7658 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7659 | /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the |
| 7660 | * context node instance */ |
| 7661 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) { |
| 7662 | /* 1.1) context node child was accessed on the child axis, certain dependency */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7663 | rc = LY_ENOT; |
| 7664 | goto cleanup; |
| 7665 | } |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7666 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) { |
| 7667 | for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {} |
| 7668 | if (sparent) { |
| 7669 | /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */ |
| 7670 | rc = LY_ENOT; |
| 7671 | goto cleanup; |
| 7672 | } |
| 7673 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7674 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7675 | /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered, |
| 7676 | * but the current node can be safely ignored, it is always the same data instance */ |
| 7677 | if ((set2.val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (cur_scnode != set2.val.scnodes[i].scnode)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7678 | rc = LY_ENOT; |
| 7679 | goto cleanup; |
| 7680 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7681 | } |
| 7682 | |
| 7683 | /* get any data instance of the context node, we checked it makes no difference */ |
| 7684 | siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree; |
| 7685 | LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup); |
| 7686 | |
| 7687 | /* evaluate the value subexpression with the root context node */ |
| 7688 | lyxp_set_free_content(&set2); |
| 7689 | LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node, |
| 7690 | ctx_node, set->tree, NULL, &set2, 0), cleanup); |
| 7691 | |
| 7692 | /* cast it into a string */ |
| 7693 | LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup); |
| 7694 | |
| 7695 | /* append the JSON predicate */ |
| 7696 | *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3); |
| 7697 | LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7698 | quot = strchr(set2.val.str, '\'') ? '\"' : '\''; |
| 7699 | *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot); |
| 7700 | |
| 7701 | cleanup: |
| 7702 | lyxp_expr_free(set->ctx, val_exp); |
| 7703 | lyxp_set_free_content(&set2); |
| 7704 | return rc; |
| 7705 | } |
| 7706 | |
| 7707 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7708 | * @brief Try to compile list or leaf-list predicate in the known format to be used for hash-based instance search. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7709 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7710 | * @param[in] exp Full parsed XPath expression. |
| 7711 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7712 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7713 | * @param[in] set Context set. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7714 | * @param[out] predicates Parsed predicates. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7715 | * @return LY_SUCCESS on success, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7716 | * @return LY_ENOT if a predicate could not be compiled. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7717 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7718 | */ |
| 7719 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7720 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode, |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 7721 | const struct lyxp_set *set, struct ly_path_predicate **predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7722 | { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7723 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7724 | uint32_t e_idx, val_start_idx, pred_idx = 0, temp_lo = 0, pred_len = 0, nested_pred; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7725 | const struct lysc_node *key; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7726 | char *pred = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7727 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7728 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7729 | assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7730 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7731 | /* turn logging off */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7732 | ly_temp_log_options(&temp_lo); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7733 | |
| 7734 | if (ctx_scnode->nodetype == LYS_LIST) { |
| 7735 | /* check for predicates "[key1=...][key2=...]..." */ |
| 7736 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7737 | /* get key count */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7738 | if (ctx_scnode->flags & LYS_KEYLESS) { |
| 7739 | rc = LY_ENOT; |
| 7740 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7741 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7742 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7743 | /* learn where the predicates end */ |
| 7744 | e_idx = *tok_idx; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7745 | for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7746 | /* '[' */ |
| 7747 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7748 | rc = LY_ENOT; |
| 7749 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7750 | } |
| 7751 | ++e_idx; |
| 7752 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7753 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7754 | /* not a key */ |
| 7755 | rc = LY_ENOT; |
| 7756 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7757 | } |
| 7758 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7759 | /* check key */ |
| 7760 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx], |
| 7761 | exp->tok_len[e_idx], ctx_scnode, set, key), cleanup); |
| 7762 | |
| 7763 | ++e_idx; |
| 7764 | |
| 7765 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7766 | /* not '=' */ |
| 7767 | rc = LY_ENOT; |
| 7768 | goto cleanup; |
| 7769 | } |
| 7770 | ++e_idx; |
| 7771 | |
| 7772 | /* value start */ |
| 7773 | val_start_idx = e_idx; |
| 7774 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7775 | /* ']' */ |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7776 | nested_pred = 1; |
| 7777 | do { |
| 7778 | ++e_idx; |
| 7779 | |
| 7780 | if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7781 | /* higher priority than '=' */ |
| 7782 | rc = LY_ENOT; |
| 7783 | goto cleanup; |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7784 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7785 | /* nested predicate */ |
| 7786 | ++nested_pred; |
| 7787 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7788 | /* predicate end */ |
| 7789 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7790 | } |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7791 | } while (nested_pred); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7792 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7793 | /* try to evaluate the value */ |
| 7794 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, |
| 7795 | set, key, &pred, &pred_len), cleanup); |
| 7796 | |
| 7797 | ++e_idx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7798 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7799 | } else { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7800 | /* check for predicate "[.=...]" */ |
| 7801 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7802 | /* learn just where this single predicate ends */ |
| 7803 | e_idx = *tok_idx; |
| 7804 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7805 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7806 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7807 | rc = LY_ENOT; |
| 7808 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7809 | } |
| 7810 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7811 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7812 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7813 | /* not the node value */ |
| 7814 | rc = LY_ENOT; |
| 7815 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7816 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7817 | ++e_idx; |
| 7818 | |
| 7819 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7820 | /* not '=' */ |
| 7821 | rc = LY_ENOT; |
| 7822 | goto cleanup; |
| 7823 | } |
| 7824 | ++e_idx; |
| 7825 | |
| 7826 | /* value start */ |
| 7827 | val_start_idx = e_idx; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7828 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7829 | /* ']' */ |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7830 | nested_pred = 1; |
| 7831 | do { |
| 7832 | ++e_idx; |
| 7833 | |
| 7834 | if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7835 | /* higher priority than '=' */ |
| 7836 | rc = LY_ENOT; |
| 7837 | goto cleanup; |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7838 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7839 | /* nested predicate */ |
| 7840 | ++nested_pred; |
| 7841 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7842 | /* predicate end */ |
| 7843 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7844 | } |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7845 | } while (nested_pred); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7846 | |
| 7847 | /* try to evaluate the value */ |
| 7848 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set, |
| 7849 | ctx_scnode, &pred, &pred_len), cleanup); |
| 7850 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7851 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7852 | } |
| 7853 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7854 | /* parse the predicate(s) */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7855 | LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL, |
| 7856 | LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7857 | |
| 7858 | /* compile */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7859 | rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2, |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 7860 | &pred_idx, LY_VALUE_JSON, NULL, predicates); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7861 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7862 | |
| 7863 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7864 | *tok_idx = e_idx; |
| 7865 | |
| 7866 | cleanup: |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7867 | ly_temp_log_options(NULL); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7868 | lyxp_expr_free(set->ctx, exp2); |
| 7869 | free(pred); |
| 7870 | return rc; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7871 | } |
| 7872 | |
| 7873 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7874 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7875 | * data node(s) could be found using a single hash-based search. |
| 7876 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7877 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7878 | * @param[in] node Next context node to check. |
| 7879 | * @param[in] name Expected node name. |
| 7880 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7881 | * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7882 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7883 | * @param[in] format Prefix format. |
| 7884 | * @param[in,out] found Previously found node, is updated. |
| 7885 | * @return LY_SUCCESS on success, |
| 7886 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7887 | */ |
| 7888 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7889 | eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7890 | uint32_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7891 | const struct lysc_node **found) |
| 7892 | { |
| 7893 | const struct lysc_node *scnode; |
| 7894 | const struct lys_module *mod; |
| 7895 | uint32_t idx = 0; |
| 7896 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7897 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7898 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7899 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7900 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7901 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7902 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7903 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7904 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7905 | if (!mod->implemented) { |
| 7906 | continue; |
| 7907 | } |
| 7908 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7909 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7910 | if (scnode) { |
| 7911 | /* we have found a match */ |
| 7912 | break; |
| 7913 | } |
| 7914 | } |
| 7915 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7916 | if (!scnode) { |
| 7917 | /* all modules searched */ |
| 7918 | idx = 0; |
| 7919 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7920 | } else { |
| 7921 | /* search in top-level */ |
| 7922 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7923 | } |
| 7924 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7925 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7926 | /* we must adjust the module to inherit the one from the context node */ |
| 7927 | moveto_mod = node->schema->module; |
| 7928 | } |
| 7929 | |
| 7930 | /* search in children, do not repeat the same search */ |
| 7931 | scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0); |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7932 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7933 | |
| 7934 | /* additional context check */ |
| 7935 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7936 | scnode = NULL; |
| 7937 | } |
| 7938 | |
| 7939 | if (scnode) { |
| 7940 | if (*found) { |
| 7941 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7942 | * (more hash-based searches would be required, not supported) */ |
| 7943 | return LY_ENOT; |
| 7944 | } else { |
| 7945 | /* remember the found schema node and continue to make sure it can be used */ |
| 7946 | *found = scnode; |
| 7947 | } |
| 7948 | } |
| 7949 | |
| 7950 | if (idx) { |
| 7951 | /* continue searching all the following models */ |
| 7952 | goto continue_search; |
| 7953 | } |
| 7954 | |
| 7955 | return LY_SUCCESS; |
| 7956 | } |
| 7957 | |
| 7958 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7959 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7960 | * |
| 7961 | * @param[in] set XPath set. |
| 7962 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7963 | * @param[in] ncname XPath NCName being evaluated. |
| 7964 | * @param[in] ncname_len Length of @p ncname. |
| 7965 | * @param[in] expr Whole XPath expression. |
| 7966 | * @param[in] options XPath options. |
| 7967 | */ |
| 7968 | static void |
| 7969 | eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7970 | uint32_t ncname_len, const char *expr, uint32_t options) |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7971 | { |
| 7972 | const char *format; |
| 7973 | char *path = NULL, *ppath = NULL; |
| 7974 | |
| 7975 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7976 | if (scparent) { |
| 7977 | /* generate path for the parent */ |
| 7978 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7979 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7980 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7981 | ppath = strdup("<root>"); |
| 7982 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7983 | ppath = strdup("<config-root>"); |
| 7984 | } |
| 7985 | } |
| 7986 | if (ppath) { |
| 7987 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7988 | if (options & LYXP_SCNODE_ERROR) { |
| 7989 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7990 | } else { |
| 7991 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7992 | } |
| 7993 | } else { |
| 7994 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7995 | if (options & LYXP_SCNODE_ERROR) { |
| 7996 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7997 | } else { |
| 7998 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7999 | } |
| 8000 | } |
| 8001 | free(path); |
| 8002 | free(ppath); |
| 8003 | } |
| 8004 | |
| 8005 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8006 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 8007 | * |
| 8008 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8009 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8010 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 8011 | * |
| 8012 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8013 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8014 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8015 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8016 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8017 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8018 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8019 | */ |
| 8020 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8021 | eval_name_test_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8022 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8023 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8024 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8025 | const char *ncname, *ncname_dict = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8026 | uint32_t i, ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8027 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8028 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8029 | struct ly_path_predicate *predicates = NULL; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8030 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8031 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8032 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8033 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8034 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8035 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8036 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8037 | goto moveto; |
| 8038 | } |
| 8039 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8040 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 8041 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8042 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8043 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8044 | /* all nodes will match */ |
| 8045 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8046 | } |
| 8047 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8048 | /* parse (and skip) module name */ |
| 8049 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8050 | LY_CHECK_GOTO(rc, cleanup); |
| 8051 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8052 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8053 | /* all nodes from the module will match */ |
| 8054 | goto moveto; |
| 8055 | } |
| 8056 | |
| 8057 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 8058 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8059 | /* find the matching schema node in some parent in the context */ |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8060 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8061 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 8062 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8063 | /* check failed */ |
| 8064 | scnode = NULL; |
| 8065 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8066 | } |
| 8067 | } |
| 8068 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8069 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 8070 | /* try to create the predicates */ |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8071 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8072 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8073 | scnode = NULL; |
| 8074 | } |
| 8075 | } |
| 8076 | } |
| 8077 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8078 | if (!scnode) { |
| 8079 | /* we are not able to match based on a schema node and not all the modules match ("*"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8080 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 8081 | LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8082 | } |
| 8083 | |
| 8084 | moveto: |
| 8085 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8086 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8087 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8088 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8089 | } else { |
| 8090 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 8091 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8092 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8093 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8094 | } |
| 8095 | LY_CHECK_GOTO(rc, cleanup); |
| 8096 | } |
| 8097 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8098 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8099 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8100 | ly_bool found = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8101 | |
| 8102 | /* remember parent if there is only one, to print in the warning */ |
| 8103 | for (i = 0; i < set->used; ++i) { |
| 8104 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 8105 | if (!scparent) { |
| 8106 | /* remember the context node */ |
| 8107 | scparent = &set->val.scnodes[i]; |
| 8108 | } else { |
| 8109 | /* several context nodes, no reasonable error possible */ |
| 8110 | scparent = NULL; |
| 8111 | break; |
| 8112 | } |
| 8113 | } |
| 8114 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8115 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8116 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8117 | /* efficient evaluation that does not add all the descendants into the set */ |
| 8118 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8119 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8120 | if (all_desc) { |
| 8121 | /* "//" == "/descendant-or-self::node()/" */ |
| 8122 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8123 | LY_CHECK_GOTO(rc, cleanup); |
| 8124 | } |
| 8125 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8126 | } |
| 8127 | LY_CHECK_GOTO(rc, cleanup); |
| 8128 | |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8129 | i = set->used; |
| 8130 | do { |
| 8131 | --i; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8132 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8133 | found = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8134 | break; |
| 8135 | } |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8136 | } while (i); |
| 8137 | if (!found) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8138 | /* generate message */ |
| 8139 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 8140 | |
| 8141 | if (options & LYXP_SCNODE_ERROR) { |
| 8142 | /* error */ |
| 8143 | rc = LY_EVALID; |
| 8144 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8145 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8146 | |
| 8147 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 8148 | rc = LY_ENOT; |
| 8149 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8150 | } |
| 8151 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8152 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8153 | /* efficient evaluation */ |
| 8154 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 8155 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 8156 | /* we can find the child nodes using hashes */ |
| 8157 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8158 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8159 | if (all_desc) { |
| 8160 | /* "//" == "/descendant-or-self::node()/" */ |
| 8161 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8162 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8163 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8164 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8165 | } |
| 8166 | LY_CHECK_GOTO(rc, cleanup); |
| 8167 | } |
| 8168 | } |
| 8169 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8170 | if (scnode_skip_pred) { |
| 8171 | /* skip predicates */ |
| 8172 | options |= LYXP_SKIP_EXPR; |
| 8173 | } |
| 8174 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8175 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8176 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8177 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8178 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8179 | } |
| 8180 | |
| 8181 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8182 | if (scnode_skip_pred) { |
| 8183 | /* restore options */ |
| 8184 | options &= ~LYXP_SKIP_EXPR; |
| 8185 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8186 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8187 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8188 | ly_path_predicates_free(set->ctx, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 8189 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8190 | return rc; |
| 8191 | } |
| 8192 | |
| 8193 | /** |
| 8194 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 8195 | * |
| 8196 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8197 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8198 | * [8] NodeType ::= 'text' | 'node' |
| 8199 | * |
| 8200 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8201 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8202 | * @param[in] axis Axis to search on. |
| 8203 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8204 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8205 | * @param[in] options XPath options. |
| 8206 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8207 | */ |
| 8208 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8209 | eval_node_type_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8210 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8211 | { |
| 8212 | LY_ERR rc; |
| 8213 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8214 | (void)all_desc; |
| 8215 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8216 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8217 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8218 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 8219 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8220 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8221 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 8222 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8223 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8224 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8225 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8226 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8227 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8228 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8229 | |
| 8230 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8231 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8232 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8233 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8234 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8235 | |
| 8236 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8237 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8238 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8239 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8240 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8241 | |
| 8242 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8243 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8244 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8245 | LY_CHECK_RET(rc); |
| 8246 | } |
| 8247 | |
| 8248 | return LY_SUCCESS; |
| 8249 | } |
| 8250 | |
| 8251 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8252 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8253 | * |
| 8254 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8255 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8256 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8257 | * |
| 8258 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8259 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8260 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8261 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8262 | * @param[in] options XPath options. |
| 8263 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8264 | */ |
| 8265 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8266 | eval_relative_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8267 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8268 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8269 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8270 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8271 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8272 | |
| 8273 | goto step; |
| 8274 | do { |
| 8275 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8276 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8277 | all_desc = 0; |
| 8278 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8279 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8280 | all_desc = 1; |
| 8281 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8282 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8283 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8284 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8285 | |
| 8286 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8287 | /* AxisSpecifier */ |
| 8288 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8289 | axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8290 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8291 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8292 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8293 | ++(*tok_idx); |
| 8294 | |
| 8295 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8296 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8297 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8298 | ++(*tok_idx); |
| 8299 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8300 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8301 | |
| 8302 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8303 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8304 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8305 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8306 | /* default */ |
| 8307 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8308 | } |
| 8309 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8310 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8311 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8312 | case LYXP_TOKEN_DOT: |
| 8313 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8314 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8315 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8316 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8317 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8318 | rc = LY_EVALID; |
| 8319 | goto cleanup; |
| 8320 | } |
| 8321 | |
| 8322 | if (all_desc) { |
| 8323 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8324 | LY_CHECK_GOTO(rc, cleanup); |
| 8325 | } |
| 8326 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8327 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8328 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8329 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8330 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8331 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8332 | break; |
| 8333 | |
| 8334 | case LYXP_TOKEN_DDOT: |
| 8335 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8336 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8337 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8338 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8339 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8340 | rc = LY_EVALID; |
| 8341 | goto cleanup; |
| 8342 | } |
| 8343 | |
| 8344 | if (all_desc) { |
| 8345 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8346 | LY_CHECK_GOTO(rc, cleanup); |
| 8347 | } |
| 8348 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8349 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8350 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8351 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8352 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8353 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8354 | break; |
| 8355 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8356 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8357 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8358 | rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8359 | if (rc == LY_ENOT) { |
| 8360 | assert(options & LYXP_SCNODE_ALL); |
| 8361 | /* skip the rest of this path */ |
| 8362 | rc = LY_SUCCESS; |
| 8363 | scnode_skip_path = 1; |
| 8364 | options |= LYXP_SKIP_EXPR; |
| 8365 | } |
| 8366 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8367 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8368 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8369 | case LYXP_TOKEN_NODETYPE: |
| 8370 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8371 | rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8372 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8373 | break; |
| 8374 | |
| 8375 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8376 | LOGINT(set->ctx); |
| 8377 | rc = LY_EINT; |
| 8378 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8379 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8380 | } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8381 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8382 | cleanup: |
| 8383 | if (scnode_skip_path) { |
| 8384 | options &= ~LYXP_SKIP_EXPR; |
| 8385 | } |
| 8386 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8387 | } |
| 8388 | |
| 8389 | /** |
| 8390 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8391 | * |
| 8392 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8393 | * |
| 8394 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8395 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8396 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8397 | * @param[in] options XPath options. |
| 8398 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8399 | */ |
| 8400 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8401 | eval_absolute_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8402 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8403 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8404 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8405 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8406 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8407 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8408 | } |
| 8409 | |
| 8410 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8411 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8412 | /* evaluate '/' - deferred */ |
| 8413 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8414 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8415 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8416 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8417 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8418 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8419 | return LY_SUCCESS; |
| 8420 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8421 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8422 | case LYXP_TOKEN_DOT: |
| 8423 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8424 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8425 | case LYXP_TOKEN_AT: |
| 8426 | case LYXP_TOKEN_NAMETEST: |
| 8427 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8428 | LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8429 | break; |
| 8430 | default: |
| 8431 | break; |
| 8432 | } |
| 8433 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8434 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8435 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8436 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8437 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8438 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8439 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8440 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8441 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8442 | LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8443 | } |
| 8444 | |
| 8445 | return LY_SUCCESS; |
| 8446 | } |
| 8447 | |
| 8448 | /** |
| 8449 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8450 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8451 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8452 | * |
| 8453 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8454 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8455 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8456 | * @param[in] options XPath options. |
| 8457 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8458 | */ |
| 8459 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8460 | eval_function_call(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8461 | { |
| 8462 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8463 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8464 | LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL; |
| 8465 | uint32_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8466 | struct lyxp_set **args = NULL, **args_aux; |
| 8467 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8468 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8469 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8470 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8471 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8472 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8473 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8474 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8475 | xpath_func = &xpath_sum; |
| 8476 | } |
| 8477 | break; |
| 8478 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8479 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8480 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8481 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8482 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8483 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8484 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8485 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8486 | xpath_func = &xpath_true; |
| 8487 | } |
| 8488 | break; |
| 8489 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8490 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8491 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8492 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8493 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8494 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8495 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8496 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8497 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8498 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8499 | xpath_func = &xpath_deref; |
| 8500 | } |
| 8501 | break; |
| 8502 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8503 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8505 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8506 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8507 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8508 | xpath_func = &xpath_string; |
| 8509 | } |
| 8510 | break; |
| 8511 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8512 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8513 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8514 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8515 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8516 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8517 | xpath_func = &xpath_current; |
| 8518 | } |
| 8519 | break; |
| 8520 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8521 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8522 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8523 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8524 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8525 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8526 | xpath_func = &xpath_re_match; |
| 8527 | } |
| 8528 | break; |
| 8529 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8530 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8531 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8532 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | xpath_func = &xpath_translate; |
| 8534 | } |
| 8535 | break; |
| 8536 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8537 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8538 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8539 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8540 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8541 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8542 | xpath_func = &xpath_bit_is_set; |
| 8543 | } |
| 8544 | break; |
| 8545 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8546 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8547 | xpath_func = &xpath_starts_with; |
| 8548 | } |
| 8549 | break; |
| 8550 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8551 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8552 | xpath_func = &xpath_derived_from; |
| 8553 | } |
| 8554 | break; |
| 8555 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8556 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8557 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8558 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8559 | xpath_func = &xpath_string_length; |
| 8560 | } |
| 8561 | break; |
| 8562 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8563 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8564 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8565 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8566 | xpath_func = &xpath_substring_after; |
| 8567 | } |
| 8568 | break; |
| 8569 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8570 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | xpath_func = &xpath_substring_before; |
| 8572 | } |
| 8573 | break; |
| 8574 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8575 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8576 | xpath_func = &xpath_derived_from_or_self; |
| 8577 | } |
| 8578 | break; |
| 8579 | } |
| 8580 | |
| 8581 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8582 | LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8583 | return LY_EVALID; |
| 8584 | } |
| 8585 | } |
| 8586 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8587 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8588 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8589 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8590 | |
| 8591 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8592 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8593 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8594 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8595 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8596 | |
| 8597 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8598 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8599 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8600 | args = malloc(sizeof *args); |
| 8601 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8602 | arg_count = 1; |
| 8603 | args[0] = set_copy(set); |
| 8604 | if (!args[0]) { |
| 8605 | rc = LY_EMEM; |
| 8606 | goto cleanup; |
| 8607 | } |
| 8608 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8609 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8610 | LY_CHECK_GOTO(rc, cleanup); |
| 8611 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8612 | rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8613 | LY_CHECK_GOTO(rc, cleanup); |
| 8614 | } |
| 8615 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8616 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8617 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8618 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8619 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8620 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8621 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8622 | ++arg_count; |
| 8623 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8624 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8625 | args = args_aux; |
| 8626 | args[arg_count - 1] = set_copy(set); |
| 8627 | if (!args[arg_count - 1]) { |
| 8628 | rc = LY_EMEM; |
| 8629 | goto cleanup; |
| 8630 | } |
| 8631 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8632 | rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8633 | LY_CHECK_GOTO(rc, cleanup); |
| 8634 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8635 | rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8636 | LY_CHECK_GOTO(rc, cleanup); |
| 8637 | } |
| 8638 | } |
| 8639 | |
| 8640 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8641 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8642 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8643 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8644 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8645 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8646 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8647 | /* evaluate function */ |
| 8648 | rc = xpath_func(args, arg_count, set, options); |
| 8649 | |
| 8650 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8651 | /* merge all nodes from arg evaluations */ |
| 8652 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8653 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8654 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8655 | } |
| 8656 | } |
| 8657 | } else { |
| 8658 | rc = LY_SUCCESS; |
| 8659 | } |
| 8660 | |
| 8661 | cleanup: |
| 8662 | for (i = 0; i < arg_count; ++i) { |
| 8663 | lyxp_set_free(args[i]); |
| 8664 | } |
| 8665 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8666 | return rc; |
| 8667 | } |
| 8668 | |
| 8669 | /** |
| 8670 | * @brief Evaluate Number. Logs directly on error. |
| 8671 | * |
| 8672 | * @param[in] ctx Context for errors. |
| 8673 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8674 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8675 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8676 | * @return LY_ERR |
| 8677 | */ |
| 8678 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8679 | eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8680 | { |
| 8681 | long double num; |
| 8682 | char *endptr; |
| 8683 | |
| 8684 | if (set) { |
| 8685 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8686 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8687 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8688 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8689 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8690 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8691 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8692 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8693 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8694 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8695 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8696 | return LY_EVALID; |
| 8697 | } |
| 8698 | |
| 8699 | set_fill_number(set, num); |
| 8700 | } |
| 8701 | |
| 8702 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8703 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8704 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8705 | return LY_SUCCESS; |
| 8706 | } |
| 8707 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8708 | LY_ERR |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8709 | lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len, |
| 8710 | struct lyxp_var **var) |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8711 | { |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8712 | LY_ARRAY_COUNT_TYPE u; |
| 8713 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8714 | assert(name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8715 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8716 | if (!name_len) { |
| 8717 | name_len = strlen(name); |
| 8718 | } |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8719 | |
| 8720 | LY_ARRAY_FOR(vars, u) { |
| 8721 | if (!strncmp(vars[u].name, name, name_len)) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8722 | if (var) { |
| 8723 | *var = (struct lyxp_var *)&vars[u]; |
| 8724 | } |
| 8725 | return LY_SUCCESS; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8726 | } |
| 8727 | } |
| 8728 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8729 | if (ctx) { |
| 8730 | LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8731 | } |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8732 | return LY_ENOTFOUND; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8733 | } |
| 8734 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8735 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8736 | * @brief Evaluate VariableReference. |
| 8737 | * |
| 8738 | * @param[in] exp Parsed XPath expression. |
| 8739 | * @param[in] tok_idx Position in the expression @p exp. |
| 8740 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8741 | * @param[in,out] set Context and result set. |
| 8742 | * @param[in] options XPath options. |
| 8743 | * @return LY_ERR value. |
| 8744 | */ |
| 8745 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8746 | eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8747 | { |
| 8748 | LY_ERR ret; |
| 8749 | const char *name; |
| 8750 | struct lyxp_var *var; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8751 | struct lyxp_expr *tokens = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8752 | uint32_t token_index, name_len; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8753 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8754 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8755 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
Michal Vasko | e68b540 | 2022-07-29 14:58:15 +0200 | [diff] [blame] | 8756 | name_len = exp->tok_len[*tok_idx]; |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8757 | ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var); |
| 8758 | LY_CHECK_RET(ret); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8759 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8760 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8761 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8762 | LY_CHECK_GOTO(ret, cleanup); |
| 8763 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8764 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8765 | token_index = 0; |
| 8766 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8767 | LY_CHECK_GOTO(ret, cleanup); |
| 8768 | |
| 8769 | cleanup: |
| 8770 | lyxp_expr_free(set->ctx, tokens); |
| 8771 | |
| 8772 | return ret; |
| 8773 | } |
| 8774 | |
| 8775 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8776 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8777 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8778 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8779 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8780 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8781 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8782 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8783 | * |
| 8784 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8785 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8786 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8787 | * @param[in] options XPath options. |
| 8788 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8789 | */ |
| 8790 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8791 | eval_path_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8792 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8793 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8794 | LY_ERR rc; |
| 8795 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8796 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8797 | case LYXP_TOKEN_PAR1: |
| 8798 | /* '(' Expr ')' */ |
| 8799 | |
| 8800 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8801 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8802 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8803 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8804 | |
| 8805 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8806 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8807 | LY_CHECK_RET(rc); |
| 8808 | |
| 8809 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8810 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8811 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8812 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8813 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8814 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8815 | goto predicate; |
| 8816 | |
| 8817 | case LYXP_TOKEN_DOT: |
| 8818 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8819 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8820 | case LYXP_TOKEN_AT: |
| 8821 | case LYXP_TOKEN_NAMETEST: |
| 8822 | case LYXP_TOKEN_NODETYPE: |
| 8823 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8824 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8825 | LY_CHECK_RET(rc); |
| 8826 | break; |
| 8827 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8828 | case LYXP_TOKEN_VARREF: |
| 8829 | /* VariableReference */ |
| 8830 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8831 | LY_CHECK_RET(rc); |
| 8832 | ++(*tok_idx); |
| 8833 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8834 | goto predicate; |
| 8835 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8836 | case LYXP_TOKEN_FUNCNAME: |
| 8837 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8838 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8839 | LY_CHECK_RET(rc); |
| 8840 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8841 | goto predicate; |
| 8842 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8843 | case LYXP_TOKEN_OPER_PATH: |
| 8844 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8845 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8846 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8847 | LY_CHECK_RET(rc); |
| 8848 | break; |
| 8849 | |
| 8850 | case LYXP_TOKEN_LITERAL: |
| 8851 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8852 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8853 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8854 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8855 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8856 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8857 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8858 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8859 | } |
| 8860 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8861 | goto predicate; |
| 8862 | |
| 8863 | case LYXP_TOKEN_NUMBER: |
| 8864 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8865 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8866 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8867 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8868 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8869 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8870 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8871 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8872 | } |
| 8873 | LY_CHECK_RET(rc); |
| 8874 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8875 | goto predicate; |
| 8876 | |
| 8877 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8878 | LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8879 | return LY_EVALID; |
| 8880 | } |
| 8881 | |
| 8882 | return LY_SUCCESS; |
| 8883 | |
| 8884 | predicate: |
| 8885 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8886 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8887 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8888 | LY_CHECK_RET(rc); |
| 8889 | } |
| 8890 | |
| 8891 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8892 | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8893 | |
| 8894 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8895 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8896 | all_desc = 0; |
| 8897 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8898 | all_desc = 1; |
| 8899 | } |
| 8900 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8901 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8902 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8903 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8904 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8905 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8906 | LY_CHECK_RET(rc); |
| 8907 | } |
| 8908 | |
| 8909 | return LY_SUCCESS; |
| 8910 | } |
| 8911 | |
| 8912 | /** |
| 8913 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8914 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8915 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8916 | * |
| 8917 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8918 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8919 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8920 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8921 | * @param[in] options XPath options. |
| 8922 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8923 | */ |
| 8924 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8925 | eval_union_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8926 | { |
| 8927 | LY_ERR rc = LY_SUCCESS; |
| 8928 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8929 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8930 | |
| 8931 | assert(repeat); |
| 8932 | |
| 8933 | set_init(&orig_set, set); |
| 8934 | set_init(&set2, set); |
| 8935 | |
| 8936 | set_fill_set(&orig_set, set); |
| 8937 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8938 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8939 | LY_CHECK_GOTO(rc, cleanup); |
| 8940 | |
| 8941 | /* ('|' PathExpr)* */ |
| 8942 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8943 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8944 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8945 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8946 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8947 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8948 | if (options & LYXP_SKIP_EXPR) { |
| 8949 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8950 | LY_CHECK_GOTO(rc, cleanup); |
| 8951 | continue; |
| 8952 | } |
| 8953 | |
| 8954 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8955 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8956 | LY_CHECK_GOTO(rc, cleanup); |
| 8957 | |
| 8958 | /* eval */ |
| 8959 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8960 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8961 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8962 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8963 | LY_CHECK_GOTO(rc, cleanup); |
| 8964 | } |
| 8965 | } |
| 8966 | |
| 8967 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8968 | lyxp_set_free_content(&orig_set); |
| 8969 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8970 | return rc; |
| 8971 | } |
| 8972 | |
| 8973 | /** |
| 8974 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8975 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8976 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8977 | * |
| 8978 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8979 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8980 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8981 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8982 | * @param[in] options XPath options. |
| 8983 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8984 | */ |
| 8985 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8986 | eval_unary_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8987 | { |
| 8988 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8989 | uint32_t this_op, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8990 | |
| 8991 | assert(repeat); |
| 8992 | |
| 8993 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8994 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8995 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8996 | assert(!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && (exp->expr[exp->tok_pos[*tok_idx]] == '-')); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8997 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8998 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8999 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9000 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9001 | } |
| 9002 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9003 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9004 | LY_CHECK_RET(rc); |
| 9005 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9006 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9007 | if (options & LYXP_SCNODE_ALL) { |
| 9008 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 9009 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9010 | rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9011 | LY_CHECK_RET(rc); |
| 9012 | } |
| 9013 | } |
| 9014 | |
| 9015 | return LY_SUCCESS; |
| 9016 | } |
| 9017 | |
| 9018 | /** |
| 9019 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 9020 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9021 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9022 | * | MultiplicativeExpr '*' UnaryExpr |
| 9023 | * | MultiplicativeExpr 'div' UnaryExpr |
| 9024 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 9025 | * |
| 9026 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9027 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9028 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9029 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9030 | * @param[in] options XPath options. |
| 9031 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9032 | */ |
| 9033 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9034 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9035 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9036 | { |
| 9037 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9038 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9039 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9040 | |
| 9041 | assert(repeat); |
| 9042 | |
| 9043 | set_init(&orig_set, set); |
| 9044 | set_init(&set2, set); |
| 9045 | |
| 9046 | set_fill_set(&orig_set, set); |
| 9047 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9048 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9049 | LY_CHECK_GOTO(rc, cleanup); |
| 9050 | |
| 9051 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 9052 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9053 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9054 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9055 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9056 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9057 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9058 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9059 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9060 | if (options & LYXP_SKIP_EXPR) { |
| 9061 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9062 | LY_CHECK_GOTO(rc, cleanup); |
| 9063 | continue; |
| 9064 | } |
| 9065 | |
| 9066 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9067 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9068 | LY_CHECK_GOTO(rc, cleanup); |
| 9069 | |
| 9070 | /* eval */ |
| 9071 | if (options & LYXP_SCNODE_ALL) { |
| 9072 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9073 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9074 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9075 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9076 | rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9077 | LY_CHECK_GOTO(rc, cleanup); |
| 9078 | } |
| 9079 | } |
| 9080 | |
| 9081 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9082 | lyxp_set_free_content(&orig_set); |
| 9083 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9084 | return rc; |
| 9085 | } |
| 9086 | |
| 9087 | /** |
| 9088 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 9089 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9090 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9091 | * | AdditiveExpr '+' MultiplicativeExpr |
| 9092 | * | AdditiveExpr '-' MultiplicativeExpr |
| 9093 | * |
| 9094 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9095 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9096 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9097 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9098 | * @param[in] options XPath options. |
| 9099 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9100 | */ |
| 9101 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9102 | eval_additive_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9103 | { |
| 9104 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9105 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9106 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9107 | |
| 9108 | assert(repeat); |
| 9109 | |
| 9110 | set_init(&orig_set, set); |
| 9111 | set_init(&set2, set); |
| 9112 | |
| 9113 | set_fill_set(&orig_set, set); |
| 9114 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9115 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9116 | LY_CHECK_GOTO(rc, cleanup); |
| 9117 | |
| 9118 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 9119 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9120 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9121 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9122 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9123 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9124 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9125 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9126 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9127 | if (options & LYXP_SKIP_EXPR) { |
| 9128 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9129 | LY_CHECK_GOTO(rc, cleanup); |
| 9130 | continue; |
| 9131 | } |
| 9132 | |
| 9133 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9134 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9135 | LY_CHECK_GOTO(rc, cleanup); |
| 9136 | |
| 9137 | /* eval */ |
| 9138 | if (options & LYXP_SCNODE_ALL) { |
| 9139 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9140 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9141 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9142 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9143 | rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9144 | LY_CHECK_GOTO(rc, cleanup); |
| 9145 | } |
| 9146 | } |
| 9147 | |
| 9148 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9149 | lyxp_set_free_content(&orig_set); |
| 9150 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9151 | return rc; |
| 9152 | } |
| 9153 | |
| 9154 | /** |
| 9155 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 9156 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9157 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9158 | * | RelationalExpr '<' AdditiveExpr |
| 9159 | * | RelationalExpr '>' AdditiveExpr |
| 9160 | * | RelationalExpr '<=' AdditiveExpr |
| 9161 | * | RelationalExpr '>=' AdditiveExpr |
| 9162 | * |
| 9163 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9164 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9165 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9166 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9167 | * @param[in] options XPath options. |
| 9168 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9169 | */ |
| 9170 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9171 | eval_relational_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9172 | { |
| 9173 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9174 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9175 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9176 | |
| 9177 | assert(repeat); |
| 9178 | |
| 9179 | set_init(&orig_set, set); |
| 9180 | set_init(&set2, set); |
| 9181 | |
| 9182 | set_fill_set(&orig_set, set); |
| 9183 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9184 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9185 | LY_CHECK_GOTO(rc, cleanup); |
| 9186 | |
| 9187 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 9188 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9189 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9190 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9191 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9192 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9193 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9194 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9195 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9196 | if (options & LYXP_SKIP_EXPR) { |
| 9197 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9198 | LY_CHECK_GOTO(rc, cleanup); |
| 9199 | continue; |
| 9200 | } |
| 9201 | |
| 9202 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9203 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9204 | LY_CHECK_GOTO(rc, cleanup); |
| 9205 | |
| 9206 | /* eval */ |
| 9207 | if (options & LYXP_SCNODE_ALL) { |
| 9208 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9209 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9210 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9211 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9212 | ly_bool result; |
| 9213 | |
| 9214 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9215 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9216 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9217 | } |
| 9218 | } |
| 9219 | |
| 9220 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9221 | lyxp_set_free_content(&orig_set); |
| 9222 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9223 | return rc; |
| 9224 | } |
| 9225 | |
| 9226 | /** |
| 9227 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 9228 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9229 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9230 | * | EqualityExpr '!=' RelationalExpr |
| 9231 | * |
| 9232 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9233 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9234 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9235 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9236 | * @param[in] options XPath options. |
| 9237 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9238 | */ |
| 9239 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9240 | eval_equality_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9241 | { |
| 9242 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9243 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9244 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9245 | |
| 9246 | assert(repeat); |
| 9247 | |
| 9248 | set_init(&orig_set, set); |
| 9249 | set_init(&set2, set); |
| 9250 | |
| 9251 | set_fill_set(&orig_set, set); |
| 9252 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9253 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9254 | LY_CHECK_GOTO(rc, cleanup); |
| 9255 | |
| 9256 | /* ('=' / '!=' RelationalExpr)* */ |
| 9257 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9258 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9259 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9260 | assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL)); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9261 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9262 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9263 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9264 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9265 | if (options & LYXP_SKIP_EXPR) { |
| 9266 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9267 | LY_CHECK_GOTO(rc, cleanup); |
| 9268 | continue; |
| 9269 | } |
| 9270 | |
| 9271 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9272 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9273 | LY_CHECK_GOTO(rc, cleanup); |
| 9274 | |
| 9275 | /* eval */ |
| 9276 | if (options & LYXP_SCNODE_ALL) { |
| 9277 | warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9278 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9279 | warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9280 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9281 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9282 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9283 | ly_bool result; |
| 9284 | |
| 9285 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9286 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9287 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9288 | } |
| 9289 | } |
| 9290 | |
| 9291 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9292 | lyxp_set_free_content(&orig_set); |
| 9293 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9294 | return rc; |
| 9295 | } |
| 9296 | |
| 9297 | /** |
| 9298 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9299 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9300 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9301 | * |
| 9302 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9303 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9304 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9305 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9306 | * @param[in] options XPath options. |
| 9307 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9308 | */ |
| 9309 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9310 | eval_and_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9311 | { |
| 9312 | LY_ERR rc; |
| 9313 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9314 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9315 | |
| 9316 | assert(repeat); |
| 9317 | |
| 9318 | set_init(&orig_set, set); |
| 9319 | set_init(&set2, set); |
| 9320 | |
| 9321 | set_fill_set(&orig_set, set); |
| 9322 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9323 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9324 | LY_CHECK_GOTO(rc, cleanup); |
| 9325 | |
Michal Vasko | 3ff82dd | 2023-05-09 14:09:21 +0200 | [diff] [blame] | 9326 | if (!(options & LYXP_SKIP_EXPR)) { |
| 9327 | if (options & LYXP_SCNODE_ALL) { |
| 9328 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
| 9329 | } else { |
| 9330 | /* cast to boolean, we know that will be the final result */ |
| 9331 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
| 9332 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9333 | } |
| 9334 | |
| 9335 | /* ('and' EqualityExpr)* */ |
| 9336 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9337 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9338 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9339 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9340 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9341 | |
| 9342 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9343 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9344 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9345 | LY_CHECK_GOTO(rc, cleanup); |
| 9346 | continue; |
| 9347 | } |
| 9348 | |
| 9349 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9350 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9351 | LY_CHECK_GOTO(rc, cleanup); |
| 9352 | |
| 9353 | /* eval - just get boolean value actually */ |
| 9354 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9355 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9356 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9357 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9358 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9359 | set_fill_set(set, &set2); |
| 9360 | } |
| 9361 | } |
| 9362 | |
| 9363 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9364 | lyxp_set_free_content(&orig_set); |
| 9365 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9366 | return rc; |
| 9367 | } |
| 9368 | |
| 9369 | /** |
| 9370 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9371 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9372 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9373 | * |
| 9374 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9375 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9376 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9377 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9378 | * @param[in] options XPath options. |
| 9379 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9380 | */ |
| 9381 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9382 | eval_or_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9383 | { |
| 9384 | LY_ERR rc; |
| 9385 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9386 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9387 | |
| 9388 | assert(repeat); |
| 9389 | |
| 9390 | set_init(&orig_set, set); |
| 9391 | set_init(&set2, set); |
| 9392 | |
| 9393 | set_fill_set(&orig_set, set); |
| 9394 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9395 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9396 | LY_CHECK_GOTO(rc, cleanup); |
| 9397 | |
Michal Vasko | 3ff82dd | 2023-05-09 14:09:21 +0200 | [diff] [blame] | 9398 | if (!(options & LYXP_SKIP_EXPR)) { |
| 9399 | if (options & LYXP_SCNODE_ALL) { |
| 9400 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
| 9401 | } else { |
| 9402 | /* cast to boolean, we know that will be the final result */ |
| 9403 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
| 9404 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9405 | } |
| 9406 | |
| 9407 | /* ('or' AndExpr)* */ |
| 9408 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9409 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9410 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9411 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9412 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9413 | |
| 9414 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9415 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9416 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9417 | LY_CHECK_GOTO(rc, cleanup); |
| 9418 | continue; |
| 9419 | } |
| 9420 | |
| 9421 | set_fill_set(&set2, &orig_set); |
| 9422 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9423 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9424 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9425 | LY_CHECK_GOTO(rc, cleanup); |
| 9426 | |
| 9427 | /* eval - just get boolean value actually */ |
| 9428 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9429 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9430 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9431 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9432 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9433 | set_fill_set(set, &set2); |
| 9434 | } |
| 9435 | } |
| 9436 | |
| 9437 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9438 | lyxp_set_free_content(&orig_set); |
| 9439 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9440 | return rc; |
| 9441 | } |
| 9442 | |
| 9443 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9444 | * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9445 | * |
| 9446 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9447 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9448 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9449 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9450 | * @param[in] options XPath options. |
| 9451 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9452 | */ |
| 9453 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9454 | eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9455 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9456 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9457 | uint32_t i, count; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9458 | enum lyxp_expr_type next_etype; |
| 9459 | LY_ERR rc; |
| 9460 | |
| 9461 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9462 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9463 | next_etype = LYXP_EXPR_NONE; |
| 9464 | } else { |
| 9465 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9466 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9467 | |
| 9468 | /* select one-priority lower because etype expression called us */ |
| 9469 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9470 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9471 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9472 | for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9473 | } else { |
| 9474 | next_etype = LYXP_EXPR_NONE; |
| 9475 | } |
| 9476 | } |
| 9477 | |
| 9478 | /* decide what expression are we parsing based on the repeat */ |
| 9479 | switch (next_etype) { |
| 9480 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9481 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9482 | break; |
| 9483 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9484 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9485 | break; |
| 9486 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9487 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9488 | break; |
| 9489 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9490 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9491 | break; |
| 9492 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9493 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9494 | break; |
| 9495 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9496 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9497 | break; |
| 9498 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9499 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9500 | break; |
| 9501 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9502 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9503 | break; |
| 9504 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9505 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9506 | break; |
| 9507 | default: |
| 9508 | LOGINT_RET(set->ctx); |
| 9509 | } |
| 9510 | |
| 9511 | return rc; |
| 9512 | } |
| 9513 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9514 | /** |
| 9515 | * @brief Get root type. |
| 9516 | * |
| 9517 | * @param[in] ctx_node Context node. |
| 9518 | * @param[in] ctx_scnode Schema context node. |
| 9519 | * @param[in] options XPath options. |
| 9520 | * @return Root type. |
| 9521 | */ |
| 9522 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9523 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, uint32_t options) |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9524 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9525 | const struct lysc_node *op; |
| 9526 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9527 | /* explicit */ |
| 9528 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9529 | return LYXP_NODE_ROOT; |
| 9530 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9531 | return LYXP_NODE_ROOT_CONFIG; |
| 9532 | } |
| 9533 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9534 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9535 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9536 | for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {} |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9537 | |
| 9538 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9539 | /* general root that can access everything */ |
| 9540 | return LYXP_NODE_ROOT; |
| 9541 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9542 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9543 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9544 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9545 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9546 | } |
| 9547 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9548 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9549 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9550 | for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {} |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9551 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9552 | if (op || !(options & LYXP_SCHEMA)) { |
| 9553 | /* general root that can access everything */ |
| 9554 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9555 | } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9556 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9557 | return LYXP_NODE_ROOT_CONFIG; |
| 9558 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9559 | return LYXP_NODE_ROOT; |
| 9560 | } |
| 9561 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9562 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9563 | lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9564 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node, |
| 9565 | const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9566 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9567 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9568 | LY_ERR rc; |
| 9569 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9570 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9571 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9572 | LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used."); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9573 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9574 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9575 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9576 | if (tree) { |
| 9577 | /* adjust the pointer to be the first top-level sibling */ |
| 9578 | while (tree->parent) { |
| 9579 | tree = lyd_parent(tree); |
| 9580 | } |
| 9581 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9582 | |
Michal Vasko | 6f78a77 | 2022-11-10 08:13:52 +0100 | [diff] [blame] | 9583 | if (lysc_data_parent(tree->schema)) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9584 | /* unable to evaluate absolute paths */ |
| 9585 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9586 | LYD_NAME(tree)); |
| 9587 | return LY_EINVAL; |
| 9588 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9589 | } |
| 9590 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9591 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9592 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9593 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9594 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9595 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9596 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9597 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9598 | set->cur_node = cur_node; |
| 9599 | for (set->context_op = cur_node ? cur_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9600 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9601 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9602 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9603 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9604 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9605 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9606 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9607 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9608 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9609 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9610 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9611 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9612 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9613 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9614 | } |
| 9615 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9616 | if (set->cur_node) { |
| 9617 | LOG_LOCBACK(0, 1, 0, 0); |
| 9618 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9619 | return rc; |
| 9620 | } |
| 9621 | |
| 9622 | #if 0 |
| 9623 | |
| 9624 | /* full xml printing of set elements, not used currently */ |
| 9625 | |
| 9626 | void |
| 9627 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9628 | { |
| 9629 | uint32_t i; |
| 9630 | char *str_num; |
| 9631 | struct lyout out; |
| 9632 | |
| 9633 | memset(&out, 0, sizeof out); |
| 9634 | |
| 9635 | out.type = LYOUT_STREAM; |
| 9636 | out.method.f = f; |
| 9637 | |
| 9638 | switch (set->type) { |
| 9639 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9640 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9641 | break; |
| 9642 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9643 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9644 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9645 | break; |
| 9646 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9647 | ly_print_(&out, "String XPath set:\n"); |
| 9648 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9649 | break; |
| 9650 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9651 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9652 | |
| 9653 | if (isnan(set->value.num)) { |
| 9654 | str_num = strdup("NaN"); |
| 9655 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9656 | str_num = strdup("0"); |
| 9657 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9658 | str_num = strdup("Infinity"); |
| 9659 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9660 | str_num = strdup("-Infinity"); |
| 9661 | } else if ((long long)set->value.num == set->value.num) { |
| 9662 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9663 | str_num = NULL; |
| 9664 | } |
| 9665 | } else { |
| 9666 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9667 | str_num = NULL; |
| 9668 | } |
| 9669 | } |
| 9670 | if (!str_num) { |
| 9671 | LOGMEM; |
| 9672 | return; |
| 9673 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9674 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9675 | free(str_num); |
| 9676 | break; |
| 9677 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9678 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9679 | |
| 9680 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9681 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9682 | switch (set->node_type[i]) { |
| 9683 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9684 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9685 | break; |
| 9686 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9687 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9688 | break; |
| 9689 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9690 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9691 | break; |
| 9692 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9693 | ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9694 | break; |
| 9695 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9696 | ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9697 | break; |
| 9698 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9699 | ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9700 | break; |
| 9701 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9702 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9703 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9704 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9705 | break; |
| 9706 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9707 | ly_print_(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9708 | break; |
| 9709 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9710 | ly_print_(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9711 | break; |
| 9712 | } |
| 9713 | } |
| 9714 | break; |
| 9715 | } |
| 9716 | } |
| 9717 | |
| 9718 | #endif |
| 9719 | |
| 9720 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9721 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9722 | { |
| 9723 | long double num; |
| 9724 | char *str; |
| 9725 | LY_ERR rc; |
| 9726 | |
| 9727 | if (!set || (set->type == target)) { |
| 9728 | return LY_SUCCESS; |
| 9729 | } |
| 9730 | |
| 9731 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9732 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9733 | |
| 9734 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9735 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9736 | return LY_EINVAL; |
| 9737 | } |
| 9738 | |
| 9739 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9740 | if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9741 | switch (set->type) { |
| 9742 | case LYXP_SET_NUMBER: |
| 9743 | if (isnan(set->val.num)) { |
| 9744 | set->val.str = strdup("NaN"); |
| 9745 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9746 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9747 | set->val.str = strdup("0"); |
| 9748 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9749 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9750 | set->val.str = strdup("Infinity"); |
| 9751 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9752 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9753 | set->val.str = strdup("-Infinity"); |
| 9754 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9755 | } else if ((long long)set->val.num == set->val.num) { |
| 9756 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9757 | LOGMEM_RET(set->ctx); |
| 9758 | } |
| 9759 | set->val.str = str; |
| 9760 | } else { |
| 9761 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9762 | LOGMEM_RET(set->ctx); |
| 9763 | } |
| 9764 | set->val.str = str; |
| 9765 | } |
| 9766 | break; |
| 9767 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9768 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9769 | set->val.str = strdup("true"); |
| 9770 | } else { |
| 9771 | set->val.str = strdup("false"); |
| 9772 | } |
| 9773 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9774 | break; |
| 9775 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9776 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9777 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9778 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9779 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9780 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9781 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9782 | set->val.str = str; |
| 9783 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9784 | default: |
| 9785 | LOGINT_RET(set->ctx); |
| 9786 | } |
| 9787 | set->type = LYXP_SET_STRING; |
| 9788 | } |
| 9789 | |
| 9790 | /* to NUMBER */ |
| 9791 | if (target == LYXP_SET_NUMBER) { |
| 9792 | switch (set->type) { |
| 9793 | case LYXP_SET_STRING: |
| 9794 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9795 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9796 | set->val.num = num; |
| 9797 | break; |
| 9798 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9799 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9800 | set->val.num = 1; |
| 9801 | } else { |
| 9802 | set->val.num = 0; |
| 9803 | } |
| 9804 | break; |
| 9805 | default: |
| 9806 | LOGINT_RET(set->ctx); |
| 9807 | } |
| 9808 | set->type = LYXP_SET_NUMBER; |
| 9809 | } |
| 9810 | |
| 9811 | /* to BOOLEAN */ |
| 9812 | if (target == LYXP_SET_BOOLEAN) { |
| 9813 | switch (set->type) { |
| 9814 | case LYXP_SET_NUMBER: |
| 9815 | if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9816 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9817 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9818 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9819 | } |
| 9820 | break; |
| 9821 | case LYXP_SET_STRING: |
| 9822 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9823 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9824 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9825 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9826 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9827 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9828 | } |
| 9829 | break; |
| 9830 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9831 | if (set->used) { |
| 9832 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9833 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9834 | } else { |
| 9835 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9836 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9837 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9838 | break; |
| 9839 | default: |
| 9840 | LOGINT_RET(set->ctx); |
| 9841 | } |
| 9842 | set->type = LYXP_SET_BOOLEAN; |
| 9843 | } |
| 9844 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9845 | return LY_SUCCESS; |
| 9846 | } |
| 9847 | |
| 9848 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9849 | lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9850 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode, |
| 9851 | const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9852 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9853 | LY_ERR ret; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9854 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9855 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9856 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9857 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9858 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9859 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9860 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9861 | |
| 9862 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9863 | memset(set, 0, sizeof *set); |
| 9864 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9865 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 9866 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, LYXP_AXIS_SELF, NULL)); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 9867 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9868 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9869 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9870 | set->cur_scnode = cur_scnode; |
| 9871 | for (set->context_op = cur_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9872 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9873 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9874 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9875 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9876 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9877 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9878 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9879 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9880 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9881 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9882 | |
Michal Vasko | f69e455 | 2022-12-02 10:27:42 +0100 | [diff] [blame] | 9883 | LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9884 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9885 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9886 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9887 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9888 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9889 | { |
| 9890 | if (!path) { |
| 9891 | return NULL; |
| 9892 | } |
| 9893 | |
| 9894 | return path->expr; |
| 9895 | } |