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 | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 46 | static LY_ERR set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
| 47 | enum lyxp_axis axis, uint32_t *index_p); |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 48 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth); |
| 49 | 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] | 50 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 51 | 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] | 52 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 53 | static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, |
| 54 | 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] | 55 | static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 56 | enum lyxp_axis axis, uint32_t options); |
| 57 | static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 58 | enum lyxp_axis axis, uint32_t options); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 59 | 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] | 60 | |
aPiecek | 96dc1e3 | 2021-10-08 15:45:59 +0200 | [diff] [blame] | 61 | /* Functions are divided into the following basic classes: |
| 62 | * |
| 63 | * (re)parse functions: |
| 64 | * Parse functions parse the expression into |
| 65 | * tokens (syntactic analysis). |
| 66 | * Reparse functions perform semantic analysis |
| 67 | * (do not save the result, just a check) of |
| 68 | * the expression and fill repeat indices. |
| 69 | * |
| 70 | * warn functions: |
| 71 | * Warn functions check specific reasonable conditions for schema XPath |
| 72 | * and print a warning if they are not satisfied. |
| 73 | * |
| 74 | * moveto functions: |
| 75 | * They and only they actually change the context (set). |
| 76 | * |
| 77 | * eval functions: |
| 78 | * They execute a parsed XPath expression on some data subtree. |
| 79 | */ |
| 80 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 81 | /** |
| 82 | * @brief Print the type of an XPath \p set. |
| 83 | * |
| 84 | * @param[in] set Set to use. |
| 85 | * @return Set type string. |
| 86 | */ |
| 87 | static const char * |
| 88 | print_set_type(struct lyxp_set *set) |
| 89 | { |
| 90 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 91 | case LYXP_SET_NODE_SET: |
| 92 | return "node set"; |
| 93 | case LYXP_SET_SCNODE_SET: |
| 94 | return "schema node set"; |
| 95 | case LYXP_SET_BOOLEAN: |
| 96 | return "boolean"; |
| 97 | case LYXP_SET_NUMBER: |
| 98 | return "number"; |
| 99 | case LYXP_SET_STRING: |
| 100 | return "string"; |
| 101 | } |
| 102 | |
| 103 | return NULL; |
| 104 | } |
| 105 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 106 | const char * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 107 | lyxp_token2str(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 108 | { |
| 109 | switch (tok) { |
| 110 | case LYXP_TOKEN_PAR1: |
| 111 | return "("; |
| 112 | case LYXP_TOKEN_PAR2: |
| 113 | return ")"; |
| 114 | case LYXP_TOKEN_BRACK1: |
| 115 | return "["; |
| 116 | case LYXP_TOKEN_BRACK2: |
| 117 | return "]"; |
| 118 | case LYXP_TOKEN_DOT: |
| 119 | return "."; |
| 120 | case LYXP_TOKEN_DDOT: |
| 121 | return ".."; |
| 122 | case LYXP_TOKEN_AT: |
| 123 | return "@"; |
| 124 | case LYXP_TOKEN_COMMA: |
| 125 | return ","; |
Michal Vasko | e0a8ce7 | 2023-04-04 08:30:44 +0200 | [diff] [blame] | 126 | case LYXP_TOKEN_DCOLON: |
| 127 | return "::"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 128 | case LYXP_TOKEN_NAMETEST: |
| 129 | return "NameTest"; |
| 130 | case LYXP_TOKEN_NODETYPE: |
| 131 | return "NodeType"; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 132 | case LYXP_TOKEN_VARREF: |
| 133 | return "VariableReference"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 134 | case LYXP_TOKEN_FUNCNAME: |
| 135 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 136 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 137 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 138 | case LYXP_TOKEN_OPER_EQUAL: |
| 139 | return "Operator(Equal)"; |
| 140 | case LYXP_TOKEN_OPER_NEQUAL: |
| 141 | return "Operator(Non-equal)"; |
| 142 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 143 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 144 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 145 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 146 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 147 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 148 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 149 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 150 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 151 | return "Operator(Recursive Path)"; |
Michal Vasko | e0a8ce7 | 2023-04-04 08:30:44 +0200 | [diff] [blame] | 152 | case LYXP_TOKEN_AXISNAME: |
| 153 | return "AxisName"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 154 | case LYXP_TOKEN_LITERAL: |
| 155 | return "Literal"; |
| 156 | case LYXP_TOKEN_NUMBER: |
| 157 | return "Number"; |
| 158 | default: |
| 159 | LOGINT(NULL); |
| 160 | return ""; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 165 | * @brief Transform string into an axis. |
| 166 | * |
| 167 | * @param[in] str String to transform. |
| 168 | * @param[in] str_len Length of @p str. |
| 169 | * @return Transformed axis. |
| 170 | */ |
| 171 | static enum lyxp_axis |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 172 | str2axis(const char *str, uint32_t str_len) |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 173 | { |
| 174 | switch (str_len) { |
| 175 | case 4: |
| 176 | assert(!strncmp("self", str, str_len)); |
| 177 | return LYXP_AXIS_SELF; |
| 178 | case 5: |
| 179 | assert(!strncmp("child", str, str_len)); |
| 180 | return LYXP_AXIS_CHILD; |
| 181 | case 6: |
| 182 | assert(!strncmp("parent", str, str_len)); |
| 183 | return LYXP_AXIS_PARENT; |
| 184 | case 8: |
| 185 | assert(!strncmp("ancestor", str, str_len)); |
| 186 | return LYXP_AXIS_ANCESTOR; |
| 187 | case 9: |
| 188 | if (str[0] == 'a') { |
| 189 | assert(!strncmp("attribute", str, str_len)); |
| 190 | return LYXP_AXIS_ATTRIBUTE; |
| 191 | } else if (str[0] == 'f') { |
| 192 | assert(!strncmp("following", str, str_len)); |
| 193 | return LYXP_AXIS_FOLLOWING; |
| 194 | } else { |
| 195 | assert(!strncmp("preceding", str, str_len)); |
| 196 | return LYXP_AXIS_PRECEDING; |
| 197 | } |
| 198 | break; |
| 199 | case 10: |
| 200 | assert(!strncmp("descendant", str, str_len)); |
| 201 | return LYXP_AXIS_DESCENDANT; |
| 202 | case 16: |
| 203 | assert(!strncmp("ancestor-or-self", str, str_len)); |
| 204 | return LYXP_AXIS_ANCESTOR_OR_SELF; |
| 205 | case 17: |
| 206 | if (str[0] == 'f') { |
| 207 | assert(!strncmp("following-sibling", str, str_len)); |
| 208 | return LYXP_AXIS_FOLLOWING_SIBLING; |
| 209 | } else { |
| 210 | assert(!strncmp("preceding-sibling", str, str_len)); |
| 211 | return LYXP_AXIS_PRECEDING_SIBLING; |
| 212 | } |
| 213 | break; |
| 214 | case 18: |
| 215 | assert(!strncmp("descendant-or-self", str, str_len)); |
| 216 | return LYXP_AXIS_DESCENDANT_OR_SELF; |
| 217 | } |
| 218 | |
| 219 | LOGINT(NULL); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 224 | * @brief Print the whole expression \p exp to debug output. |
| 225 | * |
| 226 | * @param[in] exp Expression to use. |
| 227 | */ |
| 228 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 229 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 230 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 231 | #define MSG_BUFFER_SIZE 128 |
| 232 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 233 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 234 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 235 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
| 239 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 240 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 241 | 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] | 242 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 243 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 244 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 245 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 246 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 247 | } |
| 248 | strcat(tmp, ")"); |
| 249 | } |
| 250 | LOGDBG(LY_LDGXPATH, tmp); |
| 251 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 252 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | #ifndef NDEBUG |
| 256 | |
| 257 | /** |
| 258 | * @brief Print XPath set content to debug output. |
| 259 | * |
| 260 | * @param[in] set Set to print. |
| 261 | */ |
| 262 | static void |
| 263 | print_set_debug(struct lyxp_set *set) |
| 264 | { |
| 265 | uint32_t i; |
| 266 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 267 | struct lyxp_set_node *item; |
| 268 | struct lyxp_set_scnode *sitem; |
| 269 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 270 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 271 | return; |
| 272 | } |
| 273 | |
| 274 | switch (set->type) { |
| 275 | case LYXP_SET_NODE_SET: |
| 276 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 277 | for (i = 0; i < set->used; ++i) { |
| 278 | item = &set->val.nodes[i]; |
| 279 | |
| 280 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 281 | case LYXP_NODE_NONE: |
| 282 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 283 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 284 | case LYXP_NODE_ROOT: |
| 285 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 286 | break; |
| 287 | case LYXP_NODE_ROOT_CONFIG: |
| 288 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 289 | break; |
| 290 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 291 | 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] | 292 | 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] | 293 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 294 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 295 | 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] | 296 | item->node->schema->name, lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 297 | } else { |
| 298 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 299 | } |
| 300 | break; |
| 301 | case LYXP_NODE_TEXT: |
| 302 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 303 | 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] | 304 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 305 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 306 | 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] | 307 | } |
| 308 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 309 | case LYXP_NODE_META: |
| 310 | 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] | 311 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | } |
| 315 | break; |
| 316 | |
| 317 | case LYXP_SET_SCNODE_SET: |
| 318 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 319 | for (i = 0; i < set->used; ++i) { |
| 320 | sitem = &set->val.scnodes[i]; |
| 321 | |
| 322 | switch (sitem->type) { |
| 323 | case LYXP_NODE_ROOT: |
| 324 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 325 | break; |
| 326 | case LYXP_NODE_ROOT_CONFIG: |
| 327 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 328 | break; |
| 329 | case LYXP_NODE_ELEM: |
| 330 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 331 | break; |
| 332 | default: |
| 333 | LOGINT(NULL); |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | break; |
| 338 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 339 | case LYXP_SET_BOOLEAN: |
| 340 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 341 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 342 | break; |
| 343 | |
| 344 | case LYXP_SET_STRING: |
| 345 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 346 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 347 | break; |
| 348 | |
| 349 | case LYXP_SET_NUMBER: |
| 350 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 351 | |
| 352 | if (isnan(set->val.num)) { |
| 353 | str = strdup("NaN"); |
| 354 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 355 | str = strdup("0"); |
| 356 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 357 | str = strdup("Infinity"); |
| 358 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 359 | str = strdup("-Infinity"); |
| 360 | } else if ((long long)set->val.num == set->val.num) { |
| 361 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 362 | str = NULL; |
| 363 | } |
| 364 | } else { |
| 365 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 366 | str = NULL; |
| 367 | } |
| 368 | } |
| 369 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 370 | |
| 371 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 372 | free(str); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | #endif |
| 377 | |
| 378 | /** |
| 379 | * @brief Realloc the string \p str. |
| 380 | * |
| 381 | * @param[in] ctx libyang context for logging. |
| 382 | * @param[in] needed How much free space is required. |
| 383 | * @param[in,out] str Pointer to the string to use. |
| 384 | * @param[in,out] used Used bytes in \p str. |
| 385 | * @param[in,out] size Allocated bytes in \p str. |
| 386 | * @return LY_ERR |
| 387 | */ |
| 388 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 389 | 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] | 390 | { |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 391 | if (*size - (unsigned)*used < needed) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 392 | do { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 393 | if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 394 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 395 | return LY_EINVAL; |
| 396 | } |
| 397 | *size += LYXP_STRING_CAST_SIZE_STEP; |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 398 | } while (*size - (unsigned)*used < needed); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 399 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 400 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 401 | } |
| 402 | |
| 403 | return LY_SUCCESS; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * @brief Cast nodes recursively to one string @p str. |
| 408 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 409 | * @param[in] node Node to cast, NULL if root. |
| 410 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 411 | * @param[in] indent Current indent. |
| 412 | * @param[in,out] str Resulting string. |
| 413 | * @param[in,out] used Used bytes in @p str. |
| 414 | * @param[in,out] size Allocated bytes in @p str. |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 415 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 416 | */ |
| 417 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 418 | cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used, |
| 419 | uint32_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 420 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 421 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 422 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 423 | const struct lyd_node *child; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 424 | enum lyxp_node_type child_type; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 425 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 426 | struct lyd_node_any *any; |
| 427 | LY_ERR rc; |
| 428 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 429 | 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] | 430 | return LY_SUCCESS; |
| 431 | } |
| 432 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 433 | if (!node) { |
| 434 | /* fake container */ |
| 435 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 436 | strcpy(*str + (*used - 1), "\n"); |
| 437 | ++(*used); |
| 438 | |
| 439 | ++indent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 440 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 441 | /* print all the top-level nodes */ |
| 442 | child = NULL; |
| 443 | child_type = 0; |
| 444 | while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) { |
| 445 | LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 448 | /* end fake container */ |
| 449 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 450 | strcpy(*str + (*used - 1), "\n"); |
| 451 | ++(*used); |
| 452 | |
| 453 | --indent; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 454 | } else { |
| 455 | switch (node->schema->nodetype) { |
| 456 | case LYS_CONTAINER: |
| 457 | case LYS_LIST: |
| 458 | case LYS_RPC: |
| 459 | case LYS_NOTIF: |
| 460 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
| 461 | strcpy(*str + (*used - 1), "\n"); |
| 462 | ++(*used); |
| 463 | |
| 464 | for (child = lyd_child(node); child; child = child->next) { |
| 465 | LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size)); |
| 466 | } |
| 467 | |
| 468 | break; |
| 469 | |
| 470 | case LYS_LEAF: |
| 471 | case LYS_LEAFLIST: |
| 472 | value_str = lyd_get_value(node); |
| 473 | |
| 474 | /* print indent */ |
| 475 | LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size)); |
| 476 | memset(*str + (*used - 1), ' ', indent * 2); |
| 477 | *used += indent * 2; |
| 478 | |
| 479 | /* print value */ |
| 480 | if (*used == 1) { |
| 481 | sprintf(*str + (*used - 1), "%s", value_str); |
| 482 | *used += strlen(value_str); |
| 483 | } else { |
| 484 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 485 | *used += strlen(value_str) + 1; |
| 486 | } |
| 487 | |
| 488 | break; |
| 489 | |
| 490 | case LYS_ANYXML: |
| 491 | case LYS_ANYDATA: |
| 492 | any = (struct lyd_node_any *)node; |
| 493 | if (!(void *)any->value.tree) { |
| 494 | /* no content */ |
| 495 | buf = strdup(""); |
| 496 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 497 | } else { |
| 498 | struct ly_out *out; |
| 499 | |
| 500 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 501 | /* try to parse it into a data tree */ |
| 502 | if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB, |
| 503 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
| 504 | /* successfully parsed */ |
| 505 | free(any->value.mem); |
| 506 | any->value.tree = tree; |
| 507 | any->value_type = LYD_ANYDATA_DATATREE; |
| 508 | } |
| 509 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
| 510 | } |
| 511 | |
| 512 | switch (any->value_type) { |
| 513 | case LYD_ANYDATA_STRING: |
| 514 | case LYD_ANYDATA_XML: |
| 515 | case LYD_ANYDATA_JSON: |
| 516 | buf = strdup(any->value.json); |
| 517 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 518 | break; |
| 519 | case LYD_ANYDATA_DATATREE: |
| 520 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
| 521 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
| 522 | ly_out_free(out, NULL, 0); |
| 523 | LY_CHECK_RET(rc < 0, -rc); |
| 524 | break; |
| 525 | case LYD_ANYDATA_LYB: |
| 526 | LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 527 | return LY_EINVAL; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | line = strtok_r(buf, "\n", &ptr); |
| 532 | do { |
| 533 | rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size); |
| 534 | if (rc != LY_SUCCESS) { |
| 535 | free(buf); |
| 536 | return rc; |
| 537 | } |
| 538 | memset(*str + (*used - 1), ' ', indent * 2); |
| 539 | *used += indent * 2; |
| 540 | |
| 541 | strcpy(*str + (*used - 1), line); |
| 542 | *used += strlen(line); |
| 543 | |
| 544 | strcpy(*str + (*used - 1), "\n"); |
| 545 | *used += 1; |
| 546 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 547 | |
| 548 | free(buf); |
| 549 | break; |
| 550 | |
| 551 | default: |
| 552 | LOGINT_RET(set->ctx); |
| 553 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | return LY_SUCCESS; |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * @brief Cast an element into a string. |
| 561 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 562 | * @param[in] node Node to cast, NULL if root. |
| 563 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 564 | * @param[out] str Element cast to dynamically-allocated string. |
| 565 | * @return LY_ERR |
| 566 | */ |
| 567 | static LY_ERR |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 568 | 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] | 569 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 570 | uint32_t used, size; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 571 | LY_ERR rc; |
| 572 | |
| 573 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 574 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 575 | (*str)[0] = '\0'; |
| 576 | used = 1; |
| 577 | size = LYXP_STRING_CAST_SIZE_START; |
| 578 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 579 | rc = cast_string_recursive(node, set, 0, str, &used, &size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 580 | if (rc != LY_SUCCESS) { |
| 581 | free(*str); |
| 582 | return rc; |
| 583 | } |
| 584 | |
| 585 | if (size > used) { |
| 586 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 587 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 588 | } |
| 589 | return LY_SUCCESS; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 594 | * Context position aware. |
| 595 | * |
| 596 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 597 | * @param[out] str Cast dynamically-allocated string. |
| 598 | * @return LY_ERR |
| 599 | */ |
| 600 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 601 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 602 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 603 | if (!set->used) { |
| 604 | *str = strdup(""); |
| 605 | if (!*str) { |
| 606 | LOGMEM_RET(set->ctx); |
| 607 | } |
| 608 | return LY_SUCCESS; |
| 609 | } |
| 610 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 611 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 612 | case LYXP_NODE_NONE: |
| 613 | /* invalid */ |
| 614 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 615 | case LYXP_NODE_ROOT: |
| 616 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 617 | case LYXP_NODE_ELEM: |
| 618 | case LYXP_NODE_TEXT: |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 619 | return cast_string_elem(set->val.nodes[0].node, set, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 620 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 621 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 622 | if (!*str) { |
| 623 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 624 | } |
| 625 | return LY_SUCCESS; |
| 626 | } |
| 627 | |
| 628 | LOGINT_RET(set->ctx); |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * @brief Cast a string into an XPath number. |
| 633 | * |
| 634 | * @param[in] str String to use. |
| 635 | * @return Cast number. |
| 636 | */ |
| 637 | static long double |
| 638 | cast_string_to_number(const char *str) |
| 639 | { |
| 640 | long double num; |
| 641 | char *ptr; |
| 642 | |
| 643 | errno = 0; |
| 644 | num = strtold(str, &ptr); |
| 645 | if (errno || *ptr) { |
| 646 | num = NAN; |
| 647 | } |
| 648 | return num; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * @brief Callback for checking value equality. |
| 653 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 654 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 655 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 656 | * @param[in] val1_p First value. |
| 657 | * @param[in] val2_p Second value. |
| 658 | * @param[in] mod Whether hash table is being modified. |
| 659 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 660 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 661 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 662 | static ly_bool |
| 663 | 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] | 664 | { |
| 665 | struct lyxp_set_hash_node *val1, *val2; |
| 666 | |
| 667 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 668 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 669 | |
| 670 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 671 | return 1; |
| 672 | } |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * @brief Insert node and its hash into set. |
| 679 | * |
| 680 | * @param[in] set et to insert to. |
| 681 | * @param[in] node Node with hash. |
| 682 | * @param[in] type Node type. |
| 683 | */ |
| 684 | static void |
| 685 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 686 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 687 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 688 | uint32_t i, hash; |
| 689 | struct lyxp_set_hash_node hnode; |
| 690 | |
| 691 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 692 | /* create hash table and add all the nodes */ |
| 693 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 694 | for (i = 0; i < set->used; ++i) { |
| 695 | hnode.node = set->val.nodes[i].node; |
| 696 | hnode.type = set->val.nodes[i].type; |
| 697 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame^] | 698 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 699 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 700 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 701 | |
| 702 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 703 | assert(!r); |
| 704 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 705 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 706 | if (hnode.node == node) { |
| 707 | /* it was just added, do not add it twice */ |
| 708 | node = NULL; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 714 | /* add the new node into hash table */ |
| 715 | hnode.node = node; |
| 716 | hnode.type = type; |
| 717 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame^] | 718 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 719 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 720 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 721 | |
| 722 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 723 | assert(!r); |
| 724 | (void)r; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * @brief Remove node and its hash from set. |
| 730 | * |
| 731 | * @param[in] set Set to remove from. |
| 732 | * @param[in] node Node to remove. |
| 733 | * @param[in] type Node type. |
| 734 | */ |
| 735 | static void |
| 736 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 737 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 738 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 739 | struct lyxp_set_hash_node hnode; |
| 740 | uint32_t hash; |
| 741 | |
| 742 | if (set->ht) { |
| 743 | hnode.node = node; |
| 744 | hnode.type = type; |
| 745 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame^] | 746 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 747 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 748 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 749 | |
| 750 | r = lyht_remove(set->ht, &hnode, hash); |
| 751 | assert(!r); |
| 752 | (void)r; |
| 753 | |
| 754 | if (!set->ht->used) { |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 755 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 756 | set->ht = NULL; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * @brief Check whether node is in set based on its hash. |
| 763 | * |
| 764 | * @param[in] set Set to search in. |
| 765 | * @param[in] node Node to search for. |
| 766 | * @param[in] type Node type. |
| 767 | * @param[in] skip_idx Index in @p set to skip. |
| 768 | * @return LY_ERR |
| 769 | */ |
| 770 | static LY_ERR |
| 771 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 772 | { |
| 773 | struct lyxp_set_hash_node hnode, *match_p; |
| 774 | uint32_t hash; |
| 775 | |
| 776 | hnode.node = node; |
| 777 | hnode.type = type; |
| 778 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame^] | 779 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 780 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 781 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 782 | |
| 783 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 784 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 785 | /* we found it on the index that should be skipped, find another */ |
| 786 | hnode = *match_p; |
| 787 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 788 | /* none other found */ |
| 789 | return LY_SUCCESS; |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | return LY_EEXIST; |
| 794 | } |
| 795 | |
| 796 | /* not found */ |
| 797 | return LY_SUCCESS; |
| 798 | } |
| 799 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 800 | void |
| 801 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 802 | { |
| 803 | if (!set) { |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | if (set->type == LYXP_SET_NODE_SET) { |
| 808 | free(set->val.nodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 809 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 810 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 811 | free(set->val.scnodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 812 | lyht_free(set->ht, NULL); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 813 | } else { |
| 814 | if (set->type == LYXP_SET_STRING) { |
| 815 | free(set->val.str); |
| 816 | } |
| 817 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 818 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 819 | |
| 820 | set->val.nodes = NULL; |
| 821 | set->used = 0; |
| 822 | set->size = 0; |
| 823 | set->ht = NULL; |
| 824 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 825 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 826 | } |
| 827 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 828 | /** |
| 829 | * @brief Free dynamically-allocated set. |
| 830 | * |
| 831 | * @param[in] set Set to free. |
| 832 | */ |
| 833 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 834 | lyxp_set_free(struct lyxp_set *set) |
| 835 | { |
| 836 | if (!set) { |
| 837 | return; |
| 838 | } |
| 839 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 840 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 841 | free(set); |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * @brief Initialize set context. |
| 846 | * |
| 847 | * @param[in] new Set to initialize. |
| 848 | * @param[in] set Arbitrary initialized set. |
| 849 | */ |
| 850 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 851 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 852 | { |
| 853 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 854 | if (set) { |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 855 | new->non_child_axis = set->non_child_axis; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 856 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 857 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 858 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 859 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 860 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 861 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 862 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 863 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 864 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 865 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /** |
| 869 | * @brief Create a deep copy of a set. |
| 870 | * |
| 871 | * @param[in] set Set to copy. |
| 872 | * @return Copy of @p set. |
| 873 | */ |
| 874 | static struct lyxp_set * |
| 875 | set_copy(struct lyxp_set *set) |
| 876 | { |
| 877 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 878 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 879 | |
| 880 | if (!set) { |
| 881 | return NULL; |
| 882 | } |
| 883 | |
| 884 | ret = malloc(sizeof *ret); |
| 885 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 886 | set_init(ret, set); |
| 887 | |
| 888 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 889 | ret->type = set->type; |
| 890 | |
| 891 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 892 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 893 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 894 | uint32_t idx; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 895 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 896 | LY_CHECK_ERR_RET(set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, |
| 897 | set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 898 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 899 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 900 | lyxp_set_free(ret); |
| 901 | return NULL; |
| 902 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 903 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 907 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 908 | if (set->used) { |
| 909 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 910 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 911 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 912 | } else { |
| 913 | ret->val.nodes = NULL; |
| 914 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 915 | |
| 916 | ret->used = ret->size = set->used; |
| 917 | ret->ctx_pos = set->ctx_pos; |
| 918 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 919 | if (set->ht) { |
| 920 | ret->ht = lyht_dup(set->ht); |
| 921 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 922 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 923 | memcpy(ret, set, sizeof *ret); |
| 924 | if (set->type == LYXP_SET_STRING) { |
| 925 | ret->val.str = strdup(set->val.str); |
| 926 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 927 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 935 | * |
| 936 | * @param[in] set Set to fill. |
| 937 | * @param[in] string String to fill into \p set. |
| 938 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 939 | */ |
| 940 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 941 | 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] | 942 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 943 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 944 | |
| 945 | set->type = LYXP_SET_STRING; |
| 946 | if ((str_len == 0) && (string[0] != '\0')) { |
| 947 | string = ""; |
| 948 | } |
| 949 | set->val.str = strndup(string, str_len); |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 954 | * |
| 955 | * @param[in] set Set to fill. |
| 956 | * @param[in] number Number to fill into \p set. |
| 957 | */ |
| 958 | static void |
| 959 | set_fill_number(struct lyxp_set *set, long double number) |
| 960 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 961 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 962 | |
| 963 | set->type = LYXP_SET_NUMBER; |
| 964 | set->val.num = number; |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 969 | * |
| 970 | * @param[in] set Set to fill. |
| 971 | * @param[in] boolean Boolean to fill into \p set. |
| 972 | */ |
| 973 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 974 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 975 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 976 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 977 | |
| 978 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 979 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /** |
| 983 | * @brief Fill XPath set with the value from another set (deep assign). |
| 984 | * Any current data are disposed of. |
| 985 | * |
| 986 | * @param[in] trg Set to fill. |
| 987 | * @param[in] src Source set to copy into \p trg. |
| 988 | */ |
| 989 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 990 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 991 | { |
| 992 | if (!trg || !src) { |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | if (trg->type == LYXP_SET_NODE_SET) { |
| 997 | free(trg->val.nodes); |
| 998 | } else if (trg->type == LYXP_SET_STRING) { |
| 999 | free(trg->val.str); |
| 1000 | } |
| 1001 | set_init(trg, src); |
| 1002 | |
| 1003 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 1004 | trg->type = LYXP_SET_SCNODE_SET; |
| 1005 | trg->used = src->used; |
| 1006 | trg->size = src->used; |
| 1007 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1008 | if (trg->size) { |
| 1009 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 1010 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1011 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 1012 | } else { |
| 1013 | trg->val.scnodes = NULL; |
| 1014 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1015 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1016 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1017 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1018 | set_fill_number(trg, src->val.num); |
| 1019 | } else if (src->type == LYXP_SET_STRING) { |
| 1020 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 1021 | } else { |
| 1022 | if (trg->type == LYXP_SET_NODE_SET) { |
| 1023 | free(trg->val.nodes); |
| 1024 | } else if (trg->type == LYXP_SET_STRING) { |
| 1025 | free(trg->val.str); |
| 1026 | } |
| 1027 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1028 | assert(src->type == LYXP_SET_NODE_SET); |
| 1029 | |
| 1030 | trg->type = LYXP_SET_NODE_SET; |
| 1031 | trg->used = src->used; |
| 1032 | trg->size = src->used; |
| 1033 | trg->ctx_pos = src->ctx_pos; |
| 1034 | trg->ctx_size = src->ctx_size; |
| 1035 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1036 | if (trg->size) { |
| 1037 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 1038 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1039 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 1040 | } else { |
| 1041 | trg->val.nodes = NULL; |
| 1042 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1043 | if (src->ht) { |
| 1044 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1045 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1046 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * @brief Clear context of all schema nodes. |
| 1053 | * |
| 1054 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1055 | * @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] | 1056 | */ |
| 1057 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1058 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1059 | { |
| 1060 | uint32_t i; |
| 1061 | |
| 1062 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1063 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1064 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1065 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 1066 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * @brief Remove a node from a set. Removing last node changes |
| 1073 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1074 | * |
| 1075 | * @param[in] set Set to use. |
| 1076 | * @param[in] idx Index from @p set of the node to be removed. |
| 1077 | */ |
| 1078 | static void |
| 1079 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1080 | { |
| 1081 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1082 | assert(idx < set->used); |
| 1083 | |
| 1084 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1085 | |
| 1086 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1087 | if (idx < set->used) { |
| 1088 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1089 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1090 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1095 | * @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] | 1096 | * |
| 1097 | * @param[in] set Set to use. |
| 1098 | * @param[in] idx Index from @p set of the node to be removed. |
| 1099 | */ |
| 1100 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1101 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1102 | { |
| 1103 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1104 | assert(idx < set->used); |
| 1105 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1106 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1107 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1108 | } |
| 1109 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1113 | * @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] | 1114 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1115 | * |
| 1116 | * @param[in] set Set to consolidate. |
| 1117 | */ |
| 1118 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1119 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1120 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1121 | uint32_t i, orig_used, end = 0; |
| 1122 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1123 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1124 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1125 | |
| 1126 | orig_used = set->used; |
| 1127 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1128 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1129 | start = -1; |
| 1130 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1131 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1132 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1133 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1134 | end = i; |
| 1135 | ++i; |
| 1136 | break; |
| 1137 | } |
| 1138 | |
| 1139 | ++i; |
| 1140 | if (i == orig_used) { |
| 1141 | end = i; |
| 1142 | } |
| 1143 | } while (i < orig_used); |
| 1144 | |
| 1145 | if (start > -1) { |
| 1146 | /* move the whole chunk of valid nodes together */ |
| 1147 | if (set->used != (unsigned)start) { |
| 1148 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1149 | } |
| 1150 | set->used += end - start; |
| 1151 | } |
| 1152 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1156 | * @brief Check for duplicates in a node set. |
| 1157 | * |
| 1158 | * @param[in] set Set to check. |
| 1159 | * @param[in] node Node to look for in @p set. |
| 1160 | * @param[in] node_type Type of @p node. |
| 1161 | * @param[in] skip_idx Index from @p set to skip. |
| 1162 | * @return LY_ERR |
| 1163 | */ |
| 1164 | static LY_ERR |
| 1165 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1166 | { |
| 1167 | uint32_t i; |
| 1168 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1169 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1170 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1171 | } |
| 1172 | |
| 1173 | for (i = 0; i < set->used; ++i) { |
| 1174 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
| 1178 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1179 | return LY_EEXIST; |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | return LY_SUCCESS; |
| 1184 | } |
| 1185 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1186 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1187 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1188 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1189 | { |
| 1190 | uint32_t i; |
| 1191 | |
| 1192 | for (i = 0; i < set->used; ++i) { |
| 1193 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1194 | continue; |
| 1195 | } |
| 1196 | |
| 1197 | 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] | 1198 | if (index_p) { |
| 1199 | *index_p = i; |
| 1200 | } |
| 1201 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1205 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1208 | void |
| 1209 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1210 | { |
| 1211 | uint32_t orig_used, i, j; |
| 1212 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1213 | 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] | 1214 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1215 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1216 | return; |
| 1217 | } |
| 1218 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1219 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1220 | /* release hidden allocated data (lyxp_set.size) */ |
| 1221 | lyxp_set_free_content(set1); |
| 1222 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1223 | memcpy(set1, set2, sizeof *set1); |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | if (set1->used + set2->used > set1->size) { |
| 1228 | set1->size = set1->used + set2->used; |
| 1229 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1230 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1231 | } |
| 1232 | |
| 1233 | orig_used = set1->used; |
| 1234 | |
| 1235 | for (i = 0; i < set2->used; ++i) { |
| 1236 | for (j = 0; j < orig_used; ++j) { |
| 1237 | /* detect duplicities */ |
| 1238 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1239 | break; |
| 1240 | } |
| 1241 | } |
| 1242 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1243 | if (j < orig_used) { |
| 1244 | /* node is there, but update its status if needed */ |
| 1245 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1246 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1247 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1248 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1249 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1250 | } |
| 1251 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1252 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1253 | ++set1->used; |
| 1254 | } |
| 1255 | } |
| 1256 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1257 | lyxp_set_free_content(set2); |
| 1258 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * @brief Insert a node into a set. Context position aware. |
| 1263 | * |
| 1264 | * @param[in] set Set to use. |
| 1265 | * @param[in] node Node to insert to @p set. |
| 1266 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1267 | * @param[in] node_type Node type of @p node. |
| 1268 | * @param[in] idx Index in @p set to insert into. |
| 1269 | */ |
| 1270 | static void |
| 1271 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1272 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1273 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1274 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1275 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1276 | /* first item */ |
| 1277 | if (idx) { |
| 1278 | /* no real harm done, but it is a bug */ |
| 1279 | LOGINT(set->ctx); |
| 1280 | idx = 0; |
| 1281 | } |
| 1282 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1283 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1284 | set->type = LYXP_SET_NODE_SET; |
| 1285 | set->used = 0; |
| 1286 | set->size = LYXP_SET_SIZE_START; |
| 1287 | set->ctx_pos = 1; |
| 1288 | set->ctx_size = 1; |
| 1289 | set->ht = NULL; |
| 1290 | } else { |
| 1291 | /* not an empty set */ |
| 1292 | if (set->used == set->size) { |
| 1293 | |
| 1294 | /* set is full */ |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1295 | 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] | 1296 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1297 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | if (idx > set->used) { |
| 1301 | LOGINT(set->ctx); |
| 1302 | idx = set->used; |
| 1303 | } |
| 1304 | |
| 1305 | /* make space for the new node */ |
| 1306 | if (idx < set->used) { |
| 1307 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /* finally assign the value */ |
| 1312 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1313 | set->val.nodes[idx].type = node_type; |
| 1314 | set->val.nodes[idx].pos = pos; |
| 1315 | ++set->used; |
| 1316 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1317 | /* add into hash table */ |
| 1318 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1321 | /** |
| 1322 | * @brief Insert schema node into set. |
| 1323 | * |
| 1324 | * @param[in] set Set to insert into. |
| 1325 | * @param[in] node Node to insert. |
| 1326 | * @param[in] node_type Node type of @p node. |
| 1327 | * @param[in] axis Axis that @p node was reached on. |
| 1328 | * @param[out] index_p Optional pointer to store index if the inserted @p node. |
| 1329 | * @return LY_SUCCESS on success. |
| 1330 | * @return LY_EMEM on memory allocation failure. |
| 1331 | */ |
| 1332 | static LY_ERR |
| 1333 | set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
| 1334 | enum lyxp_axis axis, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1335 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1336 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1337 | |
| 1338 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1339 | |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1340 | if (!set->size) { |
| 1341 | /* first item */ |
| 1342 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
| 1343 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
| 1344 | set->type = LYXP_SET_SCNODE_SET; |
| 1345 | set->used = 0; |
| 1346 | set->size = LYXP_SET_SIZE_START; |
| 1347 | set->ctx_pos = 1; |
| 1348 | set->ctx_size = 1; |
| 1349 | set->ht = NULL; |
| 1350 | } |
| 1351 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1352 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1353 | /* BUG if axes differs, this new one is thrown away */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1354 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1355 | } else { |
| 1356 | if (set->used == set->size) { |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1357 | 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] | 1358 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1359 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1362 | index = set->used; |
| 1363 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1364 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1365 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1366 | set->val.scnodes[index].axis = axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1367 | ++set->used; |
| 1368 | } |
| 1369 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1370 | if (index_p) { |
| 1371 | *index_p = index; |
| 1372 | } |
| 1373 | |
| 1374 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1378 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1379 | * |
| 1380 | * @param[in] set Set to modify. |
| 1381 | * @return New context value. |
| 1382 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1383 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1384 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1385 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1386 | uint32_t i; |
| 1387 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1388 | |
| 1389 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1390 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1391 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1392 | retry: |
| 1393 | for (i = 0; i < set->used; ++i) { |
| 1394 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1395 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1396 | goto retry; |
| 1397 | } |
| 1398 | } |
| 1399 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1400 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1401 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | return ret_ctx; |
| 1406 | } |
| 1407 | |
| 1408 | /** |
| 1409 | * @brief Get unique @p node position in the data. |
| 1410 | * |
| 1411 | * @param[in] node Node to find. |
| 1412 | * @param[in] node_type Node type of @p node. |
| 1413 | * @param[in] root Root node. |
| 1414 | * @param[in] root_type Type of the XPath @p root node. |
| 1415 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1416 | * be used to increase efficiency and start the DFS from this node. |
| 1417 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1418 | * @return Node position. |
| 1419 | */ |
| 1420 | static uint32_t |
| 1421 | 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] | 1422 | 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] | 1423 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1424 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1425 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1426 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1427 | |
| 1428 | assert(prev && prev_pos && !root->prev->next); |
| 1429 | |
| 1430 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | if (*prev) { |
| 1435 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1436 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1437 | 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] | 1438 | goto dfs_search; |
| 1439 | } |
| 1440 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1441 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1442 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1443 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1444 | LYD_TREE_DFS_continue = 0; |
| 1445 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1446 | if (*prev && !elem) { |
| 1447 | /* resume previous DFS */ |
| 1448 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1449 | LYD_TREE_DFS_continue = 0; |
| 1450 | } |
| 1451 | |
Michal Vasko | 482a682 | 2022-05-06 08:56:12 +0200 | [diff] [blame] | 1452 | 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] | 1453 | /* skip */ |
| 1454 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1455 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1456 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1457 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1458 | break; |
| 1459 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1460 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1461 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1462 | |
| 1463 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1467 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1468 | break; |
| 1469 | } |
| 1470 | } |
| 1471 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1472 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1473 | if (!(*prev)) { |
| 1474 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1475 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1476 | return 0; |
| 1477 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1478 | /* start the search again from the beginning */ |
| 1479 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1480 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1481 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1482 | pos = 1; |
| 1483 | goto dfs_search; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /* remember the last found node for next time */ |
| 1488 | *prev = node; |
| 1489 | *prev_pos = pos; |
| 1490 | |
| 1491 | return pos; |
| 1492 | } |
| 1493 | |
| 1494 | /** |
| 1495 | * @brief Assign (fill) missing node positions. |
| 1496 | * |
| 1497 | * @param[in] set Set to fill positions in. |
| 1498 | * @param[in] root Context root node. |
| 1499 | * @param[in] root_type Context root type. |
| 1500 | * @return LY_ERR |
| 1501 | */ |
| 1502 | static LY_ERR |
| 1503 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1504 | { |
| 1505 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1506 | uint32_t i, tmp_pos = 0; |
| 1507 | |
| 1508 | for (i = 0; i < set->used; ++i) { |
| 1509 | if (!set->val.nodes[i].pos) { |
| 1510 | tmp_node = NULL; |
| 1511 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1512 | case LYXP_NODE_META: |
| 1513 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1514 | if (!tmp_node) { |
| 1515 | LOGINT_RET(root->schema->module->ctx); |
| 1516 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1517 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1518 | case LYXP_NODE_ELEM: |
| 1519 | case LYXP_NODE_TEXT: |
| 1520 | if (!tmp_node) { |
| 1521 | tmp_node = set->val.nodes[i].node; |
| 1522 | } |
| 1523 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1524 | break; |
| 1525 | default: |
| 1526 | /* all roots have position 0 */ |
| 1527 | break; |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | return LY_SUCCESS; |
| 1533 | } |
| 1534 | |
| 1535 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1536 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1537 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1538 | * @param[in] meta Metadata to use. |
| 1539 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1540 | */ |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1541 | static uint32_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1542 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1543 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1544 | uint32_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1545 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1546 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1547 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1548 | ++pos; |
| 1549 | } |
| 1550 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1551 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1552 | return pos; |
| 1553 | } |
| 1554 | |
| 1555 | /** |
| 1556 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1557 | * |
| 1558 | * @param[in] item1 1st node. |
| 1559 | * @param[in] item2 2nd node. |
| 1560 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1561 | */ |
| 1562 | static int |
| 1563 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1564 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1565 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1566 | |
| 1567 | if (item1->pos < item2->pos) { |
| 1568 | return -1; |
| 1569 | } |
| 1570 | |
| 1571 | if (item1->pos > item2->pos) { |
| 1572 | return 1; |
| 1573 | } |
| 1574 | |
| 1575 | /* node positions are equal, the fun case */ |
| 1576 | |
| 1577 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1578 | /* special case since text nodes are actually saved as their parents */ |
| 1579 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1580 | if (item1->type == LYXP_NODE_ELEM) { |
| 1581 | assert(item2->type == LYXP_NODE_TEXT); |
| 1582 | return -1; |
| 1583 | } else { |
| 1584 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1585 | return 1; |
| 1586 | } |
| 1587 | } |
| 1588 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1589 | /* we need meta positions now */ |
| 1590 | if (item1->type == LYXP_NODE_META) { |
| 1591 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1592 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1593 | if (item2->type == LYXP_NODE_META) { |
| 1594 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1595 | } |
| 1596 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1597 | /* 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] | 1598 | /* check for duplicates */ |
| 1599 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1600 | 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] | 1601 | return 0; |
| 1602 | } |
| 1603 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1604 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1605 | /* elem is always first, 2nd node is after it */ |
| 1606 | if (item1->type == LYXP_NODE_ELEM) { |
| 1607 | assert(item2->type != LYXP_NODE_ELEM); |
| 1608 | return -1; |
| 1609 | } |
| 1610 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1611 | /* 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] | 1612 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1613 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1614 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1615 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1616 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1617 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1618 | return 1; |
| 1619 | } |
| 1620 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1621 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1622 | /* 2nd is after 1st */ |
| 1623 | return -1; |
| 1624 | } |
| 1625 | |
| 1626 | /** |
| 1627 | * @brief Set cast for comparisons. |
| 1628 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1629 | * @param[in,out] trg Target set to cast source into. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1630 | * @param[in] src Source set. |
| 1631 | * @param[in] type Target set type. |
| 1632 | * @param[in] src_idx Source set node index. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1633 | * @return LY_SUCCESS on success. |
| 1634 | * @return LY_ERR value on error. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1635 | */ |
| 1636 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1637 | 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] | 1638 | { |
| 1639 | assert(src->type == LYXP_SET_NODE_SET); |
| 1640 | |
| 1641 | set_init(trg, src); |
| 1642 | |
| 1643 | /* insert node into target set */ |
| 1644 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1645 | |
| 1646 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1647 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1648 | } |
| 1649 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1650 | /** |
| 1651 | * @brief Set content canonization for comparisons. |
| 1652 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1653 | * @param[in,out] set Set to canonize. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1654 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1655 | * @return LY_SUCCESS on success. |
| 1656 | * @return LY_ERR value on error. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1657 | */ |
| 1658 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1659 | 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] | 1660 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1661 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1662 | struct lyd_value val; |
| 1663 | struct ly_err_item *err = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1664 | LY_ERR rc; |
| 1665 | |
| 1666 | /* is there anything to canonize even? */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1667 | if (set->type == LYXP_SET_STRING) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1668 | /* do we have a type to use for canonization? */ |
| 1669 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1670 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1671 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1672 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1673 | } |
| 1674 | } |
| 1675 | if (!type) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1676 | /* no canonization needed/possible */ |
| 1677 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1678 | } |
| 1679 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1680 | /* check for built-in types without required canonization */ |
| 1681 | if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) { |
| 1682 | /* string */ |
| 1683 | return LY_SUCCESS; |
| 1684 | } |
| 1685 | if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) { |
| 1686 | /* boolean */ |
| 1687 | return LY_SUCCESS; |
| 1688 | } |
| 1689 | if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) { |
| 1690 | /* enumeration */ |
| 1691 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1692 | } |
| 1693 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1694 | /* print canonized string, ignore errors, the value may not satisfy schema constraints */ |
| 1695 | 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] | 1696 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1697 | ly_err_free(err); |
| 1698 | if (rc) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1699 | /* invalid value, function store automaticaly dealloc value when fail */ |
| 1700 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1701 | } |
| 1702 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1703 | /* use the canonized string value */ |
| 1704 | free(set->val.str); |
| 1705 | set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val)); |
| 1706 | type->plugin->free(set->ctx, &val); |
| 1707 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1708 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1709 | return LY_SUCCESS; |
| 1710 | } |
| 1711 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1712 | /** |
| 1713 | * @brief Bubble sort @p set into XPath document order. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1714 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1715 | * |
| 1716 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1717 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1718 | */ |
| 1719 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1720 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1721 | { |
| 1722 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1723 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1724 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1725 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1726 | struct lyxp_set_node item; |
| 1727 | struct lyxp_set_hash_node hnode; |
| 1728 | uint64_t hash; |
| 1729 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1730 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1731 | return 0; |
| 1732 | } |
| 1733 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1734 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1735 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1736 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1737 | |
| 1738 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1739 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1740 | return -1; |
| 1741 | } |
| 1742 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1743 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1744 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1745 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1746 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1747 | |
| 1748 | for (i = 0; i < set->used; ++i) { |
| 1749 | inverted = 0; |
| 1750 | change = 0; |
| 1751 | |
| 1752 | for (j = 1; j < set->used - i; ++j) { |
| 1753 | /* compare node positions */ |
| 1754 | if (inverted) { |
| 1755 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1756 | } else { |
| 1757 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1758 | } |
| 1759 | |
| 1760 | /* swap if needed */ |
| 1761 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1762 | change = 1; |
| 1763 | |
| 1764 | item = set->val.nodes[j - 1]; |
| 1765 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1766 | set->val.nodes[j] = item; |
| 1767 | } else { |
| 1768 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1769 | inverted = !inverted; |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | ++ret; |
| 1774 | |
| 1775 | if (!change) { |
| 1776 | break; |
| 1777 | } |
| 1778 | } |
| 1779 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1780 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1781 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1782 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1783 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1784 | |
| 1785 | /* check node hashes */ |
| 1786 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1787 | assert(set->ht); |
| 1788 | for (i = 0; i < set->used; ++i) { |
| 1789 | hnode.node = set->val.nodes[i].node; |
| 1790 | hnode.type = set->val.nodes[i].type; |
| 1791 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame^] | 1792 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1793 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1794 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1795 | |
| 1796 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | return ret - 1; |
| 1801 | } |
| 1802 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1803 | /** |
| 1804 | * @brief Merge 2 sorted sets into one. |
| 1805 | * |
| 1806 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1807 | * @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] | 1808 | * @return LY_ERR |
| 1809 | */ |
| 1810 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1811 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1812 | { |
| 1813 | uint32_t i, j, k, count, dup_count; |
| 1814 | int cmp; |
| 1815 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1816 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1817 | 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] | 1818 | return LY_EINVAL; |
| 1819 | } |
| 1820 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1821 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1822 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1823 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1824 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1825 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1826 | return LY_SUCCESS; |
| 1827 | } |
| 1828 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1829 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0143b68 | 2021-12-13 17:13:09 +0100 | [diff] [blame] | 1830 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1831 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1832 | |
| 1833 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1834 | 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] | 1835 | return LY_EINT; |
| 1836 | } |
| 1837 | |
| 1838 | #ifndef NDEBUG |
| 1839 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1840 | print_set_debug(trg); |
| 1841 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1842 | print_set_debug(src); |
| 1843 | #endif |
| 1844 | |
| 1845 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1846 | * will likely be wasted on them, too bad) */ |
| 1847 | if (trg->size - trg->used < src->used) { |
| 1848 | trg->size = trg->used + src->used; |
| 1849 | |
| 1850 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1851 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1852 | } |
| 1853 | |
| 1854 | i = 0; |
| 1855 | j = 0; |
| 1856 | count = 0; |
| 1857 | dup_count = 0; |
| 1858 | do { |
| 1859 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1860 | if (!cmp) { |
| 1861 | if (!count) { |
| 1862 | /* duplicate, just skip it */ |
| 1863 | ++i; |
| 1864 | ++j; |
| 1865 | } else { |
| 1866 | /* we are copying something already, so let's copy the duplicate too, |
| 1867 | * we are hoping that afterwards there are some more nodes to |
| 1868 | * copy and this way we can copy them all together */ |
| 1869 | ++count; |
| 1870 | ++dup_count; |
| 1871 | ++i; |
| 1872 | ++j; |
| 1873 | } |
| 1874 | } else if (cmp < 0) { |
| 1875 | /* inserting src node into trg, just remember it for now */ |
| 1876 | ++count; |
| 1877 | ++i; |
| 1878 | |
| 1879 | /* insert the hash now */ |
| 1880 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1881 | } else if (count) { |
| 1882 | copy_nodes: |
| 1883 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1884 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1885 | &trg->val.nodes[j], |
| 1886 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1887 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1888 | |
| 1889 | trg->used += count - dup_count; |
| 1890 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1891 | j += count - dup_count; |
| 1892 | |
| 1893 | count = 0; |
| 1894 | dup_count = 0; |
| 1895 | } else { |
| 1896 | ++j; |
| 1897 | } |
| 1898 | } while ((i < src->used) && (j < trg->used)); |
| 1899 | |
| 1900 | if ((i < src->used) || count) { |
| 1901 | /* insert all the hashes first */ |
| 1902 | for (k = i; k < src->used; ++k) { |
| 1903 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1904 | } |
| 1905 | |
| 1906 | /* loop ended, but we need to copy something at trg end */ |
| 1907 | count += src->used - i; |
| 1908 | i = src->used; |
| 1909 | goto copy_nodes; |
| 1910 | } |
| 1911 | |
| 1912 | /* we are inserting hashes before the actual node insert, which causes |
| 1913 | * situations when there were initially not enough items for a hash table, |
| 1914 | * but even after some were inserted, hash table was not created (during |
| 1915 | * insertion the number of items is not updated yet) */ |
| 1916 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1917 | set_insert_node_hash(trg, NULL, 0); |
| 1918 | } |
| 1919 | |
| 1920 | #ifndef NDEBUG |
| 1921 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1922 | print_set_debug(trg); |
| 1923 | #endif |
| 1924 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1925 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1926 | return LY_SUCCESS; |
| 1927 | } |
| 1928 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1929 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1930 | 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] | 1931 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1932 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1933 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1934 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1935 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1936 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1937 | } |
| 1938 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1939 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1940 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1941 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]), |
| 1942 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1944 | return LY_ENOT; |
| 1945 | } |
| 1946 | |
| 1947 | return LY_SUCCESS; |
| 1948 | } |
| 1949 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1950 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1951 | 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] | 1952 | { |
| 1953 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1954 | |
| 1955 | /* skip the token */ |
| 1956 | ++(*tok_idx); |
| 1957 | |
| 1958 | return LY_SUCCESS; |
| 1959 | } |
| 1960 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1961 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1962 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1963 | 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] | 1964 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1965 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1966 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1967 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1968 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1969 | } |
| 1970 | return LY_EINCOMPLETE; |
| 1971 | } |
| 1972 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1973 | 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] | 1974 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1975 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1976 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1977 | } |
| 1978 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | return LY_SUCCESS; |
| 1982 | } |
| 1983 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 1984 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1985 | 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] | 1986 | enum lyxp_token want_tok2) |
| 1987 | { |
| 1988 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 1989 | |
| 1990 | /* skip the token */ |
| 1991 | ++(*tok_idx); |
| 1992 | |
| 1993 | return LY_SUCCESS; |
| 1994 | } |
| 1995 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1996 | /** |
| 1997 | * @brief Stack operation push on the repeat array. |
| 1998 | * |
| 1999 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2000 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2001 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 2002 | */ |
| 2003 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2004 | exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, uint32_t repeat_op_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2005 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2006 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2007 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2008 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2009 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2010 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 2011 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 2012 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 2013 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2014 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2015 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 2016 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 2017 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | /** |
| 2022 | * @brief Reparse Predicate. Logs directly on error. |
| 2023 | * |
| 2024 | * [7] Predicate ::= '[' Expr ']' |
| 2025 | * |
| 2026 | * @param[in] ctx Context for logging. |
| 2027 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2028 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2029 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2030 | * @return LY_ERR |
| 2031 | */ |
| 2032 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2033 | 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] | 2034 | { |
| 2035 | LY_ERR rc; |
| 2036 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2037 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2038 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2039 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2040 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2041 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2042 | LY_CHECK_RET(rc); |
| 2043 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2044 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2045 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2046 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2047 | |
| 2048 | return LY_SUCCESS; |
| 2049 | } |
| 2050 | |
| 2051 | /** |
| 2052 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 2053 | * |
| 2054 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 2055 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 2056 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 2057 | * |
| 2058 | * @param[in] ctx Context for logging. |
| 2059 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2060 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2061 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2062 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 2063 | */ |
| 2064 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2065 | 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] | 2066 | { |
| 2067 | LY_ERR rc; |
| 2068 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2069 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2070 | LY_CHECK_RET(rc); |
| 2071 | |
| 2072 | goto step; |
| 2073 | do { |
| 2074 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2075 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2076 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | LY_CHECK_RET(rc); |
| 2079 | step: |
| 2080 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2081 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2082 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2083 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2084 | break; |
| 2085 | |
| 2086 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2087 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2088 | break; |
| 2089 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2090 | case LYXP_TOKEN_AXISNAME: |
| 2091 | ++(*tok_idx); |
| 2092 | |
| 2093 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON); |
| 2094 | LY_CHECK_RET(rc); |
| 2095 | |
| 2096 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2097 | case LYXP_TOKEN_AT: |
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 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2100 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2101 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2102 | 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] | 2103 | 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] | 2104 | return LY_EVALID; |
| 2105 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2106 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) { |
| 2107 | goto reparse_nodetype; |
| 2108 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2109 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2110 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2111 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2112 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2113 | |
| 2114 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2115 | reparse_nodetype: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | |
| 2118 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | |
| 2123 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2124 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2125 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2126 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2127 | |
| 2128 | reparse_predicate: |
| 2129 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2130 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2131 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2132 | LY_CHECK_RET(rc); |
| 2133 | } |
| 2134 | break; |
| 2135 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2136 | 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] | 2137 | return LY_EVALID; |
| 2138 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2139 | } 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] | 2140 | |
| 2141 | return LY_SUCCESS; |
| 2142 | } |
| 2143 | |
| 2144 | /** |
| 2145 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2146 | * |
| 2147 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2148 | * |
| 2149 | * @param[in] ctx Context for logging. |
| 2150 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2151 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2152 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | * @return LY_ERR |
| 2154 | */ |
| 2155 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2156 | 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] | 2157 | { |
| 2158 | LY_ERR rc; |
| 2159 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | 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] | 2161 | |
| 2162 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2164 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2165 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2166 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2167 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2168 | return LY_SUCCESS; |
| 2169 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2170 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2171 | case LYXP_TOKEN_DOT: |
| 2172 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2173 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2174 | case LYXP_TOKEN_AT: |
| 2175 | case LYXP_TOKEN_NAMETEST: |
| 2176 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2177 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2178 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2179 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2180 | default: |
| 2181 | break; |
| 2182 | } |
| 2183 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2184 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2185 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2186 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2187 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2188 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2189 | LY_CHECK_RET(rc); |
| 2190 | } |
| 2191 | |
| 2192 | return LY_SUCCESS; |
| 2193 | } |
| 2194 | |
| 2195 | /** |
| 2196 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2197 | * |
| 2198 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2199 | * |
| 2200 | * @param[in] ctx Context for logging. |
| 2201 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2203 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2204 | * @return LY_ERR |
| 2205 | */ |
| 2206 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2207 | 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] | 2208 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2209 | int8_t min_arg_count = -1; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2210 | uint32_t arg_count, max_arg_count = 0, func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2211 | LY_ERR rc; |
| 2212 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2213 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2214 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2215 | func_tok_idx = *tok_idx; |
| 2216 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2217 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2218 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2219 | min_arg_count = 1; |
| 2220 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2221 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2222 | min_arg_count = 1; |
| 2223 | max_arg_count = 1; |
| 2224 | } |
| 2225 | break; |
| 2226 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2227 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2228 | min_arg_count = 1; |
| 2229 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2230 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2231 | min_arg_count = 0; |
| 2232 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2233 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2234 | min_arg_count = 0; |
| 2235 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2236 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2237 | min_arg_count = 0; |
| 2238 | max_arg_count = 0; |
| 2239 | } |
| 2240 | break; |
| 2241 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2242 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2243 | min_arg_count = 1; |
| 2244 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2245 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2246 | min_arg_count = 0; |
| 2247 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2248 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2249 | min_arg_count = 1; |
| 2250 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2252 | min_arg_count = 1; |
| 2253 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2254 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | min_arg_count = 1; |
| 2256 | max_arg_count = 1; |
| 2257 | } |
| 2258 | break; |
| 2259 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2260 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2261 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2262 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2263 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2264 | min_arg_count = 0; |
| 2265 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2267 | min_arg_count = 0; |
| 2268 | max_arg_count = 1; |
| 2269 | } |
| 2270 | break; |
| 2271 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2272 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2273 | min_arg_count = 1; |
| 2274 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2275 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2276 | min_arg_count = 1; |
| 2277 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2278 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2279 | min_arg_count = 0; |
| 2280 | max_arg_count = 0; |
| 2281 | } |
| 2282 | break; |
| 2283 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2284 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2285 | min_arg_count = 2; |
| 2286 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2287 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2288 | min_arg_count = 0; |
| 2289 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2290 | } 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] | 2291 | min_arg_count = 2; |
| 2292 | max_arg_count = 2; |
| 2293 | } |
| 2294 | break; |
| 2295 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2296 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2297 | min_arg_count = 2; |
| 2298 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2299 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2300 | min_arg_count = 3; |
| 2301 | max_arg_count = 3; |
| 2302 | } |
| 2303 | break; |
| 2304 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2305 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2306 | min_arg_count = 0; |
| 2307 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2308 | } 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] | 2309 | min_arg_count = 1; |
| 2310 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2311 | } 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] | 2312 | min_arg_count = 2; |
| 2313 | max_arg_count = 2; |
| 2314 | } |
| 2315 | break; |
| 2316 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2317 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2318 | min_arg_count = 2; |
| 2319 | max_arg_count = 2; |
| 2320 | } |
| 2321 | break; |
| 2322 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2323 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2324 | min_arg_count = 2; |
| 2325 | max_arg_count = 2; |
| 2326 | } |
| 2327 | break; |
| 2328 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2329 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2330 | min_arg_count = 0; |
| 2331 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2332 | } 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] | 2333 | min_arg_count = 0; |
| 2334 | max_arg_count = 1; |
| 2335 | } |
| 2336 | break; |
| 2337 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2338 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2339 | min_arg_count = 0; |
| 2340 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2341 | } 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] | 2342 | min_arg_count = 2; |
| 2343 | max_arg_count = 2; |
| 2344 | } |
| 2345 | break; |
| 2346 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2347 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2348 | min_arg_count = 2; |
| 2349 | max_arg_count = 2; |
| 2350 | } |
| 2351 | break; |
| 2352 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2353 | 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] | 2354 | min_arg_count = 2; |
| 2355 | max_arg_count = 2; |
| 2356 | } |
| 2357 | break; |
| 2358 | } |
| 2359 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2360 | 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] | 2361 | return LY_EINVAL; |
| 2362 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2363 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2364 | |
| 2365 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2366 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2367 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2368 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2369 | |
| 2370 | /* ( Expr ( ',' Expr )* )? */ |
| 2371 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2372 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2373 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2374 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2375 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2376 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2377 | LY_CHECK_RET(rc); |
| 2378 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2379 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2380 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2381 | |
| 2382 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2383 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2384 | LY_CHECK_RET(rc); |
| 2385 | } |
| 2386 | |
| 2387 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2388 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2389 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2390 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2391 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2392 | 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] | 2393 | 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] | 2394 | return LY_EVALID; |
| 2395 | } |
| 2396 | |
| 2397 | return LY_SUCCESS; |
| 2398 | } |
| 2399 | |
| 2400 | /** |
| 2401 | * @brief Reparse PathExpr. Logs directly on error. |
| 2402 | * |
| 2403 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2404 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2405 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2406 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2407 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2408 | * |
| 2409 | * @param[in] ctx Context for logging. |
| 2410 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2411 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2412 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2413 | * @return LY_ERR |
| 2414 | */ |
| 2415 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2416 | 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] | 2417 | { |
| 2418 | LY_ERR rc; |
| 2419 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2420 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2421 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2422 | } |
| 2423 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2424 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2425 | case LYXP_TOKEN_PAR1: |
| 2426 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2427 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2428 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2429 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2430 | LY_CHECK_RET(rc); |
| 2431 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2432 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2433 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2434 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2435 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2436 | case LYXP_TOKEN_DOT: |
| 2437 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2438 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2439 | case LYXP_TOKEN_AT: |
| 2440 | case LYXP_TOKEN_NAMETEST: |
| 2441 | case LYXP_TOKEN_NODETYPE: |
| 2442 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2443 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2444 | LY_CHECK_RET(rc); |
| 2445 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2446 | case LYXP_TOKEN_VARREF: |
| 2447 | /* VariableReference */ |
| 2448 | ++(*tok_idx); |
| 2449 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2450 | case LYXP_TOKEN_FUNCNAME: |
| 2451 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2452 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | LY_CHECK_RET(rc); |
| 2454 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2455 | case LYXP_TOKEN_OPER_PATH: |
| 2456 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2457 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2458 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2459 | LY_CHECK_RET(rc); |
| 2460 | break; |
| 2461 | case LYXP_TOKEN_LITERAL: |
| 2462 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2463 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2465 | case LYXP_TOKEN_NUMBER: |
| 2466 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2467 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2468 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2469 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2470 | 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] | 2471 | return LY_EVALID; |
| 2472 | } |
| 2473 | |
| 2474 | return LY_SUCCESS; |
| 2475 | |
| 2476 | predicate: |
| 2477 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2478 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2479 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2480 | LY_CHECK_RET(rc); |
| 2481 | } |
| 2482 | |
| 2483 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2484 | 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] | 2485 | |
| 2486 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2487 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2488 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2489 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2490 | LY_CHECK_RET(rc); |
| 2491 | } |
| 2492 | |
| 2493 | return LY_SUCCESS; |
| 2494 | } |
| 2495 | |
| 2496 | /** |
| 2497 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2498 | * |
| 2499 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2500 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2501 | * |
| 2502 | * @param[in] ctx Context for logging. |
| 2503 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2504 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2505 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | * @return LY_ERR |
| 2507 | */ |
| 2508 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2509 | 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] | 2510 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2511 | uint32_t prev_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2512 | LY_ERR rc; |
| 2513 | |
| 2514 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2516 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2517 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2518 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2519 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2523 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2524 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2525 | LY_CHECK_RET(rc); |
| 2526 | |
| 2527 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2528 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2529 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2530 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2531 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2532 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2533 | LY_CHECK_RET(rc); |
| 2534 | } |
| 2535 | |
| 2536 | return LY_SUCCESS; |
| 2537 | } |
| 2538 | |
| 2539 | /** |
| 2540 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2541 | * |
| 2542 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2543 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2544 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2545 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2546 | * | MultiplicativeExpr '*' UnaryExpr |
| 2547 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2548 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2549 | * |
| 2550 | * @param[in] ctx Context for logging. |
| 2551 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2552 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2553 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2554 | * @return LY_ERR |
| 2555 | */ |
| 2556 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2557 | 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] | 2558 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2559 | uint32_t prev_add_exp, prev_mul_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2560 | LY_ERR rc; |
| 2561 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2562 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2563 | goto reparse_multiplicative_expr; |
| 2564 | |
| 2565 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2566 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2567 | ((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] | 2568 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2569 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2570 | |
| 2571 | reparse_multiplicative_expr: |
| 2572 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2573 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2574 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2575 | LY_CHECK_RET(rc); |
| 2576 | |
| 2577 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2578 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2579 | ((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] | 2580 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2581 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2582 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2583 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2584 | LY_CHECK_RET(rc); |
| 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | return LY_SUCCESS; |
| 2589 | } |
| 2590 | |
| 2591 | /** |
| 2592 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2593 | * |
| 2594 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2595 | * | EqualityExpr '!=' RelationalExpr |
| 2596 | * [14] RelationalExpr ::= AdditiveExpr |
| 2597 | * | RelationalExpr '<' AdditiveExpr |
| 2598 | * | RelationalExpr '>' AdditiveExpr |
| 2599 | * | RelationalExpr '<=' AdditiveExpr |
| 2600 | * | RelationalExpr '>=' AdditiveExpr |
| 2601 | * |
| 2602 | * @param[in] ctx Context for logging. |
| 2603 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2604 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2605 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2606 | * @return LY_ERR |
| 2607 | */ |
| 2608 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2609 | 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] | 2610 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2611 | uint32_t prev_eq_exp, prev_rel_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2612 | LY_ERR rc; |
| 2613 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2614 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2615 | goto reparse_additive_expr; |
| 2616 | |
| 2617 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2618 | 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] | 2619 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2620 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2621 | |
| 2622 | reparse_additive_expr: |
| 2623 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2624 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2625 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2626 | LY_CHECK_RET(rc); |
| 2627 | |
| 2628 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2629 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2630 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2631 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2632 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2633 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2634 | LY_CHECK_RET(rc); |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | return LY_SUCCESS; |
| 2639 | } |
| 2640 | |
| 2641 | /** |
| 2642 | * @brief Reparse OrExpr. Logs directly on error. |
| 2643 | * |
| 2644 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2645 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2646 | * |
| 2647 | * @param[in] ctx Context for logging. |
| 2648 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2649 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2650 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2651 | * @return LY_ERR |
| 2652 | */ |
| 2653 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2654 | 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] | 2655 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2656 | uint32_t prev_or_exp, prev_and_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2657 | LY_ERR rc; |
| 2658 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2659 | ++depth; |
| 2660 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2661 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2662 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2663 | goto reparse_equality_expr; |
| 2664 | |
| 2665 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2666 | 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] | 2667 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2668 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2669 | |
| 2670 | reparse_equality_expr: |
| 2671 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2672 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2673 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2674 | LY_CHECK_RET(rc); |
| 2675 | |
| 2676 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2677 | 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] | 2678 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2679 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2680 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2681 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2682 | LY_CHECK_RET(rc); |
| 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | return LY_SUCCESS; |
| 2687 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2688 | |
| 2689 | /** |
| 2690 | * @brief Parse NCName. |
| 2691 | * |
| 2692 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2693 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2694 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2695 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2696 | parse_ncname(const char *ncname) |
| 2697 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2698 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2699 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2700 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2701 | |
| 2702 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2703 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2704 | return len; |
| 2705 | } |
| 2706 | |
| 2707 | do { |
| 2708 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2709 | if (!*ncname) { |
| 2710 | break; |
| 2711 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2712 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2713 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2714 | |
| 2715 | return len; |
| 2716 | } |
| 2717 | |
| 2718 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2719 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2720 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2721 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2722 | * @param[in] exp Expression to use. |
| 2723 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2724 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2725 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2726 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2727 | */ |
| 2728 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2729 | 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] | 2730 | { |
| 2731 | uint32_t prev; |
| 2732 | |
| 2733 | if (exp->used == exp->size) { |
| 2734 | prev = exp->size; |
| 2735 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2736 | if (prev > exp->size) { |
| 2737 | LOGINT(ctx); |
| 2738 | return LY_EINT; |
| 2739 | } |
| 2740 | |
| 2741 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2742 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2743 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2744 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2745 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2746 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2747 | } |
| 2748 | |
| 2749 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2750 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2751 | exp->tok_len[exp->used] = tok_len; |
| 2752 | ++exp->used; |
| 2753 | return LY_SUCCESS; |
| 2754 | } |
| 2755 | |
| 2756 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2757 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2758 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2759 | uint32_t i; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2760 | |
| 2761 | if (!expr) { |
| 2762 | return; |
| 2763 | } |
| 2764 | |
| 2765 | lydict_remove(ctx, expr->expr); |
| 2766 | free(expr->tokens); |
| 2767 | free(expr->tok_pos); |
| 2768 | free(expr->tok_len); |
| 2769 | if (expr->repeat) { |
| 2770 | for (i = 0; i < expr->used; ++i) { |
| 2771 | free(expr->repeat[i]); |
| 2772 | } |
| 2773 | } |
| 2774 | free(expr->repeat); |
| 2775 | free(expr); |
| 2776 | } |
| 2777 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2778 | /** |
| 2779 | * @brief Parse Axis name. |
| 2780 | * |
| 2781 | * @param[in] str String to parse. |
| 2782 | * @param[in] str_len Length of @p str. |
| 2783 | * @return LY_SUCCESS if an axis. |
| 2784 | * @return LY_ENOT otherwise. |
| 2785 | */ |
| 2786 | static LY_ERR |
| 2787 | expr_parse_axis(const char *str, size_t str_len) |
| 2788 | { |
| 2789 | switch (str_len) { |
| 2790 | case 4: |
| 2791 | if (!strncmp("self", str, str_len)) { |
| 2792 | return LY_SUCCESS; |
| 2793 | } |
| 2794 | break; |
| 2795 | case 5: |
| 2796 | if (!strncmp("child", str, str_len)) { |
| 2797 | return LY_SUCCESS; |
| 2798 | } |
| 2799 | break; |
| 2800 | case 6: |
| 2801 | if (!strncmp("parent", str, str_len)) { |
| 2802 | return LY_SUCCESS; |
| 2803 | } |
| 2804 | break; |
| 2805 | case 8: |
| 2806 | if (!strncmp("ancestor", str, str_len)) { |
| 2807 | return LY_SUCCESS; |
| 2808 | } |
| 2809 | break; |
| 2810 | case 9: |
| 2811 | if (!strncmp("attribute", str, str_len)) { |
| 2812 | return LY_SUCCESS; |
| 2813 | } else if (!strncmp("following", str, str_len)) { |
| 2814 | return LY_SUCCESS; |
| 2815 | } else if (!strncmp("namespace", str, str_len)) { |
| 2816 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2817 | return LY_ENOT; |
| 2818 | } else if (!strncmp("preceding", str, str_len)) { |
| 2819 | return LY_SUCCESS; |
| 2820 | } |
| 2821 | break; |
| 2822 | case 10: |
| 2823 | if (!strncmp("descendant", str, str_len)) { |
| 2824 | return LY_SUCCESS; |
| 2825 | } |
| 2826 | break; |
| 2827 | case 16: |
| 2828 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2829 | return LY_SUCCESS; |
| 2830 | } |
| 2831 | break; |
| 2832 | case 17: |
| 2833 | if (!strncmp("following-sibling", str, str_len)) { |
| 2834 | return LY_SUCCESS; |
| 2835 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2836 | return LY_SUCCESS; |
| 2837 | } |
| 2838 | break; |
| 2839 | case 18: |
| 2840 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2841 | return LY_SUCCESS; |
| 2842 | } |
| 2843 | break; |
| 2844 | } |
| 2845 | |
| 2846 | return LY_ENOT; |
| 2847 | } |
| 2848 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2849 | LY_ERR |
| 2850 | 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] | 2851 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2852 | LY_ERR ret = LY_SUCCESS; |
| 2853 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2854 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2855 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2856 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2857 | uint32_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2858 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2859 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2860 | assert(expr_p); |
| 2861 | |
| 2862 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2863 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2864 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2868 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2869 | } |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2870 | if (expr_len > UINT32_MAX) { |
| 2871 | 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] | 2872 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2876 | expr = calloc(1, sizeof *expr); |
| 2877 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2878 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2879 | expr->used = 0; |
| 2880 | expr->size = LYXP_EXPR_SIZE_START; |
| 2881 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2882 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2883 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2884 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2885 | 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] | 2886 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2887 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2888 | 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] | 2889 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2890 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2891 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2892 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2893 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2894 | ++parsed; |
| 2895 | } |
| 2896 | |
| 2897 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2898 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2899 | |
| 2900 | /* '(' */ |
| 2901 | tok_len = 1; |
| 2902 | tok_type = LYXP_TOKEN_PAR1; |
| 2903 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2904 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2905 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2906 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2907 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2908 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2909 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2910 | /* it is NodeType after all */ |
| 2911 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2912 | |
| 2913 | prev_ntype_check = 0; |
| 2914 | prev_func_check = 0; |
| 2915 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2916 | /* it is FunctionName after all */ |
| 2917 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2918 | |
| 2919 | prev_ntype_check = 0; |
| 2920 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2921 | } |
| 2922 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2923 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2924 | |
| 2925 | /* ')' */ |
| 2926 | tok_len = 1; |
| 2927 | tok_type = LYXP_TOKEN_PAR2; |
| 2928 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2929 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2930 | |
| 2931 | /* '[' */ |
| 2932 | tok_len = 1; |
| 2933 | tok_type = LYXP_TOKEN_BRACK1; |
| 2934 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2935 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2936 | |
| 2937 | /* ']' */ |
| 2938 | tok_len = 1; |
| 2939 | tok_type = LYXP_TOKEN_BRACK2; |
| 2940 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2941 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2942 | |
| 2943 | /* '..' */ |
| 2944 | tok_len = 2; |
| 2945 | tok_type = LYXP_TOKEN_DDOT; |
| 2946 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2947 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2948 | |
| 2949 | /* '.' */ |
| 2950 | tok_len = 1; |
| 2951 | tok_type = LYXP_TOKEN_DOT; |
| 2952 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2953 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2954 | |
| 2955 | /* '@' */ |
| 2956 | tok_len = 1; |
| 2957 | tok_type = LYXP_TOKEN_AT; |
| 2958 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2959 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2960 | |
| 2961 | /* ',' */ |
| 2962 | tok_len = 1; |
| 2963 | tok_type = LYXP_TOKEN_COMMA; |
| 2964 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2965 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2966 | |
| 2967 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2968 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2969 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2970 | 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] | 2971 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2972 | ++tok_len; |
| 2973 | tok_type = LYXP_TOKEN_LITERAL; |
| 2974 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2975 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2976 | |
| 2977 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2978 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2979 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2980 | 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] | 2981 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2982 | ++tok_len; |
| 2983 | tok_type = LYXP_TOKEN_LITERAL; |
| 2984 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2985 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2986 | |
| 2987 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2988 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2989 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2990 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2991 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2992 | } |
| 2993 | tok_type = LYXP_TOKEN_NUMBER; |
| 2994 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2995 | } else if (expr_str[parsed] == '$') { |
| 2996 | |
| 2997 | /* VariableReference */ |
| 2998 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2999 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3000 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3001 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3002 | tok_len = ncname_len; |
| 3003 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 3004 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 3005 | error); |
| 3006 | tok_type = LYXP_TOKEN_VARREF; |
| 3007 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3008 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3009 | |
| 3010 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3011 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3012 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3013 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3014 | } else { |
| 3015 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3016 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3017 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3018 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3019 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3020 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3021 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3022 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3023 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 3024 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3025 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3026 | |
| 3027 | /* Operator '<=', '>=' */ |
| 3028 | tok_len = 2; |
| 3029 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3030 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3031 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3032 | |
| 3033 | /* Operator '|' */ |
| 3034 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3035 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3036 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3037 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3038 | |
| 3039 | /* Operator '+', '-' */ |
| 3040 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3041 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3042 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3043 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3044 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3045 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3046 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3047 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3048 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3049 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3050 | |
| 3051 | /* Operator '<', '>' */ |
| 3052 | tok_len = 1; |
| 3053 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3054 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3055 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3056 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3057 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3058 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3059 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3060 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3061 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3062 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3063 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3064 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3065 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3066 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3067 | |
| 3068 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3069 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3070 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3071 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3072 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3073 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3074 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3075 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3076 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3077 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3078 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3079 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3080 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3081 | } 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] | 3082 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3083 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3084 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3085 | } else if (prev_ntype_check || prev_func_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3086 | 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] | 3087 | 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] | 3088 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3089 | goto error; |
| 3090 | } else { |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3091 | 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] | 3092 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3093 | goto error; |
| 3094 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3095 | } else { |
| 3096 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3097 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3098 | if (expr_str[parsed] == '*') { |
| 3099 | ncname_len = 1; |
| 3100 | } else { |
| 3101 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3102 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3103 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3104 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3105 | tok_len = ncname_len; |
| 3106 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3107 | has_axis = 0; |
| 3108 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3109 | /* axis */ |
| 3110 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
| 3111 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error); |
| 3112 | tok_type = LYXP_TOKEN_AXISNAME; |
| 3113 | |
| 3114 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3115 | parsed += tok_len; |
| 3116 | |
| 3117 | /* '::' */ |
| 3118 | tok_len = 2; |
| 3119 | tok_type = LYXP_TOKEN_DCOLON; |
| 3120 | |
| 3121 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3122 | parsed += tok_len; |
| 3123 | |
| 3124 | if (expr_str[parsed] == '*') { |
| 3125 | ncname_len = 1; |
| 3126 | } else { |
| 3127 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3128 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3129 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3130 | } |
| 3131 | tok_len = ncname_len; |
| 3132 | |
| 3133 | has_axis = 1; |
| 3134 | } |
| 3135 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3136 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3137 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3138 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3139 | ++tok_len; |
| 3140 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3141 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 3142 | 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] | 3143 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3144 | tok_len += ncname_len; |
| 3145 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3146 | /* remove old flags to prevent ambiguities */ |
| 3147 | prev_ntype_check = 0; |
| 3148 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3149 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3150 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3151 | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 3152 | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
| 3153 | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3154 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3159 | 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] | 3160 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3161 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3162 | ++parsed; |
| 3163 | } |
| 3164 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3165 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3166 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3167 | if (reparse) { |
| 3168 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3169 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 3170 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3171 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3172 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 3173 | 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] | 3174 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3175 | 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] | 3176 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3177 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3178 | goto error; |
| 3179 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3180 | } |
| 3181 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3182 | print_expr_struct_debug(expr); |
| 3183 | *expr_p = expr; |
| 3184 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3185 | |
| 3186 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3187 | lyxp_expr_free(ctx, expr); |
| 3188 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3189 | } |
| 3190 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3191 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3192 | 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] | 3193 | struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3194 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3195 | LY_ERR ret = LY_SUCCESS; |
| 3196 | struct lyxp_expr *dup = NULL; |
Michal Vasko | d59039d | 2022-09-09 09:07:30 +0200 | [diff] [blame] | 3197 | uint32_t used = 0, i, j, expr_len; |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3198 | const char *expr_start; |
| 3199 | |
| 3200 | 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] | 3201 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3202 | if (!exp) { |
| 3203 | goto cleanup; |
| 3204 | } |
| 3205 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3206 | if (!start_idx && !end_idx) { |
| 3207 | end_idx = exp->used - 1; |
| 3208 | } |
| 3209 | |
| 3210 | expr_start = exp->expr + exp->tok_pos[start_idx]; |
| 3211 | expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx]; |
| 3212 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3213 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3214 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3215 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3216 | if (exp->used) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3217 | used = (end_idx - start_idx) + 1; |
| 3218 | |
| 3219 | dup->tokens = malloc(used * sizeof *dup->tokens); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3220 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3221 | memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3222 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3223 | dup->tok_pos = malloc(used * sizeof *dup->tok_pos); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3224 | 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] | 3225 | 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] | 3226 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3227 | if (start_idx) { |
| 3228 | /* fix the indices in the expression */ |
| 3229 | for (i = 0; i < used; ++i) { |
| 3230 | dup->tok_pos[i] -= expr_start - exp->expr; |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | dup->tok_len = malloc(used * sizeof *dup->tok_len); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3235 | 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] | 3236 | 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] | 3237 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3238 | if (exp->repeat) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3239 | dup->repeat = malloc(used * sizeof *dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3240 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3241 | for (i = start_idx; i <= end_idx; ++i) { |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3242 | if (!exp->repeat[i]) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3243 | dup->repeat[i - start_idx] = NULL; |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3244 | } else { |
| 3245 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3246 | /* the ending 0 as well */ |
| 3247 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3248 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3249 | dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat); |
| 3250 | LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3251 | 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] | 3252 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3253 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3254 | } |
| 3255 | } |
| 3256 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3257 | dup->used = used; |
| 3258 | dup->size = used; |
| 3259 | |
| 3260 | /* copy only subexpression */ |
| 3261 | 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] | 3262 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3263 | cleanup: |
| 3264 | if (ret) { |
| 3265 | lyxp_expr_free(ctx, dup); |
| 3266 | } else { |
| 3267 | *dup_p = dup; |
| 3268 | } |
| 3269 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3270 | } |
| 3271 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3272 | /** |
| 3273 | * @brief Get the last-added schema node that is currently in the context. |
| 3274 | * |
| 3275 | * @param[in] set Set to search in. |
| 3276 | * @return Last-added schema context node, NULL if no node is in context. |
| 3277 | */ |
| 3278 | static struct lysc_node * |
| 3279 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3280 | { |
| 3281 | uint32_t i; |
| 3282 | |
| 3283 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3284 | return NULL; |
| 3285 | } |
| 3286 | |
| 3287 | i = set->used; |
| 3288 | do { |
| 3289 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3290 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3291 | /* if there are more, simply return the first found (last added) */ |
| 3292 | return set->val.scnodes[i].scnode; |
| 3293 | } |
| 3294 | } while (i); |
| 3295 | |
| 3296 | return NULL; |
| 3297 | } |
| 3298 | |
| 3299 | /** |
| 3300 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3301 | * |
| 3302 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3303 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3304 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3305 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3306 | warn_is_numeric_type(struct lysc_type *type) |
| 3307 | { |
| 3308 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3309 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3310 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3311 | |
| 3312 | switch (type->basetype) { |
| 3313 | case LY_TYPE_DEC64: |
| 3314 | case LY_TYPE_INT8: |
| 3315 | case LY_TYPE_UINT8: |
| 3316 | case LY_TYPE_INT16: |
| 3317 | case LY_TYPE_UINT16: |
| 3318 | case LY_TYPE_INT32: |
| 3319 | case LY_TYPE_UINT32: |
| 3320 | case LY_TYPE_INT64: |
| 3321 | case LY_TYPE_UINT64: |
| 3322 | return 1; |
| 3323 | case LY_TYPE_UNION: |
| 3324 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3325 | LY_ARRAY_FOR(uni->types, u) { |
| 3326 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3327 | if (ret) { |
| 3328 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3329 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3330 | } |
| 3331 | } |
| 3332 | /* did not find any suitable type */ |
| 3333 | return 0; |
| 3334 | case LY_TYPE_LEAFREF: |
| 3335 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3336 | default: |
| 3337 | return 0; |
| 3338 | } |
| 3339 | } |
| 3340 | |
| 3341 | /** |
| 3342 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3343 | * |
| 3344 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3345 | * @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] | 3346 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3347 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3348 | warn_is_string_type(struct lysc_type *type) |
| 3349 | { |
| 3350 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3351 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3352 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3353 | |
| 3354 | switch (type->basetype) { |
| 3355 | case LY_TYPE_BITS: |
| 3356 | case LY_TYPE_ENUM: |
| 3357 | case LY_TYPE_IDENT: |
| 3358 | case LY_TYPE_INST: |
| 3359 | case LY_TYPE_STRING: |
| 3360 | return 1; |
| 3361 | case LY_TYPE_UNION: |
| 3362 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3363 | LY_ARRAY_FOR(uni->types, u) { |
| 3364 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3365 | if (ret) { |
| 3366 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3367 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3368 | } |
| 3369 | } |
| 3370 | /* did not find any suitable type */ |
| 3371 | return 0; |
| 3372 | case LY_TYPE_LEAFREF: |
| 3373 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3374 | default: |
| 3375 | return 0; |
| 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | /** |
| 3380 | * @brief Test whether a type is one specific type. |
| 3381 | * |
| 3382 | * @param[in] type Type to test. |
| 3383 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3384 | * @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] | 3385 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3386 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3387 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3388 | { |
| 3389 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3390 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3391 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3392 | |
| 3393 | if (type->basetype == base) { |
| 3394 | return 1; |
| 3395 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3396 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3397 | LY_ARRAY_FOR(uni->types, u) { |
| 3398 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3399 | if (ret) { |
| 3400 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3401 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3402 | } |
| 3403 | } |
| 3404 | /* did not find any suitable type */ |
| 3405 | return 0; |
| 3406 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3407 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3408 | } |
| 3409 | |
| 3410 | return 0; |
| 3411 | } |
| 3412 | |
| 3413 | /** |
| 3414 | * @brief Get next type of a (union) type. |
| 3415 | * |
| 3416 | * @param[in] type Base type. |
| 3417 | * @param[in] prev_type Previously returned type. |
| 3418 | * @return Next type or NULL. |
| 3419 | */ |
| 3420 | static struct lysc_type * |
| 3421 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3422 | { |
| 3423 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3424 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3425 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3426 | |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3427 | if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3428 | uni = (struct lysc_type_union *)type; |
| 3429 | if (!prev_type) { |
| 3430 | return uni->types[0]; |
| 3431 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3432 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3433 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3434 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3435 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3436 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3437 | found = 1; |
| 3438 | } |
| 3439 | } |
| 3440 | return NULL; |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3441 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3442 | if (prev_type) { |
| 3443 | assert(type == prev_type); |
| 3444 | return NULL; |
| 3445 | } else { |
| 3446 | return type; |
| 3447 | } |
| 3448 | } |
| 3449 | } |
| 3450 | |
| 3451 | /** |
| 3452 | * @brief Test whether 2 types have a common type. |
| 3453 | * |
| 3454 | * @param[in] type1 First type. |
| 3455 | * @param[in] type2 Second type. |
| 3456 | * @return 1 if they do, 0 otherwise. |
| 3457 | */ |
| 3458 | static int |
| 3459 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3460 | { |
| 3461 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3462 | |
| 3463 | t1 = NULL; |
| 3464 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3465 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3466 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3467 | } else { |
| 3468 | rt1 = t1; |
| 3469 | } |
| 3470 | |
| 3471 | t2 = NULL; |
| 3472 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3473 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3474 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3475 | } else { |
| 3476 | rt2 = t2; |
| 3477 | } |
| 3478 | |
| 3479 | if (rt2->basetype == rt1->basetype) { |
| 3480 | /* match found */ |
| 3481 | return 1; |
| 3482 | } |
| 3483 | } |
| 3484 | } |
| 3485 | |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
| 3489 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3490 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3491 | * |
| 3492 | * @param[in] ctx Context for logging. |
| 3493 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3494 | * @param[in] subexpr Subexpression start. |
| 3495 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3496 | * @param[in] cur_scnode Expression context node. |
| 3497 | */ |
| 3498 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3499 | 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] | 3500 | const struct lysc_node *cur_scnode) |
| 3501 | { |
| 3502 | char *path; |
| 3503 | |
| 3504 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3505 | 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] | 3506 | tok_pos, subexpr_len, subexpr, path); |
| 3507 | free(path); |
| 3508 | } |
| 3509 | |
| 3510 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3511 | * @brief Check both operands of comparison operators. |
| 3512 | * |
| 3513 | * @param[in] ctx Context for errors. |
| 3514 | * @param[in] set1 First operand set. |
| 3515 | * @param[in] set2 Second operand set. |
| 3516 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3517 | * @param[in] expr Start of the expression to print with the warning. |
| 3518 | * @param[in] tok_pos Token position. |
| 3519 | */ |
| 3520 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3521 | 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] | 3522 | uint32_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3523 | { |
| 3524 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3525 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3526 | |
| 3527 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3528 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3529 | |
| 3530 | if (!node1 && !node2) { |
| 3531 | /* no node-sets involved, nothing to do */ |
| 3532 | return; |
| 3533 | } |
| 3534 | |
| 3535 | if (node1) { |
| 3536 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3537 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3538 | warning = 1; |
| 3539 | leaves = 0; |
| 3540 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3541 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3542 | warning = 1; |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | if (node2) { |
| 3547 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3548 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3549 | warning = 1; |
| 3550 | leaves = 0; |
| 3551 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3552 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3553 | warning = 1; |
| 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3558 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3559 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3560 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3561 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3562 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3563 | warning = 1; |
| 3564 | } |
| 3565 | } |
| 3566 | |
| 3567 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3568 | 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] | 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | /** |
| 3573 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3574 | * |
| 3575 | * @param[in] exp Parsed XPath expression. |
| 3576 | * @param[in] set Set with the leaf/leaf-list. |
| 3577 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3578 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3579 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3580 | */ |
| 3581 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3582 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp, |
| 3583 | uint32_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3584 | { |
| 3585 | struct lysc_node *scnode; |
| 3586 | struct lysc_type *type; |
| 3587 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3588 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3589 | LY_ERR rc; |
| 3590 | struct ly_err_item *err = NULL; |
| 3591 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3592 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3593 | ((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] | 3594 | /* check that the node can have the specified value */ |
| 3595 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3596 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3597 | } else { |
| 3598 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3599 | } |
| 3600 | if (!value) { |
| 3601 | LOGMEM(set->ctx); |
| 3602 | return; |
| 3603 | } |
| 3604 | |
| 3605 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3606 | 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] | 3607 | " 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] | 3608 | 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] | 3609 | (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] | 3610 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3614 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3615 | 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] | 3616 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3617 | if (rc == LY_EINCOMPLETE) { |
| 3618 | rc = LY_SUCCESS; |
| 3619 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3620 | |
| 3621 | if (err) { |
| 3622 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3623 | ly_err_free(err); |
| 3624 | } else if (rc != LY_SUCCESS) { |
| 3625 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3626 | } |
| 3627 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3628 | 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] | 3629 | (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] | 3630 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3631 | } else { |
| 3632 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3633 | } |
| 3634 | } |
| 3635 | free(value); |
| 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | /* |
| 3640 | * XPath functions |
| 3641 | */ |
| 3642 | |
| 3643 | /** |
| 3644 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3645 | * depending on whether the first node bit value from the second argument is set. |
| 3646 | * |
| 3647 | * @param[in] args Array of arguments. |
| 3648 | * @param[in] arg_count Count of elements in @p args. |
| 3649 | * @param[in,out] set Context and result set at the same time. |
| 3650 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3651 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3652 | */ |
| 3653 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3654 | 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] | 3655 | { |
| 3656 | struct lyd_node_term *leaf; |
| 3657 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3658 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3659 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3660 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3661 | |
| 3662 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3663 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3664 | 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] | 3665 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3666 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3667 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3668 | sleaf->name); |
| 3669 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3670 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3671 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3675 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3676 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3677 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3678 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3679 | 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] | 3680 | } |
| 3681 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3682 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3683 | return rc; |
| 3684 | } |
| 3685 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3686 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3687 | 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] | 3688 | return LY_EVALID; |
| 3689 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3690 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3691 | LY_CHECK_RET(rc); |
| 3692 | |
| 3693 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3694 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3695 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3696 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3697 | LYD_VALUE_GET(&leaf->value, bits); |
| 3698 | LY_ARRAY_FOR(bits->items, u) { |
| 3699 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3700 | set_fill_boolean(set, 1); |
| 3701 | break; |
| 3702 | } |
| 3703 | } |
| 3704 | } |
| 3705 | } |
| 3706 | |
| 3707 | return LY_SUCCESS; |
| 3708 | } |
| 3709 | |
| 3710 | /** |
| 3711 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3712 | * with the argument converted to boolean. |
| 3713 | * |
| 3714 | * @param[in] args Array of arguments. |
| 3715 | * @param[in] arg_count Count of elements in @p args. |
| 3716 | * @param[in,out] set Context and result set at the same time. |
| 3717 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3718 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3719 | */ |
| 3720 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3721 | 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] | 3722 | { |
| 3723 | LY_ERR rc; |
| 3724 | |
| 3725 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3726 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3727 | return LY_SUCCESS; |
| 3728 | } |
| 3729 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3730 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3731 | LY_CHECK_RET(rc); |
| 3732 | set_fill_set(set, args[0]); |
| 3733 | |
| 3734 | return LY_SUCCESS; |
| 3735 | } |
| 3736 | |
| 3737 | /** |
| 3738 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3739 | * with the first argument rounded up to the nearest integer. |
| 3740 | * |
| 3741 | * @param[in] args Array of arguments. |
| 3742 | * @param[in] arg_count Count of elements in @p args. |
| 3743 | * @param[in,out] set Context and result set at the same time. |
| 3744 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3745 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3746 | */ |
| 3747 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3748 | 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] | 3749 | { |
| 3750 | struct lysc_node_leaf *sleaf; |
| 3751 | LY_ERR rc = LY_SUCCESS; |
| 3752 | |
| 3753 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3754 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3755 | 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] | 3756 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3757 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3758 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3759 | sleaf->name); |
| 3760 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3761 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3762 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3763 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3764 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3765 | return rc; |
| 3766 | } |
| 3767 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3768 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3769 | LY_CHECK_RET(rc); |
| 3770 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3771 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3772 | } else { |
| 3773 | set_fill_number(set, args[0]->val.num); |
| 3774 | } |
| 3775 | |
| 3776 | return LY_SUCCESS; |
| 3777 | } |
| 3778 | |
| 3779 | /** |
| 3780 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3781 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3782 | * |
| 3783 | * @param[in] args Array of arguments. |
| 3784 | * @param[in] arg_count Count of elements in @p args. |
| 3785 | * @param[in,out] set Context and result set at the same time. |
| 3786 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3787 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3788 | */ |
| 3789 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3790 | 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] | 3791 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3792 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3793 | char *str = NULL; |
| 3794 | size_t used = 1; |
| 3795 | LY_ERR rc = LY_SUCCESS; |
| 3796 | struct lysc_node_leaf *sleaf; |
| 3797 | |
| 3798 | if (options & LYXP_SCNODE_ALL) { |
| 3799 | for (i = 0; i < arg_count; ++i) { |
| 3800 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3801 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3802 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3803 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3804 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3805 | 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] | 3806 | } |
| 3807 | } |
| 3808 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3809 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3810 | return rc; |
| 3811 | } |
| 3812 | |
| 3813 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3814 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3815 | if (rc != LY_SUCCESS) { |
| 3816 | free(str); |
| 3817 | return rc; |
| 3818 | } |
| 3819 | |
| 3820 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3821 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3822 | strcpy(str + used - 1, args[i]->val.str); |
| 3823 | used += strlen(args[i]->val.str); |
| 3824 | } |
| 3825 | |
| 3826 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3827 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3828 | set->type = LYXP_SET_STRING; |
| 3829 | set->val.str = str; |
| 3830 | |
| 3831 | return LY_SUCCESS; |
| 3832 | } |
| 3833 | |
| 3834 | /** |
| 3835 | * @brief Execute the XPath contains(string, string) function. |
| 3836 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3837 | * be found in the first or not. |
| 3838 | * |
| 3839 | * @param[in] args Array of arguments. |
| 3840 | * @param[in] arg_count Count of elements in @p args. |
| 3841 | * @param[in,out] set Context and result set at the same time. |
| 3842 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3843 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3844 | */ |
| 3845 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3846 | 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] | 3847 | { |
| 3848 | struct lysc_node_leaf *sleaf; |
| 3849 | LY_ERR rc = LY_SUCCESS; |
| 3850 | |
| 3851 | if (options & LYXP_SCNODE_ALL) { |
| 3852 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3853 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3854 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3855 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3856 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3857 | 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] | 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3862 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3863 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3864 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3865 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3866 | 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] | 3867 | } |
| 3868 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3869 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3870 | return rc; |
| 3871 | } |
| 3872 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3873 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3874 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3875 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3876 | LY_CHECK_RET(rc); |
| 3877 | |
| 3878 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3879 | set_fill_boolean(set, 1); |
| 3880 | } else { |
| 3881 | set_fill_boolean(set, 0); |
| 3882 | } |
| 3883 | |
| 3884 | return LY_SUCCESS; |
| 3885 | } |
| 3886 | |
| 3887 | /** |
| 3888 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3889 | * with the size of the node-set from the argument. |
| 3890 | * |
| 3891 | * @param[in] args Array of arguments. |
| 3892 | * @param[in] arg_count Count of elements in @p args. |
| 3893 | * @param[in,out] set Context and result set at the same time. |
| 3894 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3895 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3896 | */ |
| 3897 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3898 | 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] | 3899 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3900 | LY_ERR rc = LY_SUCCESS; |
| 3901 | |
| 3902 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3903 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3904 | 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] | 3905 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3906 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3907 | return rc; |
| 3908 | } |
| 3909 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3910 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3911 | 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] | 3912 | return LY_EVALID; |
| 3913 | } |
| 3914 | |
| 3915 | set_fill_number(set, args[0]->used); |
| 3916 | return LY_SUCCESS; |
| 3917 | } |
| 3918 | |
| 3919 | /** |
| 3920 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3921 | * with the context with the intial node. |
| 3922 | * |
| 3923 | * @param[in] args Array of arguments. |
| 3924 | * @param[in] arg_count Count of elements in @p args. |
| 3925 | * @param[in,out] set Context and result set at the same time. |
| 3926 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3927 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3928 | */ |
| 3929 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3930 | 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] | 3931 | { |
| 3932 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3933 | 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] | 3934 | return LY_EVALID; |
| 3935 | } |
| 3936 | |
| 3937 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3938 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3939 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3940 | if (set->cur_scnode) { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 3941 | LY_CHECK_RET(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] | 3942 | } else { |
| 3943 | /* root node */ |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 3944 | LY_CHECK_RET(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] | 3945 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3946 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3947 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3948 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3949 | if (set->cur_node) { |
| 3950 | /* position is filled later */ |
| 3951 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3952 | } else { |
| 3953 | /* root node */ |
| 3954 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3955 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3956 | } |
| 3957 | |
| 3958 | return LY_SUCCESS; |
| 3959 | } |
| 3960 | |
| 3961 | /** |
| 3962 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3963 | * leafref or instance-identifier target node(s). |
| 3964 | * |
| 3965 | * @param[in] args Array of arguments. |
| 3966 | * @param[in] arg_count Count of elements in @p args. |
| 3967 | * @param[in,out] set Context and result set at the same time. |
| 3968 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3969 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3970 | */ |
| 3971 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3972 | 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] | 3973 | { |
| 3974 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3975 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3976 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3977 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3978 | struct ly_path *p; |
| 3979 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3980 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3981 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3982 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3983 | |
| 3984 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3985 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3986 | 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] | 3987 | } 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] | 3988 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3989 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3990 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3991 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3992 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3993 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3994 | __func__, sleaf->name); |
| 3995 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3996 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3997 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 3998 | 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] | 3999 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 4000 | 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] | 4001 | |
| 4002 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4003 | 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] | 4004 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4005 | if (!r) { |
| 4006 | /* get the target node */ |
| 4007 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 4008 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4009 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 4010 | LY_CHECK_RET(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] | 4011 | } /* 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] | 4012 | } |
| 4013 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4014 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4015 | } |
| 4016 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4017 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4018 | 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] | 4019 | return LY_EVALID; |
| 4020 | } |
| 4021 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4022 | lyxp_set_free_content(set); |
| 4023 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4024 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4025 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4026 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4027 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 4028 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4029 | 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] | 4030 | &node, &errmsg)) { |
Michal Vasko | 1a2b8ee | 2022-12-05 10:41:55 +0100 | [diff] [blame] | 4031 | LOGERR(set->ctx, LY_EVALID, "%s", errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4032 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4033 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4034 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4035 | } else { |
| 4036 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 4037 | if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4038 | 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] | 4039 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4040 | return LY_EVALID; |
| 4041 | } |
| 4042 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4043 | |
| 4044 | /* insert it */ |
| 4045 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4046 | } |
| 4047 | } |
| 4048 | |
| 4049 | return LY_SUCCESS; |
| 4050 | } |
| 4051 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4052 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4053 | 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] | 4054 | { |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 4055 | uint32_t i, id_len; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4056 | LY_ARRAY_COUNT_TYPE u; |
| 4057 | struct lyd_node_term *leaf; |
| 4058 | struct lysc_node_leaf *sleaf; |
| 4059 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4060 | struct lyd_value *val; |
| 4061 | const struct lys_module *mod; |
| 4062 | const char *id_name; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4063 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4064 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4065 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4066 | |
| 4067 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4068 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4069 | 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] | 4070 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4071 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4072 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4073 | sleaf->name); |
| 4074 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4075 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4076 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4080 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4081 | 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] | 4082 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4083 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4084 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4085 | } |
| 4086 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4087 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4088 | return rc; |
| 4089 | } |
| 4090 | |
| 4091 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4092 | 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] | 4093 | return LY_EVALID; |
| 4094 | } |
| 4095 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4096 | LY_CHECK_RET(rc); |
| 4097 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4098 | /* parse the identity */ |
| 4099 | id_name = args[1]->val.str; |
| 4100 | id_len = strlen(id_name); |
| 4101 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4102 | LY_CHECK_RET(rc); |
| 4103 | if (!mod) { |
| 4104 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4105 | return LY_EVALID; |
| 4106 | } |
| 4107 | |
| 4108 | /* find the identity */ |
| 4109 | found = 0; |
| 4110 | LY_ARRAY_FOR(mod->identities, u) { |
| 4111 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4112 | /* we have match */ |
| 4113 | found = 1; |
| 4114 | break; |
| 4115 | } |
| 4116 | } |
| 4117 | if (!found) { |
| 4118 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4119 | return LY_EVALID; |
| 4120 | } |
| 4121 | id = &mod->identities[u]; |
| 4122 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4123 | set_fill_boolean(set, 0); |
| 4124 | found = 0; |
| 4125 | for (i = 0; i < args[0]->used; ++i) { |
| 4126 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4127 | continue; |
| 4128 | } |
| 4129 | |
| 4130 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4131 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4132 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4133 | val = &leaf->value; |
| 4134 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 4135 | /* uninteresting */ |
| 4136 | continue; |
| 4137 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4138 | } else { |
| 4139 | meta = args[0]->val.meta[i].meta; |
| 4140 | val = &meta->value; |
| 4141 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4142 | /* uninteresting */ |
| 4143 | continue; |
| 4144 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4145 | } |
| 4146 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4147 | /* check the identity itself */ |
| 4148 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4149 | set_fill_boolean(set, 1); |
| 4150 | found = 1; |
| 4151 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4152 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4153 | set_fill_boolean(set, 1); |
| 4154 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4155 | } |
| 4156 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4157 | if (found) { |
| 4158 | break; |
| 4159 | } |
| 4160 | } |
| 4161 | |
| 4162 | return LY_SUCCESS; |
| 4163 | } |
| 4164 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4165 | /** |
| 4166 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4167 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4168 | * argument identity. |
| 4169 | * |
| 4170 | * @param[in] args Array of arguments. |
| 4171 | * @param[in] arg_count Count of elements in @p args. |
| 4172 | * @param[in,out] set Context and result set at the same time. |
| 4173 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4174 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4175 | */ |
| 4176 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4177 | 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] | 4178 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4179 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | /** |
| 4183 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4184 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4185 | * the second argument identity. |
| 4186 | * |
| 4187 | * @param[in] args Array of arguments. |
| 4188 | * @param[in] arg_count Count of elements in @p args. |
| 4189 | * @param[in,out] set Context and result set at the same time. |
| 4190 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4191 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4192 | */ |
| 4193 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4194 | 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] | 4195 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4196 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | /** |
| 4200 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4201 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4202 | * |
| 4203 | * @param[in] args Array of arguments. |
| 4204 | * @param[in] arg_count Count of elements in @p args. |
| 4205 | * @param[in,out] set Context and result set at the same time. |
| 4206 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4207 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4208 | */ |
| 4209 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4210 | 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] | 4211 | { |
| 4212 | struct lyd_node_term *leaf; |
| 4213 | struct lysc_node_leaf *sleaf; |
| 4214 | LY_ERR rc = LY_SUCCESS; |
| 4215 | |
| 4216 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4217 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4218 | 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] | 4219 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4220 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4221 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4222 | sleaf->name); |
| 4223 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4224 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4225 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4226 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4227 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4228 | return rc; |
| 4229 | } |
| 4230 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4231 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4232 | 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] | 4233 | return LY_EVALID; |
| 4234 | } |
| 4235 | |
| 4236 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4237 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4238 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4239 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4240 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4241 | set_fill_number(set, leaf->value.enum_item->value); |
| 4242 | } |
| 4243 | } |
| 4244 | |
| 4245 | return LY_SUCCESS; |
| 4246 | } |
| 4247 | |
| 4248 | /** |
| 4249 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4250 | * with false value. |
| 4251 | * |
| 4252 | * @param[in] args Array of arguments. |
| 4253 | * @param[in] arg_count Count of elements in @p args. |
| 4254 | * @param[in,out] set Context and result set at the same time. |
| 4255 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4256 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4257 | */ |
| 4258 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4259 | 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] | 4260 | { |
| 4261 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4262 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4263 | return LY_SUCCESS; |
| 4264 | } |
| 4265 | |
| 4266 | set_fill_boolean(set, 0); |
| 4267 | return LY_SUCCESS; |
| 4268 | } |
| 4269 | |
| 4270 | /** |
| 4271 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4272 | * with the first argument floored (truncated). |
| 4273 | * |
| 4274 | * @param[in] args Array of arguments. |
| 4275 | * @param[in] arg_count Count of elements in @p args. |
| 4276 | * @param[in,out] set Context and result set at the same time. |
| 4277 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4278 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4279 | */ |
| 4280 | static LY_ERR |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4281 | 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] | 4282 | { |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4283 | struct lysc_node_leaf *sleaf; |
| 4284 | LY_ERR rc = LY_SUCCESS; |
| 4285 | |
| 4286 | if (options & LYXP_SCNODE_ALL) { |
| 4287 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
| 4288 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 4289 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4290 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4291 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4292 | sleaf->name); |
| 4293 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4294 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4295 | } |
| 4296 | } |
| 4297 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 4298 | return rc; |
| 4299 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4300 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4301 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | LY_CHECK_RET(rc); |
| 4303 | if (isfinite(args[0]->val.num)) { |
| 4304 | set_fill_number(set, (long long)args[0]->val.num); |
| 4305 | } |
| 4306 | |
| 4307 | return LY_SUCCESS; |
| 4308 | } |
| 4309 | |
| 4310 | /** |
| 4311 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4312 | * whether the language of the text matches the one from the argument. |
| 4313 | * |
| 4314 | * @param[in] args Array of arguments. |
| 4315 | * @param[in] arg_count Count of elements in @p args. |
| 4316 | * @param[in,out] set Context and result set at the same time. |
| 4317 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4318 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4319 | */ |
| 4320 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4321 | 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] | 4322 | { |
| 4323 | const struct lyd_node *node; |
| 4324 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4325 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4326 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4327 | LY_ERR rc = LY_SUCCESS; |
| 4328 | |
| 4329 | if (options & LYXP_SCNODE_ALL) { |
| 4330 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4331 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4332 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4333 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4334 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4335 | 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] | 4336 | } |
| 4337 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4338 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4339 | return rc; |
| 4340 | } |
| 4341 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4342 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4343 | LY_CHECK_RET(rc); |
| 4344 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4345 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4346 | 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] | 4347 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4348 | } else if (!set->used) { |
| 4349 | set_fill_boolean(set, 0); |
| 4350 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | switch (set->val.nodes[0].type) { |
| 4354 | case LYXP_NODE_ELEM: |
| 4355 | case LYXP_NODE_TEXT: |
| 4356 | node = set->val.nodes[0].node; |
| 4357 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4358 | case LYXP_NODE_META: |
| 4359 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4360 | break; |
| 4361 | default: |
| 4362 | /* nothing to do with roots */ |
| 4363 | set_fill_boolean(set, 0); |
| 4364 | return LY_SUCCESS; |
| 4365 | } |
| 4366 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4367 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4368 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4369 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4370 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4371 | 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] | 4372 | break; |
| 4373 | } |
| 4374 | } |
| 4375 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4376 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4377 | break; |
| 4378 | } |
| 4379 | } |
| 4380 | |
| 4381 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4382 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4383 | set_fill_boolean(set, 0); |
| 4384 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4385 | uint64_t i; |
| 4386 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4387 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4388 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4389 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4390 | set_fill_boolean(set, 0); |
| 4391 | break; |
| 4392 | } |
| 4393 | } |
| 4394 | if (!args[0]->val.str[i]) { |
| 4395 | if (!val[i] || (val[i] == '-')) { |
| 4396 | set_fill_boolean(set, 1); |
| 4397 | } else { |
| 4398 | set_fill_boolean(set, 0); |
| 4399 | } |
| 4400 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4401 | } |
| 4402 | |
| 4403 | return LY_SUCCESS; |
| 4404 | } |
| 4405 | |
| 4406 | /** |
| 4407 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4408 | * with the context size. |
| 4409 | * |
| 4410 | * @param[in] args Array of arguments. |
| 4411 | * @param[in] arg_count Count of elements in @p args. |
| 4412 | * @param[in,out] set Context and result set at the same time. |
| 4413 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4414 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4415 | */ |
| 4416 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4417 | 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] | 4418 | { |
| 4419 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4420 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4421 | return LY_SUCCESS; |
| 4422 | } |
| 4423 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4424 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4425 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4426 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4427 | } else if (!set->used) { |
| 4428 | set_fill_number(set, 0); |
| 4429 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | set_fill_number(set, set->ctx_size); |
| 4433 | return LY_SUCCESS; |
| 4434 | } |
| 4435 | |
| 4436 | /** |
| 4437 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4438 | * with the node name without namespace from the argument or the context. |
| 4439 | * |
| 4440 | * @param[in] args Array of arguments. |
| 4441 | * @param[in] arg_count Count of elements in @p args. |
| 4442 | * @param[in,out] set Context and result set at the same time. |
| 4443 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4444 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4445 | */ |
| 4446 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4447 | 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] | 4448 | { |
| 4449 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4450 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4451 | /* suppress unused variable warning */ |
| 4452 | (void)options; |
| 4453 | |
| 4454 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4455 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4456 | return LY_SUCCESS; |
| 4457 | } |
| 4458 | |
| 4459 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4460 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4461 | 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] | 4462 | "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 (!args[0]->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(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4471 | |
| 4472 | item = &args[0]->val.nodes[0]; |
| 4473 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4474 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4475 | 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] | 4476 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4477 | } else if (!set->used) { |
| 4478 | set_fill_string(set, "", 0); |
| 4479 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4480 | } |
| 4481 | |
| 4482 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4483 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4484 | |
| 4485 | item = &set->val.nodes[0]; |
| 4486 | } |
| 4487 | |
| 4488 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4489 | case LYXP_NODE_NONE: |
| 4490 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4491 | case LYXP_NODE_ROOT: |
| 4492 | case LYXP_NODE_ROOT_CONFIG: |
| 4493 | case LYXP_NODE_TEXT: |
| 4494 | set_fill_string(set, "", 0); |
| 4495 | break; |
| 4496 | case LYXP_NODE_ELEM: |
| 4497 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4498 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4499 | case LYXP_NODE_META: |
| 4500 | 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] | 4501 | break; |
| 4502 | } |
| 4503 | |
| 4504 | return LY_SUCCESS; |
| 4505 | } |
| 4506 | |
| 4507 | /** |
| 4508 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4509 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4510 | * |
| 4511 | * @param[in] args Array of arguments. |
| 4512 | * @param[in] arg_count Count of elements in @p args. |
| 4513 | * @param[in,out] set Context and result set at the same time. |
| 4514 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4515 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4516 | */ |
| 4517 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4518 | 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] | 4519 | { |
| 4520 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4521 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4522 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4523 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4524 | |
| 4525 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4526 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4527 | return LY_SUCCESS; |
| 4528 | } |
| 4529 | |
| 4530 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4531 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4532 | 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] | 4533 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4534 | } else if (!args[0]->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(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4541 | |
| 4542 | item = &args[0]->val.nodes[0]; |
| 4543 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4544 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4545 | 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] | 4546 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4547 | } else if (!set->used) { |
| 4548 | set_fill_string(set, "", 0); |
| 4549 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4550 | } |
| 4551 | |
| 4552 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4553 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4554 | |
| 4555 | item = &set->val.nodes[0]; |
| 4556 | } |
| 4557 | |
| 4558 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4559 | case LYXP_NODE_NONE: |
| 4560 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4561 | case LYXP_NODE_ROOT: |
| 4562 | case LYXP_NODE_ROOT_CONFIG: |
| 4563 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4564 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4565 | break; |
| 4566 | case LYXP_NODE_ELEM: |
| 4567 | mod = item->node->schema->module; |
| 4568 | name = item->node->schema->name; |
| 4569 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4570 | case LYXP_NODE_META: |
| 4571 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4572 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4573 | break; |
| 4574 | } |
| 4575 | |
| 4576 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4577 | 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] | 4578 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4579 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4580 | set_fill_string(set, str, strlen(str)); |
| 4581 | free(str); |
| 4582 | } else { |
| 4583 | set_fill_string(set, "", 0); |
| 4584 | } |
| 4585 | |
| 4586 | return LY_SUCCESS; |
| 4587 | } |
| 4588 | |
| 4589 | /** |
| 4590 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4591 | * with the namespace of the node from the argument or the context. |
| 4592 | * |
| 4593 | * @param[in] args Array of arguments. |
| 4594 | * @param[in] arg_count Count of elements in @p args. |
| 4595 | * @param[in,out] set Context and result set at the same time. |
| 4596 | * @param[in] options XPath options. |
| 4597 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4598 | */ |
| 4599 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4600 | 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] | 4601 | { |
| 4602 | struct lyxp_set_node *item; |
| 4603 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4604 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4605 | /* suppress unused variable warning */ |
| 4606 | (void)options; |
| 4607 | |
| 4608 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4609 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4610 | return LY_SUCCESS; |
| 4611 | } |
| 4612 | |
| 4613 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4614 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4615 | 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] | 4616 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4617 | return LY_EVALID; |
| 4618 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4619 | set_fill_string(set, "", 0); |
| 4620 | return LY_SUCCESS; |
| 4621 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4625 | |
| 4626 | item = &args[0]->val.nodes[0]; |
| 4627 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4628 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4629 | 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] | 4630 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4631 | } else if (!set->used) { |
| 4632 | set_fill_string(set, "", 0); |
| 4633 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4637 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4638 | |
| 4639 | item = &set->val.nodes[0]; |
| 4640 | } |
| 4641 | |
| 4642 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4643 | case LYXP_NODE_NONE: |
| 4644 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4645 | case LYXP_NODE_ROOT: |
| 4646 | case LYXP_NODE_ROOT_CONFIG: |
| 4647 | case LYXP_NODE_TEXT: |
| 4648 | set_fill_string(set, "", 0); |
| 4649 | break; |
| 4650 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4651 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4652 | if (item->type == LYXP_NODE_ELEM) { |
| 4653 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4654 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4655 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4656 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4660 | break; |
| 4661 | } |
| 4662 | |
| 4663 | return LY_SUCCESS; |
| 4664 | } |
| 4665 | |
| 4666 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4667 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4668 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4669 | * from the argument or the context. |
| 4670 | * |
| 4671 | * @param[in] args Array of arguments. |
| 4672 | * @param[in] arg_count Count of elements in @p args. |
| 4673 | * @param[in,out] set Context and result set at the same time. |
| 4674 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4675 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4676 | */ |
| 4677 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4678 | 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] | 4679 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4680 | uint32_t i, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4681 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4682 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4683 | struct lysc_node_leaf *sleaf; |
| 4684 | LY_ERR rc = LY_SUCCESS; |
| 4685 | |
| 4686 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4687 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4688 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4689 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4690 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4691 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4692 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4693 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4694 | } |
| 4695 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4696 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4697 | return rc; |
| 4698 | } |
| 4699 | |
| 4700 | if (arg_count) { |
| 4701 | set_fill_set(set, args[0]); |
| 4702 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4703 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4704 | LY_CHECK_RET(rc); |
| 4705 | |
| 4706 | /* is there any normalization necessary? */ |
| 4707 | for (i = 0; set->val.str[i]; ++i) { |
| 4708 | if (is_xmlws(set->val.str[i])) { |
| 4709 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4710 | have_spaces = 1; |
| 4711 | break; |
| 4712 | } |
| 4713 | space_before = 1; |
| 4714 | } else { |
| 4715 | space_before = 0; |
| 4716 | } |
| 4717 | } |
| 4718 | |
| 4719 | /* yep, there is */ |
| 4720 | if (have_spaces) { |
| 4721 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4722 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4723 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4724 | new_used = 0; |
| 4725 | |
| 4726 | space_before = 0; |
| 4727 | for (i = 0; set->val.str[i]; ++i) { |
| 4728 | if (is_xmlws(set->val.str[i])) { |
| 4729 | if ((i == 0) || space_before) { |
| 4730 | space_before = 1; |
| 4731 | continue; |
| 4732 | } else { |
| 4733 | space_before = 1; |
| 4734 | } |
| 4735 | } else { |
| 4736 | space_before = 0; |
| 4737 | } |
| 4738 | |
| 4739 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4740 | ++new_used; |
| 4741 | } |
| 4742 | |
| 4743 | /* at worst there is one trailing space now */ |
| 4744 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4745 | --new_used; |
| 4746 | } |
| 4747 | |
| 4748 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4749 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4750 | new[new_used] = '\0'; |
| 4751 | |
| 4752 | free(set->val.str); |
| 4753 | set->val.str = new; |
| 4754 | } |
| 4755 | |
| 4756 | return LY_SUCCESS; |
| 4757 | } |
| 4758 | |
| 4759 | /** |
| 4760 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4761 | * with the argument converted to boolean and logically inverted. |
| 4762 | * |
| 4763 | * @param[in] args Array of arguments. |
| 4764 | * @param[in] arg_count Count of elements in @p args. |
| 4765 | * @param[in,out] set Context and result set at the same time. |
| 4766 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4767 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4768 | */ |
| 4769 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4770 | 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] | 4771 | { |
| 4772 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4773 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4774 | return LY_SUCCESS; |
| 4775 | } |
| 4776 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4777 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4778 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4779 | set_fill_boolean(set, 0); |
| 4780 | } else { |
| 4781 | set_fill_boolean(set, 1); |
| 4782 | } |
| 4783 | |
| 4784 | return LY_SUCCESS; |
| 4785 | } |
| 4786 | |
| 4787 | /** |
| 4788 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4789 | * with the number representation of either the argument or the context. |
| 4790 | * |
| 4791 | * @param[in] args Array of arguments. |
| 4792 | * @param[in] arg_count Count of elements in @p args. |
| 4793 | * @param[in,out] set Context and result set at the same time. |
| 4794 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4795 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4796 | */ |
| 4797 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4798 | 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] | 4799 | { |
| 4800 | LY_ERR rc; |
| 4801 | |
| 4802 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4803 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4804 | return LY_SUCCESS; |
| 4805 | } |
| 4806 | |
| 4807 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4808 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4809 | LY_CHECK_RET(rc); |
| 4810 | set_fill_set(set, args[0]); |
| 4811 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4812 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4813 | LY_CHECK_RET(rc); |
| 4814 | } |
| 4815 | |
| 4816 | return LY_SUCCESS; |
| 4817 | } |
| 4818 | |
| 4819 | /** |
| 4820 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4821 | * with the context position. |
| 4822 | * |
| 4823 | * @param[in] args Array of arguments. |
| 4824 | * @param[in] arg_count Count of elements in @p args. |
| 4825 | * @param[in,out] set Context and result set at the same time. |
| 4826 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4827 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4828 | */ |
| 4829 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4830 | 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] | 4831 | { |
| 4832 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4833 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4834 | return LY_SUCCESS; |
| 4835 | } |
| 4836 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4837 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4838 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4839 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4840 | } else if (!set->used) { |
| 4841 | set_fill_number(set, 0); |
| 4842 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | set_fill_number(set, set->ctx_pos); |
| 4846 | |
| 4847 | /* UNUSED in 'Release' build type */ |
| 4848 | (void)options; |
| 4849 | return LY_SUCCESS; |
| 4850 | } |
| 4851 | |
| 4852 | /** |
| 4853 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4854 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4855 | * YANG 1.1 RFC section 10.2.1. |
| 4856 | * |
| 4857 | * @param[in] args Array of arguments. |
| 4858 | * @param[in] arg_count Count of elements in @p args. |
| 4859 | * @param[in,out] set Context and result set at the same time. |
| 4860 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4861 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4862 | */ |
| 4863 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4864 | 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] | 4865 | { |
| 4866 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4867 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4868 | LY_ERR rc = LY_SUCCESS; |
| 4869 | struct ly_err_item *err; |
| 4870 | |
| 4871 | if (options & LYXP_SCNODE_ALL) { |
| 4872 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4873 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4874 | 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] | 4875 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4876 | 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] | 4877 | } |
| 4878 | } |
| 4879 | |
| 4880 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4881 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4882 | 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] | 4883 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4884 | 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] | 4885 | } |
| 4886 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4887 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4888 | return rc; |
| 4889 | } |
| 4890 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4891 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4892 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4893 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4894 | LY_CHECK_RET(rc); |
| 4895 | |
| 4896 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4897 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4898 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4899 | 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] | 4900 | if (set->cur_node) { |
| 4901 | LOG_LOCBACK(0, 1, 0, 0); |
| 4902 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4903 | if (rc != LY_SUCCESS) { |
| 4904 | LY_ARRAY_FREE(patterns); |
| 4905 | return rc; |
| 4906 | } |
| 4907 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4908 | 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] | 4909 | pcre2_code_free((*pattern)->code); |
| 4910 | free(*pattern); |
| 4911 | LY_ARRAY_FREE(patterns); |
| 4912 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4913 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4914 | ly_err_free(err); |
| 4915 | return rc; |
| 4916 | } |
| 4917 | |
| 4918 | if (rc == LY_EVALID) { |
| 4919 | ly_err_free(err); |
| 4920 | set_fill_boolean(set, 0); |
| 4921 | } else { |
| 4922 | set_fill_boolean(set, 1); |
| 4923 | } |
| 4924 | |
| 4925 | return LY_SUCCESS; |
| 4926 | } |
| 4927 | |
| 4928 | /** |
| 4929 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4930 | * with the rounded first argument. For details refer to |
| 4931 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4932 | * |
| 4933 | * @param[in] args Array of arguments. |
| 4934 | * @param[in] arg_count Count of elements in @p args. |
| 4935 | * @param[in,out] set Context and result set at the same time. |
| 4936 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4937 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4938 | */ |
| 4939 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4940 | 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] | 4941 | { |
| 4942 | struct lysc_node_leaf *sleaf; |
| 4943 | LY_ERR rc = LY_SUCCESS; |
| 4944 | |
| 4945 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4946 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4947 | 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] | 4948 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4949 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4950 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4951 | sleaf->name); |
| 4952 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4953 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4954 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4955 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4956 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4957 | return rc; |
| 4958 | } |
| 4959 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4960 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4961 | LY_CHECK_RET(rc); |
| 4962 | |
| 4963 | /* cover only the cases where floor can't be used */ |
| 4964 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4965 | set_fill_number(set, -0.0f); |
| 4966 | } else { |
| 4967 | args[0]->val.num += 0.5; |
| 4968 | rc = xpath_floor(args, 1, args[0], options); |
| 4969 | LY_CHECK_RET(rc); |
| 4970 | set_fill_number(set, args[0]->val.num); |
| 4971 | } |
| 4972 | |
| 4973 | return LY_SUCCESS; |
| 4974 | } |
| 4975 | |
| 4976 | /** |
| 4977 | * @brief Execute the XPath starts-with(string, string) function. |
| 4978 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4979 | * the prefix of the first or not. |
| 4980 | * |
| 4981 | * @param[in] args Array of arguments. |
| 4982 | * @param[in] arg_count Count of elements in @p args. |
| 4983 | * @param[in,out] set Context and result set at the same time. |
| 4984 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4985 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4986 | */ |
| 4987 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4988 | 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] | 4989 | { |
| 4990 | struct lysc_node_leaf *sleaf; |
| 4991 | LY_ERR rc = LY_SUCCESS; |
| 4992 | |
| 4993 | if (options & LYXP_SCNODE_ALL) { |
| 4994 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4995 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4996 | 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] | 4997 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4998 | 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] | 4999 | } |
| 5000 | } |
| 5001 | |
| 5002 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5003 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5004 | 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] | 5005 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5006 | 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] | 5007 | } |
| 5008 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5009 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5010 | return rc; |
| 5011 | } |
| 5012 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5013 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5014 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5015 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5016 | LY_CHECK_RET(rc); |
| 5017 | |
| 5018 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 5019 | set_fill_boolean(set, 0); |
| 5020 | } else { |
| 5021 | set_fill_boolean(set, 1); |
| 5022 | } |
| 5023 | |
| 5024 | return LY_SUCCESS; |
| 5025 | } |
| 5026 | |
| 5027 | /** |
| 5028 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 5029 | * with the string representation of either the argument or the context. |
| 5030 | * |
| 5031 | * @param[in] args Array of arguments. |
| 5032 | * @param[in] arg_count Count of elements in @p args. |
| 5033 | * @param[in,out] set Context and result set at the same time. |
| 5034 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5035 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5036 | */ |
| 5037 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5038 | 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] | 5039 | { |
| 5040 | LY_ERR rc; |
| 5041 | |
| 5042 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5043 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5044 | return LY_SUCCESS; |
| 5045 | } |
| 5046 | |
| 5047 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5048 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5049 | LY_CHECK_RET(rc); |
| 5050 | set_fill_set(set, args[0]); |
| 5051 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5052 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5053 | LY_CHECK_RET(rc); |
| 5054 | } |
| 5055 | |
| 5056 | return LY_SUCCESS; |
| 5057 | } |
| 5058 | |
| 5059 | /** |
| 5060 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5061 | * with the length of the string in either the argument or the context. |
| 5062 | * |
| 5063 | * @param[in] args Array of arguments. |
| 5064 | * @param[in] arg_count Count of elements in @p args. |
| 5065 | * @param[in,out] set Context and result set at the same time. |
| 5066 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5067 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5068 | */ |
| 5069 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5070 | 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] | 5071 | { |
| 5072 | struct lysc_node_leaf *sleaf; |
| 5073 | LY_ERR rc = LY_SUCCESS; |
| 5074 | |
| 5075 | if (options & LYXP_SCNODE_ALL) { |
| 5076 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5077 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5078 | 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] | 5079 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5080 | 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] | 5081 | } |
| 5082 | } |
| 5083 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5084 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5085 | 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] | 5086 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5087 | 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] | 5088 | } |
| 5089 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5090 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5091 | return rc; |
| 5092 | } |
| 5093 | |
| 5094 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5095 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5096 | LY_CHECK_RET(rc); |
| 5097 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5098 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5099 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5100 | LY_CHECK_RET(rc); |
| 5101 | set_fill_number(set, strlen(set->val.str)); |
| 5102 | } |
| 5103 | |
| 5104 | return LY_SUCCESS; |
| 5105 | } |
| 5106 | |
| 5107 | /** |
| 5108 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5109 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5110 | * on the second argument index ending on the third argument index, |
| 5111 | * indexed from 1. For exact definition refer to |
| 5112 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5113 | * |
| 5114 | * @param[in] args Array of arguments. |
| 5115 | * @param[in] arg_count Count of elements in @p args. |
| 5116 | * @param[in,out] set Context and result set at the same time. |
| 5117 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5118 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5119 | */ |
| 5120 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5121 | 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] | 5122 | { |
Michal Vasko | 6db996e | 2022-07-28 10:28:04 +0200 | [diff] [blame] | 5123 | int64_t start; |
| 5124 | int32_t len; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5125 | uint32_t str_start, str_len, pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5126 | struct lysc_node_leaf *sleaf; |
| 5127 | LY_ERR rc = LY_SUCCESS; |
| 5128 | |
| 5129 | if (options & LYXP_SCNODE_ALL) { |
| 5130 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5131 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5132 | 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] | 5133 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5134 | 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] | 5135 | } |
| 5136 | } |
| 5137 | |
| 5138 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5139 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5140 | 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] | 5141 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5142 | 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] | 5143 | } |
| 5144 | } |
| 5145 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5146 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5147 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5148 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5149 | 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] | 5150 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5151 | 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] | 5152 | } |
| 5153 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5154 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5155 | return rc; |
| 5156 | } |
| 5157 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5158 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5159 | LY_CHECK_RET(rc); |
| 5160 | |
| 5161 | /* start */ |
| 5162 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5163 | return -1; |
| 5164 | } |
| 5165 | if (isfinite(args[1]->val.num)) { |
| 5166 | start = args[1]->val.num - 1; |
| 5167 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5168 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5169 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5170 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | /* len */ |
| 5174 | if (arg_count == 3) { |
| 5175 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5176 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5177 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5178 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5179 | } else if (isfinite(args[2]->val.num)) { |
| 5180 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5181 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5182 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5183 | } |
| 5184 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5185 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5186 | } |
| 5187 | |
| 5188 | /* find matching character positions */ |
| 5189 | str_start = 0; |
| 5190 | str_len = 0; |
| 5191 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5192 | if (pos < start) { |
| 5193 | ++str_start; |
| 5194 | } else if (pos < start + len) { |
| 5195 | ++str_len; |
| 5196 | } else { |
| 5197 | break; |
| 5198 | } |
| 5199 | } |
| 5200 | |
| 5201 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5202 | return LY_SUCCESS; |
| 5203 | } |
| 5204 | |
| 5205 | /** |
| 5206 | * @brief Execute the XPath substring-after(string, string) function. |
| 5207 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5208 | * of the second argument in the first or an empty string. |
| 5209 | * |
| 5210 | * @param[in] args Array of arguments. |
| 5211 | * @param[in] arg_count Count of elements in @p args. |
| 5212 | * @param[in,out] set Context and result set at the same time. |
| 5213 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5214 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5215 | */ |
| 5216 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5217 | 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] | 5218 | { |
| 5219 | char *ptr; |
| 5220 | struct lysc_node_leaf *sleaf; |
| 5221 | LY_ERR rc = LY_SUCCESS; |
| 5222 | |
| 5223 | if (options & LYXP_SCNODE_ALL) { |
| 5224 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5225 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5226 | 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] | 5227 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5228 | 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] | 5229 | } |
| 5230 | } |
| 5231 | |
| 5232 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5233 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5234 | 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] | 5235 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5236 | 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] | 5237 | } |
| 5238 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5239 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5240 | return rc; |
| 5241 | } |
| 5242 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5243 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5244 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5245 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5246 | LY_CHECK_RET(rc); |
| 5247 | |
| 5248 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5249 | if (ptr) { |
| 5250 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5251 | } else { |
| 5252 | set_fill_string(set, "", 0); |
| 5253 | } |
| 5254 | |
| 5255 | return LY_SUCCESS; |
| 5256 | } |
| 5257 | |
| 5258 | /** |
| 5259 | * @brief Execute the XPath substring-before(string, string) function. |
| 5260 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5261 | * of the second argument in the first or an empty string. |
| 5262 | * |
| 5263 | * @param[in] args Array of arguments. |
| 5264 | * @param[in] arg_count Count of elements in @p args. |
| 5265 | * @param[in,out] set Context and result set at the same time. |
| 5266 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5267 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5268 | */ |
| 5269 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5270 | 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] | 5271 | { |
| 5272 | char *ptr; |
| 5273 | struct lysc_node_leaf *sleaf; |
| 5274 | LY_ERR rc = LY_SUCCESS; |
| 5275 | |
| 5276 | if (options & LYXP_SCNODE_ALL) { |
| 5277 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5278 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5279 | 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] | 5280 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5281 | 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] | 5282 | } |
| 5283 | } |
| 5284 | |
| 5285 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5286 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5287 | 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] | 5288 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5289 | 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] | 5290 | } |
| 5291 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5292 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5293 | return rc; |
| 5294 | } |
| 5295 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5296 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5297 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5298 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5299 | LY_CHECK_RET(rc); |
| 5300 | |
| 5301 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5302 | if (ptr) { |
| 5303 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5304 | } else { |
| 5305 | set_fill_string(set, "", 0); |
| 5306 | } |
| 5307 | |
| 5308 | return LY_SUCCESS; |
| 5309 | } |
| 5310 | |
| 5311 | /** |
| 5312 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5313 | * with the sum of all the nodes in the context. |
| 5314 | * |
| 5315 | * @param[in] args Array of arguments. |
| 5316 | * @param[in] arg_count Count of elements in @p args. |
| 5317 | * @param[in,out] set Context and result set at the same time. |
| 5318 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5319 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5320 | */ |
| 5321 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5322 | 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] | 5323 | { |
| 5324 | long double num; |
| 5325 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5326 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | struct lyxp_set set_item; |
| 5328 | struct lysc_node_leaf *sleaf; |
| 5329 | LY_ERR rc = LY_SUCCESS; |
| 5330 | |
| 5331 | if (options & LYXP_SCNODE_ALL) { |
| 5332 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5333 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5334 | 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] | 5335 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5336 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5337 | 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] | 5338 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5339 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5340 | 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] | 5341 | } |
| 5342 | } |
| 5343 | } |
| 5344 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5345 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5346 | return rc; |
| 5347 | } |
| 5348 | |
| 5349 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5350 | |
| 5351 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5352 | 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] | 5353 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5354 | } else if (!args[0]->used) { |
| 5355 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5356 | } |
| 5357 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5358 | set_init(&set_item, set); |
| 5359 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5360 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5361 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5362 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5363 | |
| 5364 | set_item.used = 1; |
| 5365 | set_item.size = 1; |
| 5366 | |
| 5367 | for (i = 0; i < args[0]->used; ++i) { |
| 5368 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5369 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5370 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5371 | LY_CHECK_RET(rc); |
| 5372 | num = cast_string_to_number(str); |
| 5373 | free(str); |
| 5374 | set->val.num += num; |
| 5375 | } |
| 5376 | |
| 5377 | free(set_item.val.nodes); |
| 5378 | |
| 5379 | return LY_SUCCESS; |
| 5380 | } |
| 5381 | |
| 5382 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5383 | * @brief Execute the XPath translate(string, string, string) function. |
| 5384 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5385 | * from the second argument replaced by those on the corresponding |
| 5386 | * positions in the third argument. |
| 5387 | * |
| 5388 | * @param[in] args Array of arguments. |
| 5389 | * @param[in] arg_count Count of elements in @p args. |
| 5390 | * @param[in,out] set Context and result set at the same time. |
| 5391 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5392 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5393 | */ |
| 5394 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5395 | 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] | 5396 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5397 | uint32_t i, j, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5398 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5399 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5400 | struct lysc_node_leaf *sleaf; |
| 5401 | LY_ERR rc = LY_SUCCESS; |
| 5402 | |
| 5403 | if (options & LYXP_SCNODE_ALL) { |
| 5404 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5405 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5406 | 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] | 5407 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5408 | 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] | 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5413 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5414 | 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] | 5415 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5416 | 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] | 5417 | } |
| 5418 | } |
| 5419 | |
| 5420 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5421 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5422 | 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] | 5423 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5424 | 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] | 5425 | } |
| 5426 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5427 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5428 | return rc; |
| 5429 | } |
| 5430 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5431 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5432 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5433 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5434 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5435 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5436 | LY_CHECK_RET(rc); |
| 5437 | |
| 5438 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5439 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5440 | new_used = 0; |
| 5441 | |
| 5442 | have_removed = 0; |
| 5443 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5444 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5445 | |
| 5446 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5447 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5448 | /* removing this char */ |
| 5449 | if (j >= strlen(args[2]->val.str)) { |
| 5450 | have_removed = 1; |
| 5451 | found = 1; |
| 5452 | break; |
| 5453 | } |
| 5454 | /* replacing this char */ |
| 5455 | new[new_used] = args[2]->val.str[j]; |
| 5456 | ++new_used; |
| 5457 | found = 1; |
| 5458 | break; |
| 5459 | } |
| 5460 | } |
| 5461 | |
| 5462 | /* copying this char */ |
| 5463 | if (!found) { |
| 5464 | new[new_used] = args[0]->val.str[i]; |
| 5465 | ++new_used; |
| 5466 | } |
| 5467 | } |
| 5468 | |
| 5469 | if (have_removed) { |
| 5470 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5471 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5472 | } |
| 5473 | new[new_used] = '\0'; |
| 5474 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5475 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5476 | set->type = LYXP_SET_STRING; |
| 5477 | set->val.str = new; |
| 5478 | |
| 5479 | return LY_SUCCESS; |
| 5480 | } |
| 5481 | |
| 5482 | /** |
| 5483 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5484 | * with true value. |
| 5485 | * |
| 5486 | * @param[in] args Array of arguments. |
| 5487 | * @param[in] arg_count Count of elements in @p args. |
| 5488 | * @param[in,out] set Context and result set at the same time. |
| 5489 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5490 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5491 | */ |
| 5492 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5493 | 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] | 5494 | { |
| 5495 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5496 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5497 | return LY_SUCCESS; |
| 5498 | } |
| 5499 | |
| 5500 | set_fill_boolean(set, 1); |
| 5501 | return LY_SUCCESS; |
| 5502 | } |
| 5503 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5504 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5505 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5506 | * with only nodes from the context. |
| 5507 | * |
| 5508 | * @param[in,out] set Context and result set at the same time. |
| 5509 | * @param[in] axis Axis to search on. |
| 5510 | * @param[in] options XPath options. |
| 5511 | * @return LY_ERR |
| 5512 | */ |
| 5513 | static LY_ERR |
| 5514 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5515 | { |
| 5516 | if (options & LYXP_SCNODE_ALL) { |
| 5517 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5518 | } |
| 5519 | |
| 5520 | if (set->type != LYXP_SET_NODE_SET) { |
| 5521 | lyxp_set_free_content(set); |
| 5522 | return LY_SUCCESS; |
| 5523 | } |
| 5524 | |
| 5525 | /* just like moving to a node with no restrictions */ |
| 5526 | return moveto_node(set, NULL, NULL, axis, options); |
| 5527 | } |
| 5528 | |
| 5529 | /** |
| 5530 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5531 | * with the text content of the nodes in the context. |
| 5532 | * |
| 5533 | * @param[in,out] set Context and result set at the same time. |
| 5534 | * @param[in] axis Axis to search on. |
| 5535 | * @param[in] options XPath options. |
| 5536 | * @return LY_ERR |
| 5537 | */ |
| 5538 | static LY_ERR |
| 5539 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5540 | { |
| 5541 | uint32_t i; |
| 5542 | |
| 5543 | if (options & LYXP_SCNODE_ALL) { |
| 5544 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5545 | return LY_SUCCESS; |
| 5546 | } |
| 5547 | |
| 5548 | if (set->type != LYXP_SET_NODE_SET) { |
| 5549 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5550 | return LY_EVALID; |
| 5551 | } |
| 5552 | |
| 5553 | if (axis != LYXP_AXIS_CHILD) { |
| 5554 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5555 | lyxp_set_free_content(set); |
| 5556 | return LY_SUCCESS; |
| 5557 | } |
| 5558 | |
| 5559 | for (i = 0; i < set->used; ++i) { |
| 5560 | switch (set->val.nodes[i].type) { |
| 5561 | case LYXP_NODE_NONE: |
| 5562 | LOGINT_RET(set->ctx); |
| 5563 | case LYXP_NODE_ELEM: |
| 5564 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5565 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5566 | break; |
| 5567 | } |
| 5568 | /* fall through */ |
| 5569 | case LYXP_NODE_ROOT: |
| 5570 | case LYXP_NODE_ROOT_CONFIG: |
| 5571 | case LYXP_NODE_TEXT: |
| 5572 | case LYXP_NODE_META: |
| 5573 | set_remove_node_none(set, i); |
| 5574 | break; |
| 5575 | } |
| 5576 | } |
| 5577 | set_remove_nodes_none(set); |
| 5578 | |
| 5579 | return LY_SUCCESS; |
| 5580 | } |
| 5581 | |
| 5582 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5583 | * @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] | 5584 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5585 | * XPath @p set is expected to be a (sc)node set! |
| 5586 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5587 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5588 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5589 | * @param[in] set Set with general XPath context. |
| 5590 | * @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] | 5591 | * @param[out] moveto_mod Expected module of a matching node. |
| 5592 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5593 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5594 | static LY_ERR |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 5595 | 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] | 5596 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5597 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5598 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5599 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5600 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5601 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5602 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5603 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5604 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5605 | /* specific module */ |
| 5606 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5607 | 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] | 5608 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5609 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5610 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5611 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5612 | return LY_EVALID; |
| 5613 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5614 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5615 | *qname += pref_len + 1; |
| 5616 | *qname_len -= pref_len + 1; |
| 5617 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5618 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5619 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5620 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5621 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5622 | case LY_VALUE_SCHEMA: |
| 5623 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5624 | /* current module */ |
| 5625 | mod = set->cur_mod; |
| 5626 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5627 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5628 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5629 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5630 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5631 | /* inherit parent (context node) module */ |
| 5632 | if (ctx_scnode) { |
| 5633 | mod = ctx_scnode->module; |
| 5634 | } else { |
| 5635 | mod = NULL; |
| 5636 | } |
| 5637 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5638 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5639 | /* all nodes need to be prefixed */ |
| 5640 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5641 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5642 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5643 | } |
| 5644 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5645 | *moveto_mod = mod; |
| 5646 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5650 | * @brief Move context @p set to the root. Handles absolute path. |
| 5651 | * Result is LYXP_SET_NODE_SET. |
| 5652 | * |
| 5653 | * @param[in,out] set Set to use. |
| 5654 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5655 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5656 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5657 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5658 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5659 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5660 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5661 | |
| 5662 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5663 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 5664 | LY_CHECK_RET(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] | 5665 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5666 | set->type = LYXP_SET_NODE_SET; |
| 5667 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5668 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 5669 | set->non_child_axis = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5670 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5671 | |
| 5672 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5673 | } |
| 5674 | |
| 5675 | /** |
| 5676 | * @brief Check @p node as a part of NameTest processing. |
| 5677 | * |
| 5678 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5679 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5680 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5681 | * @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] | 5682 | * @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] | 5683 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5684 | * @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] | 5685 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5686 | */ |
| 5687 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5688 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5689 | 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] | 5690 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5691 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5692 | assert(node_type == set->root_type); |
| 5693 | |
| 5694 | if (node_name || moveto_mod) { |
| 5695 | /* root will not match a specific node */ |
| 5696 | return LY_ENOT; |
| 5697 | } |
| 5698 | return LY_SUCCESS; |
| 5699 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5700 | /* other types will not match */ |
| 5701 | return LY_ENOT; |
| 5702 | } |
| 5703 | |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5704 | if (!node->schema) { |
| 5705 | /* opaque node never matches */ |
| 5706 | return LY_ENOT; |
| 5707 | } |
| 5708 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5709 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5710 | if (moveto_mod) { |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5711 | if ((set->ctx == LYD_CTX(node)) && (node->schema->module != moveto_mod)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5712 | return LY_ENOT; |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5713 | } 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] | 5714 | return LY_ENOT; |
| 5715 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5716 | } |
| 5717 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5718 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5719 | 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] | 5720 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5721 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5722 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5723 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5724 | } |
| 5725 | |
| 5726 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5727 | if (node_name) { |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5728 | if ((set->ctx == LYD_CTX(node)) && (node->schema->name != node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5729 | return LY_ENOT; |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5730 | } 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] | 5731 | return LY_ENOT; |
| 5732 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5733 | } |
| 5734 | |
Michal Vasko | 6d65712 | 2022-10-19 14:38:35 +0200 | [diff] [blame] | 5735 | /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */ |
| 5736 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE) && |
| 5737 | (node != set->cur_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5738 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5739 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5740 | |
| 5741 | /* match */ |
| 5742 | return LY_SUCCESS; |
| 5743 | } |
| 5744 | |
| 5745 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5746 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5747 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5748 | * @param[in] iter Last returned node. |
| 5749 | * @param[in] stop Node to stop the search on and not return. |
| 5750 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5751 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5752 | static const struct lyd_node * |
| 5753 | 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] | 5754 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5755 | const struct lyd_node *next = NULL; |
| 5756 | |
| 5757 | /* 1) child */ |
| 5758 | next = lyd_child(iter); |
| 5759 | if (!next) { |
| 5760 | if (iter == stop) { |
| 5761 | /* reached stop, no more descendants */ |
| 5762 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5763 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5764 | /* 2) child next sibling */ |
| 5765 | next = iter->next; |
| 5766 | } |
| 5767 | while (!next) { |
| 5768 | iter = lyd_parent(iter); |
| 5769 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5770 | return NULL; |
| 5771 | } |
| 5772 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5773 | } |
| 5774 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5775 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5776 | } |
| 5777 | |
| 5778 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5779 | * @brief Get the next node in a backward DFS. |
| 5780 | * |
| 5781 | * @param[in] iter Last returned node. |
| 5782 | * @param[in] stop Node to stop the search on and not return. |
| 5783 | * @return Next node, NULL if there are no more. |
| 5784 | */ |
| 5785 | static const struct lyd_node * |
| 5786 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5787 | { |
| 5788 | const struct lyd_node *next = NULL; |
| 5789 | |
| 5790 | /* 1) previous sibling innermost last child */ |
| 5791 | next = iter->prev->next ? iter->prev : NULL; |
| 5792 | while (next && lyd_child(next)) { |
| 5793 | next = lyd_child(next); |
| 5794 | next = next->prev; |
| 5795 | } |
| 5796 | |
| 5797 | if (!next) { |
| 5798 | /* 2) parent */ |
| 5799 | iter = lyd_parent(iter); |
| 5800 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5801 | return NULL; |
| 5802 | } |
| 5803 | next = iter; |
| 5804 | } |
| 5805 | |
| 5806 | return next; |
| 5807 | } |
| 5808 | |
| 5809 | /** |
| 5810 | * @brief Get the first node on an axis for a context node. |
| 5811 | * |
| 5812 | * @param[in,out] iter NULL, updated to the next node. |
| 5813 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5814 | * @param[in] node Context node. |
| 5815 | * @param[in] node_type Type of @p node. |
| 5816 | * @param[in] axis Axis to use. |
| 5817 | * @param[in] set XPath set with the general context. |
| 5818 | * @return LY_SUCCESS on success. |
| 5819 | * @return LY_ENOTFOUND if no next node found. |
| 5820 | */ |
| 5821 | static LY_ERR |
| 5822 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5823 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5824 | { |
| 5825 | const struct lyd_node *next = NULL; |
| 5826 | enum lyxp_node_type next_type = 0; |
| 5827 | |
| 5828 | assert(!*iter); |
| 5829 | assert(!*iter_type); |
| 5830 | |
| 5831 | switch (axis) { |
| 5832 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5833 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5834 | case LYXP_AXIS_SELF: |
| 5835 | /* return the context node */ |
| 5836 | next = node; |
| 5837 | next_type = node_type; |
| 5838 | break; |
| 5839 | |
| 5840 | case LYXP_AXIS_ANCESTOR: |
| 5841 | case LYXP_AXIS_PARENT: |
| 5842 | if (node_type == LYXP_NODE_ELEM) { |
| 5843 | next = lyd_parent(node); |
| 5844 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5845 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5846 | next = node; |
| 5847 | next_type = LYXP_NODE_ELEM; |
| 5848 | } else if (node_type == LYXP_NODE_META) { |
| 5849 | next = ((struct lyd_meta *)node)->parent; |
| 5850 | next_type = LYXP_NODE_ELEM; |
| 5851 | } /* else root does not have a parent */ |
| 5852 | break; |
| 5853 | |
| 5854 | case LYXP_AXIS_CHILD: |
| 5855 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5856 | assert(!node); |
| 5857 | |
| 5858 | /* search in all the trees */ |
| 5859 | next = set->tree; |
| 5860 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5861 | } else { |
| 5862 | /* search in children */ |
| 5863 | next = lyd_child(node); |
| 5864 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5865 | } |
| 5866 | break; |
| 5867 | |
| 5868 | case LYXP_AXIS_DESCENDANT: |
| 5869 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5870 | /* top-level nodes */ |
| 5871 | next = set->tree; |
| 5872 | next_type = LYXP_NODE_ELEM; |
| 5873 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5874 | /* start from the context node */ |
| 5875 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5876 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5877 | } /* else no children */ |
| 5878 | break; |
| 5879 | |
| 5880 | case LYXP_AXIS_FOLLOWING: |
| 5881 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5882 | if (node_type == LYXP_NODE_ELEM) { |
| 5883 | /* first next sibling */ |
| 5884 | next = node->next; |
| 5885 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5886 | } /* else no sibling */ |
| 5887 | break; |
| 5888 | |
| 5889 | case LYXP_AXIS_PRECEDING: |
| 5890 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5891 | /* skip ancestors */ |
| 5892 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5893 | assert(next); |
| 5894 | next_type = LYXP_NODE_ELEM; |
| 5895 | } /* else no sibling */ |
| 5896 | break; |
| 5897 | |
| 5898 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5899 | if (node_type == LYXP_NODE_ELEM) { |
| 5900 | /* first previous sibling */ |
| 5901 | next = node->prev->next ? node->prev : NULL; |
| 5902 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5903 | } /* else no sibling */ |
| 5904 | break; |
| 5905 | |
| 5906 | case LYXP_AXIS_ATTRIBUTE: |
| 5907 | /* handled specially */ |
| 5908 | assert(0); |
| 5909 | LOGINT(set->ctx); |
| 5910 | break; |
| 5911 | } |
| 5912 | |
| 5913 | *iter = next; |
| 5914 | *iter_type = next_type; |
| 5915 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5916 | } |
| 5917 | |
| 5918 | /** |
| 5919 | * @brief Iterate over all nodes on an axis for a context node. |
| 5920 | * |
| 5921 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5922 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5923 | * @param[in] node Context node. |
| 5924 | * @param[in] node_type Type of @p node. |
| 5925 | * @param[in] axis Axis to use. |
| 5926 | * @param[in] set XPath set with the general context. |
| 5927 | * @return LY_SUCCESS on success. |
| 5928 | * @return LY_ENOTFOUND if no next node found. |
| 5929 | */ |
| 5930 | static LY_ERR |
| 5931 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5932 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5933 | { |
| 5934 | const struct lyd_node *next = NULL; |
| 5935 | enum lyxp_node_type next_type = 0; |
| 5936 | |
| 5937 | if (!*iter_type) { |
| 5938 | /* first returned node */ |
| 5939 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5940 | } |
| 5941 | |
| 5942 | switch (axis) { |
| 5943 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5944 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5945 | /* fake first ancestor, we returned self before */ |
| 5946 | *iter = NULL; |
| 5947 | *iter_type = 0; |
| 5948 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 5949 | } /* else continue ancestor */ |
| 5950 | |
| 5951 | /* fallthrough */ |
| 5952 | case LYXP_AXIS_ANCESTOR: |
| 5953 | if (*iter_type == LYXP_NODE_ELEM) { |
| 5954 | /* iter parent */ |
| 5955 | next = lyd_parent(*iter); |
| 5956 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5957 | } /* else root, no ancestors */ |
| 5958 | break; |
| 5959 | |
| 5960 | case LYXP_AXIS_CHILD: |
| 5961 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5962 | |
| 5963 | /* next sibling (child) */ |
| 5964 | next = (*iter)->next; |
| 5965 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5966 | break; |
| 5967 | |
| 5968 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5969 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5970 | /* fake first descendant, we returned self before */ |
| 5971 | *iter = NULL; |
| 5972 | *iter_type = 0; |
| 5973 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 5974 | } /* else continue descendant */ |
| 5975 | |
| 5976 | /* fallthrough */ |
| 5977 | case LYXP_AXIS_DESCENDANT: |
| 5978 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5979 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 5980 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5981 | break; |
| 5982 | |
| 5983 | case LYXP_AXIS_FOLLOWING: |
| 5984 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5985 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 5986 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5987 | break; |
| 5988 | |
| 5989 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5990 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5991 | |
| 5992 | /* next sibling */ |
| 5993 | next = (*iter)->next; |
| 5994 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5995 | break; |
| 5996 | |
| 5997 | case LYXP_AXIS_PARENT: |
| 5998 | case LYXP_AXIS_SELF: |
| 5999 | /* parent/self was returned before */ |
| 6000 | break; |
| 6001 | |
| 6002 | case LYXP_AXIS_PRECEDING: |
| 6003 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6004 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 6005 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6006 | break; |
| 6007 | |
| 6008 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6009 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6010 | |
| 6011 | /* previous sibling */ |
| 6012 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 6013 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6014 | break; |
| 6015 | |
| 6016 | case LYXP_AXIS_ATTRIBUTE: |
| 6017 | /* handled specially */ |
| 6018 | assert(0); |
| 6019 | LOGINT(set->ctx); |
| 6020 | break; |
| 6021 | } |
| 6022 | |
| 6023 | *iter = next; |
| 6024 | *iter_type = next_type; |
| 6025 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6026 | } |
| 6027 | |
| 6028 | /** |
| 6029 | * @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] | 6030 | * |
| 6031 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6032 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6033 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6034 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6035 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6036 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6037 | */ |
| 6038 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6039 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6040 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6041 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6042 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6043 | const struct lyd_node *iter; |
| 6044 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6045 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6046 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6047 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6048 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6049 | return LY_SUCCESS; |
| 6050 | } |
| 6051 | |
| 6052 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6053 | 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] | 6054 | return LY_EVALID; |
| 6055 | } |
| 6056 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6057 | /* init result set */ |
| 6058 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6059 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6060 | for (i = 0; i < set->used; ++i) { |
| 6061 | /* iterate over all the nodes on the axis of the node */ |
| 6062 | iter = NULL; |
| 6063 | iter_type = 0; |
| 6064 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6065 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6066 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6067 | rc = r; |
| 6068 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6069 | } else if (r) { |
| 6070 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6071 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6072 | |
| 6073 | /* check for duplicates if they are possible */ |
| 6074 | switch (axis) { |
| 6075 | case LYXP_AXIS_ANCESTOR: |
| 6076 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6077 | case LYXP_AXIS_DESCENDANT: |
| 6078 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6079 | case LYXP_AXIS_FOLLOWING: |
| 6080 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6081 | case LYXP_AXIS_PARENT: |
| 6082 | case LYXP_AXIS_PRECEDING: |
| 6083 | case LYXP_AXIS_PRECEDING_SIBLING: |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6084 | result.non_child_axis = 1; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6085 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6086 | continue; |
| 6087 | } |
| 6088 | break; |
| 6089 | case LYXP_AXIS_CHILD: |
| 6090 | case LYXP_AXIS_SELF: |
| 6091 | break; |
| 6092 | case LYXP_AXIS_ATTRIBUTE: |
| 6093 | /* handled specially */ |
| 6094 | assert(0); |
| 6095 | LOGINT(set->ctx); |
| 6096 | break; |
| 6097 | } |
| 6098 | |
| 6099 | /* matching node */ |
| 6100 | set_insert_node(&result, iter, 0, iter_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6101 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6102 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6103 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6104 | /* move result to the set */ |
| 6105 | lyxp_set_free_content(set); |
| 6106 | *set = result; |
| 6107 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6108 | |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6109 | /* sort the final set if the document order could have been broken */ |
| 6110 | if (set->non_child_axis) { |
| 6111 | set_sort(set); |
| 6112 | } else { |
| 6113 | assert(!set_sort(set)); |
| 6114 | } |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6115 | |
| 6116 | cleanup: |
| 6117 | lyxp_set_free_content(&result); |
| 6118 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6119 | } |
| 6120 | |
| 6121 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6122 | * @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] | 6123 | * |
| 6124 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6125 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6126 | * @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] | 6127 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6128 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6129 | */ |
| 6130 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6131 | 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] | 6132 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6133 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6134 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6135 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6136 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6137 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6138 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6139 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6140 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6141 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6142 | /* init result set */ |
| 6143 | set_init(&result, set); |
| 6144 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6145 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6146 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6147 | } |
| 6148 | |
| 6149 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6150 | 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] | 6151 | ret = LY_EVALID; |
| 6152 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6153 | } |
| 6154 | |
| 6155 | /* context check for all the nodes since we have the schema node */ |
| 6156 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6157 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6158 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6159 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6160 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6161 | lyxp_set_free_content(set); |
| 6162 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | /* create specific data instance if needed */ |
| 6166 | if (scnode->nodetype == LYS_LIST) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 6167 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6168 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6169 | 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] | 6170 | } |
| 6171 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6172 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6173 | siblings = NULL; |
| 6174 | |
| 6175 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6176 | assert(!set->val.nodes[i].node); |
| 6177 | |
| 6178 | /* search in all the trees */ |
| 6179 | siblings = set->tree; |
| 6180 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6181 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6182 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6183 | } |
| 6184 | |
| 6185 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6186 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6187 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6188 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6189 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6190 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6191 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6192 | |
| 6193 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6194 | 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] | 6195 | ret = LY_EINCOMPLETE; |
| 6196 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | if (sub) { |
| 6200 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6201 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6202 | } |
| 6203 | } |
| 6204 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6205 | /* move result to the set */ |
| 6206 | lyxp_set_free_content(set); |
| 6207 | *set = result; |
| 6208 | result.type = LYXP_SET_NUMBER; |
| 6209 | assert(!set_sort(set)); |
| 6210 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6211 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6212 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6213 | lyd_free_tree(inst); |
| 6214 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6215 | } |
| 6216 | |
| 6217 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6218 | * @brief Check @p node as a part of schema NameTest processing. |
| 6219 | * |
| 6220 | * @param[in] node Schema node to check. |
| 6221 | * @param[in] ctx_scnode Context node. |
| 6222 | * @param[in] set Set to read general context from. |
| 6223 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6224 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6225 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6226 | */ |
| 6227 | static LY_ERR |
| 6228 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6229 | const char *node_name, const struct lys_module *moveto_mod) |
| 6230 | { |
| 6231 | if (!moveto_mod && node_name) { |
| 6232 | switch (set->format) { |
| 6233 | case LY_VALUE_SCHEMA: |
| 6234 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6235 | /* use current module */ |
| 6236 | moveto_mod = set->cur_mod; |
| 6237 | break; |
| 6238 | case LY_VALUE_JSON: |
| 6239 | case LY_VALUE_LYB: |
| 6240 | case LY_VALUE_STR_NS: |
| 6241 | /* inherit module of the context node, if any */ |
| 6242 | if (ctx_scnode) { |
| 6243 | moveto_mod = ctx_scnode->module; |
| 6244 | } |
| 6245 | break; |
| 6246 | case LY_VALUE_CANON: |
| 6247 | case LY_VALUE_XML: |
| 6248 | /* not defined */ |
| 6249 | LOGINT(set->ctx); |
| 6250 | return LY_EINVAL; |
| 6251 | } |
| 6252 | } |
| 6253 | |
| 6254 | if (!node) { |
| 6255 | /* root will not match a specific node */ |
| 6256 | if (node_name || moveto_mod) { |
| 6257 | return LY_ENOT; |
| 6258 | } |
| 6259 | return LY_SUCCESS; |
| 6260 | } |
| 6261 | |
| 6262 | /* module check */ |
| 6263 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6264 | return LY_ENOT; |
| 6265 | } |
| 6266 | |
| 6267 | /* context check */ |
| 6268 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6269 | return LY_EINVAL; |
| 6270 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6271 | return LY_EINVAL; |
| 6272 | } |
| 6273 | |
| 6274 | /* name check */ |
| 6275 | if (node_name && (node->name != node_name)) { |
| 6276 | return LY_ENOT; |
| 6277 | } |
| 6278 | |
| 6279 | /* match */ |
| 6280 | return LY_SUCCESS; |
| 6281 | } |
| 6282 | |
| 6283 | /** |
| 6284 | * @brief Get the next node in a forward schema node DFS. |
| 6285 | * |
| 6286 | * @param[in] iter Last returned node. |
| 6287 | * @param[in] stop Node to stop the search on and not return. |
| 6288 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6289 | * @return Next node, NULL if there are no more. |
| 6290 | */ |
| 6291 | static const struct lysc_node * |
| 6292 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6293 | { |
| 6294 | const struct lysc_node *next = NULL; |
| 6295 | |
| 6296 | next = lysc_node_child(iter); |
| 6297 | if (!next) { |
| 6298 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6299 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6300 | /* we are done, no next element to process */ |
| 6301 | return NULL; |
| 6302 | } |
| 6303 | |
| 6304 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6305 | } |
| 6306 | while (!next && iter) { |
| 6307 | /* parent is already processed, go to its sibling */ |
| 6308 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6309 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6310 | /* we are done, no next element to process */ |
| 6311 | return NULL; |
| 6312 | } |
| 6313 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6314 | } |
| 6315 | |
| 6316 | return next; |
| 6317 | } |
| 6318 | |
| 6319 | /** |
| 6320 | * @brief Consider schema node based on its in_ctx enum value. |
| 6321 | * |
| 6322 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6323 | * @param[in] axis Axis to use. |
| 6324 | * @return LY_SUCCESS on success. |
| 6325 | * @return LY_ENOT if the node should not be returned. |
| 6326 | */ |
| 6327 | static LY_ERR |
| 6328 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6329 | { |
| 6330 | switch (axis) { |
| 6331 | case LYXP_AXIS_SELF: |
| 6332 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6333 | /* additionally put the start node into context */ |
| 6334 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6335 | return LY_SUCCESS; |
| 6336 | } |
| 6337 | break; |
| 6338 | case LYXP_AXIS_PARENT: |
| 6339 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6340 | case LYXP_AXIS_ANCESTOR: |
| 6341 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6342 | case LYXP_AXIS_DESCENDANT: |
| 6343 | case LYXP_AXIS_FOLLOWING: |
| 6344 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6345 | case LYXP_AXIS_PRECEDING: |
| 6346 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6347 | case LYXP_AXIS_CHILD: |
| 6348 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6349 | /* remember that context node was used */ |
| 6350 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6351 | return LY_SUCCESS; |
| 6352 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6353 | /* traversed */ |
| 6354 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6355 | return LY_SUCCESS; |
| 6356 | } |
| 6357 | break; |
| 6358 | case LYXP_AXIS_ATTRIBUTE: |
| 6359 | /* unreachable */ |
| 6360 | assert(0); |
| 6361 | LOGINT(NULL); |
| 6362 | break; |
| 6363 | } |
| 6364 | |
| 6365 | return LY_ENOT; |
| 6366 | } |
| 6367 | |
| 6368 | /** |
| 6369 | * @brief Get previous sibling for a schema node. |
| 6370 | * |
| 6371 | * @param[in] scnode Schema node. |
| 6372 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6373 | * @return Previous sibling, NULL if none. |
| 6374 | */ |
| 6375 | static const struct lysc_node * |
| 6376 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6377 | { |
| 6378 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6379 | |
| 6380 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6381 | if (next == scnode) { |
| 6382 | break; |
| 6383 | } |
| 6384 | |
| 6385 | prev = next; |
| 6386 | } |
| 6387 | |
| 6388 | return prev; |
| 6389 | } |
| 6390 | |
| 6391 | /** |
| 6392 | * @brief Get the first schema node on an axis for a context node. |
| 6393 | * |
| 6394 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6395 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6396 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6397 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6398 | * @param[in] scnode Context node. |
| 6399 | * @param[in] node_type Type of @p scnode. |
| 6400 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6401 | * @param[in] axis Axis to use. |
| 6402 | * @param[in] set XPath set with the general context. |
| 6403 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6404 | * @return LY_SUCCESS on success. |
| 6405 | * @return LY_ENOTFOUND if no next node found. |
| 6406 | */ |
| 6407 | static LY_ERR |
| 6408 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6409 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6410 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6411 | { |
| 6412 | const struct lysc_node *next = NULL; |
| 6413 | enum lyxp_node_type next_type = 0; |
| 6414 | |
| 6415 | assert(!*iter); |
| 6416 | assert(!*iter_type); |
| 6417 | |
| 6418 | *iter_mod = NULL; |
| 6419 | *iter_mod_idx = 0; |
| 6420 | |
| 6421 | switch (axis) { |
| 6422 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6423 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6424 | case LYXP_AXIS_SELF: |
| 6425 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6426 | /* just return the node */ |
| 6427 | next = scnode; |
| 6428 | next_type = node_type; |
| 6429 | } |
| 6430 | break; |
| 6431 | |
| 6432 | case LYXP_AXIS_ANCESTOR: |
| 6433 | case LYXP_AXIS_PARENT: |
| 6434 | if (node_type == LYXP_NODE_ELEM) { |
| 6435 | next = lysc_data_parent(scnode); |
| 6436 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6437 | } /* else no parent */ |
| 6438 | break; |
| 6439 | |
| 6440 | case LYXP_AXIS_DESCENDANT: |
| 6441 | case LYXP_AXIS_CHILD: |
| 6442 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6443 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6444 | * it can be in a top-level augment */ |
| 6445 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6446 | /* module may not be implemented or not compiled yet */ |
| 6447 | if (!(*iter_mod)->compiled) { |
| 6448 | continue; |
| 6449 | } |
| 6450 | |
| 6451 | /* get next node */ |
| 6452 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6453 | next_type = LYXP_NODE_ELEM; |
| 6454 | break; |
| 6455 | } |
| 6456 | } |
| 6457 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6458 | /* get next node */ |
| 6459 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6460 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6461 | } |
| 6462 | break; |
| 6463 | |
| 6464 | case LYXP_AXIS_FOLLOWING: |
| 6465 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6466 | if (node_type == LYXP_NODE_ELEM) { |
| 6467 | /* first next sibling */ |
| 6468 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6469 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6470 | } /* else no sibling */ |
| 6471 | break; |
| 6472 | |
| 6473 | case LYXP_AXIS_PRECEDING: |
| 6474 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6475 | if (node_type == LYXP_NODE_ELEM) { |
| 6476 | /* first parent sibling */ |
| 6477 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6478 | if (next == scnode) { |
| 6479 | /* no preceding sibling */ |
| 6480 | next = NULL; |
| 6481 | } |
| 6482 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6483 | } /* else no sibling */ |
| 6484 | break; |
| 6485 | |
| 6486 | case LYXP_AXIS_ATTRIBUTE: |
| 6487 | /* unreachable */ |
| 6488 | assert(0); |
| 6489 | LOGINT(set->ctx); |
| 6490 | break; |
| 6491 | } |
| 6492 | |
| 6493 | *iter = next; |
| 6494 | *iter_type = next_type; |
| 6495 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6496 | } |
| 6497 | |
| 6498 | /** |
| 6499 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6500 | * |
| 6501 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6502 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6503 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6504 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6505 | * @param[in] scnode Context node. |
| 6506 | * @param[in] node_type Type of @p scnode. |
| 6507 | * @param[in] axis Axis to use. |
| 6508 | * @param[in] set XPath set with the general context. |
| 6509 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6510 | * @return LY_SUCCESS on success. |
| 6511 | * @return LY_ENOTFOUND if no next node found. |
| 6512 | */ |
| 6513 | static LY_ERR |
| 6514 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6515 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6516 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6517 | { |
| 6518 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6519 | enum lyxp_node_type next_type = 0; |
| 6520 | |
| 6521 | if (!*iter_type) { |
| 6522 | /* first returned node */ |
| 6523 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6524 | getnext_opts); |
| 6525 | } |
| 6526 | |
| 6527 | switch (axis) { |
| 6528 | case LYXP_AXIS_PARENT: |
| 6529 | case LYXP_AXIS_SELF: |
| 6530 | /* parent/self was returned before */ |
| 6531 | break; |
| 6532 | |
| 6533 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6534 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6535 | /* fake first ancestor, we returned self before */ |
| 6536 | *iter = NULL; |
| 6537 | *iter_type = 0; |
| 6538 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6539 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
| 6540 | } /* else continue ancestor */ |
| 6541 | |
| 6542 | /* fallthrough */ |
| 6543 | case LYXP_AXIS_ANCESTOR: |
| 6544 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6545 | next = lysc_data_parent(*iter); |
| 6546 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6547 | } /* else no ancestor */ |
| 6548 | break; |
| 6549 | |
| 6550 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6551 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6552 | /* fake first descendant, we returned self before */ |
| 6553 | *iter = NULL; |
| 6554 | *iter_type = 0; |
| 6555 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6556 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6557 | } /* else DFS until context node */ |
| 6558 | dfs_stop = scnode; |
| 6559 | |
| 6560 | /* fallthrough */ |
| 6561 | case LYXP_AXIS_DESCENDANT: |
| 6562 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6563 | /* DFS until the context node */ |
| 6564 | dfs_stop = scnode; |
| 6565 | } |
| 6566 | |
| 6567 | /* fallthrough */ |
| 6568 | case LYXP_AXIS_PRECEDING: |
| 6569 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6570 | /* DFS until the previous sibling */ |
| 6571 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6572 | assert(dfs_stop); |
| 6573 | |
| 6574 | if (*iter == dfs_stop) { |
| 6575 | /* we are done */ |
| 6576 | break; |
| 6577 | } |
| 6578 | } |
| 6579 | |
| 6580 | /* fallthrough */ |
| 6581 | case LYXP_AXIS_FOLLOWING: |
| 6582 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6583 | /* DFS through the whole module */ |
| 6584 | dfs_stop = NULL; |
| 6585 | } |
| 6586 | |
| 6587 | /* nested nodes */ |
| 6588 | assert(*iter); |
| 6589 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6590 | if (next) { |
| 6591 | next_type = LYXP_NODE_ELEM; |
| 6592 | break; |
| 6593 | } /* else get next top-level node just like a child */ |
| 6594 | |
| 6595 | /* fallthrough */ |
| 6596 | case LYXP_AXIS_CHILD: |
| 6597 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6598 | if (!*iter_mod) { |
| 6599 | /* nodes from a single module */ |
| 6600 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6601 | next_type = LYXP_NODE_ELEM; |
| 6602 | break; |
| 6603 | } |
| 6604 | |
| 6605 | assert(scnode); |
Michal Vasko | a353cce | 2022-11-14 10:09:55 +0100 | [diff] [blame] | 6606 | if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6607 | /* iterating over top-level nodes, find next */ |
| 6608 | while (lysc_data_parent(*iter)) { |
| 6609 | *iter = lysc_data_parent(*iter); |
| 6610 | } |
| 6611 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6612 | next_type = LYXP_NODE_ELEM; |
| 6613 | break; |
| 6614 | } |
| 6615 | } |
| 6616 | } |
| 6617 | |
| 6618 | while (*iter_mod) { |
| 6619 | /* module top-level nodes */ |
| 6620 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6621 | next_type = LYXP_NODE_ELEM; |
| 6622 | break; |
| 6623 | } |
| 6624 | |
| 6625 | /* get next module */ |
| 6626 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6627 | /* module may not be implemented or not compiled yet */ |
| 6628 | if ((*iter_mod)->compiled) { |
| 6629 | break; |
| 6630 | } |
| 6631 | } |
| 6632 | |
| 6633 | /* new module, start over */ |
| 6634 | *iter = NULL; |
| 6635 | } |
| 6636 | break; |
| 6637 | |
| 6638 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6639 | assert(*iter); |
| 6640 | |
| 6641 | /* next parent sibling until scnode */ |
| 6642 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6643 | if (next == scnode) { |
| 6644 | /* no previous sibling */ |
| 6645 | next = NULL; |
| 6646 | } |
| 6647 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6648 | break; |
| 6649 | |
| 6650 | case LYXP_AXIS_ATTRIBUTE: |
| 6651 | /* unreachable */ |
| 6652 | assert(0); |
| 6653 | LOGINT(set->ctx); |
| 6654 | break; |
| 6655 | } |
| 6656 | |
| 6657 | *iter = next; |
| 6658 | *iter_type = next_type; |
| 6659 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6660 | } |
| 6661 | |
| 6662 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6663 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6664 | * |
| 6665 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6666 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6667 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6668 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6669 | * @param[in] options XPath options. |
| 6670 | * @return LY_ERR |
| 6671 | */ |
| 6672 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6673 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6674 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6675 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6676 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6677 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6678 | const struct lys_module *mod; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6679 | const struct lysc_node *iter; |
| 6680 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6681 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6682 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6683 | return LY_SUCCESS; |
| 6684 | } |
| 6685 | |
| 6686 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6687 | 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] | 6688 | return LY_EVALID; |
| 6689 | } |
| 6690 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6691 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6692 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6693 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6694 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6695 | } |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 6696 | if (options & LYXP_SCNODE_SCHEMAMOUNT) { |
| 6697 | getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT; |
| 6698 | } |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6699 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6700 | orig_used = set->used; |
| 6701 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6702 | /* update in_ctx first */ |
| 6703 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6704 | /* not usable, skip */ |
| 6705 | continue; |
| 6706 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6707 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6708 | iter = NULL; |
| 6709 | iter_type = 0; |
| 6710 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6711 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6712 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6713 | continue; |
| 6714 | } |
| 6715 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6716 | /* insert */ |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 6717 | LY_CHECK_RET(set_scnode_insert_node(set, iter, iter_type, axis, &idx)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6718 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6719 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6720 | if ((idx < orig_used) && (idx > i)) { |
| 6721 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6722 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6723 | } |
| 6724 | } |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 6725 | |
| 6726 | if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) && |
| 6727 | (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode, |
| 6728 | moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) { |
| 6729 | /* there is a matching node from an extension, use it */ |
| 6730 | LY_CHECK_RET(set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx)); |
| 6731 | if ((idx < orig_used) && (idx > i)) { |
| 6732 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6733 | temp_ctx = 1; |
| 6734 | } |
| 6735 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6736 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6737 | |
| 6738 | /* correct temporary in_ctx values */ |
| 6739 | if (temp_ctx) { |
| 6740 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6741 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6742 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6743 | } |
| 6744 | } |
| 6745 | } |
| 6746 | |
| 6747 | return LY_SUCCESS; |
| 6748 | } |
| 6749 | |
| 6750 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6751 | * @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] | 6752 | * Context position aware. |
| 6753 | * |
| 6754 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6755 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6756 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6757 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6758 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6759 | */ |
| 6760 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6761 | 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] | 6762 | { |
| 6763 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6764 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6765 | struct lyxp_set ret_set; |
| 6766 | LY_ERR rc; |
| 6767 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6768 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6769 | return LY_SUCCESS; |
| 6770 | } |
| 6771 | |
| 6772 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6773 | 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] | 6774 | return LY_EVALID; |
| 6775 | } |
| 6776 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6777 | /* 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] | 6778 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6779 | LY_CHECK_RET(rc); |
| 6780 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6781 | /* 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] | 6782 | set_init(&ret_set, set); |
| 6783 | for (i = 0; i < set->used; ++i) { |
| 6784 | |
| 6785 | /* TREE DFS */ |
| 6786 | start = set->val.nodes[i].node; |
| 6787 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6788 | 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] | 6789 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6790 | /* add matching node into result set */ |
| 6791 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6792 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6793 | /* the node is a duplicate, we'll process it later in the set */ |
| 6794 | goto skip_children; |
| 6795 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6796 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6797 | return rc; |
| 6798 | } else if (rc == LY_EINVAL) { |
| 6799 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6800 | } |
| 6801 | |
| 6802 | /* TREE DFS NEXT ELEM */ |
| 6803 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6804 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6805 | if (!next) { |
| 6806 | skip_children: |
| 6807 | /* no children, so try siblings, but only if it's not the start, |
| 6808 | * that is considered to be the root and it's siblings are not traversed */ |
| 6809 | if (elem != start) { |
| 6810 | next = elem->next; |
| 6811 | } else { |
| 6812 | break; |
| 6813 | } |
| 6814 | } |
| 6815 | while (!next) { |
| 6816 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6817 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6818 | /* we are done, no next element to process */ |
| 6819 | break; |
| 6820 | } |
| 6821 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6822 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6823 | next = elem->next; |
| 6824 | } |
| 6825 | } |
| 6826 | } |
| 6827 | |
| 6828 | /* make the temporary set the current one */ |
| 6829 | ret_set.ctx_pos = set->ctx_pos; |
| 6830 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6831 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6832 | memcpy(set, &ret_set, sizeof *set); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6833 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6834 | |
| 6835 | return LY_SUCCESS; |
| 6836 | } |
| 6837 | |
| 6838 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6839 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6840 | * Result is LYXP_SET_NODE_SET. |
| 6841 | * |
| 6842 | * @param[in] set Set to use. |
| 6843 | * @param[in] start Start node whose subtree to add. |
| 6844 | * @param[in] start_idx Index of @p start in @p set. |
| 6845 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6846 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6847 | * @param[in] options XPath options. |
| 6848 | * @return LY_ERR value. |
| 6849 | */ |
| 6850 | static LY_ERR |
| 6851 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6852 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6853 | { |
| 6854 | const struct lysc_node *next, *elem; |
| 6855 | uint32_t idx; |
| 6856 | LY_ERR rc; |
| 6857 | |
| 6858 | /* TREE DFS */ |
| 6859 | for (elem = next = start; elem; elem = next) { |
| 6860 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6861 | /* schema-only nodes, skip root */ |
| 6862 | goto next_iter; |
| 6863 | } |
| 6864 | |
| 6865 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6866 | if (!rc) { |
| 6867 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6868 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6869 | if (idx > start_idx) { |
| 6870 | /* we will process it later in the set */ |
| 6871 | goto skip_children; |
| 6872 | } |
| 6873 | } else { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 6874 | LY_CHECK_RET(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] | 6875 | } |
| 6876 | } else if (rc == LY_EINVAL) { |
| 6877 | goto skip_children; |
| 6878 | } |
| 6879 | |
| 6880 | next_iter: |
| 6881 | /* TREE DFS NEXT ELEM */ |
| 6882 | /* select element for the next run - children first */ |
| 6883 | next = lysc_node_child(elem); |
| 6884 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6885 | next = next->next; |
| 6886 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6887 | next = next->next; |
| 6888 | } |
| 6889 | if (!next) { |
| 6890 | skip_children: |
| 6891 | /* no children, so try siblings, but only if it's not the start, |
| 6892 | * that is considered to be the root and it's siblings are not traversed */ |
| 6893 | if (elem != start) { |
| 6894 | next = elem->next; |
| 6895 | } else { |
| 6896 | break; |
| 6897 | } |
| 6898 | } |
| 6899 | while (!next) { |
| 6900 | /* no siblings, go back through the parents */ |
| 6901 | if (elem->parent == start) { |
| 6902 | /* we are done, no next element to process */ |
| 6903 | break; |
| 6904 | } |
| 6905 | /* parent is already processed, go to its sibling */ |
| 6906 | elem = elem->parent; |
| 6907 | next = elem->next; |
| 6908 | } |
| 6909 | } |
| 6910 | |
| 6911 | return LY_SUCCESS; |
| 6912 | } |
| 6913 | |
| 6914 | /** |
| 6915 | * @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] | 6916 | * |
| 6917 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6918 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6919 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6920 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6921 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6922 | */ |
| 6923 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6924 | 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] | 6925 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6926 | uint32_t i, orig_used, mod_idx; |
| 6927 | const struct lys_module *mod; |
| 6928 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6929 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6930 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6931 | return LY_SUCCESS; |
| 6932 | } |
| 6933 | |
| 6934 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6935 | 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] | 6936 | return LY_EVALID; |
| 6937 | } |
| 6938 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6939 | orig_used = set->used; |
| 6940 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6941 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6942 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6943 | continue; |
| 6944 | } |
| 6945 | |
| 6946 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6947 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6948 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6949 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6950 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6951 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6952 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6953 | /* traverse all top-level nodes in all the modules */ |
| 6954 | mod_idx = 0; |
| 6955 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6956 | /* module may not be implemented or not compiled yet */ |
| 6957 | if (!mod->compiled) { |
| 6958 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6959 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6960 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6961 | root = NULL; |
| 6962 | /* no getnext opts needed */ |
| 6963 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 6964 | 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] | 6965 | } |
| 6966 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6967 | |
| 6968 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6969 | /* add all the descendants recursively */ |
| 6970 | 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] | 6971 | } |
| 6972 | } |
| 6973 | |
| 6974 | return LY_SUCCESS; |
| 6975 | } |
| 6976 | |
| 6977 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6978 | * @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] | 6979 | * Indirectly context position aware. |
| 6980 | * |
| 6981 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6982 | * @param[in] mod Matching metadata module, NULL for any. |
| 6983 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6984 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6985 | * @return LY_ERR |
| 6986 | */ |
| 6987 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6988 | 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] | 6989 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6990 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6991 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6992 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6993 | return LY_SUCCESS; |
| 6994 | } |
| 6995 | |
| 6996 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6997 | 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] | 6998 | return LY_EVALID; |
| 6999 | } |
| 7000 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7001 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7002 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7003 | |
| 7004 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 7005 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7006 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7007 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7008 | |
| 7009 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7010 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7011 | continue; |
| 7012 | } |
| 7013 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7014 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7015 | /* match */ |
| 7016 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7017 | set->val.meta[i].meta = sub; |
| 7018 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7019 | /* pos does not change */ |
| 7020 | replaced = 1; |
| 7021 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7022 | 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] | 7023 | } |
| 7024 | ++i; |
| 7025 | } |
| 7026 | } |
| 7027 | } |
| 7028 | |
| 7029 | if (!replaced) { |
| 7030 | /* no match */ |
| 7031 | set_remove_node(set, i); |
| 7032 | } |
| 7033 | } |
| 7034 | |
| 7035 | return LY_SUCCESS; |
| 7036 | } |
| 7037 | |
| 7038 | /** |
| 7039 | * @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] | 7040 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7041 | * |
| 7042 | * @param[in,out] set1 Set to use for the result. |
| 7043 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7044 | * @return LY_ERR |
| 7045 | */ |
| 7046 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7047 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7048 | { |
| 7049 | LY_ERR rc; |
| 7050 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7051 | 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] | 7052 | 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] | 7053 | return LY_EVALID; |
| 7054 | } |
| 7055 | |
| 7056 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7057 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7058 | return LY_SUCCESS; |
| 7059 | } |
| 7060 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7061 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 7062 | /* release hidden allocated data (lyxp_set.size) */ |
| 7063 | lyxp_set_free_content(set1); |
| 7064 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7065 | memcpy(set1, set2, sizeof *set1); |
| 7066 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7067 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7068 | return LY_SUCCESS; |
| 7069 | } |
| 7070 | |
| 7071 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7072 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7073 | |
| 7074 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7075 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7076 | LY_CHECK_RET(rc); |
| 7077 | |
| 7078 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7079 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7080 | |
| 7081 | return LY_SUCCESS; |
| 7082 | } |
| 7083 | |
| 7084 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7085 | * @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] | 7086 | * Context position aware. |
| 7087 | * |
| 7088 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7089 | * @param[in] mod Matching metadata module, NULL for any. |
| 7090 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7091 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7092 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7093 | */ |
| 7094 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7095 | 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] | 7096 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7097 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7098 | struct lyxp_set *set_all_desc = NULL; |
| 7099 | LY_ERR rc; |
| 7100 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7101 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7102 | return LY_SUCCESS; |
| 7103 | } |
| 7104 | |
| 7105 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7106 | 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] | 7107 | return LY_EVALID; |
| 7108 | } |
| 7109 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7110 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7111 | * but it likely won't be used much, so it's a waste of time */ |
| 7112 | /* copy the context */ |
| 7113 | set_all_desc = set_copy(set); |
| 7114 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7115 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7116 | if (rc != LY_SUCCESS) { |
| 7117 | lyxp_set_free(set_all_desc); |
| 7118 | return rc; |
| 7119 | } |
| 7120 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7121 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7122 | if (rc != LY_SUCCESS) { |
| 7123 | lyxp_set_free(set_all_desc); |
| 7124 | return rc; |
| 7125 | } |
| 7126 | lyxp_set_free(set_all_desc); |
| 7127 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7128 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7129 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7130 | |
| 7131 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7132 | * we have all attributes qualified in lyd tree */ |
| 7133 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7134 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7135 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7136 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7137 | continue; |
| 7138 | } |
| 7139 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7140 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7141 | /* match */ |
| 7142 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7143 | set->val.meta[i].meta = sub; |
| 7144 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7145 | /* pos does not change */ |
| 7146 | replaced = 1; |
| 7147 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7148 | 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] | 7149 | } |
| 7150 | ++i; |
| 7151 | } |
| 7152 | } |
| 7153 | } |
| 7154 | |
| 7155 | if (!replaced) { |
| 7156 | /* no match */ |
| 7157 | set_remove_node(set, i); |
| 7158 | } |
| 7159 | } |
| 7160 | |
| 7161 | return LY_SUCCESS; |
| 7162 | } |
| 7163 | |
| 7164 | /** |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7165 | * @brief Move context @p set1 single item to the result of a comparison. |
| 7166 | * |
| 7167 | * @param[in] set1 First set with the item to compare. |
| 7168 | * @param[in] idx1 Index of the item in @p set1. |
| 7169 | * @param[in] set2 Second set. |
| 7170 | * @param[in] op Comparison operator to process. |
| 7171 | * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`. |
| 7172 | * @param[out] result Result of the comparison. |
| 7173 | * @return LY_ERR value. |
| 7174 | */ |
| 7175 | static LY_ERR |
| 7176 | moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op, |
| 7177 | ly_bool switch_operands, ly_bool *result) |
| 7178 | { |
| 7179 | struct lyxp_set tmp1 = {0}; |
| 7180 | LY_ERR rc = LY_SUCCESS; |
| 7181 | |
| 7182 | assert(set1->type == LYXP_SET_NODE_SET); |
| 7183 | |
| 7184 | /* cast set1 */ |
| 7185 | switch (set2->type) { |
| 7186 | case LYXP_SET_NUMBER: |
| 7187 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); |
| 7188 | break; |
| 7189 | case LYXP_SET_BOOLEAN: |
| 7190 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); |
| 7191 | break; |
| 7192 | default: |
| 7193 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); |
| 7194 | break; |
| 7195 | } |
| 7196 | LY_CHECK_GOTO(rc, cleanup); |
| 7197 | |
| 7198 | /* canonize set2 */ |
| 7199 | LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup); |
| 7200 | |
| 7201 | /* compare recursively and store the result */ |
| 7202 | if (switch_operands) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7203 | LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7204 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7205 | LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7206 | } |
| 7207 | |
| 7208 | cleanup: |
| 7209 | lyxp_set_free_content(&tmp1); |
| 7210 | return rc; |
| 7211 | } |
| 7212 | |
| 7213 | /** |
| 7214 | * @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] | 7215 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7216 | * |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7217 | * @param[in] set1 Set acting as the first operand for @p op. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7218 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7219 | * @param[in] op Comparison operator to process. |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7220 | * @param[out] result Result of the comparison. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7221 | * @return LY_ERR |
| 7222 | */ |
| 7223 | static LY_ERR |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7224 | 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] | 7225 | { |
| 7226 | /* |
| 7227 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7228 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7229 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7230 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7231 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7232 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7233 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7234 | * |
| 7235 | * '=' or '!=' |
| 7236 | * BOOLEAN + BOOLEAN |
| 7237 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7238 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7239 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7240 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7241 | * NUMBER + NUMBER |
| 7242 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7243 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7244 | * STRING + STRING |
| 7245 | * |
| 7246 | * '<=', '<', '>=', '>' |
| 7247 | * NUMBER + NUMBER |
| 7248 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7249 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7250 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7251 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7252 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7253 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7254 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7255 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7256 | */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7257 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7258 | LY_ERR rc; |
| 7259 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7260 | /* iterative evaluation with node-sets */ |
| 7261 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7262 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7263 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7264 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7265 | 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] | 7266 | |
| 7267 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7268 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7269 | return LY_SUCCESS; |
| 7270 | } |
| 7271 | } |
| 7272 | } else { |
| 7273 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7274 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7275 | 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] | 7276 | |
| 7277 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7278 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7279 | return LY_SUCCESS; |
| 7280 | } |
| 7281 | } |
| 7282 | } |
| 7283 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7284 | /* false for all the nodes */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7285 | *result = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7286 | return LY_SUCCESS; |
| 7287 | } |
| 7288 | |
| 7289 | /* first convert properly */ |
| 7290 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7291 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7292 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7293 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7294 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7295 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7296 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7297 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7298 | LY_CHECK_RET(rc); |
| 7299 | } /* else we have 2 strings */ |
| 7300 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7301 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7302 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7303 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7304 | LY_CHECK_RET(rc); |
| 7305 | } |
| 7306 | |
| 7307 | assert(set1->type == set2->type); |
| 7308 | |
| 7309 | /* compute result */ |
| 7310 | if (op[0] == '=') { |
| 7311 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7312 | *result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7313 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7314 | *result = (set1->val.num == set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7315 | } else { |
| 7316 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7317 | *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7318 | } |
| 7319 | } else if (op[0] == '!') { |
| 7320 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7321 | *result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7322 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7323 | *result = (set1->val.num != set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7324 | } else { |
| 7325 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7326 | *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7327 | } |
| 7328 | } else { |
| 7329 | assert(set1->type == LYXP_SET_NUMBER); |
| 7330 | if (op[0] == '<') { |
| 7331 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7332 | *result = (set1->val.num <= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7333 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7334 | *result = (set1->val.num < set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7335 | } |
| 7336 | } else { |
| 7337 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7338 | *result = (set1->val.num >= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7339 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7340 | *result = (set1->val.num > set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7341 | } |
| 7342 | } |
| 7343 | } |
| 7344 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7345 | return LY_SUCCESS; |
| 7346 | } |
| 7347 | |
| 7348 | /** |
| 7349 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7350 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7351 | * |
| 7352 | * @param[in,out] set1 Set to use for the result. |
| 7353 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7354 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7355 | * @return LY_ERR |
| 7356 | */ |
| 7357 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7358 | 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] | 7359 | { |
| 7360 | LY_ERR rc; |
| 7361 | |
| 7362 | /* unary '-' */ |
| 7363 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7364 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7365 | LY_CHECK_RET(rc); |
| 7366 | set1->val.num *= -1; |
| 7367 | lyxp_set_free(set2); |
| 7368 | return LY_SUCCESS; |
| 7369 | } |
| 7370 | |
| 7371 | assert(set1 && set2); |
| 7372 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7373 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7374 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7375 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7376 | LY_CHECK_RET(rc); |
| 7377 | |
| 7378 | switch (op[0]) { |
| 7379 | /* '+' */ |
| 7380 | case '+': |
| 7381 | set1->val.num += set2->val.num; |
| 7382 | break; |
| 7383 | |
| 7384 | /* '-' */ |
| 7385 | case '-': |
| 7386 | set1->val.num -= set2->val.num; |
| 7387 | break; |
| 7388 | |
| 7389 | /* '*' */ |
| 7390 | case '*': |
| 7391 | set1->val.num *= set2->val.num; |
| 7392 | break; |
| 7393 | |
| 7394 | /* 'div' */ |
| 7395 | case 'd': |
| 7396 | set1->val.num /= set2->val.num; |
| 7397 | break; |
| 7398 | |
| 7399 | /* 'mod' */ |
| 7400 | case 'm': |
| 7401 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7402 | break; |
| 7403 | |
| 7404 | default: |
| 7405 | LOGINT_RET(set1->ctx); |
| 7406 | } |
| 7407 | |
| 7408 | return LY_SUCCESS; |
| 7409 | } |
| 7410 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7411 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7412 | * @brief Evaluate Predicate. Logs directly on error. |
| 7413 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7414 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7415 | * |
| 7416 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7417 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7418 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7419 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7420 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7421 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7422 | */ |
| 7423 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7424 | 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] | 7425 | { |
| 7426 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7427 | uint32_t i, orig_exp, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7428 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7429 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7430 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7431 | |
| 7432 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7433 | 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] | 7434 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7435 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7436 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7437 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7438 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7439 | 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] | 7440 | LY_CHECK_RET(rc); |
| 7441 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7442 | /* 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] | 7443 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7444 | |
| 7445 | /* empty set, nothing to evaluate */ |
| 7446 | if (!set->used) { |
| 7447 | goto only_parse; |
| 7448 | } |
| 7449 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7450 | /* decide forward or reverse axis */ |
| 7451 | switch (axis) { |
| 7452 | case LYXP_AXIS_ANCESTOR: |
| 7453 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7454 | case LYXP_AXIS_PRECEDING: |
| 7455 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7456 | reverse_axis = 1; |
| 7457 | break; |
| 7458 | case LYXP_AXIS_DESCENDANT: |
| 7459 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7460 | case LYXP_AXIS_FOLLOWING: |
| 7461 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7462 | case LYXP_AXIS_PARENT: |
| 7463 | case LYXP_AXIS_CHILD: |
| 7464 | case LYXP_AXIS_SELF: |
| 7465 | case LYXP_AXIS_ATTRIBUTE: |
| 7466 | reverse_axis = 0; |
| 7467 | break; |
| 7468 | } |
| 7469 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7470 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7471 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7472 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7473 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7474 | set_init(&set2, set); |
| 7475 | 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] | 7476 | |
| 7477 | /* remember the node context position for position() and context size for last() */ |
| 7478 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7479 | |
| 7480 | set2.ctx_pos = orig_pos; |
| 7481 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7482 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7483 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7484 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7485 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7486 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7487 | return rc; |
| 7488 | } |
| 7489 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7490 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7491 | if (set2.type == LYXP_SET_NUMBER) { |
| 7492 | if ((long long)set2.val.num == orig_pos) { |
| 7493 | set2.val.num = 1; |
| 7494 | } else { |
| 7495 | set2.val.num = 0; |
| 7496 | } |
| 7497 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7498 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7499 | |
| 7500 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7501 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7502 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7503 | } |
| 7504 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7505 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7506 | |
| 7507 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7508 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7509 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7510 | /* there is a currently-valid node */ |
| 7511 | break; |
| 7512 | } |
| 7513 | } |
| 7514 | /* empty set, nothing to evaluate */ |
| 7515 | if (i == set->used) { |
| 7516 | goto only_parse; |
| 7517 | } |
| 7518 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7519 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7520 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7521 | /* set special in_ctx to all the valid snodes */ |
| 7522 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7523 | |
| 7524 | /* use the valid snodes one-by-one */ |
| 7525 | for (i = 0; i < set->used; ++i) { |
| 7526 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7527 | continue; |
| 7528 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7529 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7530 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7531 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7532 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7533 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7534 | LY_CHECK_RET(rc); |
| 7535 | |
| 7536 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7537 | } |
| 7538 | |
| 7539 | /* restore the state as it was before the predicate */ |
| 7540 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7541 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7542 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7543 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7544 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | } |
| 7546 | } |
| 7547 | |
| 7548 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7549 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7550 | set_fill_set(&set2, set); |
| 7551 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7552 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7553 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7554 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7555 | return rc; |
| 7556 | } |
| 7557 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7558 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7559 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7560 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7561 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7562 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7563 | } |
| 7564 | |
| 7565 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7566 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7567 | 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] | 7568 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7569 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7570 | |
| 7571 | return LY_SUCCESS; |
| 7572 | } |
| 7573 | |
| 7574 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7575 | * @brief Evaluate Literal. Logs directly on error. |
| 7576 | * |
| 7577 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7578 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7579 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7580 | */ |
| 7581 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7582 | 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] | 7583 | { |
| 7584 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7586 | set_fill_string(set, "", 0); |
| 7587 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7588 | 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] | 7589 | } |
| 7590 | } |
| 7591 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7592 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7593 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7594 | } |
| 7595 | |
| 7596 | /** |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7597 | * @brief Check that a nametest in a predicate matches a key node. |
| 7598 | * |
| 7599 | * @param[in] nametest Nametest to check. |
| 7600 | * @param[in] len Length of @p nametest. |
| 7601 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7602 | * @param[in] set Context set. |
| 7603 | * @param[in] key Expected key node. |
| 7604 | * @return LY_SUCCESS on success, |
| 7605 | * @return LY_ENOT if a predicate could not be compiled. |
| 7606 | * @return LY_ERR on any error. |
| 7607 | */ |
| 7608 | static LY_ERR |
| 7609 | eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode, |
| 7610 | const struct lyxp_set *set, const struct lysc_node *key) |
| 7611 | { |
| 7612 | const struct lys_module *mod; |
| 7613 | |
| 7614 | /* prefix (module) */ |
| 7615 | LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod)); |
| 7616 | if (mod != key->module) { |
| 7617 | return LY_ENOT; |
| 7618 | } |
| 7619 | |
| 7620 | /* node name */ |
| 7621 | if (ly_strncmp(key->name, nametest, len)) { |
| 7622 | return LY_ENOT; |
| 7623 | } |
| 7624 | |
| 7625 | return LY_SUCCESS; |
| 7626 | } |
| 7627 | |
| 7628 | /** |
| 7629 | * @brief Append a simple predicate for the node. |
| 7630 | * |
| 7631 | * @param[in] exp Full parsed XPath expression. |
| 7632 | * @param[in] tok_idx Predicate start index in @p exp. |
| 7633 | * @param[in] end_tok_idx Predicate end index in @p exp. |
| 7634 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7635 | * @param[in] set Context set. |
| 7636 | * @param[in] pred_node Node with the value referenced in the predicate. |
| 7637 | * @param[in,out] pred Predicate to append to. |
| 7638 | * @param[in,out] pred_len Length of @p pred, is updated. |
| 7639 | * @return LY_SUCCESS on success, |
| 7640 | * @return LY_ENOT if a predicate could not be compiled. |
| 7641 | * @return LY_ERR on any error. |
| 7642 | */ |
| 7643 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7644 | 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] | 7645 | const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred, |
| 7646 | uint32_t *pred_len) |
| 7647 | { |
| 7648 | LY_ERR rc = LY_SUCCESS; |
| 7649 | uint32_t i; |
| 7650 | const struct lyd_node *siblings; |
| 7651 | struct lyd_node *ctx_node; |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7652 | const struct lysc_node *sparent, *cur_scnode; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7653 | struct lyxp_expr *val_exp = NULL; |
| 7654 | struct lyxp_set set2 = {0}; |
| 7655 | char quot; |
| 7656 | |
| 7657 | /* duplicate the value expression */ |
| 7658 | LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup); |
| 7659 | |
| 7660 | /* get its atoms */ |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7661 | cur_scnode = set->cur_node ? set->cur_node->schema : NULL; |
| 7662 | LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode, |
| 7663 | ctx_scnode, &set2, LYXP_SCNODE), cleanup); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7664 | |
| 7665 | /* check whether we can compile a single predicate (evaluation result value is always the same) */ |
| 7666 | for (i = 0; i < set2.used; ++i) { |
| 7667 | if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) { |
| 7668 | /* skip root and context node */ |
| 7669 | continue; |
| 7670 | } |
| 7671 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7672 | /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the |
| 7673 | * context node instance */ |
| 7674 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) { |
| 7675 | /* 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] | 7676 | rc = LY_ENOT; |
| 7677 | goto cleanup; |
| 7678 | } |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7679 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) { |
| 7680 | for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {} |
| 7681 | if (sparent) { |
| 7682 | /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */ |
| 7683 | rc = LY_ENOT; |
| 7684 | goto cleanup; |
| 7685 | } |
| 7686 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7687 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7688 | /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered, |
| 7689 | * but the current node can be safely ignored, it is always the same data instance */ |
| 7690 | 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] | 7691 | rc = LY_ENOT; |
| 7692 | goto cleanup; |
| 7693 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7694 | } |
| 7695 | |
| 7696 | /* get any data instance of the context node, we checked it makes no difference */ |
| 7697 | siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree; |
| 7698 | LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup); |
| 7699 | |
| 7700 | /* evaluate the value subexpression with the root context node */ |
| 7701 | lyxp_set_free_content(&set2); |
| 7702 | LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node, |
| 7703 | ctx_node, set->tree, NULL, &set2, 0), cleanup); |
| 7704 | |
| 7705 | /* cast it into a string */ |
| 7706 | LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup); |
| 7707 | |
| 7708 | /* append the JSON predicate */ |
| 7709 | *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3); |
| 7710 | LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7711 | quot = strchr(set2.val.str, '\'') ? '\"' : '\''; |
| 7712 | *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot); |
| 7713 | |
| 7714 | cleanup: |
| 7715 | lyxp_expr_free(set->ctx, val_exp); |
| 7716 | lyxp_set_free_content(&set2); |
| 7717 | return rc; |
| 7718 | } |
| 7719 | |
| 7720 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7721 | * @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] | 7722 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7723 | * @param[in] exp Full parsed XPath expression. |
| 7724 | * @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] | 7725 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7726 | * @param[in] set Context set. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7727 | * @param[out] predicates Parsed predicates. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7728 | * @return LY_SUCCESS on success, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7729 | * @return LY_ENOT if a predicate could not be compiled. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7730 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7731 | */ |
| 7732 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7733 | 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] | 7734 | const struct lyxp_set *set, struct ly_path_predicate **predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7735 | { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7736 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7737 | 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] | 7738 | const struct lysc_node *key; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7739 | char *pred = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7740 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7741 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7742 | assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7743 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7744 | /* turn logging off */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7745 | ly_temp_log_options(&temp_lo); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7746 | |
| 7747 | if (ctx_scnode->nodetype == LYS_LIST) { |
| 7748 | /* check for predicates "[key1=...][key2=...]..." */ |
| 7749 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7750 | /* get key count */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7751 | if (ctx_scnode->flags & LYS_KEYLESS) { |
| 7752 | rc = LY_ENOT; |
| 7753 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7754 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7755 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7756 | /* learn where the predicates end */ |
| 7757 | e_idx = *tok_idx; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7758 | 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] | 7759 | /* '[' */ |
| 7760 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7761 | rc = LY_ENOT; |
| 7762 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7763 | } |
| 7764 | ++e_idx; |
| 7765 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7766 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7767 | /* not a key */ |
| 7768 | rc = LY_ENOT; |
| 7769 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7770 | } |
| 7771 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7772 | /* check key */ |
| 7773 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx], |
| 7774 | exp->tok_len[e_idx], ctx_scnode, set, key), cleanup); |
| 7775 | |
| 7776 | ++e_idx; |
| 7777 | |
| 7778 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7779 | /* not '=' */ |
| 7780 | rc = LY_ENOT; |
| 7781 | goto cleanup; |
| 7782 | } |
| 7783 | ++e_idx; |
| 7784 | |
| 7785 | /* value start */ |
| 7786 | val_start_idx = e_idx; |
| 7787 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7788 | /* ']' */ |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7789 | nested_pred = 1; |
| 7790 | do { |
| 7791 | ++e_idx; |
| 7792 | |
| 7793 | 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] | 7794 | /* higher priority than '=' */ |
| 7795 | rc = LY_ENOT; |
| 7796 | goto cleanup; |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7797 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7798 | /* nested predicate */ |
| 7799 | ++nested_pred; |
| 7800 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7801 | /* predicate end */ |
| 7802 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7803 | } |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7804 | } while (nested_pred); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7805 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7806 | /* try to evaluate the value */ |
| 7807 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, |
| 7808 | set, key, &pred, &pred_len), cleanup); |
| 7809 | |
| 7810 | ++e_idx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7811 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7812 | } else { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7813 | /* check for predicate "[.=...]" */ |
| 7814 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7815 | /* learn just where this single predicate ends */ |
| 7816 | e_idx = *tok_idx; |
| 7817 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7818 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7819 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7820 | rc = LY_ENOT; |
| 7821 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7822 | } |
| 7823 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7824 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7825 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7826 | /* not the node value */ |
| 7827 | rc = LY_ENOT; |
| 7828 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7829 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7830 | ++e_idx; |
| 7831 | |
| 7832 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7833 | /* not '=' */ |
| 7834 | rc = LY_ENOT; |
| 7835 | goto cleanup; |
| 7836 | } |
| 7837 | ++e_idx; |
| 7838 | |
| 7839 | /* value start */ |
| 7840 | val_start_idx = e_idx; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7841 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7842 | /* ']' */ |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7843 | nested_pred = 1; |
| 7844 | do { |
| 7845 | ++e_idx; |
| 7846 | |
| 7847 | 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] | 7848 | /* higher priority than '=' */ |
| 7849 | rc = LY_ENOT; |
| 7850 | goto cleanup; |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7851 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7852 | /* nested predicate */ |
| 7853 | ++nested_pred; |
| 7854 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7855 | /* predicate end */ |
| 7856 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7857 | } |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7858 | } while (nested_pred); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7859 | |
| 7860 | /* try to evaluate the value */ |
| 7861 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set, |
| 7862 | ctx_scnode, &pred, &pred_len), cleanup); |
| 7863 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7864 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7865 | } |
| 7866 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7867 | /* parse the predicate(s) */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7868 | LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL, |
| 7869 | LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7870 | |
| 7871 | /* compile */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7872 | 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] | 7873 | &pred_idx, LY_VALUE_JSON, NULL, predicates); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7874 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7875 | |
| 7876 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7877 | *tok_idx = e_idx; |
| 7878 | |
| 7879 | cleanup: |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7880 | ly_temp_log_options(NULL); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7881 | lyxp_expr_free(set->ctx, exp2); |
| 7882 | free(pred); |
| 7883 | return rc; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7884 | } |
| 7885 | |
| 7886 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7887 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7888 | * data node(s) could be found using a single hash-based search. |
| 7889 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7890 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7891 | * @param[in] node Next context node to check. |
| 7892 | * @param[in] name Expected node name. |
| 7893 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7894 | * @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] | 7895 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7896 | * @param[in] format Prefix format. |
| 7897 | * @param[in,out] found Previously found node, is updated. |
| 7898 | * @return LY_SUCCESS on success, |
| 7899 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7900 | */ |
| 7901 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7902 | 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] | 7903 | 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] | 7904 | const struct lysc_node **found) |
| 7905 | { |
| 7906 | const struct lysc_node *scnode; |
| 7907 | const struct lys_module *mod; |
| 7908 | uint32_t idx = 0; |
| 7909 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7910 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7911 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7912 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7913 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7914 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7915 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7916 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7917 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7918 | if (!mod->implemented) { |
| 7919 | continue; |
| 7920 | } |
| 7921 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7922 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7923 | if (scnode) { |
| 7924 | /* we have found a match */ |
| 7925 | break; |
| 7926 | } |
| 7927 | } |
| 7928 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7929 | if (!scnode) { |
| 7930 | /* all modules searched */ |
| 7931 | idx = 0; |
| 7932 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7933 | } else { |
| 7934 | /* search in top-level */ |
| 7935 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7936 | } |
| 7937 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7938 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7939 | /* we must adjust the module to inherit the one from the context node */ |
| 7940 | moveto_mod = node->schema->module; |
| 7941 | } |
| 7942 | |
| 7943 | /* search in children, do not repeat the same search */ |
| 7944 | 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] | 7945 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7946 | |
| 7947 | /* additional context check */ |
| 7948 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7949 | scnode = NULL; |
| 7950 | } |
| 7951 | |
| 7952 | if (scnode) { |
| 7953 | if (*found) { |
| 7954 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7955 | * (more hash-based searches would be required, not supported) */ |
| 7956 | return LY_ENOT; |
| 7957 | } else { |
| 7958 | /* remember the found schema node and continue to make sure it can be used */ |
| 7959 | *found = scnode; |
| 7960 | } |
| 7961 | } |
| 7962 | |
| 7963 | if (idx) { |
| 7964 | /* continue searching all the following models */ |
| 7965 | goto continue_search; |
| 7966 | } |
| 7967 | |
| 7968 | return LY_SUCCESS; |
| 7969 | } |
| 7970 | |
| 7971 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7972 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7973 | * |
| 7974 | * @param[in] set XPath set. |
| 7975 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7976 | * @param[in] ncname XPath NCName being evaluated. |
| 7977 | * @param[in] ncname_len Length of @p ncname. |
| 7978 | * @param[in] expr Whole XPath expression. |
| 7979 | * @param[in] options XPath options. |
| 7980 | */ |
| 7981 | static void |
| 7982 | 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] | 7983 | uint32_t ncname_len, const char *expr, uint32_t options) |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7984 | { |
| 7985 | const char *format; |
| 7986 | char *path = NULL, *ppath = NULL; |
| 7987 | |
| 7988 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7989 | if (scparent) { |
| 7990 | /* generate path for the parent */ |
| 7991 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7992 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7993 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7994 | ppath = strdup("<root>"); |
| 7995 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7996 | ppath = strdup("<config-root>"); |
| 7997 | } |
| 7998 | } |
| 7999 | if (ppath) { |
| 8000 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 8001 | if (options & LYXP_SCNODE_ERROR) { |
| 8002 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 8003 | } else { |
| 8004 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 8005 | } |
| 8006 | } else { |
| 8007 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 8008 | if (options & LYXP_SCNODE_ERROR) { |
| 8009 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 8010 | } else { |
| 8011 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 8012 | } |
| 8013 | } |
| 8014 | free(path); |
| 8015 | free(ppath); |
| 8016 | } |
| 8017 | |
| 8018 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8019 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 8020 | * |
| 8021 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8022 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8023 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 8024 | * |
| 8025 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8026 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8027 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8028 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8029 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8030 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8031 | * @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] | 8032 | */ |
| 8033 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8034 | 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] | 8035 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8036 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8037 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8038 | const char *ncname, *ncname_dict = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8039 | uint32_t i, ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8040 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8041 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8042 | struct ly_path_predicate *predicates = NULL; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8043 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8044 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8045 | 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] | 8046 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8047 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8048 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8049 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8050 | goto moveto; |
| 8051 | } |
| 8052 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8053 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 8054 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8055 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8056 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8057 | /* all nodes will match */ |
| 8058 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8059 | } |
| 8060 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8061 | /* parse (and skip) module name */ |
| 8062 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8063 | LY_CHECK_GOTO(rc, cleanup); |
| 8064 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8065 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8066 | /* all nodes from the module will match */ |
| 8067 | goto moveto; |
| 8068 | } |
| 8069 | |
| 8070 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 8071 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8072 | /* find the matching schema node in some parent in the context */ |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8073 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8074 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 8075 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8076 | /* check failed */ |
| 8077 | scnode = NULL; |
| 8078 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8079 | } |
| 8080 | } |
| 8081 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8082 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 8083 | /* try to create the predicates */ |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8084 | 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] | 8085 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8086 | scnode = NULL; |
| 8087 | } |
| 8088 | } |
| 8089 | } |
| 8090 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8091 | if (!scnode) { |
| 8092 | /* 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] | 8093 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 8094 | 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] | 8095 | } |
| 8096 | |
| 8097 | moveto: |
| 8098 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8099 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8100 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8101 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8102 | } else { |
| 8103 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 8104 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8105 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8106 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8107 | } |
| 8108 | LY_CHECK_GOTO(rc, cleanup); |
| 8109 | } |
| 8110 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8111 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8112 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8113 | ly_bool found = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8114 | |
| 8115 | /* remember parent if there is only one, to print in the warning */ |
| 8116 | for (i = 0; i < set->used; ++i) { |
| 8117 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 8118 | if (!scparent) { |
| 8119 | /* remember the context node */ |
| 8120 | scparent = &set->val.scnodes[i]; |
| 8121 | } else { |
| 8122 | /* several context nodes, no reasonable error possible */ |
| 8123 | scparent = NULL; |
| 8124 | break; |
| 8125 | } |
| 8126 | } |
| 8127 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8128 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8129 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8130 | /* efficient evaluation that does not add all the descendants into the set */ |
| 8131 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8132 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8133 | if (all_desc) { |
| 8134 | /* "//" == "/descendant-or-self::node()/" */ |
| 8135 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8136 | LY_CHECK_GOTO(rc, cleanup); |
| 8137 | } |
| 8138 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8139 | } |
| 8140 | LY_CHECK_GOTO(rc, cleanup); |
| 8141 | |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8142 | i = set->used; |
| 8143 | do { |
| 8144 | --i; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8145 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8146 | found = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8147 | break; |
| 8148 | } |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8149 | } while (i); |
| 8150 | if (!found) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8151 | /* generate message */ |
| 8152 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 8153 | |
| 8154 | if (options & LYXP_SCNODE_ERROR) { |
| 8155 | /* error */ |
| 8156 | rc = LY_EVALID; |
| 8157 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8158 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8159 | |
| 8160 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 8161 | rc = LY_ENOT; |
| 8162 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8163 | } |
| 8164 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8165 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8166 | /* efficient evaluation */ |
| 8167 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 8168 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 8169 | /* we can find the child nodes using hashes */ |
| 8170 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8171 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8172 | if (all_desc) { |
| 8173 | /* "//" == "/descendant-or-self::node()/" */ |
| 8174 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8175 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8176 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8177 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8178 | } |
| 8179 | LY_CHECK_GOTO(rc, cleanup); |
| 8180 | } |
| 8181 | } |
| 8182 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8183 | if (scnode_skip_pred) { |
| 8184 | /* skip predicates */ |
| 8185 | options |= LYXP_SKIP_EXPR; |
| 8186 | } |
| 8187 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8188 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8189 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8190 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8191 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8192 | } |
| 8193 | |
| 8194 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8195 | if (scnode_skip_pred) { |
| 8196 | /* restore options */ |
| 8197 | options &= ~LYXP_SKIP_EXPR; |
| 8198 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8199 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8200 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8201 | ly_path_predicates_free(set->ctx, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 8202 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8203 | return rc; |
| 8204 | } |
| 8205 | |
| 8206 | /** |
| 8207 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 8208 | * |
| 8209 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8210 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8211 | * [8] NodeType ::= 'text' | 'node' |
| 8212 | * |
| 8213 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8214 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8215 | * @param[in] axis Axis to search on. |
| 8216 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8217 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8218 | * @param[in] options XPath options. |
| 8219 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8220 | */ |
| 8221 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8222 | 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] | 8223 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8224 | { |
| 8225 | LY_ERR rc; |
| 8226 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8227 | (void)all_desc; |
| 8228 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8229 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8230 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8231 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 8232 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8233 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8234 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 8235 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8236 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8237 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8238 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8239 | 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] | 8240 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8241 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8242 | |
| 8243 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8244 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8245 | 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] | 8246 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8247 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8248 | |
| 8249 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8250 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8251 | 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] | 8252 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8253 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8254 | |
| 8255 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8256 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8257 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8258 | LY_CHECK_RET(rc); |
| 8259 | } |
| 8260 | |
| 8261 | return LY_SUCCESS; |
| 8262 | } |
| 8263 | |
| 8264 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8265 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8266 | * |
| 8267 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8268 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8269 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8270 | * |
| 8271 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8272 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8273 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8274 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8275 | * @param[in] options XPath options. |
| 8276 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8277 | */ |
| 8278 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8279 | 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] | 8280 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8281 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8282 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8283 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8284 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8285 | |
| 8286 | goto step; |
| 8287 | do { |
| 8288 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8289 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8290 | all_desc = 0; |
| 8291 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8292 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8293 | all_desc = 1; |
| 8294 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8295 | 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] | 8296 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8297 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8298 | |
| 8299 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8300 | /* AxisSpecifier */ |
| 8301 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8302 | 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] | 8303 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8304 | 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] | 8305 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8306 | ++(*tok_idx); |
| 8307 | |
| 8308 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8309 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8310 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8311 | ++(*tok_idx); |
| 8312 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8313 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8314 | |
| 8315 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8316 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8317 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8318 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8319 | /* default */ |
| 8320 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8321 | } |
| 8322 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8323 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8324 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8325 | case LYXP_TOKEN_DOT: |
| 8326 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8327 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8328 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8329 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8330 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8331 | rc = LY_EVALID; |
| 8332 | goto cleanup; |
| 8333 | } |
| 8334 | |
| 8335 | if (all_desc) { |
| 8336 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8337 | LY_CHECK_GOTO(rc, cleanup); |
| 8338 | } |
| 8339 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8340 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8341 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8342 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8343 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8344 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8345 | break; |
| 8346 | |
| 8347 | case LYXP_TOKEN_DDOT: |
| 8348 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8349 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8350 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8351 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8352 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8353 | rc = LY_EVALID; |
| 8354 | goto cleanup; |
| 8355 | } |
| 8356 | |
| 8357 | if (all_desc) { |
| 8358 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8359 | LY_CHECK_GOTO(rc, cleanup); |
| 8360 | } |
| 8361 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8362 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8363 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8364 | 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] | 8365 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8366 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8367 | break; |
| 8368 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8369 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8370 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8371 | 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] | 8372 | if (rc == LY_ENOT) { |
| 8373 | assert(options & LYXP_SCNODE_ALL); |
| 8374 | /* skip the rest of this path */ |
| 8375 | rc = LY_SUCCESS; |
| 8376 | scnode_skip_path = 1; |
| 8377 | options |= LYXP_SKIP_EXPR; |
| 8378 | } |
| 8379 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8380 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8381 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8382 | case LYXP_TOKEN_NODETYPE: |
| 8383 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8384 | 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] | 8385 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8386 | break; |
| 8387 | |
| 8388 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8389 | LOGINT(set->ctx); |
| 8390 | rc = LY_EINT; |
| 8391 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8392 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8393 | } 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] | 8394 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8395 | cleanup: |
| 8396 | if (scnode_skip_path) { |
| 8397 | options &= ~LYXP_SKIP_EXPR; |
| 8398 | } |
| 8399 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8400 | } |
| 8401 | |
| 8402 | /** |
| 8403 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8404 | * |
| 8405 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8406 | * |
| 8407 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8408 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8409 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8410 | * @param[in] options XPath options. |
| 8411 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8412 | */ |
| 8413 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8414 | 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] | 8415 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8416 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8417 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8418 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8419 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8420 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8421 | } |
| 8422 | |
| 8423 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8424 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8425 | /* evaluate '/' - deferred */ |
| 8426 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8427 | 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] | 8428 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8429 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8430 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8431 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8432 | return LY_SUCCESS; |
| 8433 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8434 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8435 | case LYXP_TOKEN_DOT: |
| 8436 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8437 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8438 | case LYXP_TOKEN_AT: |
| 8439 | case LYXP_TOKEN_NAMETEST: |
| 8440 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8441 | 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] | 8442 | break; |
| 8443 | default: |
| 8444 | break; |
| 8445 | } |
| 8446 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8447 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8448 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8449 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8450 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8451 | 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] | 8452 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8453 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8454 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8455 | 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] | 8456 | } |
| 8457 | |
| 8458 | return LY_SUCCESS; |
| 8459 | } |
| 8460 | |
| 8461 | /** |
| 8462 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8463 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8464 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8465 | * |
| 8466 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8467 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8468 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8469 | * @param[in] options XPath options. |
| 8470 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8471 | */ |
| 8472 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8473 | 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] | 8474 | { |
| 8475 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8476 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8477 | LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL; |
| 8478 | uint32_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8479 | struct lyxp_set **args = NULL, **args_aux; |
| 8480 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8481 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8482 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8483 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8484 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8485 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8486 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8487 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8488 | xpath_func = &xpath_sum; |
| 8489 | } |
| 8490 | break; |
| 8491 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8492 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8493 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8494 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8495 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8496 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8497 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8498 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8499 | xpath_func = &xpath_true; |
| 8500 | } |
| 8501 | break; |
| 8502 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8503 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8505 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8506 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8507 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8508 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8509 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8510 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8511 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8512 | xpath_func = &xpath_deref; |
| 8513 | } |
| 8514 | break; |
| 8515 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8516 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8517 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8518 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8519 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8520 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8521 | xpath_func = &xpath_string; |
| 8522 | } |
| 8523 | break; |
| 8524 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8525 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8526 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8527 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8528 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8529 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8530 | xpath_func = &xpath_current; |
| 8531 | } |
| 8532 | break; |
| 8533 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8534 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8535 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8536 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8537 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8538 | } 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] | 8539 | xpath_func = &xpath_re_match; |
| 8540 | } |
| 8541 | break; |
| 8542 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8543 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8544 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8545 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8546 | xpath_func = &xpath_translate; |
| 8547 | } |
| 8548 | break; |
| 8549 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8550 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8551 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8552 | } 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] | 8553 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8554 | } 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] | 8555 | xpath_func = &xpath_bit_is_set; |
| 8556 | } |
| 8557 | break; |
| 8558 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8559 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8560 | xpath_func = &xpath_starts_with; |
| 8561 | } |
| 8562 | break; |
| 8563 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8564 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8565 | xpath_func = &xpath_derived_from; |
| 8566 | } |
| 8567 | break; |
| 8568 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8569 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8570 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8571 | } 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] | 8572 | xpath_func = &xpath_string_length; |
| 8573 | } |
| 8574 | break; |
| 8575 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8576 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8577 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8578 | } 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] | 8579 | xpath_func = &xpath_substring_after; |
| 8580 | } |
| 8581 | break; |
| 8582 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8583 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8584 | xpath_func = &xpath_substring_before; |
| 8585 | } |
| 8586 | break; |
| 8587 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8588 | 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] | 8589 | xpath_func = &xpath_derived_from_or_self; |
| 8590 | } |
| 8591 | break; |
| 8592 | } |
| 8593 | |
| 8594 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8595 | 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] | 8596 | return LY_EVALID; |
| 8597 | } |
| 8598 | } |
| 8599 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8600 | 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] | 8601 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8602 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8603 | |
| 8604 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8605 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8606 | 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] | 8607 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8608 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8609 | |
| 8610 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8611 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8612 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8613 | args = malloc(sizeof *args); |
| 8614 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8615 | arg_count = 1; |
| 8616 | args[0] = set_copy(set); |
| 8617 | if (!args[0]) { |
| 8618 | rc = LY_EMEM; |
| 8619 | goto cleanup; |
| 8620 | } |
| 8621 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8622 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8623 | LY_CHECK_GOTO(rc, cleanup); |
| 8624 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8625 | 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] | 8626 | LY_CHECK_GOTO(rc, cleanup); |
| 8627 | } |
| 8628 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8629 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8630 | 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] | 8631 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8632 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8633 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8634 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8635 | ++arg_count; |
| 8636 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8637 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8638 | args = args_aux; |
| 8639 | args[arg_count - 1] = set_copy(set); |
| 8640 | if (!args[arg_count - 1]) { |
| 8641 | rc = LY_EMEM; |
| 8642 | goto cleanup; |
| 8643 | } |
| 8644 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8645 | 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] | 8646 | LY_CHECK_GOTO(rc, cleanup); |
| 8647 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8648 | 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] | 8649 | LY_CHECK_GOTO(rc, cleanup); |
| 8650 | } |
| 8651 | } |
| 8652 | |
| 8653 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8654 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8655 | 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] | 8656 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8657 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8658 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8659 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8660 | /* evaluate function */ |
| 8661 | rc = xpath_func(args, arg_count, set, options); |
| 8662 | |
| 8663 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8664 | /* merge all nodes from arg evaluations */ |
| 8665 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8666 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8667 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8668 | } |
| 8669 | } |
| 8670 | } else { |
| 8671 | rc = LY_SUCCESS; |
| 8672 | } |
| 8673 | |
| 8674 | cleanup: |
| 8675 | for (i = 0; i < arg_count; ++i) { |
| 8676 | lyxp_set_free(args[i]); |
| 8677 | } |
| 8678 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8679 | return rc; |
| 8680 | } |
| 8681 | |
| 8682 | /** |
| 8683 | * @brief Evaluate Number. Logs directly on error. |
| 8684 | * |
| 8685 | * @param[in] ctx Context for errors. |
| 8686 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8687 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8688 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8689 | * @return LY_ERR |
| 8690 | */ |
| 8691 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8692 | 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] | 8693 | { |
| 8694 | long double num; |
| 8695 | char *endptr; |
| 8696 | |
| 8697 | if (set) { |
| 8698 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8699 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8700 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8701 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8702 | 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] | 8703 | 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] | 8704 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8705 | } 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] | 8706 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8707 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8708 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8709 | return LY_EVALID; |
| 8710 | } |
| 8711 | |
| 8712 | set_fill_number(set, num); |
| 8713 | } |
| 8714 | |
| 8715 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8716 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8717 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8718 | return LY_SUCCESS; |
| 8719 | } |
| 8720 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8721 | LY_ERR |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8722 | lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len, |
| 8723 | struct lyxp_var **var) |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8724 | { |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8725 | LY_ARRAY_COUNT_TYPE u; |
| 8726 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8727 | assert(name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8728 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8729 | if (!name_len) { |
| 8730 | name_len = strlen(name); |
| 8731 | } |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8732 | |
| 8733 | LY_ARRAY_FOR(vars, u) { |
| 8734 | if (!strncmp(vars[u].name, name, name_len)) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8735 | if (var) { |
| 8736 | *var = (struct lyxp_var *)&vars[u]; |
| 8737 | } |
| 8738 | return LY_SUCCESS; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8739 | } |
| 8740 | } |
| 8741 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8742 | if (ctx) { |
| 8743 | LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8744 | } |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8745 | return LY_ENOTFOUND; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8746 | } |
| 8747 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8748 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8749 | * @brief Evaluate VariableReference. |
| 8750 | * |
| 8751 | * @param[in] exp Parsed XPath expression. |
| 8752 | * @param[in] tok_idx Position in the expression @p exp. |
| 8753 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8754 | * @param[in,out] set Context and result set. |
| 8755 | * @param[in] options XPath options. |
| 8756 | * @return LY_ERR value. |
| 8757 | */ |
| 8758 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8759 | 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] | 8760 | { |
| 8761 | LY_ERR ret; |
| 8762 | const char *name; |
| 8763 | struct lyxp_var *var; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8764 | struct lyxp_expr *tokens = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8765 | uint32_t token_index, name_len; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8766 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8767 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8768 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
Michal Vasko | e68b540 | 2022-07-29 14:58:15 +0200 | [diff] [blame] | 8769 | name_len = exp->tok_len[*tok_idx]; |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8770 | ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var); |
| 8771 | LY_CHECK_RET(ret); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8772 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8773 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8774 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8775 | LY_CHECK_GOTO(ret, cleanup); |
| 8776 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8777 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8778 | token_index = 0; |
| 8779 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8780 | LY_CHECK_GOTO(ret, cleanup); |
| 8781 | |
| 8782 | cleanup: |
| 8783 | lyxp_expr_free(set->ctx, tokens); |
| 8784 | |
| 8785 | return ret; |
| 8786 | } |
| 8787 | |
| 8788 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8789 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8790 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8791 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8792 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8793 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8794 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8795 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8796 | * |
| 8797 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8798 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8799 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8800 | * @param[in] options XPath options. |
| 8801 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8802 | */ |
| 8803 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8804 | 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] | 8805 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8806 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8807 | LY_ERR rc; |
| 8808 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8809 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8810 | case LYXP_TOKEN_PAR1: |
| 8811 | /* '(' Expr ')' */ |
| 8812 | |
| 8813 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8814 | 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] | 8815 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8816 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8817 | |
| 8818 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8819 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8820 | LY_CHECK_RET(rc); |
| 8821 | |
| 8822 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8823 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8824 | 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] | 8825 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8826 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8827 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8828 | goto predicate; |
| 8829 | |
| 8830 | case LYXP_TOKEN_DOT: |
| 8831 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8832 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8833 | case LYXP_TOKEN_AT: |
| 8834 | case LYXP_TOKEN_NAMETEST: |
| 8835 | case LYXP_TOKEN_NODETYPE: |
| 8836 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8837 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8838 | LY_CHECK_RET(rc); |
| 8839 | break; |
| 8840 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8841 | case LYXP_TOKEN_VARREF: |
| 8842 | /* VariableReference */ |
| 8843 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8844 | LY_CHECK_RET(rc); |
| 8845 | ++(*tok_idx); |
| 8846 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8847 | goto predicate; |
| 8848 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8849 | case LYXP_TOKEN_FUNCNAME: |
| 8850 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8851 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8852 | LY_CHECK_RET(rc); |
| 8853 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8854 | goto predicate; |
| 8855 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8856 | case LYXP_TOKEN_OPER_PATH: |
| 8857 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8858 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8859 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8860 | LY_CHECK_RET(rc); |
| 8861 | break; |
| 8862 | |
| 8863 | case LYXP_TOKEN_LITERAL: |
| 8864 | /* Literal */ |
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 | eval_literal(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 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8872 | } |
| 8873 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8874 | goto predicate; |
| 8875 | |
| 8876 | case LYXP_TOKEN_NUMBER: |
| 8877 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8878 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8879 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8880 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8881 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8882 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8883 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8884 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8885 | } |
| 8886 | LY_CHECK_RET(rc); |
| 8887 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8888 | goto predicate; |
| 8889 | |
| 8890 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8891 | 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] | 8892 | return LY_EVALID; |
| 8893 | } |
| 8894 | |
| 8895 | return LY_SUCCESS; |
| 8896 | |
| 8897 | predicate: |
| 8898 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8899 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8900 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8901 | LY_CHECK_RET(rc); |
| 8902 | } |
| 8903 | |
| 8904 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8905 | 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] | 8906 | |
| 8907 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8908 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8909 | all_desc = 0; |
| 8910 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8911 | all_desc = 1; |
| 8912 | } |
| 8913 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8914 | 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] | 8915 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8916 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8917 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8918 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8919 | LY_CHECK_RET(rc); |
| 8920 | } |
| 8921 | |
| 8922 | return LY_SUCCESS; |
| 8923 | } |
| 8924 | |
| 8925 | /** |
| 8926 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8927 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8928 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8929 | * |
| 8930 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8931 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8932 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8933 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8934 | * @param[in] options XPath options. |
| 8935 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8936 | */ |
| 8937 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8938 | 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] | 8939 | { |
| 8940 | LY_ERR rc = LY_SUCCESS; |
| 8941 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8942 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8943 | |
| 8944 | assert(repeat); |
| 8945 | |
| 8946 | set_init(&orig_set, set); |
| 8947 | set_init(&set2, set); |
| 8948 | |
| 8949 | set_fill_set(&orig_set, set); |
| 8950 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8951 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8952 | LY_CHECK_GOTO(rc, cleanup); |
| 8953 | |
| 8954 | /* ('|' PathExpr)* */ |
| 8955 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8956 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8957 | 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] | 8958 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8959 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8960 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8961 | if (options & LYXP_SKIP_EXPR) { |
| 8962 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8963 | LY_CHECK_GOTO(rc, cleanup); |
| 8964 | continue; |
| 8965 | } |
| 8966 | |
| 8967 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8968 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8969 | LY_CHECK_GOTO(rc, cleanup); |
| 8970 | |
| 8971 | /* eval */ |
| 8972 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8973 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8974 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8975 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8976 | LY_CHECK_GOTO(rc, cleanup); |
| 8977 | } |
| 8978 | } |
| 8979 | |
| 8980 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8981 | lyxp_set_free_content(&orig_set); |
| 8982 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8983 | return rc; |
| 8984 | } |
| 8985 | |
| 8986 | /** |
| 8987 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8988 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8989 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8990 | * |
| 8991 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8992 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8993 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8994 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8995 | * @param[in] options XPath options. |
| 8996 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8997 | */ |
| 8998 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8999 | 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] | 9000 | { |
| 9001 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9002 | uint32_t this_op, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9003 | |
| 9004 | assert(repeat); |
| 9005 | |
| 9006 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9007 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9008 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9009 | 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] | 9010 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9011 | 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] | 9012 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9013 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9014 | } |
| 9015 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9016 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9017 | LY_CHECK_RET(rc); |
| 9018 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9019 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9020 | if (options & LYXP_SCNODE_ALL) { |
| 9021 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 9022 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9023 | 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] | 9024 | LY_CHECK_RET(rc); |
| 9025 | } |
| 9026 | } |
| 9027 | |
| 9028 | return LY_SUCCESS; |
| 9029 | } |
| 9030 | |
| 9031 | /** |
| 9032 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 9033 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9034 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9035 | * | MultiplicativeExpr '*' UnaryExpr |
| 9036 | * | MultiplicativeExpr 'div' UnaryExpr |
| 9037 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 9038 | * |
| 9039 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9040 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9041 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9042 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9043 | * @param[in] options XPath options. |
| 9044 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9045 | */ |
| 9046 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9047 | 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] | 9048 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9049 | { |
| 9050 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9051 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9052 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9053 | |
| 9054 | assert(repeat); |
| 9055 | |
| 9056 | set_init(&orig_set, set); |
| 9057 | set_init(&set2, set); |
| 9058 | |
| 9059 | set_fill_set(&orig_set, set); |
| 9060 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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 | |
| 9064 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 9065 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9066 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9067 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9068 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9069 | 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] | 9070 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9071 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9072 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9073 | if (options & LYXP_SKIP_EXPR) { |
| 9074 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9075 | LY_CHECK_GOTO(rc, cleanup); |
| 9076 | continue; |
| 9077 | } |
| 9078 | |
| 9079 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9080 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9081 | LY_CHECK_GOTO(rc, cleanup); |
| 9082 | |
| 9083 | /* eval */ |
| 9084 | if (options & LYXP_SCNODE_ALL) { |
| 9085 | 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] | 9086 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9087 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9088 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9089 | 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] | 9090 | LY_CHECK_GOTO(rc, cleanup); |
| 9091 | } |
| 9092 | } |
| 9093 | |
| 9094 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9095 | lyxp_set_free_content(&orig_set); |
| 9096 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9097 | return rc; |
| 9098 | } |
| 9099 | |
| 9100 | /** |
| 9101 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 9102 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9103 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9104 | * | AdditiveExpr '+' MultiplicativeExpr |
| 9105 | * | AdditiveExpr '-' MultiplicativeExpr |
| 9106 | * |
| 9107 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9108 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9109 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9110 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9111 | * @param[in] options XPath options. |
| 9112 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9113 | */ |
| 9114 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9115 | 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] | 9116 | { |
| 9117 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9118 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9119 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9120 | |
| 9121 | assert(repeat); |
| 9122 | |
| 9123 | set_init(&orig_set, set); |
| 9124 | set_init(&set2, set); |
| 9125 | |
| 9126 | set_fill_set(&orig_set, set); |
| 9127 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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 | |
| 9131 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 9132 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9133 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9134 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9135 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9136 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9137 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9138 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9139 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9140 | if (options & LYXP_SKIP_EXPR) { |
| 9141 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9142 | LY_CHECK_GOTO(rc, cleanup); |
| 9143 | continue; |
| 9144 | } |
| 9145 | |
| 9146 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9147 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9148 | LY_CHECK_GOTO(rc, cleanup); |
| 9149 | |
| 9150 | /* eval */ |
| 9151 | if (options & LYXP_SCNODE_ALL) { |
| 9152 | 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] | 9153 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9154 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9155 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9156 | 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] | 9157 | LY_CHECK_GOTO(rc, cleanup); |
| 9158 | } |
| 9159 | } |
| 9160 | |
| 9161 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9162 | lyxp_set_free_content(&orig_set); |
| 9163 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9164 | return rc; |
| 9165 | } |
| 9166 | |
| 9167 | /** |
| 9168 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 9169 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9170 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9171 | * | RelationalExpr '<' AdditiveExpr |
| 9172 | * | RelationalExpr '>' AdditiveExpr |
| 9173 | * | RelationalExpr '<=' AdditiveExpr |
| 9174 | * | RelationalExpr '>=' AdditiveExpr |
| 9175 | * |
| 9176 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9177 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9178 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9179 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9180 | * @param[in] options XPath options. |
| 9181 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9182 | */ |
| 9183 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9184 | 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] | 9185 | { |
| 9186 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9187 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9188 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9189 | |
| 9190 | assert(repeat); |
| 9191 | |
| 9192 | set_init(&orig_set, set); |
| 9193 | set_init(&set2, set); |
| 9194 | |
| 9195 | set_fill_set(&orig_set, set); |
| 9196 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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 | |
| 9200 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 9201 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9202 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9203 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9204 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9205 | 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] | 9206 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9207 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9208 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9209 | if (options & LYXP_SKIP_EXPR) { |
| 9210 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9211 | LY_CHECK_GOTO(rc, cleanup); |
| 9212 | continue; |
| 9213 | } |
| 9214 | |
| 9215 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9216 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9217 | LY_CHECK_GOTO(rc, cleanup); |
| 9218 | |
| 9219 | /* eval */ |
| 9220 | if (options & LYXP_SCNODE_ALL) { |
| 9221 | 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] | 9222 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9223 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9224 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9225 | ly_bool result; |
| 9226 | |
| 9227 | 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] | 9228 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9229 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9230 | } |
| 9231 | } |
| 9232 | |
| 9233 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9234 | lyxp_set_free_content(&orig_set); |
| 9235 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9236 | return rc; |
| 9237 | } |
| 9238 | |
| 9239 | /** |
| 9240 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 9241 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9242 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9243 | * | EqualityExpr '!=' RelationalExpr |
| 9244 | * |
| 9245 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9246 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9247 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9248 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9249 | * @param[in] options XPath options. |
| 9250 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9251 | */ |
| 9252 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9253 | 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] | 9254 | { |
| 9255 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9256 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9257 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9258 | |
| 9259 | assert(repeat); |
| 9260 | |
| 9261 | set_init(&orig_set, set); |
| 9262 | set_init(&set2, set); |
| 9263 | |
| 9264 | set_fill_set(&orig_set, set); |
| 9265 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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 | |
| 9269 | /* ('=' / '!=' RelationalExpr)* */ |
| 9270 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9271 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9272 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9273 | 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] | 9274 | 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] | 9275 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9276 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9277 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9278 | if (options & LYXP_SKIP_EXPR) { |
| 9279 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9280 | LY_CHECK_GOTO(rc, cleanup); |
| 9281 | continue; |
| 9282 | } |
| 9283 | |
| 9284 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9285 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9286 | LY_CHECK_GOTO(rc, cleanup); |
| 9287 | |
| 9288 | /* eval */ |
| 9289 | if (options & LYXP_SCNODE_ALL) { |
| 9290 | 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] | 9291 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9292 | 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] | 9293 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9294 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9295 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9296 | ly_bool result; |
| 9297 | |
| 9298 | 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] | 9299 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9300 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9301 | } |
| 9302 | } |
| 9303 | |
| 9304 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9305 | lyxp_set_free_content(&orig_set); |
| 9306 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9307 | return rc; |
| 9308 | } |
| 9309 | |
| 9310 | /** |
| 9311 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9312 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9313 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9314 | * |
| 9315 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9316 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9317 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9318 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9319 | * @param[in] options XPath options. |
| 9320 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9321 | */ |
| 9322 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9323 | 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] | 9324 | { |
| 9325 | LY_ERR rc; |
| 9326 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9327 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9328 | |
| 9329 | assert(repeat); |
| 9330 | |
| 9331 | set_init(&orig_set, set); |
| 9332 | set_init(&set2, set); |
| 9333 | |
| 9334 | set_fill_set(&orig_set, set); |
| 9335 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9336 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9337 | LY_CHECK_GOTO(rc, cleanup); |
| 9338 | |
| 9339 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9340 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9341 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9342 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9343 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9344 | } |
| 9345 | |
| 9346 | /* ('and' EqualityExpr)* */ |
| 9347 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9348 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9349 | 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] | 9350 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9351 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9352 | |
| 9353 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9354 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9355 | 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] | 9356 | LY_CHECK_GOTO(rc, cleanup); |
| 9357 | continue; |
| 9358 | } |
| 9359 | |
| 9360 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9361 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9362 | LY_CHECK_GOTO(rc, cleanup); |
| 9363 | |
| 9364 | /* eval - just get boolean value actually */ |
| 9365 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9366 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9367 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9368 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9369 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9370 | set_fill_set(set, &set2); |
| 9371 | } |
| 9372 | } |
| 9373 | |
| 9374 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9375 | lyxp_set_free_content(&orig_set); |
| 9376 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9377 | return rc; |
| 9378 | } |
| 9379 | |
| 9380 | /** |
| 9381 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9382 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9383 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9384 | * |
| 9385 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9386 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9387 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9388 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9389 | * @param[in] options XPath options. |
| 9390 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9391 | */ |
| 9392 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9393 | 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] | 9394 | { |
| 9395 | LY_ERR rc; |
| 9396 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9397 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9398 | |
| 9399 | assert(repeat); |
| 9400 | |
| 9401 | set_init(&orig_set, set); |
| 9402 | set_init(&set2, set); |
| 9403 | |
| 9404 | set_fill_set(&orig_set, set); |
| 9405 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9406 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9407 | LY_CHECK_GOTO(rc, cleanup); |
| 9408 | |
| 9409 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9410 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9411 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9412 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9413 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9414 | } |
| 9415 | |
| 9416 | /* ('or' AndExpr)* */ |
| 9417 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9418 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9419 | 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] | 9420 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9421 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9422 | |
| 9423 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9424 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9425 | 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] | 9426 | LY_CHECK_GOTO(rc, cleanup); |
| 9427 | continue; |
| 9428 | } |
| 9429 | |
| 9430 | set_fill_set(&set2, &orig_set); |
| 9431 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9432 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9433 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9434 | LY_CHECK_GOTO(rc, cleanup); |
| 9435 | |
| 9436 | /* eval - just get boolean value actually */ |
| 9437 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9438 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9439 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9440 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9441 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9442 | set_fill_set(set, &set2); |
| 9443 | } |
| 9444 | } |
| 9445 | |
| 9446 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9447 | lyxp_set_free_content(&orig_set); |
| 9448 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9449 | return rc; |
| 9450 | } |
| 9451 | |
| 9452 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9453 | * @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] | 9454 | * |
| 9455 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9456 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9457 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9458 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9459 | * @param[in] options XPath options. |
| 9460 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9461 | */ |
| 9462 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9463 | 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] | 9464 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9465 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9466 | uint32_t i, count; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9467 | enum lyxp_expr_type next_etype; |
| 9468 | LY_ERR rc; |
| 9469 | |
| 9470 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9471 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9472 | next_etype = LYXP_EXPR_NONE; |
| 9473 | } else { |
| 9474 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9475 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9476 | |
| 9477 | /* select one-priority lower because etype expression called us */ |
| 9478 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9479 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9480 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9481 | 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] | 9482 | } else { |
| 9483 | next_etype = LYXP_EXPR_NONE; |
| 9484 | } |
| 9485 | } |
| 9486 | |
| 9487 | /* decide what expression are we parsing based on the repeat */ |
| 9488 | switch (next_etype) { |
| 9489 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9490 | rc = eval_or_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_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9493 | rc = eval_and_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_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9496 | rc = eval_equality_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_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9499 | rc = eval_relational_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_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9502 | rc = eval_additive_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_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9505 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9506 | break; |
| 9507 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9508 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9509 | break; |
| 9510 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9511 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9512 | break; |
| 9513 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9514 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9515 | break; |
| 9516 | default: |
| 9517 | LOGINT_RET(set->ctx); |
| 9518 | } |
| 9519 | |
| 9520 | return rc; |
| 9521 | } |
| 9522 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9523 | /** |
| 9524 | * @brief Get root type. |
| 9525 | * |
| 9526 | * @param[in] ctx_node Context node. |
| 9527 | * @param[in] ctx_scnode Schema context node. |
| 9528 | * @param[in] options XPath options. |
| 9529 | * @return Root type. |
| 9530 | */ |
| 9531 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9532 | 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] | 9533 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9534 | const struct lysc_node *op; |
| 9535 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9536 | /* explicit */ |
| 9537 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9538 | return LYXP_NODE_ROOT; |
| 9539 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9540 | return LYXP_NODE_ROOT_CONFIG; |
| 9541 | } |
| 9542 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9543 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9544 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9545 | 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] | 9546 | |
| 9547 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9548 | /* general root that can access everything */ |
| 9549 | return LYXP_NODE_ROOT; |
| 9550 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9551 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9552 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9553 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9554 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9555 | } |
| 9556 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9557 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9558 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9559 | 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] | 9560 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9561 | if (op || !(options & LYXP_SCHEMA)) { |
| 9562 | /* general root that can access everything */ |
| 9563 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9564 | } 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] | 9565 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9566 | return LYXP_NODE_ROOT_CONFIG; |
| 9567 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9568 | return LYXP_NODE_ROOT; |
| 9569 | } |
| 9570 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9571 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9572 | 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] | 9573 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node, |
| 9574 | 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] | 9575 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9576 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9577 | LY_ERR rc; |
| 9578 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9579 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9580 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9581 | 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] | 9582 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9583 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9584 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9585 | if (tree) { |
| 9586 | /* adjust the pointer to be the first top-level sibling */ |
| 9587 | while (tree->parent) { |
| 9588 | tree = lyd_parent(tree); |
| 9589 | } |
| 9590 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9591 | |
Michal Vasko | 6f78a77 | 2022-11-10 08:13:52 +0100 | [diff] [blame] | 9592 | if (lysc_data_parent(tree->schema)) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9593 | /* unable to evaluate absolute paths */ |
| 9594 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9595 | LYD_NAME(tree)); |
| 9596 | return LY_EINVAL; |
| 9597 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9598 | } |
| 9599 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9600 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9601 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9602 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9603 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9604 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9605 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9606 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9607 | set->cur_node = cur_node; |
| 9608 | for (set->context_op = cur_node ? cur_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9609 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9610 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9611 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9612 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9613 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9614 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9615 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9616 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9617 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9618 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9619 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9620 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9621 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9622 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9623 | } |
| 9624 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9625 | if (set->cur_node) { |
| 9626 | LOG_LOCBACK(0, 1, 0, 0); |
| 9627 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9628 | return rc; |
| 9629 | } |
| 9630 | |
| 9631 | #if 0 |
| 9632 | |
| 9633 | /* full xml printing of set elements, not used currently */ |
| 9634 | |
| 9635 | void |
| 9636 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9637 | { |
| 9638 | uint32_t i; |
| 9639 | char *str_num; |
| 9640 | struct lyout out; |
| 9641 | |
| 9642 | memset(&out, 0, sizeof out); |
| 9643 | |
| 9644 | out.type = LYOUT_STREAM; |
| 9645 | out.method.f = f; |
| 9646 | |
| 9647 | switch (set->type) { |
| 9648 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9649 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9650 | break; |
| 9651 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9652 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9653 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9654 | break; |
| 9655 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9656 | ly_print_(&out, "String XPath set:\n"); |
| 9657 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9658 | break; |
| 9659 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9660 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9661 | |
| 9662 | if (isnan(set->value.num)) { |
| 9663 | str_num = strdup("NaN"); |
| 9664 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9665 | str_num = strdup("0"); |
| 9666 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9667 | str_num = strdup("Infinity"); |
| 9668 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9669 | str_num = strdup("-Infinity"); |
| 9670 | } else if ((long long)set->value.num == set->value.num) { |
| 9671 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9672 | str_num = NULL; |
| 9673 | } |
| 9674 | } else { |
| 9675 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9676 | str_num = NULL; |
| 9677 | } |
| 9678 | } |
| 9679 | if (!str_num) { |
| 9680 | LOGMEM; |
| 9681 | return; |
| 9682 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9683 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9684 | free(str_num); |
| 9685 | break; |
| 9686 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9687 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9688 | |
| 9689 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9690 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9691 | switch (set->node_type[i]) { |
| 9692 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9693 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9694 | break; |
| 9695 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9696 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9697 | break; |
| 9698 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9699 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9700 | break; |
| 9701 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9702 | 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] | 9703 | break; |
| 9704 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9705 | 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] | 9706 | break; |
| 9707 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9708 | 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] | 9709 | break; |
| 9710 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9711 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9712 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9713 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9714 | break; |
| 9715 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9716 | 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] | 9717 | break; |
| 9718 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9719 | 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] | 9720 | break; |
| 9721 | } |
| 9722 | } |
| 9723 | break; |
| 9724 | } |
| 9725 | } |
| 9726 | |
| 9727 | #endif |
| 9728 | |
| 9729 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9730 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9731 | { |
| 9732 | long double num; |
| 9733 | char *str; |
| 9734 | LY_ERR rc; |
| 9735 | |
| 9736 | if (!set || (set->type == target)) { |
| 9737 | return LY_SUCCESS; |
| 9738 | } |
| 9739 | |
| 9740 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9741 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9742 | |
| 9743 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9744 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9745 | return LY_EINVAL; |
| 9746 | } |
| 9747 | |
| 9748 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9749 | 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] | 9750 | switch (set->type) { |
| 9751 | case LYXP_SET_NUMBER: |
| 9752 | if (isnan(set->val.num)) { |
| 9753 | set->val.str = strdup("NaN"); |
| 9754 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9755 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9756 | set->val.str = strdup("0"); |
| 9757 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9758 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9759 | set->val.str = strdup("Infinity"); |
| 9760 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9761 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9762 | set->val.str = strdup("-Infinity"); |
| 9763 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9764 | } else if ((long long)set->val.num == set->val.num) { |
| 9765 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9766 | LOGMEM_RET(set->ctx); |
| 9767 | } |
| 9768 | set->val.str = str; |
| 9769 | } else { |
| 9770 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9771 | LOGMEM_RET(set->ctx); |
| 9772 | } |
| 9773 | set->val.str = str; |
| 9774 | } |
| 9775 | break; |
| 9776 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9777 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9778 | set->val.str = strdup("true"); |
| 9779 | } else { |
| 9780 | set->val.str = strdup("false"); |
| 9781 | } |
| 9782 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9783 | break; |
| 9784 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9785 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9786 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9787 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9788 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9789 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9790 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9791 | set->val.str = str; |
| 9792 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9793 | default: |
| 9794 | LOGINT_RET(set->ctx); |
| 9795 | } |
| 9796 | set->type = LYXP_SET_STRING; |
| 9797 | } |
| 9798 | |
| 9799 | /* to NUMBER */ |
| 9800 | if (target == LYXP_SET_NUMBER) { |
| 9801 | switch (set->type) { |
| 9802 | case LYXP_SET_STRING: |
| 9803 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9804 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9805 | set->val.num = num; |
| 9806 | break; |
| 9807 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9808 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9809 | set->val.num = 1; |
| 9810 | } else { |
| 9811 | set->val.num = 0; |
| 9812 | } |
| 9813 | break; |
| 9814 | default: |
| 9815 | LOGINT_RET(set->ctx); |
| 9816 | } |
| 9817 | set->type = LYXP_SET_NUMBER; |
| 9818 | } |
| 9819 | |
| 9820 | /* to BOOLEAN */ |
| 9821 | if (target == LYXP_SET_BOOLEAN) { |
| 9822 | switch (set->type) { |
| 9823 | case LYXP_SET_NUMBER: |
| 9824 | 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] | 9825 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9826 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9827 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9828 | } |
| 9829 | break; |
| 9830 | case LYXP_SET_STRING: |
| 9831 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9834 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9837 | } |
| 9838 | break; |
| 9839 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9840 | if (set->used) { |
| 9841 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9842 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9843 | } else { |
| 9844 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9845 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9846 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9847 | break; |
| 9848 | default: |
| 9849 | LOGINT_RET(set->ctx); |
| 9850 | } |
| 9851 | set->type = LYXP_SET_BOOLEAN; |
| 9852 | } |
| 9853 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9854 | return LY_SUCCESS; |
| 9855 | } |
| 9856 | |
| 9857 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9858 | 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] | 9859 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode, |
| 9860 | 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] | 9861 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9862 | LY_ERR ret; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9863 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9864 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9865 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9866 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9867 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9868 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9869 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9870 | |
| 9871 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9872 | memset(set, 0, sizeof *set); |
| 9873 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9874 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 9875 | LY_CHECK_RET(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] | 9876 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9877 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9878 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9879 | set->cur_scnode = cur_scnode; |
| 9880 | for (set->context_op = cur_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9881 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9882 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9883 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9884 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9885 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9886 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9887 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9888 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9889 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9890 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9891 | |
Michal Vasko | f69e455 | 2022-12-02 10:27:42 +0100 | [diff] [blame] | 9892 | LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9893 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9894 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9895 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9896 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9897 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9898 | { |
| 9899 | if (!path) { |
| 9900 | return NULL; |
| 9901 | } |
| 9902 | |
| 9903 | return path->expr; |
| 9904 | } |