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 ","; |
| 126 | case LYXP_TOKEN_NAMETEST: |
| 127 | return "NameTest"; |
| 128 | case LYXP_TOKEN_NODETYPE: |
| 129 | return "NodeType"; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 130 | case LYXP_TOKEN_VARREF: |
| 131 | return "VariableReference"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 132 | case LYXP_TOKEN_FUNCNAME: |
| 133 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 134 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 135 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 136 | case LYXP_TOKEN_OPER_EQUAL: |
| 137 | return "Operator(Equal)"; |
| 138 | case LYXP_TOKEN_OPER_NEQUAL: |
| 139 | return "Operator(Non-equal)"; |
| 140 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 141 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 142 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 143 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 144 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 145 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 146 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 147 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 148 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 149 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 150 | case LYXP_TOKEN_LITERAL: |
| 151 | return "Literal"; |
| 152 | case LYXP_TOKEN_NUMBER: |
| 153 | return "Number"; |
| 154 | default: |
| 155 | LOGINT(NULL); |
| 156 | return ""; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 161 | * @brief Transform string into an axis. |
| 162 | * |
| 163 | * @param[in] str String to transform. |
| 164 | * @param[in] str_len Length of @p str. |
| 165 | * @return Transformed axis. |
| 166 | */ |
| 167 | static enum lyxp_axis |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 168 | str2axis(const char *str, uint32_t str_len) |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 169 | { |
| 170 | switch (str_len) { |
| 171 | case 4: |
| 172 | assert(!strncmp("self", str, str_len)); |
| 173 | return LYXP_AXIS_SELF; |
| 174 | case 5: |
| 175 | assert(!strncmp("child", str, str_len)); |
| 176 | return LYXP_AXIS_CHILD; |
| 177 | case 6: |
| 178 | assert(!strncmp("parent", str, str_len)); |
| 179 | return LYXP_AXIS_PARENT; |
| 180 | case 8: |
| 181 | assert(!strncmp("ancestor", str, str_len)); |
| 182 | return LYXP_AXIS_ANCESTOR; |
| 183 | case 9: |
| 184 | if (str[0] == 'a') { |
| 185 | assert(!strncmp("attribute", str, str_len)); |
| 186 | return LYXP_AXIS_ATTRIBUTE; |
| 187 | } else if (str[0] == 'f') { |
| 188 | assert(!strncmp("following", str, str_len)); |
| 189 | return LYXP_AXIS_FOLLOWING; |
| 190 | } else { |
| 191 | assert(!strncmp("preceding", str, str_len)); |
| 192 | return LYXP_AXIS_PRECEDING; |
| 193 | } |
| 194 | break; |
| 195 | case 10: |
| 196 | assert(!strncmp("descendant", str, str_len)); |
| 197 | return LYXP_AXIS_DESCENDANT; |
| 198 | case 16: |
| 199 | assert(!strncmp("ancestor-or-self", str, str_len)); |
| 200 | return LYXP_AXIS_ANCESTOR_OR_SELF; |
| 201 | case 17: |
| 202 | if (str[0] == 'f') { |
| 203 | assert(!strncmp("following-sibling", str, str_len)); |
| 204 | return LYXP_AXIS_FOLLOWING_SIBLING; |
| 205 | } else { |
| 206 | assert(!strncmp("preceding-sibling", str, str_len)); |
| 207 | return LYXP_AXIS_PRECEDING_SIBLING; |
| 208 | } |
| 209 | break; |
| 210 | case 18: |
| 211 | assert(!strncmp("descendant-or-self", str, str_len)); |
| 212 | return LYXP_AXIS_DESCENDANT_OR_SELF; |
| 213 | } |
| 214 | |
| 215 | LOGINT(NULL); |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 220 | * @brief Print the whole expression \p exp to debug output. |
| 221 | * |
| 222 | * @param[in] exp Expression to use. |
| 223 | */ |
| 224 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 225 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 226 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 227 | #define MSG_BUFFER_SIZE 128 |
| 228 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 229 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 230 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 231 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 236 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 237 | 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] | 238 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 239 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 240 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 241 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 242 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 243 | } |
| 244 | strcat(tmp, ")"); |
| 245 | } |
| 246 | LOGDBG(LY_LDGXPATH, tmp); |
| 247 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 248 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | #ifndef NDEBUG |
| 252 | |
| 253 | /** |
| 254 | * @brief Print XPath set content to debug output. |
| 255 | * |
| 256 | * @param[in] set Set to print. |
| 257 | */ |
| 258 | static void |
| 259 | print_set_debug(struct lyxp_set *set) |
| 260 | { |
| 261 | uint32_t i; |
| 262 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 263 | struct lyxp_set_node *item; |
| 264 | struct lyxp_set_scnode *sitem; |
| 265 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 266 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
| 270 | switch (set->type) { |
| 271 | case LYXP_SET_NODE_SET: |
| 272 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 273 | for (i = 0; i < set->used; ++i) { |
| 274 | item = &set->val.nodes[i]; |
| 275 | |
| 276 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 277 | case LYXP_NODE_NONE: |
| 278 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 279 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 280 | case LYXP_NODE_ROOT: |
| 281 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 282 | break; |
| 283 | case LYXP_NODE_ROOT_CONFIG: |
| 284 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 285 | break; |
| 286 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 287 | 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] | 288 | 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] | 289 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 290 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 291 | 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] | 292 | item->node->schema->name, lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 293 | } else { |
| 294 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 295 | } |
| 296 | break; |
| 297 | case LYXP_NODE_TEXT: |
| 298 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 299 | 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] | 300 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 301 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 302 | 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] | 303 | } |
| 304 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 305 | case LYXP_NODE_META: |
| 306 | 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] | 307 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 308 | break; |
| 309 | } |
| 310 | } |
| 311 | break; |
| 312 | |
| 313 | case LYXP_SET_SCNODE_SET: |
| 314 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 315 | for (i = 0; i < set->used; ++i) { |
| 316 | sitem = &set->val.scnodes[i]; |
| 317 | |
| 318 | switch (sitem->type) { |
| 319 | case LYXP_NODE_ROOT: |
| 320 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 321 | break; |
| 322 | case LYXP_NODE_ROOT_CONFIG: |
| 323 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 324 | break; |
| 325 | case LYXP_NODE_ELEM: |
| 326 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 327 | break; |
| 328 | default: |
| 329 | LOGINT(NULL); |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | break; |
| 334 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 335 | case LYXP_SET_BOOLEAN: |
| 336 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 337 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 338 | break; |
| 339 | |
| 340 | case LYXP_SET_STRING: |
| 341 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 342 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 343 | break; |
| 344 | |
| 345 | case LYXP_SET_NUMBER: |
| 346 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 347 | |
| 348 | if (isnan(set->val.num)) { |
| 349 | str = strdup("NaN"); |
| 350 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 351 | str = strdup("0"); |
| 352 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 353 | str = strdup("Infinity"); |
| 354 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 355 | str = strdup("-Infinity"); |
| 356 | } else if ((long long)set->val.num == set->val.num) { |
| 357 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 358 | str = NULL; |
| 359 | } |
| 360 | } else { |
| 361 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 362 | str = NULL; |
| 363 | } |
| 364 | } |
| 365 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 366 | |
| 367 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 368 | free(str); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | #endif |
| 373 | |
| 374 | /** |
| 375 | * @brief Realloc the string \p str. |
| 376 | * |
| 377 | * @param[in] ctx libyang context for logging. |
| 378 | * @param[in] needed How much free space is required. |
| 379 | * @param[in,out] str Pointer to the string to use. |
| 380 | * @param[in,out] used Used bytes in \p str. |
| 381 | * @param[in,out] size Allocated bytes in \p str. |
| 382 | * @return LY_ERR |
| 383 | */ |
| 384 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 385 | 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] | 386 | { |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 387 | if (*size - (unsigned)*used < needed) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 388 | do { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 389 | if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 390 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 391 | return LY_EINVAL; |
| 392 | } |
| 393 | *size += LYXP_STRING_CAST_SIZE_STEP; |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 394 | } while (*size - (unsigned)*used < needed); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 395 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 396 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 397 | } |
| 398 | |
| 399 | return LY_SUCCESS; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * @brief Cast nodes recursively to one string @p str. |
| 404 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 405 | * @param[in] node Node to cast, NULL if root. |
| 406 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 407 | * @param[in] indent Current indent. |
| 408 | * @param[in,out] str Resulting string. |
| 409 | * @param[in,out] used Used bytes in @p str. |
| 410 | * @param[in,out] size Allocated bytes in @p str. |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 411 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 412 | */ |
| 413 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 414 | cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used, |
| 415 | uint32_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 416 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 417 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 419 | const struct lyd_node *child; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 420 | enum lyxp_node_type child_type; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 421 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 422 | struct lyd_node_any *any; |
| 423 | LY_ERR rc; |
| 424 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 425 | 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] | 426 | return LY_SUCCESS; |
| 427 | } |
| 428 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 429 | if (!node) { |
| 430 | /* fake container */ |
| 431 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 432 | strcpy(*str + (*used - 1), "\n"); |
| 433 | ++(*used); |
| 434 | |
| 435 | ++indent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 436 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 437 | /* print all the top-level nodes */ |
| 438 | child = NULL; |
| 439 | child_type = 0; |
| 440 | while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) { |
| 441 | LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 444 | /* end fake container */ |
| 445 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 446 | strcpy(*str + (*used - 1), "\n"); |
| 447 | ++(*used); |
| 448 | |
| 449 | --indent; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 450 | } else { |
| 451 | switch (node->schema->nodetype) { |
| 452 | case LYS_CONTAINER: |
| 453 | case LYS_LIST: |
| 454 | case LYS_RPC: |
| 455 | case LYS_NOTIF: |
| 456 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
| 457 | strcpy(*str + (*used - 1), "\n"); |
| 458 | ++(*used); |
| 459 | |
| 460 | for (child = lyd_child(node); child; child = child->next) { |
| 461 | LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size)); |
| 462 | } |
| 463 | |
| 464 | break; |
| 465 | |
| 466 | case LYS_LEAF: |
| 467 | case LYS_LEAFLIST: |
| 468 | value_str = lyd_get_value(node); |
| 469 | |
| 470 | /* print indent */ |
| 471 | LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size)); |
| 472 | memset(*str + (*used - 1), ' ', indent * 2); |
| 473 | *used += indent * 2; |
| 474 | |
| 475 | /* print value */ |
| 476 | if (*used == 1) { |
| 477 | sprintf(*str + (*used - 1), "%s", value_str); |
| 478 | *used += strlen(value_str); |
| 479 | } else { |
| 480 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 481 | *used += strlen(value_str) + 1; |
| 482 | } |
| 483 | |
| 484 | break; |
| 485 | |
| 486 | case LYS_ANYXML: |
| 487 | case LYS_ANYDATA: |
| 488 | any = (struct lyd_node_any *)node; |
| 489 | if (!(void *)any->value.tree) { |
| 490 | /* no content */ |
| 491 | buf = strdup(""); |
| 492 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 493 | } else { |
| 494 | struct ly_out *out; |
| 495 | |
| 496 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 497 | /* try to parse it into a data tree */ |
| 498 | if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB, |
| 499 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
| 500 | /* successfully parsed */ |
| 501 | free(any->value.mem); |
| 502 | any->value.tree = tree; |
| 503 | any->value_type = LYD_ANYDATA_DATATREE; |
| 504 | } |
| 505 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
| 506 | } |
| 507 | |
| 508 | switch (any->value_type) { |
| 509 | case LYD_ANYDATA_STRING: |
| 510 | case LYD_ANYDATA_XML: |
| 511 | case LYD_ANYDATA_JSON: |
| 512 | buf = strdup(any->value.json); |
| 513 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 514 | break; |
| 515 | case LYD_ANYDATA_DATATREE: |
| 516 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
| 517 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
| 518 | ly_out_free(out, NULL, 0); |
| 519 | LY_CHECK_RET(rc < 0, -rc); |
| 520 | break; |
| 521 | case LYD_ANYDATA_LYB: |
| 522 | LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 523 | return LY_EINVAL; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | line = strtok_r(buf, "\n", &ptr); |
| 528 | do { |
| 529 | rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size); |
| 530 | if (rc != LY_SUCCESS) { |
| 531 | free(buf); |
| 532 | return rc; |
| 533 | } |
| 534 | memset(*str + (*used - 1), ' ', indent * 2); |
| 535 | *used += indent * 2; |
| 536 | |
| 537 | strcpy(*str + (*used - 1), line); |
| 538 | *used += strlen(line); |
| 539 | |
| 540 | strcpy(*str + (*used - 1), "\n"); |
| 541 | *used += 1; |
| 542 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 543 | |
| 544 | free(buf); |
| 545 | break; |
| 546 | |
| 547 | default: |
| 548 | LOGINT_RET(set->ctx); |
| 549 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | return LY_SUCCESS; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * @brief Cast an element into a string. |
| 557 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 558 | * @param[in] node Node to cast, NULL if root. |
| 559 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 560 | * @param[out] str Element cast to dynamically-allocated string. |
| 561 | * @return LY_ERR |
| 562 | */ |
| 563 | static LY_ERR |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 564 | 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] | 565 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 566 | uint32_t used, size; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 567 | LY_ERR rc; |
| 568 | |
| 569 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 570 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 571 | (*str)[0] = '\0'; |
| 572 | used = 1; |
| 573 | size = LYXP_STRING_CAST_SIZE_START; |
| 574 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 575 | rc = cast_string_recursive(node, set, 0, str, &used, &size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 576 | if (rc != LY_SUCCESS) { |
| 577 | free(*str); |
| 578 | return rc; |
| 579 | } |
| 580 | |
| 581 | if (size > used) { |
| 582 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 583 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 584 | } |
| 585 | return LY_SUCCESS; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 590 | * Context position aware. |
| 591 | * |
| 592 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 593 | * @param[out] str Cast dynamically-allocated string. |
| 594 | * @return LY_ERR |
| 595 | */ |
| 596 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 597 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 598 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 599 | if (!set->used) { |
| 600 | *str = strdup(""); |
| 601 | if (!*str) { |
| 602 | LOGMEM_RET(set->ctx); |
| 603 | } |
| 604 | return LY_SUCCESS; |
| 605 | } |
| 606 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 607 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 608 | case LYXP_NODE_NONE: |
| 609 | /* invalid */ |
| 610 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 611 | case LYXP_NODE_ROOT: |
| 612 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 613 | case LYXP_NODE_ELEM: |
| 614 | case LYXP_NODE_TEXT: |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 615 | return cast_string_elem(set->val.nodes[0].node, set, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 616 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 617 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 618 | if (!*str) { |
| 619 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 620 | } |
| 621 | return LY_SUCCESS; |
| 622 | } |
| 623 | |
| 624 | LOGINT_RET(set->ctx); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * @brief Cast a string into an XPath number. |
| 629 | * |
| 630 | * @param[in] str String to use. |
| 631 | * @return Cast number. |
| 632 | */ |
| 633 | static long double |
| 634 | cast_string_to_number(const char *str) |
| 635 | { |
| 636 | long double num; |
| 637 | char *ptr; |
| 638 | |
| 639 | errno = 0; |
| 640 | num = strtold(str, &ptr); |
| 641 | if (errno || *ptr) { |
| 642 | num = NAN; |
| 643 | } |
| 644 | return num; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * @brief Callback for checking value equality. |
| 649 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 650 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 651 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 652 | * @param[in] val1_p First value. |
| 653 | * @param[in] val2_p Second value. |
| 654 | * @param[in] mod Whether hash table is being modified. |
| 655 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 656 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 657 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 658 | static ly_bool |
| 659 | 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] | 660 | { |
| 661 | struct lyxp_set_hash_node *val1, *val2; |
| 662 | |
| 663 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 664 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 665 | |
| 666 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 667 | return 1; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * @brief Insert node and its hash into set. |
| 675 | * |
| 676 | * @param[in] set et to insert to. |
| 677 | * @param[in] node Node with hash. |
| 678 | * @param[in] type Node type. |
| 679 | */ |
| 680 | static void |
| 681 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 682 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 683 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 684 | uint32_t i, hash; |
| 685 | struct lyxp_set_hash_node hnode; |
| 686 | |
| 687 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 688 | /* create hash table and add all the nodes */ |
| 689 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 690 | for (i = 0; i < set->used; ++i) { |
| 691 | hnode.node = set->val.nodes[i].node; |
| 692 | hnode.type = set->val.nodes[i].type; |
| 693 | |
| 694 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 695 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 696 | hash = dict_hash_multi(hash, NULL, 0); |
| 697 | |
| 698 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 699 | assert(!r); |
| 700 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 701 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 702 | if (hnode.node == node) { |
| 703 | /* it was just added, do not add it twice */ |
| 704 | node = NULL; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 710 | /* add the new node into hash table */ |
| 711 | hnode.node = node; |
| 712 | hnode.type = type; |
| 713 | |
| 714 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 715 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 716 | hash = dict_hash_multi(hash, NULL, 0); |
| 717 | |
| 718 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 719 | assert(!r); |
| 720 | (void)r; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * @brief Remove node and its hash from set. |
| 726 | * |
| 727 | * @param[in] set Set to remove from. |
| 728 | * @param[in] node Node to remove. |
| 729 | * @param[in] type Node type. |
| 730 | */ |
| 731 | static void |
| 732 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 733 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 734 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 735 | struct lyxp_set_hash_node hnode; |
| 736 | uint32_t hash; |
| 737 | |
| 738 | if (set->ht) { |
| 739 | hnode.node = node; |
| 740 | hnode.type = type; |
| 741 | |
| 742 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 743 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 744 | hash = dict_hash_multi(hash, NULL, 0); |
| 745 | |
| 746 | r = lyht_remove(set->ht, &hnode, hash); |
| 747 | assert(!r); |
| 748 | (void)r; |
| 749 | |
| 750 | if (!set->ht->used) { |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 751 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 752 | set->ht = NULL; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * @brief Check whether node is in set based on its hash. |
| 759 | * |
| 760 | * @param[in] set Set to search in. |
| 761 | * @param[in] node Node to search for. |
| 762 | * @param[in] type Node type. |
| 763 | * @param[in] skip_idx Index in @p set to skip. |
| 764 | * @return LY_ERR |
| 765 | */ |
| 766 | static LY_ERR |
| 767 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 768 | { |
| 769 | struct lyxp_set_hash_node hnode, *match_p; |
| 770 | uint32_t hash; |
| 771 | |
| 772 | hnode.node = node; |
| 773 | hnode.type = type; |
| 774 | |
| 775 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 776 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 777 | hash = dict_hash_multi(hash, NULL, 0); |
| 778 | |
| 779 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 780 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 781 | /* we found it on the index that should be skipped, find another */ |
| 782 | hnode = *match_p; |
| 783 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 784 | /* none other found */ |
| 785 | return LY_SUCCESS; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | return LY_EEXIST; |
| 790 | } |
| 791 | |
| 792 | /* not found */ |
| 793 | return LY_SUCCESS; |
| 794 | } |
| 795 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 796 | void |
| 797 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 798 | { |
| 799 | if (!set) { |
| 800 | return; |
| 801 | } |
| 802 | |
| 803 | if (set->type == LYXP_SET_NODE_SET) { |
| 804 | free(set->val.nodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 805 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 806 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 807 | free(set->val.scnodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 808 | lyht_free(set->ht, NULL); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 809 | } else { |
| 810 | if (set->type == LYXP_SET_STRING) { |
| 811 | free(set->val.str); |
| 812 | } |
| 813 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 814 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 815 | |
| 816 | set->val.nodes = NULL; |
| 817 | set->used = 0; |
| 818 | set->size = 0; |
| 819 | set->ht = NULL; |
| 820 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 821 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 824 | /** |
| 825 | * @brief Free dynamically-allocated set. |
| 826 | * |
| 827 | * @param[in] set Set to free. |
| 828 | */ |
| 829 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 830 | lyxp_set_free(struct lyxp_set *set) |
| 831 | { |
| 832 | if (!set) { |
| 833 | return; |
| 834 | } |
| 835 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 836 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 837 | free(set); |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * @brief Initialize set context. |
| 842 | * |
| 843 | * @param[in] new Set to initialize. |
| 844 | * @param[in] set Arbitrary initialized set. |
| 845 | */ |
| 846 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 847 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 848 | { |
| 849 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 850 | if (set) { |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 851 | new->non_child_axis = set->non_child_axis; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 852 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 853 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 854 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 855 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 856 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 857 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 858 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 859 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 860 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 861 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | /** |
| 865 | * @brief Create a deep copy of a set. |
| 866 | * |
| 867 | * @param[in] set Set to copy. |
| 868 | * @return Copy of @p set. |
| 869 | */ |
| 870 | static struct lyxp_set * |
| 871 | set_copy(struct lyxp_set *set) |
| 872 | { |
| 873 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 874 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 875 | |
| 876 | if (!set) { |
| 877 | return NULL; |
| 878 | } |
| 879 | |
| 880 | ret = malloc(sizeof *ret); |
| 881 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 882 | set_init(ret, set); |
| 883 | |
| 884 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 885 | ret->type = set->type; |
| 886 | |
| 887 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 888 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 889 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 890 | uint32_t idx; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 891 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 892 | LY_CHECK_ERR_RET(set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, |
| 893 | set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 894 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 895 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 896 | lyxp_set_free(ret); |
| 897 | return NULL; |
| 898 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 899 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 903 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 904 | if (set->used) { |
| 905 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 906 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 907 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 908 | } else { |
| 909 | ret->val.nodes = NULL; |
| 910 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 911 | |
| 912 | ret->used = ret->size = set->used; |
| 913 | ret->ctx_pos = set->ctx_pos; |
| 914 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 915 | if (set->ht) { |
| 916 | ret->ht = lyht_dup(set->ht); |
| 917 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 918 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 919 | memcpy(ret, set, sizeof *ret); |
| 920 | if (set->type == LYXP_SET_STRING) { |
| 921 | ret->val.str = strdup(set->val.str); |
| 922 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 923 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | return ret; |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 931 | * |
| 932 | * @param[in] set Set to fill. |
| 933 | * @param[in] string String to fill into \p set. |
| 934 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 935 | */ |
| 936 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 937 | 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] | 938 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 939 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 940 | |
| 941 | set->type = LYXP_SET_STRING; |
| 942 | if ((str_len == 0) && (string[0] != '\0')) { |
| 943 | string = ""; |
| 944 | } |
| 945 | set->val.str = strndup(string, str_len); |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 950 | * |
| 951 | * @param[in] set Set to fill. |
| 952 | * @param[in] number Number to fill into \p set. |
| 953 | */ |
| 954 | static void |
| 955 | set_fill_number(struct lyxp_set *set, long double number) |
| 956 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 957 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 958 | |
| 959 | set->type = LYXP_SET_NUMBER; |
| 960 | set->val.num = number; |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 965 | * |
| 966 | * @param[in] set Set to fill. |
| 967 | * @param[in] boolean Boolean to fill into \p set. |
| 968 | */ |
| 969 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 970 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 971 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 972 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 973 | |
| 974 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 975 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /** |
| 979 | * @brief Fill XPath set with the value from another set (deep assign). |
| 980 | * Any current data are disposed of. |
| 981 | * |
| 982 | * @param[in] trg Set to fill. |
| 983 | * @param[in] src Source set to copy into \p trg. |
| 984 | */ |
| 985 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 986 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 987 | { |
| 988 | if (!trg || !src) { |
| 989 | return; |
| 990 | } |
| 991 | |
| 992 | if (trg->type == LYXP_SET_NODE_SET) { |
| 993 | free(trg->val.nodes); |
| 994 | } else if (trg->type == LYXP_SET_STRING) { |
| 995 | free(trg->val.str); |
| 996 | } |
| 997 | set_init(trg, src); |
| 998 | |
| 999 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 1000 | trg->type = LYXP_SET_SCNODE_SET; |
| 1001 | trg->used = src->used; |
| 1002 | trg->size = src->used; |
| 1003 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1004 | if (trg->size) { |
| 1005 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 1006 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1007 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 1008 | } else { |
| 1009 | trg->val.scnodes = NULL; |
| 1010 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1011 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1012 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1013 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1014 | set_fill_number(trg, src->val.num); |
| 1015 | } else if (src->type == LYXP_SET_STRING) { |
| 1016 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 1017 | } else { |
| 1018 | if (trg->type == LYXP_SET_NODE_SET) { |
| 1019 | free(trg->val.nodes); |
| 1020 | } else if (trg->type == LYXP_SET_STRING) { |
| 1021 | free(trg->val.str); |
| 1022 | } |
| 1023 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1024 | assert(src->type == LYXP_SET_NODE_SET); |
| 1025 | |
| 1026 | trg->type = LYXP_SET_NODE_SET; |
| 1027 | trg->used = src->used; |
| 1028 | trg->size = src->used; |
| 1029 | trg->ctx_pos = src->ctx_pos; |
| 1030 | trg->ctx_size = src->ctx_size; |
| 1031 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1032 | if (trg->size) { |
| 1033 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 1034 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1035 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 1036 | } else { |
| 1037 | trg->val.nodes = NULL; |
| 1038 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1039 | if (src->ht) { |
| 1040 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1041 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1042 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * @brief Clear context of all schema nodes. |
| 1049 | * |
| 1050 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1051 | * @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] | 1052 | */ |
| 1053 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1054 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1055 | { |
| 1056 | uint32_t i; |
| 1057 | |
| 1058 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1059 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1060 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1061 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 1062 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /** |
| 1068 | * @brief Remove a node from a set. Removing last node changes |
| 1069 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1070 | * |
| 1071 | * @param[in] set Set to use. |
| 1072 | * @param[in] idx Index from @p set of the node to be removed. |
| 1073 | */ |
| 1074 | static void |
| 1075 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1076 | { |
| 1077 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1078 | assert(idx < set->used); |
| 1079 | |
| 1080 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1081 | |
| 1082 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1083 | if (idx < set->used) { |
| 1084 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1085 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1086 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1091 | * @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] | 1092 | * |
| 1093 | * @param[in] set Set to use. |
| 1094 | * @param[in] idx Index from @p set of the node to be removed. |
| 1095 | */ |
| 1096 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1097 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1098 | { |
| 1099 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1100 | assert(idx < set->used); |
| 1101 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1102 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1103 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1104 | } |
| 1105 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1109 | * @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] | 1110 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1111 | * |
| 1112 | * @param[in] set Set to consolidate. |
| 1113 | */ |
| 1114 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1115 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1116 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1117 | uint32_t i, orig_used, end = 0; |
| 1118 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1119 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1120 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1121 | |
| 1122 | orig_used = set->used; |
| 1123 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1124 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1125 | start = -1; |
| 1126 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1127 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1128 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1129 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1130 | end = i; |
| 1131 | ++i; |
| 1132 | break; |
| 1133 | } |
| 1134 | |
| 1135 | ++i; |
| 1136 | if (i == orig_used) { |
| 1137 | end = i; |
| 1138 | } |
| 1139 | } while (i < orig_used); |
| 1140 | |
| 1141 | if (start > -1) { |
| 1142 | /* move the whole chunk of valid nodes together */ |
| 1143 | if (set->used != (unsigned)start) { |
| 1144 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1145 | } |
| 1146 | set->used += end - start; |
| 1147 | } |
| 1148 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1152 | * @brief Check for duplicates in a node set. |
| 1153 | * |
| 1154 | * @param[in] set Set to check. |
| 1155 | * @param[in] node Node to look for in @p set. |
| 1156 | * @param[in] node_type Type of @p node. |
| 1157 | * @param[in] skip_idx Index from @p set to skip. |
| 1158 | * @return LY_ERR |
| 1159 | */ |
| 1160 | static LY_ERR |
| 1161 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1162 | { |
| 1163 | uint32_t i; |
| 1164 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1165 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1166 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1167 | } |
| 1168 | |
| 1169 | for (i = 0; i < set->used; ++i) { |
| 1170 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1171 | continue; |
| 1172 | } |
| 1173 | |
| 1174 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1175 | return LY_EEXIST; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | return LY_SUCCESS; |
| 1180 | } |
| 1181 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1182 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1183 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1184 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1185 | { |
| 1186 | uint32_t i; |
| 1187 | |
| 1188 | for (i = 0; i < set->used; ++i) { |
| 1189 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1190 | continue; |
| 1191 | } |
| 1192 | |
| 1193 | 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] | 1194 | if (index_p) { |
| 1195 | *index_p = i; |
| 1196 | } |
| 1197 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1201 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1204 | void |
| 1205 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1206 | { |
| 1207 | uint32_t orig_used, i, j; |
| 1208 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1209 | 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] | 1210 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1211 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1212 | return; |
| 1213 | } |
| 1214 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1215 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1216 | /* release hidden allocated data (lyxp_set.size) */ |
| 1217 | lyxp_set_free_content(set1); |
| 1218 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1219 | memcpy(set1, set2, sizeof *set1); |
| 1220 | return; |
| 1221 | } |
| 1222 | |
| 1223 | if (set1->used + set2->used > set1->size) { |
| 1224 | set1->size = set1->used + set2->used; |
| 1225 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1226 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1227 | } |
| 1228 | |
| 1229 | orig_used = set1->used; |
| 1230 | |
| 1231 | for (i = 0; i < set2->used; ++i) { |
| 1232 | for (j = 0; j < orig_used; ++j) { |
| 1233 | /* detect duplicities */ |
| 1234 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1235 | break; |
| 1236 | } |
| 1237 | } |
| 1238 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1239 | if (j < orig_used) { |
| 1240 | /* node is there, but update its status if needed */ |
| 1241 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1242 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1243 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1244 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1245 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1246 | } |
| 1247 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1248 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1249 | ++set1->used; |
| 1250 | } |
| 1251 | } |
| 1252 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1253 | lyxp_set_free_content(set2); |
| 1254 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * @brief Insert a node into a set. Context position aware. |
| 1259 | * |
| 1260 | * @param[in] set Set to use. |
| 1261 | * @param[in] node Node to insert to @p set. |
| 1262 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1263 | * @param[in] node_type Node type of @p node. |
| 1264 | * @param[in] idx Index in @p set to insert into. |
| 1265 | */ |
| 1266 | static void |
| 1267 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1268 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1269 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1270 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1271 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1272 | /* first item */ |
| 1273 | if (idx) { |
| 1274 | /* no real harm done, but it is a bug */ |
| 1275 | LOGINT(set->ctx); |
| 1276 | idx = 0; |
| 1277 | } |
| 1278 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1279 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1280 | set->type = LYXP_SET_NODE_SET; |
| 1281 | set->used = 0; |
| 1282 | set->size = LYXP_SET_SIZE_START; |
| 1283 | set->ctx_pos = 1; |
| 1284 | set->ctx_size = 1; |
| 1285 | set->ht = NULL; |
| 1286 | } else { |
| 1287 | /* not an empty set */ |
| 1288 | if (set->used == set->size) { |
| 1289 | |
| 1290 | /* set is full */ |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1291 | 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] | 1292 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1293 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | if (idx > set->used) { |
| 1297 | LOGINT(set->ctx); |
| 1298 | idx = set->used; |
| 1299 | } |
| 1300 | |
| 1301 | /* make space for the new node */ |
| 1302 | if (idx < set->used) { |
| 1303 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | /* finally assign the value */ |
| 1308 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1309 | set->val.nodes[idx].type = node_type; |
| 1310 | set->val.nodes[idx].pos = pos; |
| 1311 | ++set->used; |
| 1312 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1313 | /* add into hash table */ |
| 1314 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1317 | /** |
| 1318 | * @brief Insert schema node into set. |
| 1319 | * |
| 1320 | * @param[in] set Set to insert into. |
| 1321 | * @param[in] node Node to insert. |
| 1322 | * @param[in] node_type Node type of @p node. |
| 1323 | * @param[in] axis Axis that @p node was reached on. |
| 1324 | * @param[out] index_p Optional pointer to store index if the inserted @p node. |
| 1325 | * @return LY_SUCCESS on success. |
| 1326 | * @return LY_EMEM on memory allocation failure. |
| 1327 | */ |
| 1328 | static LY_ERR |
| 1329 | set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
| 1330 | enum lyxp_axis axis, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1331 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1332 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1333 | |
| 1334 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1335 | |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1336 | if (!set->size) { |
| 1337 | /* first item */ |
| 1338 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
| 1339 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
| 1340 | set->type = LYXP_SET_SCNODE_SET; |
| 1341 | set->used = 0; |
| 1342 | set->size = LYXP_SET_SIZE_START; |
| 1343 | set->ctx_pos = 1; |
| 1344 | set->ctx_size = 1; |
| 1345 | set->ht = NULL; |
| 1346 | } |
| 1347 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1348 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1349 | /* BUG if axes differs, this new one is thrown away */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1350 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1351 | } else { |
| 1352 | if (set->used == set->size) { |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1353 | 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] | 1354 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1355 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1356 | } |
| 1357 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1358 | index = set->used; |
| 1359 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1360 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1361 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1362 | set->val.scnodes[index].axis = axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1363 | ++set->used; |
| 1364 | } |
| 1365 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1366 | if (index_p) { |
| 1367 | *index_p = index; |
| 1368 | } |
| 1369 | |
| 1370 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1374 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1375 | * |
| 1376 | * @param[in] set Set to modify. |
| 1377 | * @return New context value. |
| 1378 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1379 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1380 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1381 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1382 | uint32_t i; |
| 1383 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1384 | |
| 1385 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1386 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1387 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1388 | retry: |
| 1389 | for (i = 0; i < set->used; ++i) { |
| 1390 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1391 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1392 | goto retry; |
| 1393 | } |
| 1394 | } |
| 1395 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1396 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1397 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | return ret_ctx; |
| 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * @brief Get unique @p node position in the data. |
| 1406 | * |
| 1407 | * @param[in] node Node to find. |
| 1408 | * @param[in] node_type Node type of @p node. |
| 1409 | * @param[in] root Root node. |
| 1410 | * @param[in] root_type Type of the XPath @p root node. |
| 1411 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1412 | * be used to increase efficiency and start the DFS from this node. |
| 1413 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1414 | * @return Node position. |
| 1415 | */ |
| 1416 | static uint32_t |
| 1417 | 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] | 1418 | 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] | 1419 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1420 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1421 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1422 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1423 | |
| 1424 | assert(prev && prev_pos && !root->prev->next); |
| 1425 | |
| 1426 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | if (*prev) { |
| 1431 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1432 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1433 | 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] | 1434 | goto dfs_search; |
| 1435 | } |
| 1436 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1437 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1438 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1439 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1440 | LYD_TREE_DFS_continue = 0; |
| 1441 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1442 | if (*prev && !elem) { |
| 1443 | /* resume previous DFS */ |
| 1444 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1445 | LYD_TREE_DFS_continue = 0; |
| 1446 | } |
| 1447 | |
Michal Vasko | 482a682 | 2022-05-06 08:56:12 +0200 | [diff] [blame] | 1448 | 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] | 1449 | /* skip */ |
| 1450 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1451 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1452 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1453 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1454 | break; |
| 1455 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1456 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1457 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1458 | |
| 1459 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1463 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1464 | break; |
| 1465 | } |
| 1466 | } |
| 1467 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1468 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1469 | if (!(*prev)) { |
| 1470 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1471 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1472 | return 0; |
| 1473 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1474 | /* start the search again from the beginning */ |
| 1475 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1476 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1477 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1478 | pos = 1; |
| 1479 | goto dfs_search; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /* remember the last found node for next time */ |
| 1484 | *prev = node; |
| 1485 | *prev_pos = pos; |
| 1486 | |
| 1487 | return pos; |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * @brief Assign (fill) missing node positions. |
| 1492 | * |
| 1493 | * @param[in] set Set to fill positions in. |
| 1494 | * @param[in] root Context root node. |
| 1495 | * @param[in] root_type Context root type. |
| 1496 | * @return LY_ERR |
| 1497 | */ |
| 1498 | static LY_ERR |
| 1499 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1500 | { |
| 1501 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1502 | uint32_t i, tmp_pos = 0; |
| 1503 | |
| 1504 | for (i = 0; i < set->used; ++i) { |
| 1505 | if (!set->val.nodes[i].pos) { |
| 1506 | tmp_node = NULL; |
| 1507 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1508 | case LYXP_NODE_META: |
| 1509 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1510 | if (!tmp_node) { |
| 1511 | LOGINT_RET(root->schema->module->ctx); |
| 1512 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1513 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1514 | case LYXP_NODE_ELEM: |
| 1515 | case LYXP_NODE_TEXT: |
| 1516 | if (!tmp_node) { |
| 1517 | tmp_node = set->val.nodes[i].node; |
| 1518 | } |
| 1519 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1520 | break; |
| 1521 | default: |
| 1522 | /* all roots have position 0 */ |
| 1523 | break; |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | return LY_SUCCESS; |
| 1529 | } |
| 1530 | |
| 1531 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1532 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1533 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1534 | * @param[in] meta Metadata to use. |
| 1535 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1536 | */ |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1537 | static uint32_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1538 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1539 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1540 | uint32_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1541 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1542 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1543 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1544 | ++pos; |
| 1545 | } |
| 1546 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1547 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1548 | return pos; |
| 1549 | } |
| 1550 | |
| 1551 | /** |
| 1552 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1553 | * |
| 1554 | * @param[in] item1 1st node. |
| 1555 | * @param[in] item2 2nd node. |
| 1556 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1557 | */ |
| 1558 | static int |
| 1559 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1560 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1561 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1562 | |
| 1563 | if (item1->pos < item2->pos) { |
| 1564 | return -1; |
| 1565 | } |
| 1566 | |
| 1567 | if (item1->pos > item2->pos) { |
| 1568 | return 1; |
| 1569 | } |
| 1570 | |
| 1571 | /* node positions are equal, the fun case */ |
| 1572 | |
| 1573 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1574 | /* special case since text nodes are actually saved as their parents */ |
| 1575 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1576 | if (item1->type == LYXP_NODE_ELEM) { |
| 1577 | assert(item2->type == LYXP_NODE_TEXT); |
| 1578 | return -1; |
| 1579 | } else { |
| 1580 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1581 | return 1; |
| 1582 | } |
| 1583 | } |
| 1584 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1585 | /* we need meta positions now */ |
| 1586 | if (item1->type == LYXP_NODE_META) { |
| 1587 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1588 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1589 | if (item2->type == LYXP_NODE_META) { |
| 1590 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1591 | } |
| 1592 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1593 | /* 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] | 1594 | /* check for duplicates */ |
| 1595 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1596 | 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] | 1597 | return 0; |
| 1598 | } |
| 1599 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1600 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1601 | /* elem is always first, 2nd node is after it */ |
| 1602 | if (item1->type == LYXP_NODE_ELEM) { |
| 1603 | assert(item2->type != LYXP_NODE_ELEM); |
| 1604 | return -1; |
| 1605 | } |
| 1606 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1607 | /* 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] | 1608 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1609 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1610 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1611 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1612 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1613 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1614 | return 1; |
| 1615 | } |
| 1616 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1617 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1618 | /* 2nd is after 1st */ |
| 1619 | return -1; |
| 1620 | } |
| 1621 | |
| 1622 | /** |
| 1623 | * @brief Set cast for comparisons. |
| 1624 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1625 | * @param[in,out] trg Target set to cast source into. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1626 | * @param[in] src Source set. |
| 1627 | * @param[in] type Target set type. |
| 1628 | * @param[in] src_idx Source set node index. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1629 | * @return LY_SUCCESS on success. |
| 1630 | * @return LY_ERR value on error. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1631 | */ |
| 1632 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1633 | 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] | 1634 | { |
| 1635 | assert(src->type == LYXP_SET_NODE_SET); |
| 1636 | |
| 1637 | set_init(trg, src); |
| 1638 | |
| 1639 | /* insert node into target set */ |
| 1640 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1641 | |
| 1642 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1643 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1644 | } |
| 1645 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1646 | /** |
| 1647 | * @brief Set content canonization for comparisons. |
| 1648 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1649 | * @param[in,out] set Set to canonize. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1650 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1651 | * @return LY_SUCCESS on success. |
| 1652 | * @return LY_ERR value on error. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1653 | */ |
| 1654 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1655 | 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] | 1656 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1657 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1658 | struct lyd_value val; |
| 1659 | struct ly_err_item *err = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1660 | LY_ERR rc; |
| 1661 | |
| 1662 | /* is there anything to canonize even? */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1663 | if (set->type == LYXP_SET_STRING) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1664 | /* do we have a type to use for canonization? */ |
| 1665 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1666 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1667 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1668 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1669 | } |
| 1670 | } |
| 1671 | if (!type) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1672 | /* no canonization needed/possible */ |
| 1673 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1674 | } |
| 1675 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1676 | /* check for built-in types without required canonization */ |
| 1677 | if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) { |
| 1678 | /* string */ |
| 1679 | return LY_SUCCESS; |
| 1680 | } |
| 1681 | if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) { |
| 1682 | /* boolean */ |
| 1683 | return LY_SUCCESS; |
| 1684 | } |
| 1685 | if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) { |
| 1686 | /* enumeration */ |
| 1687 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1688 | } |
| 1689 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1690 | /* print canonized string, ignore errors, the value may not satisfy schema constraints */ |
| 1691 | 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] | 1692 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1693 | ly_err_free(err); |
| 1694 | if (rc) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1695 | /* invalid value, function store automaticaly dealloc value when fail */ |
| 1696 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1699 | /* use the canonized string value */ |
| 1700 | free(set->val.str); |
| 1701 | set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val)); |
| 1702 | type->plugin->free(set->ctx, &val); |
| 1703 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1704 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1705 | return LY_SUCCESS; |
| 1706 | } |
| 1707 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1708 | /** |
| 1709 | * @brief Bubble sort @p set into XPath document order. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1710 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1711 | * |
| 1712 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1713 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1714 | */ |
| 1715 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1716 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1717 | { |
| 1718 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1719 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1720 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1721 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1722 | struct lyxp_set_node item; |
| 1723 | struct lyxp_set_hash_node hnode; |
| 1724 | uint64_t hash; |
| 1725 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1726 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1727 | return 0; |
| 1728 | } |
| 1729 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1730 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1731 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1732 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1733 | |
| 1734 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1735 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1736 | return -1; |
| 1737 | } |
| 1738 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1739 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1740 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1741 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1742 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1743 | |
| 1744 | for (i = 0; i < set->used; ++i) { |
| 1745 | inverted = 0; |
| 1746 | change = 0; |
| 1747 | |
| 1748 | for (j = 1; j < set->used - i; ++j) { |
| 1749 | /* compare node positions */ |
| 1750 | if (inverted) { |
| 1751 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1752 | } else { |
| 1753 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1754 | } |
| 1755 | |
| 1756 | /* swap if needed */ |
| 1757 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1758 | change = 1; |
| 1759 | |
| 1760 | item = set->val.nodes[j - 1]; |
| 1761 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1762 | set->val.nodes[j] = item; |
| 1763 | } else { |
| 1764 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1765 | inverted = !inverted; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | ++ret; |
| 1770 | |
| 1771 | if (!change) { |
| 1772 | break; |
| 1773 | } |
| 1774 | } |
| 1775 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1776 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1777 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1778 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1779 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1780 | |
| 1781 | /* check node hashes */ |
| 1782 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1783 | assert(set->ht); |
| 1784 | for (i = 0; i < set->used; ++i) { |
| 1785 | hnode.node = set->val.nodes[i].node; |
| 1786 | hnode.type = set->val.nodes[i].type; |
| 1787 | |
| 1788 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1789 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1790 | hash = dict_hash_multi(hash, NULL, 0); |
| 1791 | |
| 1792 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | return ret - 1; |
| 1797 | } |
| 1798 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1799 | /** |
| 1800 | * @brief Merge 2 sorted sets into one. |
| 1801 | * |
| 1802 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1803 | * @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] | 1804 | * @return LY_ERR |
| 1805 | */ |
| 1806 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1807 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1808 | { |
| 1809 | uint32_t i, j, k, count, dup_count; |
| 1810 | int cmp; |
| 1811 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1812 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1813 | 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] | 1814 | return LY_EINVAL; |
| 1815 | } |
| 1816 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1817 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1818 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1819 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1820 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1821 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1822 | return LY_SUCCESS; |
| 1823 | } |
| 1824 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1825 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0143b68 | 2021-12-13 17:13:09 +0100 | [diff] [blame] | 1826 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1827 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1828 | |
| 1829 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1830 | 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] | 1831 | return LY_EINT; |
| 1832 | } |
| 1833 | |
| 1834 | #ifndef NDEBUG |
| 1835 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1836 | print_set_debug(trg); |
| 1837 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1838 | print_set_debug(src); |
| 1839 | #endif |
| 1840 | |
| 1841 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1842 | * will likely be wasted on them, too bad) */ |
| 1843 | if (trg->size - trg->used < src->used) { |
| 1844 | trg->size = trg->used + src->used; |
| 1845 | |
| 1846 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1847 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1848 | } |
| 1849 | |
| 1850 | i = 0; |
| 1851 | j = 0; |
| 1852 | count = 0; |
| 1853 | dup_count = 0; |
| 1854 | do { |
| 1855 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1856 | if (!cmp) { |
| 1857 | if (!count) { |
| 1858 | /* duplicate, just skip it */ |
| 1859 | ++i; |
| 1860 | ++j; |
| 1861 | } else { |
| 1862 | /* we are copying something already, so let's copy the duplicate too, |
| 1863 | * we are hoping that afterwards there are some more nodes to |
| 1864 | * copy and this way we can copy them all together */ |
| 1865 | ++count; |
| 1866 | ++dup_count; |
| 1867 | ++i; |
| 1868 | ++j; |
| 1869 | } |
| 1870 | } else if (cmp < 0) { |
| 1871 | /* inserting src node into trg, just remember it for now */ |
| 1872 | ++count; |
| 1873 | ++i; |
| 1874 | |
| 1875 | /* insert the hash now */ |
| 1876 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1877 | } else if (count) { |
| 1878 | copy_nodes: |
| 1879 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1880 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1881 | &trg->val.nodes[j], |
| 1882 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1883 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1884 | |
| 1885 | trg->used += count - dup_count; |
| 1886 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1887 | j += count - dup_count; |
| 1888 | |
| 1889 | count = 0; |
| 1890 | dup_count = 0; |
| 1891 | } else { |
| 1892 | ++j; |
| 1893 | } |
| 1894 | } while ((i < src->used) && (j < trg->used)); |
| 1895 | |
| 1896 | if ((i < src->used) || count) { |
| 1897 | /* insert all the hashes first */ |
| 1898 | for (k = i; k < src->used; ++k) { |
| 1899 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1900 | } |
| 1901 | |
| 1902 | /* loop ended, but we need to copy something at trg end */ |
| 1903 | count += src->used - i; |
| 1904 | i = src->used; |
| 1905 | goto copy_nodes; |
| 1906 | } |
| 1907 | |
| 1908 | /* we are inserting hashes before the actual node insert, which causes |
| 1909 | * situations when there were initially not enough items for a hash table, |
| 1910 | * but even after some were inserted, hash table was not created (during |
| 1911 | * insertion the number of items is not updated yet) */ |
| 1912 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1913 | set_insert_node_hash(trg, NULL, 0); |
| 1914 | } |
| 1915 | |
| 1916 | #ifndef NDEBUG |
| 1917 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1918 | print_set_debug(trg); |
| 1919 | #endif |
| 1920 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1921 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1922 | return LY_SUCCESS; |
| 1923 | } |
| 1924 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1925 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1926 | 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] | 1927 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1928 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1929 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1930 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1931 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1932 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1933 | } |
| 1934 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1935 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1936 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1937 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]), |
| 1938 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1939 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1940 | return LY_ENOT; |
| 1941 | } |
| 1942 | |
| 1943 | return LY_SUCCESS; |
| 1944 | } |
| 1945 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1946 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1947 | 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] | 1948 | { |
| 1949 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1950 | |
| 1951 | /* skip the token */ |
| 1952 | ++(*tok_idx); |
| 1953 | |
| 1954 | return LY_SUCCESS; |
| 1955 | } |
| 1956 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1957 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1958 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1959 | 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] | 1960 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1961 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1962 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1963 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1964 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1965 | } |
| 1966 | return LY_EINCOMPLETE; |
| 1967 | } |
| 1968 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1969 | 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] | 1970 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1971 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1972 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1973 | } |
| 1974 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | return LY_SUCCESS; |
| 1978 | } |
| 1979 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 1980 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1981 | 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] | 1982 | enum lyxp_token want_tok2) |
| 1983 | { |
| 1984 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 1985 | |
| 1986 | /* skip the token */ |
| 1987 | ++(*tok_idx); |
| 1988 | |
| 1989 | return LY_SUCCESS; |
| 1990 | } |
| 1991 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1992 | /** |
| 1993 | * @brief Stack operation push on the repeat array. |
| 1994 | * |
| 1995 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1996 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1997 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1998 | */ |
| 1999 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2000 | 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] | 2001 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2002 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2003 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2004 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2005 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2006 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 2007 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 2008 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 2009 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2010 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2011 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 2012 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 2013 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | /** |
| 2018 | * @brief Reparse Predicate. Logs directly on error. |
| 2019 | * |
| 2020 | * [7] Predicate ::= '[' Expr ']' |
| 2021 | * |
| 2022 | * @param[in] ctx Context for logging. |
| 2023 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2024 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2025 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2026 | * @return LY_ERR |
| 2027 | */ |
| 2028 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2029 | 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] | 2030 | { |
| 2031 | LY_ERR rc; |
| 2032 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2033 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2034 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2035 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2036 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2037 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2038 | LY_CHECK_RET(rc); |
| 2039 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2040 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2041 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2042 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2043 | |
| 2044 | return LY_SUCCESS; |
| 2045 | } |
| 2046 | |
| 2047 | /** |
| 2048 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 2049 | * |
| 2050 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 2051 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 2052 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 2053 | * |
| 2054 | * @param[in] ctx Context for logging. |
| 2055 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2056 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2057 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2058 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 2059 | */ |
| 2060 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2061 | 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] | 2062 | { |
| 2063 | LY_ERR rc; |
| 2064 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2065 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2066 | LY_CHECK_RET(rc); |
| 2067 | |
| 2068 | goto step; |
| 2069 | do { |
| 2070 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2071 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2073 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2074 | LY_CHECK_RET(rc); |
| 2075 | step: |
| 2076 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2079 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2080 | break; |
| 2081 | |
| 2082 | case LYXP_TOKEN_DDOT: |
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 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2086 | case LYXP_TOKEN_AXISNAME: |
| 2087 | ++(*tok_idx); |
| 2088 | |
| 2089 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON); |
| 2090 | LY_CHECK_RET(rc); |
| 2091 | |
| 2092 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2093 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2094 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2095 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2096 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2097 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | 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] | 2099 | 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] | 2100 | return LY_EVALID; |
| 2101 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2102 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) { |
| 2103 | goto reparse_nodetype; |
| 2104 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2105 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2106 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2107 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2109 | |
| 2110 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2111 | reparse_nodetype: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2112 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2113 | |
| 2114 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2115 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2116 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2117 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2118 | |
| 2119 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2120 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2121 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2122 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2123 | |
| 2124 | reparse_predicate: |
| 2125 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2126 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2127 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2128 | LY_CHECK_RET(rc); |
| 2129 | } |
| 2130 | break; |
| 2131 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2132 | 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] | 2133 | return LY_EVALID; |
| 2134 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2135 | } 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] | 2136 | |
| 2137 | return LY_SUCCESS; |
| 2138 | } |
| 2139 | |
| 2140 | /** |
| 2141 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2142 | * |
| 2143 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2144 | * |
| 2145 | * @param[in] ctx Context for logging. |
| 2146 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2147 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2148 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | * @return LY_ERR |
| 2150 | */ |
| 2151 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2152 | 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] | 2153 | { |
| 2154 | LY_ERR rc; |
| 2155 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2156 | 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] | 2157 | |
| 2158 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2159 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2160 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2161 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2162 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2164 | return LY_SUCCESS; |
| 2165 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2166 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | case LYXP_TOKEN_DOT: |
| 2168 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2169 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2170 | case LYXP_TOKEN_AT: |
| 2171 | case LYXP_TOKEN_NAMETEST: |
| 2172 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2173 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2174 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2175 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2176 | default: |
| 2177 | break; |
| 2178 | } |
| 2179 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2180 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2181 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2182 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2183 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2184 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2185 | LY_CHECK_RET(rc); |
| 2186 | } |
| 2187 | |
| 2188 | return LY_SUCCESS; |
| 2189 | } |
| 2190 | |
| 2191 | /** |
| 2192 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2193 | * |
| 2194 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2195 | * |
| 2196 | * @param[in] ctx Context for logging. |
| 2197 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2198 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2199 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | * @return LY_ERR |
| 2201 | */ |
| 2202 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2203 | 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] | 2204 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2205 | int8_t min_arg_count = -1; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2206 | uint32_t arg_count, max_arg_count = 0, func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2207 | LY_ERR rc; |
| 2208 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2209 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2210 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | func_tok_idx = *tok_idx; |
| 2212 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2213 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2215 | min_arg_count = 1; |
| 2216 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | min_arg_count = 1; |
| 2219 | max_arg_count = 1; |
| 2220 | } |
| 2221 | break; |
| 2222 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | min_arg_count = 1; |
| 2225 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | min_arg_count = 0; |
| 2228 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | min_arg_count = 0; |
| 2231 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | min_arg_count = 0; |
| 2234 | max_arg_count = 0; |
| 2235 | } |
| 2236 | break; |
| 2237 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2239 | min_arg_count = 1; |
| 2240 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2242 | min_arg_count = 0; |
| 2243 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2244 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2245 | min_arg_count = 1; |
| 2246 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2248 | min_arg_count = 1; |
| 2249 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2250 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2251 | min_arg_count = 1; |
| 2252 | max_arg_count = 1; |
| 2253 | } |
| 2254 | break; |
| 2255 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2258 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | min_arg_count = 0; |
| 2261 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | min_arg_count = 0; |
| 2264 | max_arg_count = 1; |
| 2265 | } |
| 2266 | break; |
| 2267 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2268 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | min_arg_count = 1; |
| 2270 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | min_arg_count = 1; |
| 2273 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2274 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2275 | min_arg_count = 0; |
| 2276 | max_arg_count = 0; |
| 2277 | } |
| 2278 | break; |
| 2279 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2280 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2281 | min_arg_count = 2; |
| 2282 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2283 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2284 | min_arg_count = 0; |
| 2285 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2286 | } 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] | 2287 | min_arg_count = 2; |
| 2288 | max_arg_count = 2; |
| 2289 | } |
| 2290 | break; |
| 2291 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2292 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2293 | min_arg_count = 2; |
| 2294 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2295 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | min_arg_count = 3; |
| 2297 | max_arg_count = 3; |
| 2298 | } |
| 2299 | break; |
| 2300 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2301 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2302 | min_arg_count = 0; |
| 2303 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2304 | } 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] | 2305 | min_arg_count = 1; |
| 2306 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2307 | } 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] | 2308 | min_arg_count = 2; |
| 2309 | max_arg_count = 2; |
| 2310 | } |
| 2311 | break; |
| 2312 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2313 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2314 | min_arg_count = 2; |
| 2315 | max_arg_count = 2; |
| 2316 | } |
| 2317 | break; |
| 2318 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2320 | min_arg_count = 2; |
| 2321 | max_arg_count = 2; |
| 2322 | } |
| 2323 | break; |
| 2324 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2325 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2326 | min_arg_count = 0; |
| 2327 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | min_arg_count = 0; |
| 2330 | max_arg_count = 1; |
| 2331 | } |
| 2332 | break; |
| 2333 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2334 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2335 | min_arg_count = 0; |
| 2336 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2337 | } 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] | 2338 | min_arg_count = 2; |
| 2339 | max_arg_count = 2; |
| 2340 | } |
| 2341 | break; |
| 2342 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2343 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2344 | min_arg_count = 2; |
| 2345 | max_arg_count = 2; |
| 2346 | } |
| 2347 | break; |
| 2348 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2349 | 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] | 2350 | min_arg_count = 2; |
| 2351 | max_arg_count = 2; |
| 2352 | } |
| 2353 | break; |
| 2354 | } |
| 2355 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2356 | 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] | 2357 | return LY_EINVAL; |
| 2358 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2359 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2360 | |
| 2361 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2362 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2363 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2364 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2365 | |
| 2366 | /* ( Expr ( ',' Expr )* )? */ |
| 2367 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2368 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2369 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2370 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2371 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2372 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2373 | LY_CHECK_RET(rc); |
| 2374 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2375 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2376 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2377 | |
| 2378 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2379 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2380 | LY_CHECK_RET(rc); |
| 2381 | } |
| 2382 | |
| 2383 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2384 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2385 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2386 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2387 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2388 | 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] | 2389 | 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] | 2390 | return LY_EVALID; |
| 2391 | } |
| 2392 | |
| 2393 | return LY_SUCCESS; |
| 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * @brief Reparse PathExpr. Logs directly on error. |
| 2398 | * |
| 2399 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2400 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2401 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2402 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2403 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2404 | * |
| 2405 | * @param[in] ctx Context for logging. |
| 2406 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2407 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2408 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2409 | * @return LY_ERR |
| 2410 | */ |
| 2411 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2412 | 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] | 2413 | { |
| 2414 | LY_ERR rc; |
| 2415 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2417 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2418 | } |
| 2419 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2420 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2421 | case LYXP_TOKEN_PAR1: |
| 2422 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2423 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2424 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2425 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2426 | LY_CHECK_RET(rc); |
| 2427 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2428 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2429 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2430 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2431 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2432 | case LYXP_TOKEN_DOT: |
| 2433 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2434 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2435 | case LYXP_TOKEN_AT: |
| 2436 | case LYXP_TOKEN_NAMETEST: |
| 2437 | case LYXP_TOKEN_NODETYPE: |
| 2438 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2439 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2440 | LY_CHECK_RET(rc); |
| 2441 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2442 | case LYXP_TOKEN_VARREF: |
| 2443 | /* VariableReference */ |
| 2444 | ++(*tok_idx); |
| 2445 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2446 | case LYXP_TOKEN_FUNCNAME: |
| 2447 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2448 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2449 | LY_CHECK_RET(rc); |
| 2450 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2451 | case LYXP_TOKEN_OPER_PATH: |
| 2452 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2454 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2455 | LY_CHECK_RET(rc); |
| 2456 | break; |
| 2457 | case LYXP_TOKEN_LITERAL: |
| 2458 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2459 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2460 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2461 | case LYXP_TOKEN_NUMBER: |
| 2462 | /* Number */ |
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 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2466 | 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] | 2467 | return LY_EVALID; |
| 2468 | } |
| 2469 | |
| 2470 | return LY_SUCCESS; |
| 2471 | |
| 2472 | predicate: |
| 2473 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2474 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2475 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2476 | LY_CHECK_RET(rc); |
| 2477 | } |
| 2478 | |
| 2479 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2480 | 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] | 2481 | |
| 2482 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2483 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2484 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2485 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2486 | LY_CHECK_RET(rc); |
| 2487 | } |
| 2488 | |
| 2489 | return LY_SUCCESS; |
| 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2494 | * |
| 2495 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2496 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2497 | * |
| 2498 | * @param[in] ctx Context for logging. |
| 2499 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2501 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2502 | * @return LY_ERR |
| 2503 | */ |
| 2504 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2505 | 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] | 2506 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2507 | uint32_t prev_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2508 | LY_ERR rc; |
| 2509 | |
| 2510 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2512 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2513 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2514 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2519 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2520 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | LY_CHECK_RET(rc); |
| 2522 | |
| 2523 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2524 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2525 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2526 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2527 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2528 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2529 | LY_CHECK_RET(rc); |
| 2530 | } |
| 2531 | |
| 2532 | return LY_SUCCESS; |
| 2533 | } |
| 2534 | |
| 2535 | /** |
| 2536 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2537 | * |
| 2538 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2539 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2540 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2541 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2542 | * | MultiplicativeExpr '*' UnaryExpr |
| 2543 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2544 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2545 | * |
| 2546 | * @param[in] ctx Context for logging. |
| 2547 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2548 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2549 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2550 | * @return LY_ERR |
| 2551 | */ |
| 2552 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2553 | 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] | 2554 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2555 | uint32_t prev_add_exp, prev_mul_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2556 | LY_ERR rc; |
| 2557 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2558 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2559 | goto reparse_multiplicative_expr; |
| 2560 | |
| 2561 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2562 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2563 | ((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] | 2564 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2565 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2566 | |
| 2567 | reparse_multiplicative_expr: |
| 2568 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2569 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2570 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2571 | LY_CHECK_RET(rc); |
| 2572 | |
| 2573 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2574 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2575 | ((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] | 2576 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2577 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2578 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2579 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2580 | LY_CHECK_RET(rc); |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | return LY_SUCCESS; |
| 2585 | } |
| 2586 | |
| 2587 | /** |
| 2588 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2589 | * |
| 2590 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2591 | * | EqualityExpr '!=' RelationalExpr |
| 2592 | * [14] RelationalExpr ::= AdditiveExpr |
| 2593 | * | RelationalExpr '<' AdditiveExpr |
| 2594 | * | RelationalExpr '>' AdditiveExpr |
| 2595 | * | RelationalExpr '<=' AdditiveExpr |
| 2596 | * | RelationalExpr '>=' AdditiveExpr |
| 2597 | * |
| 2598 | * @param[in] ctx Context for logging. |
| 2599 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2600 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2601 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2602 | * @return LY_ERR |
| 2603 | */ |
| 2604 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2605 | 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] | 2606 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2607 | uint32_t prev_eq_exp, prev_rel_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2608 | LY_ERR rc; |
| 2609 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2610 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2611 | goto reparse_additive_expr; |
| 2612 | |
| 2613 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2614 | 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] | 2615 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2616 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | |
| 2618 | reparse_additive_expr: |
| 2619 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2620 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2621 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2622 | LY_CHECK_RET(rc); |
| 2623 | |
| 2624 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2625 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2626 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2627 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2628 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2629 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2630 | LY_CHECK_RET(rc); |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | return LY_SUCCESS; |
| 2635 | } |
| 2636 | |
| 2637 | /** |
| 2638 | * @brief Reparse OrExpr. Logs directly on error. |
| 2639 | * |
| 2640 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2641 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2642 | * |
| 2643 | * @param[in] ctx Context for logging. |
| 2644 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2645 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2646 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2647 | * @return LY_ERR |
| 2648 | */ |
| 2649 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2650 | 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] | 2651 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2652 | uint32_t prev_or_exp, prev_and_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2653 | LY_ERR rc; |
| 2654 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2655 | ++depth; |
| 2656 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2657 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2658 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2659 | goto reparse_equality_expr; |
| 2660 | |
| 2661 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2662 | 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] | 2663 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2664 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2665 | |
| 2666 | reparse_equality_expr: |
| 2667 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2668 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2669 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2670 | LY_CHECK_RET(rc); |
| 2671 | |
| 2672 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2673 | 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] | 2674 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2675 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2676 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2677 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2678 | LY_CHECK_RET(rc); |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | return LY_SUCCESS; |
| 2683 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2684 | |
| 2685 | /** |
| 2686 | * @brief Parse NCName. |
| 2687 | * |
| 2688 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2689 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2690 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2691 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2692 | parse_ncname(const char *ncname) |
| 2693 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2694 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2695 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2696 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2697 | |
| 2698 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2699 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2700 | return len; |
| 2701 | } |
| 2702 | |
| 2703 | do { |
| 2704 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2705 | if (!*ncname) { |
| 2706 | break; |
| 2707 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2708 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2709 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2710 | |
| 2711 | return len; |
| 2712 | } |
| 2713 | |
| 2714 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2715 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2716 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2717 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2718 | * @param[in] exp Expression to use. |
| 2719 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2720 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2721 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2722 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2723 | */ |
| 2724 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2725 | 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] | 2726 | { |
| 2727 | uint32_t prev; |
| 2728 | |
| 2729 | if (exp->used == exp->size) { |
| 2730 | prev = exp->size; |
| 2731 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2732 | if (prev > exp->size) { |
| 2733 | LOGINT(ctx); |
| 2734 | return LY_EINT; |
| 2735 | } |
| 2736 | |
| 2737 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2738 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2739 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2740 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2741 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2742 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2743 | } |
| 2744 | |
| 2745 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2746 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2747 | exp->tok_len[exp->used] = tok_len; |
| 2748 | ++exp->used; |
| 2749 | return LY_SUCCESS; |
| 2750 | } |
| 2751 | |
| 2752 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2753 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2754 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2755 | uint32_t i; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2756 | |
| 2757 | if (!expr) { |
| 2758 | return; |
| 2759 | } |
| 2760 | |
| 2761 | lydict_remove(ctx, expr->expr); |
| 2762 | free(expr->tokens); |
| 2763 | free(expr->tok_pos); |
| 2764 | free(expr->tok_len); |
| 2765 | if (expr->repeat) { |
| 2766 | for (i = 0; i < expr->used; ++i) { |
| 2767 | free(expr->repeat[i]); |
| 2768 | } |
| 2769 | } |
| 2770 | free(expr->repeat); |
| 2771 | free(expr); |
| 2772 | } |
| 2773 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2774 | /** |
| 2775 | * @brief Parse Axis name. |
| 2776 | * |
| 2777 | * @param[in] str String to parse. |
| 2778 | * @param[in] str_len Length of @p str. |
| 2779 | * @return LY_SUCCESS if an axis. |
| 2780 | * @return LY_ENOT otherwise. |
| 2781 | */ |
| 2782 | static LY_ERR |
| 2783 | expr_parse_axis(const char *str, size_t str_len) |
| 2784 | { |
| 2785 | switch (str_len) { |
| 2786 | case 4: |
| 2787 | if (!strncmp("self", str, str_len)) { |
| 2788 | return LY_SUCCESS; |
| 2789 | } |
| 2790 | break; |
| 2791 | case 5: |
| 2792 | if (!strncmp("child", str, str_len)) { |
| 2793 | return LY_SUCCESS; |
| 2794 | } |
| 2795 | break; |
| 2796 | case 6: |
| 2797 | if (!strncmp("parent", str, str_len)) { |
| 2798 | return LY_SUCCESS; |
| 2799 | } |
| 2800 | break; |
| 2801 | case 8: |
| 2802 | if (!strncmp("ancestor", str, str_len)) { |
| 2803 | return LY_SUCCESS; |
| 2804 | } |
| 2805 | break; |
| 2806 | case 9: |
| 2807 | if (!strncmp("attribute", str, str_len)) { |
| 2808 | return LY_SUCCESS; |
| 2809 | } else if (!strncmp("following", str, str_len)) { |
| 2810 | return LY_SUCCESS; |
| 2811 | } else if (!strncmp("namespace", str, str_len)) { |
| 2812 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2813 | return LY_ENOT; |
| 2814 | } else if (!strncmp("preceding", str, str_len)) { |
| 2815 | return LY_SUCCESS; |
| 2816 | } |
| 2817 | break; |
| 2818 | case 10: |
| 2819 | if (!strncmp("descendant", str, str_len)) { |
| 2820 | return LY_SUCCESS; |
| 2821 | } |
| 2822 | break; |
| 2823 | case 16: |
| 2824 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2825 | return LY_SUCCESS; |
| 2826 | } |
| 2827 | break; |
| 2828 | case 17: |
| 2829 | if (!strncmp("following-sibling", str, str_len)) { |
| 2830 | return LY_SUCCESS; |
| 2831 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2832 | return LY_SUCCESS; |
| 2833 | } |
| 2834 | break; |
| 2835 | case 18: |
| 2836 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2837 | return LY_SUCCESS; |
| 2838 | } |
| 2839 | break; |
| 2840 | } |
| 2841 | |
| 2842 | return LY_ENOT; |
| 2843 | } |
| 2844 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2845 | LY_ERR |
| 2846 | 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] | 2847 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2848 | LY_ERR ret = LY_SUCCESS; |
| 2849 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2850 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2851 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2852 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2853 | uint32_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2854 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2855 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2856 | assert(expr_p); |
| 2857 | |
| 2858 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2859 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2860 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2864 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2865 | } |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2866 | if (expr_len > UINT32_MAX) { |
| 2867 | 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] | 2868 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2872 | expr = calloc(1, sizeof *expr); |
| 2873 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2874 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2875 | expr->used = 0; |
| 2876 | expr->size = LYXP_EXPR_SIZE_START; |
| 2877 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2878 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2879 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2880 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2881 | 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] | 2882 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2883 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2884 | 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] | 2885 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2886 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2887 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2888 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2889 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2890 | ++parsed; |
| 2891 | } |
| 2892 | |
| 2893 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2894 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2895 | |
| 2896 | /* '(' */ |
| 2897 | tok_len = 1; |
| 2898 | tok_type = LYXP_TOKEN_PAR1; |
| 2899 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2900 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2901 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2902 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2903 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2904 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2905 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2906 | /* it is NodeType after all */ |
| 2907 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2908 | |
| 2909 | prev_ntype_check = 0; |
| 2910 | prev_func_check = 0; |
| 2911 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2912 | /* it is FunctionName after all */ |
| 2913 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2914 | |
| 2915 | prev_ntype_check = 0; |
| 2916 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2917 | } |
| 2918 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2919 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2920 | |
| 2921 | /* ')' */ |
| 2922 | tok_len = 1; |
| 2923 | tok_type = LYXP_TOKEN_PAR2; |
| 2924 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2925 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2926 | |
| 2927 | /* '[' */ |
| 2928 | tok_len = 1; |
| 2929 | tok_type = LYXP_TOKEN_BRACK1; |
| 2930 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2931 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2932 | |
| 2933 | /* ']' */ |
| 2934 | tok_len = 1; |
| 2935 | tok_type = LYXP_TOKEN_BRACK2; |
| 2936 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2937 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2938 | |
| 2939 | /* '..' */ |
| 2940 | tok_len = 2; |
| 2941 | tok_type = LYXP_TOKEN_DDOT; |
| 2942 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2943 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2944 | |
| 2945 | /* '.' */ |
| 2946 | tok_len = 1; |
| 2947 | tok_type = LYXP_TOKEN_DOT; |
| 2948 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2949 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2950 | |
| 2951 | /* '@' */ |
| 2952 | tok_len = 1; |
| 2953 | tok_type = LYXP_TOKEN_AT; |
| 2954 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2955 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2956 | |
| 2957 | /* ',' */ |
| 2958 | tok_len = 1; |
| 2959 | tok_type = LYXP_TOKEN_COMMA; |
| 2960 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2961 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2962 | |
| 2963 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2964 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2965 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2966 | 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] | 2967 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2968 | ++tok_len; |
| 2969 | tok_type = LYXP_TOKEN_LITERAL; |
| 2970 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2971 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2972 | |
| 2973 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2974 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2975 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2976 | 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] | 2977 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2978 | ++tok_len; |
| 2979 | tok_type = LYXP_TOKEN_LITERAL; |
| 2980 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2981 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2982 | |
| 2983 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2984 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2985 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2986 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2987 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2988 | } |
| 2989 | tok_type = LYXP_TOKEN_NUMBER; |
| 2990 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2991 | } else if (expr_str[parsed] == '$') { |
| 2992 | |
| 2993 | /* VariableReference */ |
| 2994 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2995 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2996 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 2997 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 2998 | tok_len = ncname_len; |
| 2999 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 3000 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 3001 | error); |
| 3002 | tok_type = LYXP_TOKEN_VARREF; |
| 3003 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3004 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3005 | |
| 3006 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3007 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3008 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3009 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3010 | } else { |
| 3011 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3012 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3013 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3014 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3015 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3016 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3017 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3018 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3019 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 3020 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3021 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3022 | |
| 3023 | /* Operator '<=', '>=' */ |
| 3024 | tok_len = 2; |
| 3025 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3026 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3027 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3028 | |
| 3029 | /* Operator '|' */ |
| 3030 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3031 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3032 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3033 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3034 | |
| 3035 | /* Operator '+', '-' */ |
| 3036 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3037 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3038 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3039 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3040 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3041 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3042 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3043 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3044 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3045 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3046 | |
| 3047 | /* Operator '<', '>' */ |
| 3048 | tok_len = 1; |
| 3049 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3050 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3051 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3052 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3053 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3054 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3055 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3056 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3057 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3058 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3059 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3060 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3061 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3062 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3063 | |
| 3064 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3065 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3066 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3067 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3068 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3069 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3070 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3071 | tok_type = LYXP_TOKEN_OPER_LOG; |
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], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3074 | tok_len = 3; |
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], "mod", 3) || !strncmp(&expr_str[parsed], "div", 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_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3080 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3081 | } else if (prev_ntype_check || prev_func_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3082 | 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] | 3083 | 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] | 3084 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3085 | goto error; |
| 3086 | } else { |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3087 | 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] | 3088 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3089 | goto error; |
| 3090 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3091 | } else { |
| 3092 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3093 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3094 | if (expr_str[parsed] == '*') { |
| 3095 | ncname_len = 1; |
| 3096 | } else { |
| 3097 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3098 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3099 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3100 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3101 | tok_len = ncname_len; |
| 3102 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3103 | has_axis = 0; |
| 3104 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3105 | /* axis */ |
| 3106 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
| 3107 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error); |
| 3108 | tok_type = LYXP_TOKEN_AXISNAME; |
| 3109 | |
| 3110 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3111 | parsed += tok_len; |
| 3112 | |
| 3113 | /* '::' */ |
| 3114 | tok_len = 2; |
| 3115 | tok_type = LYXP_TOKEN_DCOLON; |
| 3116 | |
| 3117 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3118 | parsed += tok_len; |
| 3119 | |
| 3120 | if (expr_str[parsed] == '*') { |
| 3121 | ncname_len = 1; |
| 3122 | } else { |
| 3123 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3124 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3125 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3126 | } |
| 3127 | tok_len = ncname_len; |
| 3128 | |
| 3129 | has_axis = 1; |
| 3130 | } |
| 3131 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3132 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3133 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3134 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3135 | ++tok_len; |
| 3136 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3137 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 3138 | 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] | 3139 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3140 | tok_len += ncname_len; |
| 3141 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3142 | /* remove old flags to prevent ambiguities */ |
| 3143 | prev_ntype_check = 0; |
| 3144 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3145 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3146 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3147 | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 3148 | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
| 3149 | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3150 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3155 | 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] | 3156 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3157 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3158 | ++parsed; |
| 3159 | } |
| 3160 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3161 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3162 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3163 | if (reparse) { |
| 3164 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3165 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 3166 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3167 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3168 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 3169 | 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] | 3170 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3171 | 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] | 3172 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3173 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3174 | goto error; |
| 3175 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3176 | } |
| 3177 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3178 | print_expr_struct_debug(expr); |
| 3179 | *expr_p = expr; |
| 3180 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3181 | |
| 3182 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3183 | lyxp_expr_free(ctx, expr); |
| 3184 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3185 | } |
| 3186 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3187 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3188 | 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] | 3189 | struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3190 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3191 | LY_ERR ret = LY_SUCCESS; |
| 3192 | struct lyxp_expr *dup = NULL; |
Michal Vasko | d59039d | 2022-09-09 09:07:30 +0200 | [diff] [blame] | 3193 | uint32_t used = 0, i, j, expr_len; |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3194 | const char *expr_start; |
| 3195 | |
| 3196 | 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] | 3197 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3198 | if (!exp) { |
| 3199 | goto cleanup; |
| 3200 | } |
| 3201 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3202 | if (!start_idx && !end_idx) { |
| 3203 | end_idx = exp->used - 1; |
| 3204 | } |
| 3205 | |
| 3206 | expr_start = exp->expr + exp->tok_pos[start_idx]; |
| 3207 | expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx]; |
| 3208 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3209 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3210 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3211 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3212 | if (exp->used) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3213 | used = (end_idx - start_idx) + 1; |
| 3214 | |
| 3215 | dup->tokens = malloc(used * sizeof *dup->tokens); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3216 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3217 | memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3218 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3219 | dup->tok_pos = malloc(used * sizeof *dup->tok_pos); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3220 | 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] | 3221 | 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] | 3222 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3223 | if (start_idx) { |
| 3224 | /* fix the indices in the expression */ |
| 3225 | for (i = 0; i < used; ++i) { |
| 3226 | dup->tok_pos[i] -= expr_start - exp->expr; |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | dup->tok_len = malloc(used * sizeof *dup->tok_len); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3231 | 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] | 3232 | 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] | 3233 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3234 | if (exp->repeat) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3235 | dup->repeat = malloc(used * sizeof *dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3236 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3237 | for (i = start_idx; i <= end_idx; ++i) { |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3238 | if (!exp->repeat[i]) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3239 | dup->repeat[i - start_idx] = NULL; |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3240 | } else { |
| 3241 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3242 | /* the ending 0 as well */ |
| 3243 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3244 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3245 | dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat); |
| 3246 | LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3247 | 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] | 3248 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3249 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3250 | } |
| 3251 | } |
| 3252 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3253 | dup->used = used; |
| 3254 | dup->size = used; |
| 3255 | |
| 3256 | /* copy only subexpression */ |
| 3257 | 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] | 3258 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3259 | cleanup: |
| 3260 | if (ret) { |
| 3261 | lyxp_expr_free(ctx, dup); |
| 3262 | } else { |
| 3263 | *dup_p = dup; |
| 3264 | } |
| 3265 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3266 | } |
| 3267 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3268 | /** |
| 3269 | * @brief Get the last-added schema node that is currently in the context. |
| 3270 | * |
| 3271 | * @param[in] set Set to search in. |
| 3272 | * @return Last-added schema context node, NULL if no node is in context. |
| 3273 | */ |
| 3274 | static struct lysc_node * |
| 3275 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3276 | { |
| 3277 | uint32_t i; |
| 3278 | |
| 3279 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3280 | return NULL; |
| 3281 | } |
| 3282 | |
| 3283 | i = set->used; |
| 3284 | do { |
| 3285 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3286 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3287 | /* if there are more, simply return the first found (last added) */ |
| 3288 | return set->val.scnodes[i].scnode; |
| 3289 | } |
| 3290 | } while (i); |
| 3291 | |
| 3292 | return NULL; |
| 3293 | } |
| 3294 | |
| 3295 | /** |
| 3296 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3297 | * |
| 3298 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3299 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3300 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3301 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3302 | warn_is_numeric_type(struct lysc_type *type) |
| 3303 | { |
| 3304 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3305 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3306 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3307 | |
| 3308 | switch (type->basetype) { |
| 3309 | case LY_TYPE_DEC64: |
| 3310 | case LY_TYPE_INT8: |
| 3311 | case LY_TYPE_UINT8: |
| 3312 | case LY_TYPE_INT16: |
| 3313 | case LY_TYPE_UINT16: |
| 3314 | case LY_TYPE_INT32: |
| 3315 | case LY_TYPE_UINT32: |
| 3316 | case LY_TYPE_INT64: |
| 3317 | case LY_TYPE_UINT64: |
| 3318 | return 1; |
| 3319 | case LY_TYPE_UNION: |
| 3320 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3321 | LY_ARRAY_FOR(uni->types, u) { |
| 3322 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3323 | if (ret) { |
| 3324 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3325 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3326 | } |
| 3327 | } |
| 3328 | /* did not find any suitable type */ |
| 3329 | return 0; |
| 3330 | case LY_TYPE_LEAFREF: |
| 3331 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3332 | default: |
| 3333 | return 0; |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | /** |
| 3338 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3339 | * |
| 3340 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3341 | * @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] | 3342 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3343 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3344 | warn_is_string_type(struct lysc_type *type) |
| 3345 | { |
| 3346 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3347 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3348 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3349 | |
| 3350 | switch (type->basetype) { |
| 3351 | case LY_TYPE_BITS: |
| 3352 | case LY_TYPE_ENUM: |
| 3353 | case LY_TYPE_IDENT: |
| 3354 | case LY_TYPE_INST: |
| 3355 | case LY_TYPE_STRING: |
| 3356 | return 1; |
| 3357 | case LY_TYPE_UNION: |
| 3358 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3359 | LY_ARRAY_FOR(uni->types, u) { |
| 3360 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3361 | if (ret) { |
| 3362 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3363 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3364 | } |
| 3365 | } |
| 3366 | /* did not find any suitable type */ |
| 3367 | return 0; |
| 3368 | case LY_TYPE_LEAFREF: |
| 3369 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3370 | default: |
| 3371 | return 0; |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | /** |
| 3376 | * @brief Test whether a type is one specific type. |
| 3377 | * |
| 3378 | * @param[in] type Type to test. |
| 3379 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3380 | * @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] | 3381 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3382 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3383 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3384 | { |
| 3385 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3386 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3387 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3388 | |
| 3389 | if (type->basetype == base) { |
| 3390 | return 1; |
| 3391 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3392 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3393 | LY_ARRAY_FOR(uni->types, u) { |
| 3394 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3395 | if (ret) { |
| 3396 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3397 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3398 | } |
| 3399 | } |
| 3400 | /* did not find any suitable type */ |
| 3401 | return 0; |
| 3402 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3403 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3404 | } |
| 3405 | |
| 3406 | return 0; |
| 3407 | } |
| 3408 | |
| 3409 | /** |
| 3410 | * @brief Get next type of a (union) type. |
| 3411 | * |
| 3412 | * @param[in] type Base type. |
| 3413 | * @param[in] prev_type Previously returned type. |
| 3414 | * @return Next type or NULL. |
| 3415 | */ |
| 3416 | static struct lysc_type * |
| 3417 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3418 | { |
| 3419 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3420 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3421 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3422 | |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3423 | if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3424 | uni = (struct lysc_type_union *)type; |
| 3425 | if (!prev_type) { |
| 3426 | return uni->types[0]; |
| 3427 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3428 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3429 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3430 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3431 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3432 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3433 | found = 1; |
| 3434 | } |
| 3435 | } |
| 3436 | return NULL; |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3437 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3438 | if (prev_type) { |
| 3439 | assert(type == prev_type); |
| 3440 | return NULL; |
| 3441 | } else { |
| 3442 | return type; |
| 3443 | } |
| 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | /** |
| 3448 | * @brief Test whether 2 types have a common type. |
| 3449 | * |
| 3450 | * @param[in] type1 First type. |
| 3451 | * @param[in] type2 Second type. |
| 3452 | * @return 1 if they do, 0 otherwise. |
| 3453 | */ |
| 3454 | static int |
| 3455 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3456 | { |
| 3457 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3458 | |
| 3459 | t1 = NULL; |
| 3460 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3461 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3462 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3463 | } else { |
| 3464 | rt1 = t1; |
| 3465 | } |
| 3466 | |
| 3467 | t2 = NULL; |
| 3468 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3469 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3470 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3471 | } else { |
| 3472 | rt2 = t2; |
| 3473 | } |
| 3474 | |
| 3475 | if (rt2->basetype == rt1->basetype) { |
| 3476 | /* match found */ |
| 3477 | return 1; |
| 3478 | } |
| 3479 | } |
| 3480 | } |
| 3481 | |
| 3482 | return 0; |
| 3483 | } |
| 3484 | |
| 3485 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3486 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3487 | * |
| 3488 | * @param[in] ctx Context for logging. |
| 3489 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3490 | * @param[in] subexpr Subexpression start. |
| 3491 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3492 | * @param[in] cur_scnode Expression context node. |
| 3493 | */ |
| 3494 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3495 | 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] | 3496 | const struct lysc_node *cur_scnode) |
| 3497 | { |
| 3498 | char *path; |
| 3499 | |
| 3500 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3501 | 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] | 3502 | tok_pos, subexpr_len, subexpr, path); |
| 3503 | free(path); |
| 3504 | } |
| 3505 | |
| 3506 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3507 | * @brief Check both operands of comparison operators. |
| 3508 | * |
| 3509 | * @param[in] ctx Context for errors. |
| 3510 | * @param[in] set1 First operand set. |
| 3511 | * @param[in] set2 Second operand set. |
| 3512 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3513 | * @param[in] expr Start of the expression to print with the warning. |
| 3514 | * @param[in] tok_pos Token position. |
| 3515 | */ |
| 3516 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3517 | 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] | 3518 | uint32_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3519 | { |
| 3520 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3521 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3522 | |
| 3523 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3524 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3525 | |
| 3526 | if (!node1 && !node2) { |
| 3527 | /* no node-sets involved, nothing to do */ |
| 3528 | return; |
| 3529 | } |
| 3530 | |
| 3531 | if (node1) { |
| 3532 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3533 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3534 | warning = 1; |
| 3535 | leaves = 0; |
| 3536 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3537 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3538 | warning = 1; |
| 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | if (node2) { |
| 3543 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3544 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3545 | warning = 1; |
| 3546 | leaves = 0; |
| 3547 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3548 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3549 | warning = 1; |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3554 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3555 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3556 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3557 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3558 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3559 | warning = 1; |
| 3560 | } |
| 3561 | } |
| 3562 | |
| 3563 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3564 | 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] | 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | /** |
| 3569 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3570 | * |
| 3571 | * @param[in] exp Parsed XPath expression. |
| 3572 | * @param[in] set Set with the leaf/leaf-list. |
| 3573 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3574 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3575 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3576 | */ |
| 3577 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3578 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp, |
| 3579 | uint32_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3580 | { |
| 3581 | struct lysc_node *scnode; |
| 3582 | struct lysc_type *type; |
| 3583 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3584 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3585 | LY_ERR rc; |
| 3586 | struct ly_err_item *err = NULL; |
| 3587 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3588 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3589 | ((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] | 3590 | /* check that the node can have the specified value */ |
| 3591 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3592 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3593 | } else { |
| 3594 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3595 | } |
| 3596 | if (!value) { |
| 3597 | LOGMEM(set->ctx); |
| 3598 | return; |
| 3599 | } |
| 3600 | |
| 3601 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3602 | 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] | 3603 | " 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] | 3604 | 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] | 3605 | (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] | 3606 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3610 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3611 | 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] | 3612 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3613 | if (rc == LY_EINCOMPLETE) { |
| 3614 | rc = LY_SUCCESS; |
| 3615 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3616 | |
| 3617 | if (err) { |
| 3618 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3619 | ly_err_free(err); |
| 3620 | } else if (rc != LY_SUCCESS) { |
| 3621 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3622 | } |
| 3623 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3624 | 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] | 3625 | (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] | 3626 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3627 | } else { |
| 3628 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3629 | } |
| 3630 | } |
| 3631 | free(value); |
| 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | /* |
| 3636 | * XPath functions |
| 3637 | */ |
| 3638 | |
| 3639 | /** |
| 3640 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3641 | * depending on whether the first node bit value from the second argument is set. |
| 3642 | * |
| 3643 | * @param[in] args Array of arguments. |
| 3644 | * @param[in] arg_count Count of elements in @p args. |
| 3645 | * @param[in,out] set Context and result set at the same time. |
| 3646 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3647 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3648 | */ |
| 3649 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3650 | 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] | 3651 | { |
| 3652 | struct lyd_node_term *leaf; |
| 3653 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3654 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3655 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3656 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3657 | |
| 3658 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3659 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3660 | 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] | 3661 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3662 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3663 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3664 | sleaf->name); |
| 3665 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3666 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3667 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3671 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3672 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3673 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3674 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3675 | 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] | 3676 | } |
| 3677 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3678 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3679 | return rc; |
| 3680 | } |
| 3681 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3682 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3683 | 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] | 3684 | return LY_EVALID; |
| 3685 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3686 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3687 | LY_CHECK_RET(rc); |
| 3688 | |
| 3689 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3690 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3691 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3692 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3693 | LYD_VALUE_GET(&leaf->value, bits); |
| 3694 | LY_ARRAY_FOR(bits->items, u) { |
| 3695 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3696 | set_fill_boolean(set, 1); |
| 3697 | break; |
| 3698 | } |
| 3699 | } |
| 3700 | } |
| 3701 | } |
| 3702 | |
| 3703 | return LY_SUCCESS; |
| 3704 | } |
| 3705 | |
| 3706 | /** |
| 3707 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3708 | * with the argument converted to boolean. |
| 3709 | * |
| 3710 | * @param[in] args Array of arguments. |
| 3711 | * @param[in] arg_count Count of elements in @p args. |
| 3712 | * @param[in,out] set Context and result set at the same time. |
| 3713 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3714 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3715 | */ |
| 3716 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3717 | 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] | 3718 | { |
| 3719 | LY_ERR rc; |
| 3720 | |
| 3721 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3722 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3723 | return LY_SUCCESS; |
| 3724 | } |
| 3725 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3726 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3727 | LY_CHECK_RET(rc); |
| 3728 | set_fill_set(set, args[0]); |
| 3729 | |
| 3730 | return LY_SUCCESS; |
| 3731 | } |
| 3732 | |
| 3733 | /** |
| 3734 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3735 | * with the first argument rounded up to the nearest integer. |
| 3736 | * |
| 3737 | * @param[in] args Array of arguments. |
| 3738 | * @param[in] arg_count Count of elements in @p args. |
| 3739 | * @param[in,out] set Context and result set at the same time. |
| 3740 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3741 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3742 | */ |
| 3743 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3744 | 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] | 3745 | { |
| 3746 | struct lysc_node_leaf *sleaf; |
| 3747 | LY_ERR rc = LY_SUCCESS; |
| 3748 | |
| 3749 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3750 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3751 | 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] | 3752 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3753 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3754 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3755 | sleaf->name); |
| 3756 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3757 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3758 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3759 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3760 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3761 | return rc; |
| 3762 | } |
| 3763 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3764 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3765 | LY_CHECK_RET(rc); |
| 3766 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3767 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3768 | } else { |
| 3769 | set_fill_number(set, args[0]->val.num); |
| 3770 | } |
| 3771 | |
| 3772 | return LY_SUCCESS; |
| 3773 | } |
| 3774 | |
| 3775 | /** |
| 3776 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3777 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3778 | * |
| 3779 | * @param[in] args Array of arguments. |
| 3780 | * @param[in] arg_count Count of elements in @p args. |
| 3781 | * @param[in,out] set Context and result set at the same time. |
| 3782 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3783 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3784 | */ |
| 3785 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3786 | 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] | 3787 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3788 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3789 | char *str = NULL; |
| 3790 | size_t used = 1; |
| 3791 | LY_ERR rc = LY_SUCCESS; |
| 3792 | struct lysc_node_leaf *sleaf; |
| 3793 | |
| 3794 | if (options & LYXP_SCNODE_ALL) { |
| 3795 | for (i = 0; i < arg_count; ++i) { |
| 3796 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3797 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3798 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3799 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3800 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3801 | 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] | 3802 | } |
| 3803 | } |
| 3804 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3805 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3806 | return rc; |
| 3807 | } |
| 3808 | |
| 3809 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3810 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3811 | if (rc != LY_SUCCESS) { |
| 3812 | free(str); |
| 3813 | return rc; |
| 3814 | } |
| 3815 | |
| 3816 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3817 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3818 | strcpy(str + used - 1, args[i]->val.str); |
| 3819 | used += strlen(args[i]->val.str); |
| 3820 | } |
| 3821 | |
| 3822 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3823 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3824 | set->type = LYXP_SET_STRING; |
| 3825 | set->val.str = str; |
| 3826 | |
| 3827 | return LY_SUCCESS; |
| 3828 | } |
| 3829 | |
| 3830 | /** |
| 3831 | * @brief Execute the XPath contains(string, string) function. |
| 3832 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3833 | * be found in the first or not. |
| 3834 | * |
| 3835 | * @param[in] args Array of arguments. |
| 3836 | * @param[in] arg_count Count of elements in @p args. |
| 3837 | * @param[in,out] set Context and result set at the same time. |
| 3838 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3839 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3840 | */ |
| 3841 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3842 | 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] | 3843 | { |
| 3844 | struct lysc_node_leaf *sleaf; |
| 3845 | LY_ERR rc = LY_SUCCESS; |
| 3846 | |
| 3847 | if (options & LYXP_SCNODE_ALL) { |
| 3848 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3849 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3850 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3851 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3852 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3853 | LOGWRN(set->ctx, "Argument #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] | 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3858 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3859 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3860 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3861 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3862 | 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] | 3863 | } |
| 3864 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3865 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3866 | return rc; |
| 3867 | } |
| 3868 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3869 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3870 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3871 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3872 | LY_CHECK_RET(rc); |
| 3873 | |
| 3874 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3875 | set_fill_boolean(set, 1); |
| 3876 | } else { |
| 3877 | set_fill_boolean(set, 0); |
| 3878 | } |
| 3879 | |
| 3880 | return LY_SUCCESS; |
| 3881 | } |
| 3882 | |
| 3883 | /** |
| 3884 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3885 | * with the size of the node-set from the argument. |
| 3886 | * |
| 3887 | * @param[in] args Array of arguments. |
| 3888 | * @param[in] arg_count Count of elements in @p args. |
| 3889 | * @param[in,out] set Context and result set at the same time. |
| 3890 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3891 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3892 | */ |
| 3893 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3894 | 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] | 3895 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3896 | LY_ERR rc = LY_SUCCESS; |
| 3897 | |
| 3898 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3899 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3900 | 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] | 3901 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3902 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3903 | return rc; |
| 3904 | } |
| 3905 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3906 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3907 | 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] | 3908 | return LY_EVALID; |
| 3909 | } |
| 3910 | |
| 3911 | set_fill_number(set, args[0]->used); |
| 3912 | return LY_SUCCESS; |
| 3913 | } |
| 3914 | |
| 3915 | /** |
| 3916 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3917 | * with the context with the intial node. |
| 3918 | * |
| 3919 | * @param[in] args Array of arguments. |
| 3920 | * @param[in] arg_count Count of elements in @p args. |
| 3921 | * @param[in,out] set Context and result set at the same time. |
| 3922 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3923 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3924 | */ |
| 3925 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3926 | 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] | 3927 | { |
| 3928 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3929 | 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] | 3930 | return LY_EVALID; |
| 3931 | } |
| 3932 | |
| 3933 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3934 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3935 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3936 | if (set->cur_scnode) { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 3937 | 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] | 3938 | } else { |
| 3939 | /* root node */ |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 3940 | 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] | 3941 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3942 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3943 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3944 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3945 | if (set->cur_node) { |
| 3946 | /* position is filled later */ |
| 3947 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3948 | } else { |
| 3949 | /* root node */ |
| 3950 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3951 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | return LY_SUCCESS; |
| 3955 | } |
| 3956 | |
| 3957 | /** |
| 3958 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3959 | * leafref or instance-identifier target node(s). |
| 3960 | * |
| 3961 | * @param[in] args Array of arguments. |
| 3962 | * @param[in] arg_count Count of elements in @p args. |
| 3963 | * @param[in,out] set Context and result set at the same time. |
| 3964 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3965 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3966 | */ |
| 3967 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3968 | 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] | 3969 | { |
| 3970 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3971 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3972 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3973 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3974 | struct ly_path *p; |
| 3975 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3976 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3977 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3978 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3979 | |
| 3980 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3981 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3982 | 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] | 3983 | } 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] | 3984 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3985 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3986 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3987 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3988 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3989 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3990 | __func__, sleaf->name); |
| 3991 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3992 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3993 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 3994 | 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] | 3995 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 3996 | 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] | 3997 | |
| 3998 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3999 | 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] | 4000 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4001 | if (!r) { |
| 4002 | /* get the target node */ |
| 4003 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 4004 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4005 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 4006 | 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] | 4007 | } /* 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] | 4008 | } |
| 4009 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4010 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4011 | } |
| 4012 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4013 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4014 | 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] | 4015 | return LY_EVALID; |
| 4016 | } |
| 4017 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4018 | lyxp_set_free_content(set); |
| 4019 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4020 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4021 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4022 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4023 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 4024 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4025 | 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] | 4026 | &node, &errmsg)) { |
Michal Vasko | 1a2b8ee | 2022-12-05 10:41:55 +0100 | [diff] [blame] | 4027 | LOGERR(set->ctx, LY_EVALID, "%s", errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4028 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4029 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4030 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4031 | } else { |
| 4032 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4033 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4034 | 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] | 4035 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4036 | return LY_EVALID; |
| 4037 | } |
| 4038 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4039 | |
| 4040 | /* insert it */ |
| 4041 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | return LY_SUCCESS; |
| 4046 | } |
| 4047 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4048 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4049 | 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] | 4050 | { |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 4051 | uint32_t i, id_len; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4052 | LY_ARRAY_COUNT_TYPE u; |
| 4053 | struct lyd_node_term *leaf; |
| 4054 | struct lysc_node_leaf *sleaf; |
| 4055 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4056 | struct lyd_value *val; |
| 4057 | const struct lys_module *mod; |
| 4058 | const char *id_name; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4059 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4060 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4061 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4062 | |
| 4063 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4064 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4065 | 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] | 4066 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4067 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4068 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4069 | sleaf->name); |
| 4070 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4071 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4072 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4076 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4077 | 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] | 4078 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4079 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4080 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4081 | } |
| 4082 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4083 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4084 | return rc; |
| 4085 | } |
| 4086 | |
| 4087 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4088 | 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] | 4089 | return LY_EVALID; |
| 4090 | } |
| 4091 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4092 | LY_CHECK_RET(rc); |
| 4093 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4094 | /* parse the identity */ |
| 4095 | id_name = args[1]->val.str; |
| 4096 | id_len = strlen(id_name); |
| 4097 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4098 | LY_CHECK_RET(rc); |
| 4099 | if (!mod) { |
| 4100 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4101 | return LY_EVALID; |
| 4102 | } |
| 4103 | |
| 4104 | /* find the identity */ |
| 4105 | found = 0; |
| 4106 | LY_ARRAY_FOR(mod->identities, u) { |
| 4107 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4108 | /* we have match */ |
| 4109 | found = 1; |
| 4110 | break; |
| 4111 | } |
| 4112 | } |
| 4113 | if (!found) { |
| 4114 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4115 | return LY_EVALID; |
| 4116 | } |
| 4117 | id = &mod->identities[u]; |
| 4118 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4119 | set_fill_boolean(set, 0); |
| 4120 | found = 0; |
| 4121 | for (i = 0; i < args[0]->used; ++i) { |
| 4122 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4123 | continue; |
| 4124 | } |
| 4125 | |
| 4126 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4127 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4128 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4129 | val = &leaf->value; |
| 4130 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 4131 | /* uninteresting */ |
| 4132 | continue; |
| 4133 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4134 | } else { |
| 4135 | meta = args[0]->val.meta[i].meta; |
| 4136 | val = &meta->value; |
| 4137 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4138 | /* uninteresting */ |
| 4139 | continue; |
| 4140 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4141 | } |
| 4142 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4143 | /* check the identity itself */ |
| 4144 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4145 | set_fill_boolean(set, 1); |
| 4146 | found = 1; |
| 4147 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4148 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4149 | set_fill_boolean(set, 1); |
| 4150 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4151 | } |
| 4152 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4153 | if (found) { |
| 4154 | break; |
| 4155 | } |
| 4156 | } |
| 4157 | |
| 4158 | return LY_SUCCESS; |
| 4159 | } |
| 4160 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4161 | /** |
| 4162 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4163 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4164 | * argument identity. |
| 4165 | * |
| 4166 | * @param[in] args Array of arguments. |
| 4167 | * @param[in] arg_count Count of elements in @p args. |
| 4168 | * @param[in,out] set Context and result set at the same time. |
| 4169 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4170 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4171 | */ |
| 4172 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4173 | 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] | 4174 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4175 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | /** |
| 4179 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4180 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4181 | * the second argument identity. |
| 4182 | * |
| 4183 | * @param[in] args Array of arguments. |
| 4184 | * @param[in] arg_count Count of elements in @p args. |
| 4185 | * @param[in,out] set Context and result set at the same time. |
| 4186 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4187 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4188 | */ |
| 4189 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4190 | 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] | 4191 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4192 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | /** |
| 4196 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4197 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4198 | * |
| 4199 | * @param[in] args Array of arguments. |
| 4200 | * @param[in] arg_count Count of elements in @p args. |
| 4201 | * @param[in,out] set Context and result set at the same time. |
| 4202 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4203 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4204 | */ |
| 4205 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4206 | 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] | 4207 | { |
| 4208 | struct lyd_node_term *leaf; |
| 4209 | struct lysc_node_leaf *sleaf; |
| 4210 | LY_ERR rc = LY_SUCCESS; |
| 4211 | |
| 4212 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4213 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4214 | 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] | 4215 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4216 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4217 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4218 | sleaf->name); |
| 4219 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4220 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4221 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4223 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4224 | return rc; |
| 4225 | } |
| 4226 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4227 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4228 | 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] | 4229 | return LY_EVALID; |
| 4230 | } |
| 4231 | |
| 4232 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4233 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4234 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4235 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4236 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4237 | set_fill_number(set, leaf->value.enum_item->value); |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | return LY_SUCCESS; |
| 4242 | } |
| 4243 | |
| 4244 | /** |
| 4245 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4246 | * with false value. |
| 4247 | * |
| 4248 | * @param[in] args Array of arguments. |
| 4249 | * @param[in] arg_count Count of elements in @p args. |
| 4250 | * @param[in,out] set Context and result set at the same time. |
| 4251 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4252 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4253 | */ |
| 4254 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4255 | 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] | 4256 | { |
| 4257 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4258 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4259 | return LY_SUCCESS; |
| 4260 | } |
| 4261 | |
| 4262 | set_fill_boolean(set, 0); |
| 4263 | return LY_SUCCESS; |
| 4264 | } |
| 4265 | |
| 4266 | /** |
| 4267 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4268 | * with the first argument floored (truncated). |
| 4269 | * |
| 4270 | * @param[in] args Array of arguments. |
| 4271 | * @param[in] arg_count Count of elements in @p args. |
| 4272 | * @param[in,out] set Context and result set at the same time. |
| 4273 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4274 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4275 | */ |
| 4276 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4277 | xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4278 | { |
| 4279 | LY_ERR rc; |
| 4280 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4281 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4282 | LY_CHECK_RET(rc); |
| 4283 | if (isfinite(args[0]->val.num)) { |
| 4284 | set_fill_number(set, (long long)args[0]->val.num); |
| 4285 | } |
| 4286 | |
| 4287 | return LY_SUCCESS; |
| 4288 | } |
| 4289 | |
| 4290 | /** |
| 4291 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4292 | * whether the language of the text matches the one from the argument. |
| 4293 | * |
| 4294 | * @param[in] args Array of arguments. |
| 4295 | * @param[in] arg_count Count of elements in @p args. |
| 4296 | * @param[in,out] set Context and result set at the same time. |
| 4297 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4298 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4299 | */ |
| 4300 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4301 | 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] | 4302 | { |
| 4303 | const struct lyd_node *node; |
| 4304 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4305 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4306 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4307 | LY_ERR rc = LY_SUCCESS; |
| 4308 | |
| 4309 | if (options & LYXP_SCNODE_ALL) { |
| 4310 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4311 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4312 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4313 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4314 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4315 | 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] | 4316 | } |
| 4317 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4318 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4319 | return rc; |
| 4320 | } |
| 4321 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4322 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4323 | LY_CHECK_RET(rc); |
| 4324 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4325 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4326 | 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] | 4327 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4328 | } else if (!set->used) { |
| 4329 | set_fill_boolean(set, 0); |
| 4330 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | switch (set->val.nodes[0].type) { |
| 4334 | case LYXP_NODE_ELEM: |
| 4335 | case LYXP_NODE_TEXT: |
| 4336 | node = set->val.nodes[0].node; |
| 4337 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4338 | case LYXP_NODE_META: |
| 4339 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4340 | break; |
| 4341 | default: |
| 4342 | /* nothing to do with roots */ |
| 4343 | set_fill_boolean(set, 0); |
| 4344 | return LY_SUCCESS; |
| 4345 | } |
| 4346 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4347 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4348 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4349 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4350 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4351 | 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] | 4352 | break; |
| 4353 | } |
| 4354 | } |
| 4355 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4356 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4357 | break; |
| 4358 | } |
| 4359 | } |
| 4360 | |
| 4361 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4362 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4363 | set_fill_boolean(set, 0); |
| 4364 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4365 | uint64_t i; |
| 4366 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4367 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4368 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4369 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4370 | set_fill_boolean(set, 0); |
| 4371 | break; |
| 4372 | } |
| 4373 | } |
| 4374 | if (!args[0]->val.str[i]) { |
| 4375 | if (!val[i] || (val[i] == '-')) { |
| 4376 | set_fill_boolean(set, 1); |
| 4377 | } else { |
| 4378 | set_fill_boolean(set, 0); |
| 4379 | } |
| 4380 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | return LY_SUCCESS; |
| 4384 | } |
| 4385 | |
| 4386 | /** |
| 4387 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4388 | * with the context size. |
| 4389 | * |
| 4390 | * @param[in] args Array of arguments. |
| 4391 | * @param[in] arg_count Count of elements in @p args. |
| 4392 | * @param[in,out] set Context and result set at the same time. |
| 4393 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4394 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4395 | */ |
| 4396 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4397 | 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] | 4398 | { |
| 4399 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4400 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4401 | return LY_SUCCESS; |
| 4402 | } |
| 4403 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4404 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4405 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4406 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4407 | } else if (!set->used) { |
| 4408 | set_fill_number(set, 0); |
| 4409 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | set_fill_number(set, set->ctx_size); |
| 4413 | return LY_SUCCESS; |
| 4414 | } |
| 4415 | |
| 4416 | /** |
| 4417 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4418 | * with the node name without namespace from the argument or the context. |
| 4419 | * |
| 4420 | * @param[in] args Array of arguments. |
| 4421 | * @param[in] arg_count Count of elements in @p args. |
| 4422 | * @param[in,out] set Context and result set at the same time. |
| 4423 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4424 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4425 | */ |
| 4426 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4427 | 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] | 4428 | { |
| 4429 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4430 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4431 | /* suppress unused variable warning */ |
| 4432 | (void)options; |
| 4433 | |
| 4434 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4435 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4436 | return LY_SUCCESS; |
| 4437 | } |
| 4438 | |
| 4439 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4440 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4441 | 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] | 4442 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4443 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4444 | } else if (!args[0]->used) { |
| 4445 | set_fill_string(set, "", 0); |
| 4446 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4447 | } |
| 4448 | |
| 4449 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4450 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4451 | |
| 4452 | item = &args[0]->val.nodes[0]; |
| 4453 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4454 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4455 | 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] | 4456 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4457 | } else if (!set->used) { |
| 4458 | set_fill_string(set, "", 0); |
| 4459 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4460 | } |
| 4461 | |
| 4462 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4463 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4464 | |
| 4465 | item = &set->val.nodes[0]; |
| 4466 | } |
| 4467 | |
| 4468 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4469 | case LYXP_NODE_NONE: |
| 4470 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4471 | case LYXP_NODE_ROOT: |
| 4472 | case LYXP_NODE_ROOT_CONFIG: |
| 4473 | case LYXP_NODE_TEXT: |
| 4474 | set_fill_string(set, "", 0); |
| 4475 | break; |
| 4476 | case LYXP_NODE_ELEM: |
| 4477 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4478 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4479 | case LYXP_NODE_META: |
| 4480 | 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] | 4481 | break; |
| 4482 | } |
| 4483 | |
| 4484 | return LY_SUCCESS; |
| 4485 | } |
| 4486 | |
| 4487 | /** |
| 4488 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4489 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4490 | * |
| 4491 | * @param[in] args Array of arguments. |
| 4492 | * @param[in] arg_count Count of elements in @p args. |
| 4493 | * @param[in,out] set Context and result set at the same time. |
| 4494 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4495 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4496 | */ |
| 4497 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4498 | 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] | 4499 | { |
| 4500 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4501 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4502 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4503 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4504 | |
| 4505 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4506 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4507 | return LY_SUCCESS; |
| 4508 | } |
| 4509 | |
| 4510 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4511 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4512 | 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] | 4513 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4514 | } else if (!args[0]->used) { |
| 4515 | set_fill_string(set, "", 0); |
| 4516 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4520 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4521 | |
| 4522 | item = &args[0]->val.nodes[0]; |
| 4523 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4524 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4525 | 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] | 4526 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4527 | } else if (!set->used) { |
| 4528 | set_fill_string(set, "", 0); |
| 4529 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4533 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4534 | |
| 4535 | item = &set->val.nodes[0]; |
| 4536 | } |
| 4537 | |
| 4538 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4539 | case LYXP_NODE_NONE: |
| 4540 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4541 | case LYXP_NODE_ROOT: |
| 4542 | case LYXP_NODE_ROOT_CONFIG: |
| 4543 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4544 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4545 | break; |
| 4546 | case LYXP_NODE_ELEM: |
| 4547 | mod = item->node->schema->module; |
| 4548 | name = item->node->schema->name; |
| 4549 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4550 | case LYXP_NODE_META: |
| 4551 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4552 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4553 | break; |
| 4554 | } |
| 4555 | |
| 4556 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4557 | 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] | 4558 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4559 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4560 | set_fill_string(set, str, strlen(str)); |
| 4561 | free(str); |
| 4562 | } else { |
| 4563 | set_fill_string(set, "", 0); |
| 4564 | } |
| 4565 | |
| 4566 | return LY_SUCCESS; |
| 4567 | } |
| 4568 | |
| 4569 | /** |
| 4570 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4571 | * with the namespace of the node from the argument or the context. |
| 4572 | * |
| 4573 | * @param[in] args Array of arguments. |
| 4574 | * @param[in] arg_count Count of elements in @p args. |
| 4575 | * @param[in,out] set Context and result set at the same time. |
| 4576 | * @param[in] options XPath options. |
| 4577 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4578 | */ |
| 4579 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4580 | 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] | 4581 | { |
| 4582 | struct lyxp_set_node *item; |
| 4583 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4584 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4585 | /* suppress unused variable warning */ |
| 4586 | (void)options; |
| 4587 | |
| 4588 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4589 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4590 | return LY_SUCCESS; |
| 4591 | } |
| 4592 | |
| 4593 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4594 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4595 | 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] | 4596 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4597 | return LY_EVALID; |
| 4598 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4599 | set_fill_string(set, "", 0); |
| 4600 | return LY_SUCCESS; |
| 4601 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4602 | |
| 4603 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4604 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4605 | |
| 4606 | item = &args[0]->val.nodes[0]; |
| 4607 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4608 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4609 | 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] | 4610 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4611 | } else if (!set->used) { |
| 4612 | set_fill_string(set, "", 0); |
| 4613 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4617 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4618 | |
| 4619 | item = &set->val.nodes[0]; |
| 4620 | } |
| 4621 | |
| 4622 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4623 | case LYXP_NODE_NONE: |
| 4624 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4625 | case LYXP_NODE_ROOT: |
| 4626 | case LYXP_NODE_ROOT_CONFIG: |
| 4627 | case LYXP_NODE_TEXT: |
| 4628 | set_fill_string(set, "", 0); |
| 4629 | break; |
| 4630 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4631 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4632 | if (item->type == LYXP_NODE_ELEM) { |
| 4633 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4634 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4635 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4636 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4640 | break; |
| 4641 | } |
| 4642 | |
| 4643 | return LY_SUCCESS; |
| 4644 | } |
| 4645 | |
| 4646 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4647 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4648 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4649 | * from the argument or the context. |
| 4650 | * |
| 4651 | * @param[in] args Array of arguments. |
| 4652 | * @param[in] arg_count Count of elements in @p args. |
| 4653 | * @param[in,out] set Context and result set at the same time. |
| 4654 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4655 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4656 | */ |
| 4657 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4658 | 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] | 4659 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4660 | uint32_t i, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4661 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4662 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4663 | struct lysc_node_leaf *sleaf; |
| 4664 | LY_ERR rc = LY_SUCCESS; |
| 4665 | |
| 4666 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4667 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4668 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4669 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4670 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4671 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4672 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4673 | 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] | 4674 | } |
| 4675 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4676 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4677 | return rc; |
| 4678 | } |
| 4679 | |
| 4680 | if (arg_count) { |
| 4681 | set_fill_set(set, args[0]); |
| 4682 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4683 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4684 | LY_CHECK_RET(rc); |
| 4685 | |
| 4686 | /* is there any normalization necessary? */ |
| 4687 | for (i = 0; set->val.str[i]; ++i) { |
| 4688 | if (is_xmlws(set->val.str[i])) { |
| 4689 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4690 | have_spaces = 1; |
| 4691 | break; |
| 4692 | } |
| 4693 | space_before = 1; |
| 4694 | } else { |
| 4695 | space_before = 0; |
| 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | /* yep, there is */ |
| 4700 | if (have_spaces) { |
| 4701 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4702 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4703 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4704 | new_used = 0; |
| 4705 | |
| 4706 | space_before = 0; |
| 4707 | for (i = 0; set->val.str[i]; ++i) { |
| 4708 | if (is_xmlws(set->val.str[i])) { |
| 4709 | if ((i == 0) || space_before) { |
| 4710 | space_before = 1; |
| 4711 | continue; |
| 4712 | } else { |
| 4713 | space_before = 1; |
| 4714 | } |
| 4715 | } else { |
| 4716 | space_before = 0; |
| 4717 | } |
| 4718 | |
| 4719 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4720 | ++new_used; |
| 4721 | } |
| 4722 | |
| 4723 | /* at worst there is one trailing space now */ |
| 4724 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4725 | --new_used; |
| 4726 | } |
| 4727 | |
| 4728 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4729 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4730 | new[new_used] = '\0'; |
| 4731 | |
| 4732 | free(set->val.str); |
| 4733 | set->val.str = new; |
| 4734 | } |
| 4735 | |
| 4736 | return LY_SUCCESS; |
| 4737 | } |
| 4738 | |
| 4739 | /** |
| 4740 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4741 | * with the argument converted to boolean and logically inverted. |
| 4742 | * |
| 4743 | * @param[in] args Array of arguments. |
| 4744 | * @param[in] arg_count Count of elements in @p args. |
| 4745 | * @param[in,out] set Context and result set at the same time. |
| 4746 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4747 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4748 | */ |
| 4749 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4750 | 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] | 4751 | { |
| 4752 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4753 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4754 | return LY_SUCCESS; |
| 4755 | } |
| 4756 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4757 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4758 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4759 | set_fill_boolean(set, 0); |
| 4760 | } else { |
| 4761 | set_fill_boolean(set, 1); |
| 4762 | } |
| 4763 | |
| 4764 | return LY_SUCCESS; |
| 4765 | } |
| 4766 | |
| 4767 | /** |
| 4768 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4769 | * with the number representation of either the argument or the context. |
| 4770 | * |
| 4771 | * @param[in] args Array of arguments. |
| 4772 | * @param[in] arg_count Count of elements in @p args. |
| 4773 | * @param[in,out] set Context and result set at the same time. |
| 4774 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4775 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4776 | */ |
| 4777 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4778 | 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] | 4779 | { |
| 4780 | LY_ERR rc; |
| 4781 | |
| 4782 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4783 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4784 | return LY_SUCCESS; |
| 4785 | } |
| 4786 | |
| 4787 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4788 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4789 | LY_CHECK_RET(rc); |
| 4790 | set_fill_set(set, args[0]); |
| 4791 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4792 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4793 | LY_CHECK_RET(rc); |
| 4794 | } |
| 4795 | |
| 4796 | return LY_SUCCESS; |
| 4797 | } |
| 4798 | |
| 4799 | /** |
| 4800 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4801 | * with the context position. |
| 4802 | * |
| 4803 | * @param[in] args Array of arguments. |
| 4804 | * @param[in] arg_count Count of elements in @p args. |
| 4805 | * @param[in,out] set Context and result set at the same time. |
| 4806 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4807 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4808 | */ |
| 4809 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4810 | 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] | 4811 | { |
| 4812 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4813 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4814 | return LY_SUCCESS; |
| 4815 | } |
| 4816 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4817 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4818 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4819 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4820 | } else if (!set->used) { |
| 4821 | set_fill_number(set, 0); |
| 4822 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4823 | } |
| 4824 | |
| 4825 | set_fill_number(set, set->ctx_pos); |
| 4826 | |
| 4827 | /* UNUSED in 'Release' build type */ |
| 4828 | (void)options; |
| 4829 | return LY_SUCCESS; |
| 4830 | } |
| 4831 | |
| 4832 | /** |
| 4833 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4834 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4835 | * YANG 1.1 RFC section 10.2.1. |
| 4836 | * |
| 4837 | * @param[in] args Array of arguments. |
| 4838 | * @param[in] arg_count Count of elements in @p args. |
| 4839 | * @param[in,out] set Context and result set at the same time. |
| 4840 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4841 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4842 | */ |
| 4843 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4844 | 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] | 4845 | { |
| 4846 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4847 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4848 | LY_ERR rc = LY_SUCCESS; |
| 4849 | struct ly_err_item *err; |
| 4850 | |
| 4851 | if (options & LYXP_SCNODE_ALL) { |
| 4852 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4853 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4854 | 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] | 4855 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4856 | 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] | 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4861 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4862 | 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] | 4863 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4864 | 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] | 4865 | } |
| 4866 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4867 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4868 | return rc; |
| 4869 | } |
| 4870 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4871 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4872 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4873 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4874 | LY_CHECK_RET(rc); |
| 4875 | |
| 4876 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4877 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4878 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4879 | 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] | 4880 | if (set->cur_node) { |
| 4881 | LOG_LOCBACK(0, 1, 0, 0); |
| 4882 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4883 | if (rc != LY_SUCCESS) { |
| 4884 | LY_ARRAY_FREE(patterns); |
| 4885 | return rc; |
| 4886 | } |
| 4887 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4888 | 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] | 4889 | pcre2_code_free((*pattern)->code); |
| 4890 | free(*pattern); |
| 4891 | LY_ARRAY_FREE(patterns); |
| 4892 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4893 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4894 | ly_err_free(err); |
| 4895 | return rc; |
| 4896 | } |
| 4897 | |
| 4898 | if (rc == LY_EVALID) { |
| 4899 | ly_err_free(err); |
| 4900 | set_fill_boolean(set, 0); |
| 4901 | } else { |
| 4902 | set_fill_boolean(set, 1); |
| 4903 | } |
| 4904 | |
| 4905 | return LY_SUCCESS; |
| 4906 | } |
| 4907 | |
| 4908 | /** |
| 4909 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4910 | * with the rounded first argument. For details refer to |
| 4911 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4912 | * |
| 4913 | * @param[in] args Array of arguments. |
| 4914 | * @param[in] arg_count Count of elements in @p args. |
| 4915 | * @param[in,out] set Context and result set at the same time. |
| 4916 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4917 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4918 | */ |
| 4919 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4920 | 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] | 4921 | { |
| 4922 | struct lysc_node_leaf *sleaf; |
| 4923 | LY_ERR rc = LY_SUCCESS; |
| 4924 | |
| 4925 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4926 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4927 | 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] | 4928 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4929 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4930 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4931 | sleaf->name); |
| 4932 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4933 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4934 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4935 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4936 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4937 | return rc; |
| 4938 | } |
| 4939 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4940 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4941 | LY_CHECK_RET(rc); |
| 4942 | |
| 4943 | /* cover only the cases where floor can't be used */ |
| 4944 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4945 | set_fill_number(set, -0.0f); |
| 4946 | } else { |
| 4947 | args[0]->val.num += 0.5; |
| 4948 | rc = xpath_floor(args, 1, args[0], options); |
| 4949 | LY_CHECK_RET(rc); |
| 4950 | set_fill_number(set, args[0]->val.num); |
| 4951 | } |
| 4952 | |
| 4953 | return LY_SUCCESS; |
| 4954 | } |
| 4955 | |
| 4956 | /** |
| 4957 | * @brief Execute the XPath starts-with(string, string) function. |
| 4958 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4959 | * the prefix of the first or not. |
| 4960 | * |
| 4961 | * @param[in] args Array of arguments. |
| 4962 | * @param[in] arg_count Count of elements in @p args. |
| 4963 | * @param[in,out] set Context and result set at the same time. |
| 4964 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4965 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4966 | */ |
| 4967 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4968 | 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] | 4969 | { |
| 4970 | struct lysc_node_leaf *sleaf; |
| 4971 | LY_ERR rc = LY_SUCCESS; |
| 4972 | |
| 4973 | if (options & LYXP_SCNODE_ALL) { |
| 4974 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4975 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4976 | 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] | 4977 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4978 | 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] | 4979 | } |
| 4980 | } |
| 4981 | |
| 4982 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4983 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4984 | 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] | 4985 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4986 | 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] | 4987 | } |
| 4988 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4989 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4990 | return rc; |
| 4991 | } |
| 4992 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4993 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4994 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4995 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4996 | LY_CHECK_RET(rc); |
| 4997 | |
| 4998 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4999 | set_fill_boolean(set, 0); |
| 5000 | } else { |
| 5001 | set_fill_boolean(set, 1); |
| 5002 | } |
| 5003 | |
| 5004 | return LY_SUCCESS; |
| 5005 | } |
| 5006 | |
| 5007 | /** |
| 5008 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 5009 | * with the string representation of either the argument or the context. |
| 5010 | * |
| 5011 | * @param[in] args Array of arguments. |
| 5012 | * @param[in] arg_count Count of elements in @p args. |
| 5013 | * @param[in,out] set Context and result set at the same time. |
| 5014 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5015 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5016 | */ |
| 5017 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5018 | 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] | 5019 | { |
| 5020 | LY_ERR rc; |
| 5021 | |
| 5022 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5023 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5024 | return LY_SUCCESS; |
| 5025 | } |
| 5026 | |
| 5027 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5028 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5029 | LY_CHECK_RET(rc); |
| 5030 | set_fill_set(set, args[0]); |
| 5031 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5032 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5033 | LY_CHECK_RET(rc); |
| 5034 | } |
| 5035 | |
| 5036 | return LY_SUCCESS; |
| 5037 | } |
| 5038 | |
| 5039 | /** |
| 5040 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5041 | * with the length of the string in either the argument or the context. |
| 5042 | * |
| 5043 | * @param[in] args Array of arguments. |
| 5044 | * @param[in] arg_count Count of elements in @p args. |
| 5045 | * @param[in,out] set Context and result set at the same time. |
| 5046 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5047 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5048 | */ |
| 5049 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5050 | 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] | 5051 | { |
| 5052 | struct lysc_node_leaf *sleaf; |
| 5053 | LY_ERR rc = LY_SUCCESS; |
| 5054 | |
| 5055 | if (options & LYXP_SCNODE_ALL) { |
| 5056 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5057 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5058 | 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] | 5059 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5060 | 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] | 5061 | } |
| 5062 | } |
| 5063 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5064 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5065 | 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] | 5066 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5067 | 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] | 5068 | } |
| 5069 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5070 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5071 | return rc; |
| 5072 | } |
| 5073 | |
| 5074 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5075 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5076 | LY_CHECK_RET(rc); |
| 5077 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5078 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5079 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5080 | LY_CHECK_RET(rc); |
| 5081 | set_fill_number(set, strlen(set->val.str)); |
| 5082 | } |
| 5083 | |
| 5084 | return LY_SUCCESS; |
| 5085 | } |
| 5086 | |
| 5087 | /** |
| 5088 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5089 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5090 | * on the second argument index ending on the third argument index, |
| 5091 | * indexed from 1. For exact definition refer to |
| 5092 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5093 | * |
| 5094 | * @param[in] args Array of arguments. |
| 5095 | * @param[in] arg_count Count of elements in @p args. |
| 5096 | * @param[in,out] set Context and result set at the same time. |
| 5097 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5098 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5099 | */ |
| 5100 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5101 | 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] | 5102 | { |
Michal Vasko | 6db996e | 2022-07-28 10:28:04 +0200 | [diff] [blame] | 5103 | int64_t start; |
| 5104 | int32_t len; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5105 | uint32_t str_start, str_len, pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5106 | struct lysc_node_leaf *sleaf; |
| 5107 | LY_ERR rc = LY_SUCCESS; |
| 5108 | |
| 5109 | if (options & LYXP_SCNODE_ALL) { |
| 5110 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5111 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5112 | 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] | 5113 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5114 | 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] | 5115 | } |
| 5116 | } |
| 5117 | |
| 5118 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5119 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5120 | 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] | 5121 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5122 | 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] | 5123 | } |
| 5124 | } |
| 5125 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5126 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5127 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5128 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5129 | 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] | 5130 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5131 | 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] | 5132 | } |
| 5133 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5134 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5135 | return rc; |
| 5136 | } |
| 5137 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5138 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5139 | LY_CHECK_RET(rc); |
| 5140 | |
| 5141 | /* start */ |
| 5142 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5143 | return -1; |
| 5144 | } |
| 5145 | if (isfinite(args[1]->val.num)) { |
| 5146 | start = args[1]->val.num - 1; |
| 5147 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5148 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5149 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5150 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | /* len */ |
| 5154 | if (arg_count == 3) { |
| 5155 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5156 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5157 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5158 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5159 | } else if (isfinite(args[2]->val.num)) { |
| 5160 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5161 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5162 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5163 | } |
| 5164 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5165 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | /* find matching character positions */ |
| 5169 | str_start = 0; |
| 5170 | str_len = 0; |
| 5171 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5172 | if (pos < start) { |
| 5173 | ++str_start; |
| 5174 | } else if (pos < start + len) { |
| 5175 | ++str_len; |
| 5176 | } else { |
| 5177 | break; |
| 5178 | } |
| 5179 | } |
| 5180 | |
| 5181 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5182 | return LY_SUCCESS; |
| 5183 | } |
| 5184 | |
| 5185 | /** |
| 5186 | * @brief Execute the XPath substring-after(string, string) function. |
| 5187 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5188 | * of the second argument in the first or an empty string. |
| 5189 | * |
| 5190 | * @param[in] args Array of arguments. |
| 5191 | * @param[in] arg_count Count of elements in @p args. |
| 5192 | * @param[in,out] set Context and result set at the same time. |
| 5193 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5194 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5195 | */ |
| 5196 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5197 | 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] | 5198 | { |
| 5199 | char *ptr; |
| 5200 | struct lysc_node_leaf *sleaf; |
| 5201 | LY_ERR rc = LY_SUCCESS; |
| 5202 | |
| 5203 | if (options & LYXP_SCNODE_ALL) { |
| 5204 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5205 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5206 | 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] | 5207 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5208 | 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] | 5209 | } |
| 5210 | } |
| 5211 | |
| 5212 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5213 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5214 | 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] | 5215 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5216 | 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] | 5217 | } |
| 5218 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5219 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5220 | return rc; |
| 5221 | } |
| 5222 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5223 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5224 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5225 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5226 | LY_CHECK_RET(rc); |
| 5227 | |
| 5228 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5229 | if (ptr) { |
| 5230 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5231 | } else { |
| 5232 | set_fill_string(set, "", 0); |
| 5233 | } |
| 5234 | |
| 5235 | return LY_SUCCESS; |
| 5236 | } |
| 5237 | |
| 5238 | /** |
| 5239 | * @brief Execute the XPath substring-before(string, string) function. |
| 5240 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5241 | * of the second argument in the first or an empty string. |
| 5242 | * |
| 5243 | * @param[in] args Array of arguments. |
| 5244 | * @param[in] arg_count Count of elements in @p args. |
| 5245 | * @param[in,out] set Context and result set at the same time. |
| 5246 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5247 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5248 | */ |
| 5249 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5250 | 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] | 5251 | { |
| 5252 | char *ptr; |
| 5253 | struct lysc_node_leaf *sleaf; |
| 5254 | LY_ERR rc = LY_SUCCESS; |
| 5255 | |
| 5256 | if (options & LYXP_SCNODE_ALL) { |
| 5257 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5258 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5259 | 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] | 5260 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5261 | 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] | 5262 | } |
| 5263 | } |
| 5264 | |
| 5265 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5266 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5267 | 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] | 5268 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5269 | 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] | 5270 | } |
| 5271 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5272 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5273 | return rc; |
| 5274 | } |
| 5275 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5276 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5277 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5278 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5279 | LY_CHECK_RET(rc); |
| 5280 | |
| 5281 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5282 | if (ptr) { |
| 5283 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5284 | } else { |
| 5285 | set_fill_string(set, "", 0); |
| 5286 | } |
| 5287 | |
| 5288 | return LY_SUCCESS; |
| 5289 | } |
| 5290 | |
| 5291 | /** |
| 5292 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5293 | * with the sum of all the nodes in the context. |
| 5294 | * |
| 5295 | * @param[in] args Array of arguments. |
| 5296 | * @param[in] arg_count Count of elements in @p args. |
| 5297 | * @param[in,out] set Context and result set at the same time. |
| 5298 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5299 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5300 | */ |
| 5301 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5302 | 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] | 5303 | { |
| 5304 | long double num; |
| 5305 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5306 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5307 | struct lyxp_set set_item; |
| 5308 | struct lysc_node_leaf *sleaf; |
| 5309 | LY_ERR rc = LY_SUCCESS; |
| 5310 | |
| 5311 | if (options & LYXP_SCNODE_ALL) { |
| 5312 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5313 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5314 | 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] | 5315 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5316 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5317 | 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] | 5318 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5319 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5320 | 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] | 5321 | } |
| 5322 | } |
| 5323 | } |
| 5324 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5325 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5326 | return rc; |
| 5327 | } |
| 5328 | |
| 5329 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5330 | |
| 5331 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5332 | 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] | 5333 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5334 | } else if (!args[0]->used) { |
| 5335 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5336 | } |
| 5337 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5338 | set_init(&set_item, set); |
| 5339 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5340 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5341 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5342 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5343 | |
| 5344 | set_item.used = 1; |
| 5345 | set_item.size = 1; |
| 5346 | |
| 5347 | for (i = 0; i < args[0]->used; ++i) { |
| 5348 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5349 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5350 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5351 | LY_CHECK_RET(rc); |
| 5352 | num = cast_string_to_number(str); |
| 5353 | free(str); |
| 5354 | set->val.num += num; |
| 5355 | } |
| 5356 | |
| 5357 | free(set_item.val.nodes); |
| 5358 | |
| 5359 | return LY_SUCCESS; |
| 5360 | } |
| 5361 | |
| 5362 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5363 | * @brief Execute the XPath translate(string, string, string) function. |
| 5364 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5365 | * from the second argument replaced by those on the corresponding |
| 5366 | * positions in the third argument. |
| 5367 | * |
| 5368 | * @param[in] args Array of arguments. |
| 5369 | * @param[in] arg_count Count of elements in @p args. |
| 5370 | * @param[in,out] set Context and result set at the same time. |
| 5371 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5372 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5373 | */ |
| 5374 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5375 | 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] | 5376 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5377 | uint32_t i, j, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5378 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5379 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5380 | struct lysc_node_leaf *sleaf; |
| 5381 | LY_ERR rc = LY_SUCCESS; |
| 5382 | |
| 5383 | if (options & LYXP_SCNODE_ALL) { |
| 5384 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5385 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5386 | 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] | 5387 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5388 | 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] | 5389 | } |
| 5390 | } |
| 5391 | |
| 5392 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5393 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5394 | 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] | 5395 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5396 | 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] | 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5401 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5402 | 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] | 5403 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5404 | 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] | 5405 | } |
| 5406 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5407 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5408 | return rc; |
| 5409 | } |
| 5410 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5411 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5412 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5413 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5414 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5415 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5416 | LY_CHECK_RET(rc); |
| 5417 | |
| 5418 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5419 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5420 | new_used = 0; |
| 5421 | |
| 5422 | have_removed = 0; |
| 5423 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5424 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5425 | |
| 5426 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5427 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5428 | /* removing this char */ |
| 5429 | if (j >= strlen(args[2]->val.str)) { |
| 5430 | have_removed = 1; |
| 5431 | found = 1; |
| 5432 | break; |
| 5433 | } |
| 5434 | /* replacing this char */ |
| 5435 | new[new_used] = args[2]->val.str[j]; |
| 5436 | ++new_used; |
| 5437 | found = 1; |
| 5438 | break; |
| 5439 | } |
| 5440 | } |
| 5441 | |
| 5442 | /* copying this char */ |
| 5443 | if (!found) { |
| 5444 | new[new_used] = args[0]->val.str[i]; |
| 5445 | ++new_used; |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | if (have_removed) { |
| 5450 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5451 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5452 | } |
| 5453 | new[new_used] = '\0'; |
| 5454 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5455 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5456 | set->type = LYXP_SET_STRING; |
| 5457 | set->val.str = new; |
| 5458 | |
| 5459 | return LY_SUCCESS; |
| 5460 | } |
| 5461 | |
| 5462 | /** |
| 5463 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5464 | * with true value. |
| 5465 | * |
| 5466 | * @param[in] args Array of arguments. |
| 5467 | * @param[in] arg_count Count of elements in @p args. |
| 5468 | * @param[in,out] set Context and result set at the same time. |
| 5469 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5470 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5471 | */ |
| 5472 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5473 | 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] | 5474 | { |
| 5475 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5476 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5477 | return LY_SUCCESS; |
| 5478 | } |
| 5479 | |
| 5480 | set_fill_boolean(set, 1); |
| 5481 | return LY_SUCCESS; |
| 5482 | } |
| 5483 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5484 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5485 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5486 | * with only nodes from the context. |
| 5487 | * |
| 5488 | * @param[in,out] set Context and result set at the same time. |
| 5489 | * @param[in] axis Axis to search on. |
| 5490 | * @param[in] options XPath options. |
| 5491 | * @return LY_ERR |
| 5492 | */ |
| 5493 | static LY_ERR |
| 5494 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5495 | { |
| 5496 | if (options & LYXP_SCNODE_ALL) { |
| 5497 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5498 | } |
| 5499 | |
| 5500 | if (set->type != LYXP_SET_NODE_SET) { |
| 5501 | lyxp_set_free_content(set); |
| 5502 | return LY_SUCCESS; |
| 5503 | } |
| 5504 | |
| 5505 | /* just like moving to a node with no restrictions */ |
| 5506 | return moveto_node(set, NULL, NULL, axis, options); |
| 5507 | } |
| 5508 | |
| 5509 | /** |
| 5510 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5511 | * with the text content of the nodes in the context. |
| 5512 | * |
| 5513 | * @param[in,out] set Context and result set at the same time. |
| 5514 | * @param[in] axis Axis to search on. |
| 5515 | * @param[in] options XPath options. |
| 5516 | * @return LY_ERR |
| 5517 | */ |
| 5518 | static LY_ERR |
| 5519 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5520 | { |
| 5521 | uint32_t i; |
| 5522 | |
| 5523 | if (options & LYXP_SCNODE_ALL) { |
| 5524 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5525 | return LY_SUCCESS; |
| 5526 | } |
| 5527 | |
| 5528 | if (set->type != LYXP_SET_NODE_SET) { |
| 5529 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5530 | return LY_EVALID; |
| 5531 | } |
| 5532 | |
| 5533 | if (axis != LYXP_AXIS_CHILD) { |
| 5534 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5535 | lyxp_set_free_content(set); |
| 5536 | return LY_SUCCESS; |
| 5537 | } |
| 5538 | |
| 5539 | for (i = 0; i < set->used; ++i) { |
| 5540 | switch (set->val.nodes[i].type) { |
| 5541 | case LYXP_NODE_NONE: |
| 5542 | LOGINT_RET(set->ctx); |
| 5543 | case LYXP_NODE_ELEM: |
| 5544 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5545 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5546 | break; |
| 5547 | } |
| 5548 | /* fall through */ |
| 5549 | case LYXP_NODE_ROOT: |
| 5550 | case LYXP_NODE_ROOT_CONFIG: |
| 5551 | case LYXP_NODE_TEXT: |
| 5552 | case LYXP_NODE_META: |
| 5553 | set_remove_node_none(set, i); |
| 5554 | break; |
| 5555 | } |
| 5556 | } |
| 5557 | set_remove_nodes_none(set); |
| 5558 | |
| 5559 | return LY_SUCCESS; |
| 5560 | } |
| 5561 | |
| 5562 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5563 | * @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] | 5564 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5565 | * XPath @p set is expected to be a (sc)node set! |
| 5566 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5567 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5568 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5569 | * @param[in] set Set with general XPath context. |
| 5570 | * @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] | 5571 | * @param[out] moveto_mod Expected module of a matching node. |
| 5572 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5573 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5574 | static LY_ERR |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 5575 | 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] | 5576 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5577 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5578 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5579 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5580 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5581 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5582 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5583 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5584 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5585 | /* specific module */ |
| 5586 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5587 | 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] | 5588 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5589 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5590 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5591 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5592 | return LY_EVALID; |
| 5593 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5594 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5595 | *qname += pref_len + 1; |
| 5596 | *qname_len -= pref_len + 1; |
| 5597 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5598 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5599 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5600 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5601 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5602 | case LY_VALUE_SCHEMA: |
| 5603 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5604 | /* current module */ |
| 5605 | mod = set->cur_mod; |
| 5606 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5607 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5608 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5609 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5610 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5611 | /* inherit parent (context node) module */ |
| 5612 | if (ctx_scnode) { |
| 5613 | mod = ctx_scnode->module; |
| 5614 | } else { |
| 5615 | mod = NULL; |
| 5616 | } |
| 5617 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5618 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5619 | /* all nodes need to be prefixed */ |
| 5620 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5621 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5622 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5623 | } |
| 5624 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5625 | *moveto_mod = mod; |
| 5626 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5627 | } |
| 5628 | |
| 5629 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5630 | * @brief Move context @p set to the root. Handles absolute path. |
| 5631 | * Result is LYXP_SET_NODE_SET. |
| 5632 | * |
| 5633 | * @param[in,out] set Set to use. |
| 5634 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5635 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5636 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5637 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5638 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5639 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5640 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5641 | |
| 5642 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5643 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 5644 | 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] | 5645 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5646 | set->type = LYXP_SET_NODE_SET; |
| 5647 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5648 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 5649 | set->non_child_axis = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5650 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5651 | |
| 5652 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5653 | } |
| 5654 | |
| 5655 | /** |
| 5656 | * @brief Check @p node as a part of NameTest processing. |
| 5657 | * |
| 5658 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5659 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5660 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5661 | * @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] | 5662 | * @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] | 5663 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5664 | * @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] | 5665 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5666 | */ |
| 5667 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5668 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5669 | 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] | 5670 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5671 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5672 | assert(node_type == set->root_type); |
| 5673 | |
| 5674 | if (node_name || moveto_mod) { |
| 5675 | /* root will not match a specific node */ |
| 5676 | return LY_ENOT; |
| 5677 | } |
| 5678 | return LY_SUCCESS; |
| 5679 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5680 | /* other types will not match */ |
| 5681 | return LY_ENOT; |
| 5682 | } |
| 5683 | |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5684 | if (!node->schema) { |
| 5685 | /* opaque node never matches */ |
| 5686 | return LY_ENOT; |
| 5687 | } |
| 5688 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5689 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5690 | if (moveto_mod) { |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5691 | if ((set->ctx == LYD_CTX(node)) && (node->schema->module != moveto_mod)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5692 | return LY_ENOT; |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5693 | } 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] | 5694 | return LY_ENOT; |
| 5695 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5696 | } |
| 5697 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5698 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5699 | 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] | 5700 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5701 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5702 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5703 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5704 | } |
| 5705 | |
| 5706 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5707 | if (node_name) { |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5708 | if ((set->ctx == LYD_CTX(node)) && (node->schema->name != node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5709 | return LY_ENOT; |
Michal Vasko | 0a1eb75 | 2022-09-29 11:00:34 +0200 | [diff] [blame] | 5710 | } 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] | 5711 | return LY_ENOT; |
| 5712 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5713 | } |
| 5714 | |
Michal Vasko | 6d65712 | 2022-10-19 14:38:35 +0200 | [diff] [blame] | 5715 | /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */ |
| 5716 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE) && |
| 5717 | (node != set->cur_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5718 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5719 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5720 | |
| 5721 | /* match */ |
| 5722 | return LY_SUCCESS; |
| 5723 | } |
| 5724 | |
| 5725 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5726 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5727 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5728 | * @param[in] iter Last returned node. |
| 5729 | * @param[in] stop Node to stop the search on and not return. |
| 5730 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5731 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5732 | static const struct lyd_node * |
| 5733 | 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] | 5734 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5735 | const struct lyd_node *next = NULL; |
| 5736 | |
| 5737 | /* 1) child */ |
| 5738 | next = lyd_child(iter); |
| 5739 | if (!next) { |
| 5740 | if (iter == stop) { |
| 5741 | /* reached stop, no more descendants */ |
| 5742 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5743 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5744 | /* 2) child next sibling */ |
| 5745 | next = iter->next; |
| 5746 | } |
| 5747 | while (!next) { |
| 5748 | iter = lyd_parent(iter); |
| 5749 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5750 | return NULL; |
| 5751 | } |
| 5752 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5753 | } |
| 5754 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5755 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5756 | } |
| 5757 | |
| 5758 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5759 | * @brief Get the next node in a backward DFS. |
| 5760 | * |
| 5761 | * @param[in] iter Last returned node. |
| 5762 | * @param[in] stop Node to stop the search on and not return. |
| 5763 | * @return Next node, NULL if there are no more. |
| 5764 | */ |
| 5765 | static const struct lyd_node * |
| 5766 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5767 | { |
| 5768 | const struct lyd_node *next = NULL; |
| 5769 | |
| 5770 | /* 1) previous sibling innermost last child */ |
| 5771 | next = iter->prev->next ? iter->prev : NULL; |
| 5772 | while (next && lyd_child(next)) { |
| 5773 | next = lyd_child(next); |
| 5774 | next = next->prev; |
| 5775 | } |
| 5776 | |
| 5777 | if (!next) { |
| 5778 | /* 2) parent */ |
| 5779 | iter = lyd_parent(iter); |
| 5780 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5781 | return NULL; |
| 5782 | } |
| 5783 | next = iter; |
| 5784 | } |
| 5785 | |
| 5786 | return next; |
| 5787 | } |
| 5788 | |
| 5789 | /** |
| 5790 | * @brief Get the first node on an axis for a context node. |
| 5791 | * |
| 5792 | * @param[in,out] iter NULL, updated to the next node. |
| 5793 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5794 | * @param[in] node Context node. |
| 5795 | * @param[in] node_type Type of @p node. |
| 5796 | * @param[in] axis Axis to use. |
| 5797 | * @param[in] set XPath set with the general context. |
| 5798 | * @return LY_SUCCESS on success. |
| 5799 | * @return LY_ENOTFOUND if no next node found. |
| 5800 | */ |
| 5801 | static LY_ERR |
| 5802 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5803 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5804 | { |
| 5805 | const struct lyd_node *next = NULL; |
| 5806 | enum lyxp_node_type next_type = 0; |
| 5807 | |
| 5808 | assert(!*iter); |
| 5809 | assert(!*iter_type); |
| 5810 | |
| 5811 | switch (axis) { |
| 5812 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5813 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5814 | case LYXP_AXIS_SELF: |
| 5815 | /* return the context node */ |
| 5816 | next = node; |
| 5817 | next_type = node_type; |
| 5818 | break; |
| 5819 | |
| 5820 | case LYXP_AXIS_ANCESTOR: |
| 5821 | case LYXP_AXIS_PARENT: |
| 5822 | if (node_type == LYXP_NODE_ELEM) { |
| 5823 | next = lyd_parent(node); |
| 5824 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5825 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5826 | next = node; |
| 5827 | next_type = LYXP_NODE_ELEM; |
| 5828 | } else if (node_type == LYXP_NODE_META) { |
| 5829 | next = ((struct lyd_meta *)node)->parent; |
| 5830 | next_type = LYXP_NODE_ELEM; |
| 5831 | } /* else root does not have a parent */ |
| 5832 | break; |
| 5833 | |
| 5834 | case LYXP_AXIS_CHILD: |
| 5835 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5836 | assert(!node); |
| 5837 | |
| 5838 | /* search in all the trees */ |
| 5839 | next = set->tree; |
| 5840 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5841 | } else { |
| 5842 | /* search in children */ |
| 5843 | next = lyd_child(node); |
| 5844 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5845 | } |
| 5846 | break; |
| 5847 | |
| 5848 | case LYXP_AXIS_DESCENDANT: |
| 5849 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5850 | /* top-level nodes */ |
| 5851 | next = set->tree; |
| 5852 | next_type = LYXP_NODE_ELEM; |
| 5853 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5854 | /* start from the context node */ |
| 5855 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5856 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5857 | } /* else no children */ |
| 5858 | break; |
| 5859 | |
| 5860 | case LYXP_AXIS_FOLLOWING: |
| 5861 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5862 | if (node_type == LYXP_NODE_ELEM) { |
| 5863 | /* first next sibling */ |
| 5864 | next = node->next; |
| 5865 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5866 | } /* else no sibling */ |
| 5867 | break; |
| 5868 | |
| 5869 | case LYXP_AXIS_PRECEDING: |
| 5870 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5871 | /* skip ancestors */ |
| 5872 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5873 | assert(next); |
| 5874 | next_type = LYXP_NODE_ELEM; |
| 5875 | } /* else no sibling */ |
| 5876 | break; |
| 5877 | |
| 5878 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5879 | if (node_type == LYXP_NODE_ELEM) { |
| 5880 | /* first previous sibling */ |
| 5881 | next = node->prev->next ? node->prev : NULL; |
| 5882 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5883 | } /* else no sibling */ |
| 5884 | break; |
| 5885 | |
| 5886 | case LYXP_AXIS_ATTRIBUTE: |
| 5887 | /* handled specially */ |
| 5888 | assert(0); |
| 5889 | LOGINT(set->ctx); |
| 5890 | break; |
| 5891 | } |
| 5892 | |
| 5893 | *iter = next; |
| 5894 | *iter_type = next_type; |
| 5895 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5896 | } |
| 5897 | |
| 5898 | /** |
| 5899 | * @brief Iterate over all nodes on an axis for a context node. |
| 5900 | * |
| 5901 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5902 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5903 | * @param[in] node Context node. |
| 5904 | * @param[in] node_type Type of @p node. |
| 5905 | * @param[in] axis Axis to use. |
| 5906 | * @param[in] set XPath set with the general context. |
| 5907 | * @return LY_SUCCESS on success. |
| 5908 | * @return LY_ENOTFOUND if no next node found. |
| 5909 | */ |
| 5910 | static LY_ERR |
| 5911 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5912 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5913 | { |
| 5914 | const struct lyd_node *next = NULL; |
| 5915 | enum lyxp_node_type next_type = 0; |
| 5916 | |
| 5917 | if (!*iter_type) { |
| 5918 | /* first returned node */ |
| 5919 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5920 | } |
| 5921 | |
| 5922 | switch (axis) { |
| 5923 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5924 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5925 | /* fake first ancestor, we returned self before */ |
| 5926 | *iter = NULL; |
| 5927 | *iter_type = 0; |
| 5928 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 5929 | } /* else continue ancestor */ |
| 5930 | |
| 5931 | /* fallthrough */ |
| 5932 | case LYXP_AXIS_ANCESTOR: |
| 5933 | if (*iter_type == LYXP_NODE_ELEM) { |
| 5934 | /* iter parent */ |
| 5935 | next = lyd_parent(*iter); |
| 5936 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5937 | } /* else root, no ancestors */ |
| 5938 | break; |
| 5939 | |
| 5940 | case LYXP_AXIS_CHILD: |
| 5941 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5942 | |
| 5943 | /* next sibling (child) */ |
| 5944 | next = (*iter)->next; |
| 5945 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5946 | break; |
| 5947 | |
| 5948 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5949 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5950 | /* fake first descendant, we returned self before */ |
| 5951 | *iter = NULL; |
| 5952 | *iter_type = 0; |
| 5953 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 5954 | } /* else continue descendant */ |
| 5955 | |
| 5956 | /* fallthrough */ |
| 5957 | case LYXP_AXIS_DESCENDANT: |
| 5958 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5959 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 5960 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5961 | break; |
| 5962 | |
| 5963 | case LYXP_AXIS_FOLLOWING: |
| 5964 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5965 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 5966 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5967 | break; |
| 5968 | |
| 5969 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5970 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5971 | |
| 5972 | /* next sibling */ |
| 5973 | next = (*iter)->next; |
| 5974 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5975 | break; |
| 5976 | |
| 5977 | case LYXP_AXIS_PARENT: |
| 5978 | case LYXP_AXIS_SELF: |
| 5979 | /* parent/self was returned before */ |
| 5980 | break; |
| 5981 | |
| 5982 | case LYXP_AXIS_PRECEDING: |
| 5983 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5984 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 5985 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5986 | break; |
| 5987 | |
| 5988 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5989 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5990 | |
| 5991 | /* previous sibling */ |
| 5992 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 5993 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5994 | break; |
| 5995 | |
| 5996 | case LYXP_AXIS_ATTRIBUTE: |
| 5997 | /* handled specially */ |
| 5998 | assert(0); |
| 5999 | LOGINT(set->ctx); |
| 6000 | break; |
| 6001 | } |
| 6002 | |
| 6003 | *iter = next; |
| 6004 | *iter_type = next_type; |
| 6005 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6006 | } |
| 6007 | |
| 6008 | /** |
| 6009 | * @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] | 6010 | * |
| 6011 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6012 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6013 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6014 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6015 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6016 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6017 | */ |
| 6018 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6019 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6020 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6021 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6022 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6023 | const struct lyd_node *iter; |
| 6024 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6025 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6026 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6027 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6028 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6029 | return LY_SUCCESS; |
| 6030 | } |
| 6031 | |
| 6032 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6033 | 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] | 6034 | return LY_EVALID; |
| 6035 | } |
| 6036 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6037 | /* init result set */ |
| 6038 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6039 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6040 | for (i = 0; i < set->used; ++i) { |
| 6041 | /* iterate over all the nodes on the axis of the node */ |
| 6042 | iter = NULL; |
| 6043 | iter_type = 0; |
| 6044 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6045 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6046 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6047 | rc = r; |
| 6048 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6049 | } else if (r) { |
| 6050 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6051 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6052 | |
| 6053 | /* check for duplicates if they are possible */ |
| 6054 | switch (axis) { |
| 6055 | case LYXP_AXIS_ANCESTOR: |
| 6056 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6057 | case LYXP_AXIS_DESCENDANT: |
| 6058 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6059 | case LYXP_AXIS_FOLLOWING: |
| 6060 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6061 | case LYXP_AXIS_PARENT: |
| 6062 | case LYXP_AXIS_PRECEDING: |
| 6063 | case LYXP_AXIS_PRECEDING_SIBLING: |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6064 | result.non_child_axis = 1; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6065 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6066 | continue; |
| 6067 | } |
| 6068 | break; |
| 6069 | case LYXP_AXIS_CHILD: |
| 6070 | case LYXP_AXIS_SELF: |
| 6071 | break; |
| 6072 | case LYXP_AXIS_ATTRIBUTE: |
| 6073 | /* handled specially */ |
| 6074 | assert(0); |
| 6075 | LOGINT(set->ctx); |
| 6076 | break; |
| 6077 | } |
| 6078 | |
| 6079 | /* matching node */ |
| 6080 | set_insert_node(&result, iter, 0, iter_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6081 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6082 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6083 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6084 | /* move result to the set */ |
| 6085 | lyxp_set_free_content(set); |
| 6086 | *set = result; |
| 6087 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6088 | |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6089 | /* sort the final set if the document order could have been broken */ |
| 6090 | if (set->non_child_axis) { |
| 6091 | set_sort(set); |
| 6092 | } else { |
| 6093 | assert(!set_sort(set)); |
| 6094 | } |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6095 | |
| 6096 | cleanup: |
| 6097 | lyxp_set_free_content(&result); |
| 6098 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6099 | } |
| 6100 | |
| 6101 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6102 | * @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] | 6103 | * |
| 6104 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6105 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6106 | * @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] | 6107 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6108 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6109 | */ |
| 6110 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6111 | 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] | 6112 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6113 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6114 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6115 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6116 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6117 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6118 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6119 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6120 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6121 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6122 | /* init result set */ |
| 6123 | set_init(&result, set); |
| 6124 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6125 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6126 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6127 | } |
| 6128 | |
| 6129 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6130 | 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] | 6131 | ret = LY_EVALID; |
| 6132 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6133 | } |
| 6134 | |
| 6135 | /* context check for all the nodes since we have the schema node */ |
| 6136 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6137 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6138 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6139 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6140 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6141 | lyxp_set_free_content(set); |
| 6142 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6143 | } |
| 6144 | |
| 6145 | /* create specific data instance if needed */ |
| 6146 | if (scnode->nodetype == LYS_LIST) { |
| 6147 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 6148 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6149 | 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] | 6150 | } |
| 6151 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6152 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6153 | siblings = NULL; |
| 6154 | |
| 6155 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6156 | assert(!set->val.nodes[i].node); |
| 6157 | |
| 6158 | /* search in all the trees */ |
| 6159 | siblings = set->tree; |
| 6160 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6161 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6162 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6166 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6167 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6168 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6169 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6170 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6171 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6172 | |
| 6173 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6174 | 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] | 6175 | ret = LY_EINCOMPLETE; |
| 6176 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6177 | } |
| 6178 | |
| 6179 | if (sub) { |
| 6180 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6181 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6182 | } |
| 6183 | } |
| 6184 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6185 | /* move result to the set */ |
| 6186 | lyxp_set_free_content(set); |
| 6187 | *set = result; |
| 6188 | result.type = LYXP_SET_NUMBER; |
| 6189 | assert(!set_sort(set)); |
| 6190 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6191 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6192 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6193 | lyd_free_tree(inst); |
| 6194 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6195 | } |
| 6196 | |
| 6197 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6198 | * @brief Check @p node as a part of schema NameTest processing. |
| 6199 | * |
| 6200 | * @param[in] node Schema node to check. |
| 6201 | * @param[in] ctx_scnode Context node. |
| 6202 | * @param[in] set Set to read general context from. |
| 6203 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6204 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6205 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6206 | */ |
| 6207 | static LY_ERR |
| 6208 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6209 | const char *node_name, const struct lys_module *moveto_mod) |
| 6210 | { |
| 6211 | if (!moveto_mod && node_name) { |
| 6212 | switch (set->format) { |
| 6213 | case LY_VALUE_SCHEMA: |
| 6214 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6215 | /* use current module */ |
| 6216 | moveto_mod = set->cur_mod; |
| 6217 | break; |
| 6218 | case LY_VALUE_JSON: |
| 6219 | case LY_VALUE_LYB: |
| 6220 | case LY_VALUE_STR_NS: |
| 6221 | /* inherit module of the context node, if any */ |
| 6222 | if (ctx_scnode) { |
| 6223 | moveto_mod = ctx_scnode->module; |
| 6224 | } |
| 6225 | break; |
| 6226 | case LY_VALUE_CANON: |
| 6227 | case LY_VALUE_XML: |
| 6228 | /* not defined */ |
| 6229 | LOGINT(set->ctx); |
| 6230 | return LY_EINVAL; |
| 6231 | } |
| 6232 | } |
| 6233 | |
| 6234 | if (!node) { |
| 6235 | /* root will not match a specific node */ |
| 6236 | if (node_name || moveto_mod) { |
| 6237 | return LY_ENOT; |
| 6238 | } |
| 6239 | return LY_SUCCESS; |
| 6240 | } |
| 6241 | |
| 6242 | /* module check */ |
| 6243 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6244 | return LY_ENOT; |
| 6245 | } |
| 6246 | |
| 6247 | /* context check */ |
| 6248 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6249 | return LY_EINVAL; |
| 6250 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6251 | return LY_EINVAL; |
| 6252 | } |
| 6253 | |
| 6254 | /* name check */ |
| 6255 | if (node_name && (node->name != node_name)) { |
| 6256 | return LY_ENOT; |
| 6257 | } |
| 6258 | |
| 6259 | /* match */ |
| 6260 | return LY_SUCCESS; |
| 6261 | } |
| 6262 | |
| 6263 | /** |
| 6264 | * @brief Get the next node in a forward schema node DFS. |
| 6265 | * |
| 6266 | * @param[in] iter Last returned node. |
| 6267 | * @param[in] stop Node to stop the search on and not return. |
| 6268 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6269 | * @return Next node, NULL if there are no more. |
| 6270 | */ |
| 6271 | static const struct lysc_node * |
| 6272 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6273 | { |
| 6274 | const struct lysc_node *next = NULL; |
| 6275 | |
| 6276 | next = lysc_node_child(iter); |
| 6277 | if (!next) { |
| 6278 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6279 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6280 | /* we are done, no next element to process */ |
| 6281 | return NULL; |
| 6282 | } |
| 6283 | |
| 6284 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6285 | } |
| 6286 | while (!next && iter) { |
| 6287 | /* parent is already processed, go to its sibling */ |
| 6288 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6289 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6290 | /* we are done, no next element to process */ |
| 6291 | return NULL; |
| 6292 | } |
| 6293 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6294 | } |
| 6295 | |
| 6296 | return next; |
| 6297 | } |
| 6298 | |
| 6299 | /** |
| 6300 | * @brief Consider schema node based on its in_ctx enum value. |
| 6301 | * |
| 6302 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6303 | * @param[in] axis Axis to use. |
| 6304 | * @return LY_SUCCESS on success. |
| 6305 | * @return LY_ENOT if the node should not be returned. |
| 6306 | */ |
| 6307 | static LY_ERR |
| 6308 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6309 | { |
| 6310 | switch (axis) { |
| 6311 | case LYXP_AXIS_SELF: |
| 6312 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6313 | /* additionally put the start node into context */ |
| 6314 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6315 | return LY_SUCCESS; |
| 6316 | } |
| 6317 | break; |
| 6318 | case LYXP_AXIS_PARENT: |
| 6319 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6320 | case LYXP_AXIS_ANCESTOR: |
| 6321 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6322 | case LYXP_AXIS_DESCENDANT: |
| 6323 | case LYXP_AXIS_FOLLOWING: |
| 6324 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6325 | case LYXP_AXIS_PRECEDING: |
| 6326 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6327 | case LYXP_AXIS_CHILD: |
| 6328 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6329 | /* remember that context node was used */ |
| 6330 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6331 | return LY_SUCCESS; |
| 6332 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6333 | /* traversed */ |
| 6334 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6335 | return LY_SUCCESS; |
| 6336 | } |
| 6337 | break; |
| 6338 | case LYXP_AXIS_ATTRIBUTE: |
| 6339 | /* unreachable */ |
| 6340 | assert(0); |
| 6341 | LOGINT(NULL); |
| 6342 | break; |
| 6343 | } |
| 6344 | |
| 6345 | return LY_ENOT; |
| 6346 | } |
| 6347 | |
| 6348 | /** |
| 6349 | * @brief Get previous sibling for a schema node. |
| 6350 | * |
| 6351 | * @param[in] scnode Schema node. |
| 6352 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6353 | * @return Previous sibling, NULL if none. |
| 6354 | */ |
| 6355 | static const struct lysc_node * |
| 6356 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6357 | { |
| 6358 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6359 | |
| 6360 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6361 | if (next == scnode) { |
| 6362 | break; |
| 6363 | } |
| 6364 | |
| 6365 | prev = next; |
| 6366 | } |
| 6367 | |
| 6368 | return prev; |
| 6369 | } |
| 6370 | |
| 6371 | /** |
| 6372 | * @brief Get the first schema node on an axis for a context node. |
| 6373 | * |
| 6374 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6375 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6376 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6377 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6378 | * @param[in] scnode Context node. |
| 6379 | * @param[in] node_type Type of @p scnode. |
| 6380 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6381 | * @param[in] axis Axis to use. |
| 6382 | * @param[in] set XPath set with the general context. |
| 6383 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6384 | * @return LY_SUCCESS on success. |
| 6385 | * @return LY_ENOTFOUND if no next node found. |
| 6386 | */ |
| 6387 | static LY_ERR |
| 6388 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6389 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6390 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6391 | { |
| 6392 | const struct lysc_node *next = NULL; |
| 6393 | enum lyxp_node_type next_type = 0; |
| 6394 | |
| 6395 | assert(!*iter); |
| 6396 | assert(!*iter_type); |
| 6397 | |
| 6398 | *iter_mod = NULL; |
| 6399 | *iter_mod_idx = 0; |
| 6400 | |
| 6401 | switch (axis) { |
| 6402 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6403 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6404 | case LYXP_AXIS_SELF: |
| 6405 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6406 | /* just return the node */ |
| 6407 | next = scnode; |
| 6408 | next_type = node_type; |
| 6409 | } |
| 6410 | break; |
| 6411 | |
| 6412 | case LYXP_AXIS_ANCESTOR: |
| 6413 | case LYXP_AXIS_PARENT: |
| 6414 | if (node_type == LYXP_NODE_ELEM) { |
| 6415 | next = lysc_data_parent(scnode); |
| 6416 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6417 | } /* else no parent */ |
| 6418 | break; |
| 6419 | |
| 6420 | case LYXP_AXIS_DESCENDANT: |
| 6421 | case LYXP_AXIS_CHILD: |
| 6422 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6423 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6424 | * it can be in a top-level augment */ |
| 6425 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6426 | /* module may not be implemented or not compiled yet */ |
| 6427 | if (!(*iter_mod)->compiled) { |
| 6428 | continue; |
| 6429 | } |
| 6430 | |
| 6431 | /* get next node */ |
| 6432 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6433 | next_type = LYXP_NODE_ELEM; |
| 6434 | break; |
| 6435 | } |
| 6436 | } |
| 6437 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6438 | /* get next node */ |
| 6439 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6440 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6441 | } |
| 6442 | break; |
| 6443 | |
| 6444 | case LYXP_AXIS_FOLLOWING: |
| 6445 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6446 | if (node_type == LYXP_NODE_ELEM) { |
| 6447 | /* first next sibling */ |
| 6448 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6449 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6450 | } /* else no sibling */ |
| 6451 | break; |
| 6452 | |
| 6453 | case LYXP_AXIS_PRECEDING: |
| 6454 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6455 | if (node_type == LYXP_NODE_ELEM) { |
| 6456 | /* first parent sibling */ |
| 6457 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6458 | if (next == scnode) { |
| 6459 | /* no preceding sibling */ |
| 6460 | next = NULL; |
| 6461 | } |
| 6462 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6463 | } /* else no sibling */ |
| 6464 | break; |
| 6465 | |
| 6466 | case LYXP_AXIS_ATTRIBUTE: |
| 6467 | /* unreachable */ |
| 6468 | assert(0); |
| 6469 | LOGINT(set->ctx); |
| 6470 | break; |
| 6471 | } |
| 6472 | |
| 6473 | *iter = next; |
| 6474 | *iter_type = next_type; |
| 6475 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6476 | } |
| 6477 | |
| 6478 | /** |
| 6479 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6480 | * |
| 6481 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6482 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6483 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6484 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6485 | * @param[in] scnode Context node. |
| 6486 | * @param[in] node_type Type of @p scnode. |
| 6487 | * @param[in] axis Axis to use. |
| 6488 | * @param[in] set XPath set with the general context. |
| 6489 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6490 | * @return LY_SUCCESS on success. |
| 6491 | * @return LY_ENOTFOUND if no next node found. |
| 6492 | */ |
| 6493 | static LY_ERR |
| 6494 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6495 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6496 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6497 | { |
| 6498 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6499 | enum lyxp_node_type next_type = 0; |
| 6500 | |
| 6501 | if (!*iter_type) { |
| 6502 | /* first returned node */ |
| 6503 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6504 | getnext_opts); |
| 6505 | } |
| 6506 | |
| 6507 | switch (axis) { |
| 6508 | case LYXP_AXIS_PARENT: |
| 6509 | case LYXP_AXIS_SELF: |
| 6510 | /* parent/self was returned before */ |
| 6511 | break; |
| 6512 | |
| 6513 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6514 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6515 | /* fake first ancestor, we returned self before */ |
| 6516 | *iter = NULL; |
| 6517 | *iter_type = 0; |
| 6518 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6519 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
| 6520 | } /* else continue ancestor */ |
| 6521 | |
| 6522 | /* fallthrough */ |
| 6523 | case LYXP_AXIS_ANCESTOR: |
| 6524 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6525 | next = lysc_data_parent(*iter); |
| 6526 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6527 | } /* else no ancestor */ |
| 6528 | break; |
| 6529 | |
| 6530 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6531 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6532 | /* fake first descendant, we returned self before */ |
| 6533 | *iter = NULL; |
| 6534 | *iter_type = 0; |
| 6535 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6536 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6537 | } /* else DFS until context node */ |
| 6538 | dfs_stop = scnode; |
| 6539 | |
| 6540 | /* fallthrough */ |
| 6541 | case LYXP_AXIS_DESCENDANT: |
| 6542 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6543 | /* DFS until the context node */ |
| 6544 | dfs_stop = scnode; |
| 6545 | } |
| 6546 | |
| 6547 | /* fallthrough */ |
| 6548 | case LYXP_AXIS_PRECEDING: |
| 6549 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6550 | /* DFS until the previous sibling */ |
| 6551 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6552 | assert(dfs_stop); |
| 6553 | |
| 6554 | if (*iter == dfs_stop) { |
| 6555 | /* we are done */ |
| 6556 | break; |
| 6557 | } |
| 6558 | } |
| 6559 | |
| 6560 | /* fallthrough */ |
| 6561 | case LYXP_AXIS_FOLLOWING: |
| 6562 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6563 | /* DFS through the whole module */ |
| 6564 | dfs_stop = NULL; |
| 6565 | } |
| 6566 | |
| 6567 | /* nested nodes */ |
| 6568 | assert(*iter); |
| 6569 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6570 | if (next) { |
| 6571 | next_type = LYXP_NODE_ELEM; |
| 6572 | break; |
| 6573 | } /* else get next top-level node just like a child */ |
| 6574 | |
| 6575 | /* fallthrough */ |
| 6576 | case LYXP_AXIS_CHILD: |
| 6577 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6578 | if (!*iter_mod) { |
| 6579 | /* nodes from a single module */ |
| 6580 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6581 | next_type = LYXP_NODE_ELEM; |
| 6582 | break; |
| 6583 | } |
| 6584 | |
| 6585 | assert(scnode); |
Michal Vasko | a353cce | 2022-11-14 10:09:55 +0100 | [diff] [blame] | 6586 | if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6587 | /* iterating over top-level nodes, find next */ |
| 6588 | while (lysc_data_parent(*iter)) { |
| 6589 | *iter = lysc_data_parent(*iter); |
| 6590 | } |
| 6591 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6592 | next_type = LYXP_NODE_ELEM; |
| 6593 | break; |
| 6594 | } |
| 6595 | } |
| 6596 | } |
| 6597 | |
| 6598 | while (*iter_mod) { |
| 6599 | /* module top-level nodes */ |
| 6600 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6601 | next_type = LYXP_NODE_ELEM; |
| 6602 | break; |
| 6603 | } |
| 6604 | |
| 6605 | /* get next module */ |
| 6606 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6607 | /* module may not be implemented or not compiled yet */ |
| 6608 | if ((*iter_mod)->compiled) { |
| 6609 | break; |
| 6610 | } |
| 6611 | } |
| 6612 | |
| 6613 | /* new module, start over */ |
| 6614 | *iter = NULL; |
| 6615 | } |
| 6616 | break; |
| 6617 | |
| 6618 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6619 | assert(*iter); |
| 6620 | |
| 6621 | /* next parent sibling until scnode */ |
| 6622 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6623 | if (next == scnode) { |
| 6624 | /* no previous sibling */ |
| 6625 | next = NULL; |
| 6626 | } |
| 6627 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6628 | break; |
| 6629 | |
| 6630 | case LYXP_AXIS_ATTRIBUTE: |
| 6631 | /* unreachable */ |
| 6632 | assert(0); |
| 6633 | LOGINT(set->ctx); |
| 6634 | break; |
| 6635 | } |
| 6636 | |
| 6637 | *iter = next; |
| 6638 | *iter_type = next_type; |
| 6639 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6640 | } |
| 6641 | |
| 6642 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6643 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6644 | * |
| 6645 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6646 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6647 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6648 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6649 | * @param[in] options XPath options. |
| 6650 | * @return LY_ERR |
| 6651 | */ |
| 6652 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6653 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6654 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6655 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6656 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6657 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6658 | const struct lys_module *mod; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6659 | const struct lysc_node *iter; |
| 6660 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6661 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6662 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6663 | return LY_SUCCESS; |
| 6664 | } |
| 6665 | |
| 6666 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6667 | 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] | 6668 | return LY_EVALID; |
| 6669 | } |
| 6670 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6671 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6672 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6673 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6674 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6675 | } |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 6676 | if (options & LYXP_SCNODE_SCHEMAMOUNT) { |
| 6677 | getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT; |
| 6678 | } |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6679 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6680 | orig_used = set->used; |
| 6681 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6682 | /* update in_ctx first */ |
| 6683 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6684 | /* not usable, skip */ |
| 6685 | continue; |
| 6686 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6687 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6688 | iter = NULL; |
| 6689 | iter_type = 0; |
| 6690 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6691 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6692 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6693 | continue; |
| 6694 | } |
| 6695 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6696 | /* insert */ |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 6697 | 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] | 6698 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6699 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6700 | if ((idx < orig_used) && (idx > i)) { |
| 6701 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6702 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6703 | } |
| 6704 | } |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 6705 | |
| 6706 | if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) && |
| 6707 | (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode, |
| 6708 | moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) { |
| 6709 | /* there is a matching node from an extension, use it */ |
| 6710 | LY_CHECK_RET(set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx)); |
| 6711 | if ((idx < orig_used) && (idx > i)) { |
| 6712 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6713 | temp_ctx = 1; |
| 6714 | } |
| 6715 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6716 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6717 | |
| 6718 | /* correct temporary in_ctx values */ |
| 6719 | if (temp_ctx) { |
| 6720 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6721 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6722 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6723 | } |
| 6724 | } |
| 6725 | } |
| 6726 | |
| 6727 | return LY_SUCCESS; |
| 6728 | } |
| 6729 | |
| 6730 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6731 | * @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] | 6732 | * Context position aware. |
| 6733 | * |
| 6734 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6735 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6736 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6737 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6738 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6739 | */ |
| 6740 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6741 | 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] | 6742 | { |
| 6743 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6744 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6745 | struct lyxp_set ret_set; |
| 6746 | LY_ERR rc; |
| 6747 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6748 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6749 | return LY_SUCCESS; |
| 6750 | } |
| 6751 | |
| 6752 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6753 | 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] | 6754 | return LY_EVALID; |
| 6755 | } |
| 6756 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6757 | /* 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] | 6758 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6759 | LY_CHECK_RET(rc); |
| 6760 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6761 | /* 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] | 6762 | set_init(&ret_set, set); |
| 6763 | for (i = 0; i < set->used; ++i) { |
| 6764 | |
| 6765 | /* TREE DFS */ |
| 6766 | start = set->val.nodes[i].node; |
| 6767 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6768 | 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] | 6769 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6770 | /* add matching node into result set */ |
| 6771 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6772 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6773 | /* the node is a duplicate, we'll process it later in the set */ |
| 6774 | goto skip_children; |
| 6775 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6776 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6777 | return rc; |
| 6778 | } else if (rc == LY_EINVAL) { |
| 6779 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | } |
| 6781 | |
| 6782 | /* TREE DFS NEXT ELEM */ |
| 6783 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6784 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6785 | if (!next) { |
| 6786 | skip_children: |
| 6787 | /* no children, so try siblings, but only if it's not the start, |
| 6788 | * that is considered to be the root and it's siblings are not traversed */ |
| 6789 | if (elem != start) { |
| 6790 | next = elem->next; |
| 6791 | } else { |
| 6792 | break; |
| 6793 | } |
| 6794 | } |
| 6795 | while (!next) { |
| 6796 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6797 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6798 | /* we are done, no next element to process */ |
| 6799 | break; |
| 6800 | } |
| 6801 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6802 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6803 | next = elem->next; |
| 6804 | } |
| 6805 | } |
| 6806 | } |
| 6807 | |
| 6808 | /* make the temporary set the current one */ |
| 6809 | ret_set.ctx_pos = set->ctx_pos; |
| 6810 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6811 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6812 | memcpy(set, &ret_set, sizeof *set); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6813 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6814 | |
| 6815 | return LY_SUCCESS; |
| 6816 | } |
| 6817 | |
| 6818 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6819 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6820 | * Result is LYXP_SET_NODE_SET. |
| 6821 | * |
| 6822 | * @param[in] set Set to use. |
| 6823 | * @param[in] start Start node whose subtree to add. |
| 6824 | * @param[in] start_idx Index of @p start in @p set. |
| 6825 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6826 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6827 | * @param[in] options XPath options. |
| 6828 | * @return LY_ERR value. |
| 6829 | */ |
| 6830 | static LY_ERR |
| 6831 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6832 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6833 | { |
| 6834 | const struct lysc_node *next, *elem; |
| 6835 | uint32_t idx; |
| 6836 | LY_ERR rc; |
| 6837 | |
| 6838 | /* TREE DFS */ |
| 6839 | for (elem = next = start; elem; elem = next) { |
| 6840 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6841 | /* schema-only nodes, skip root */ |
| 6842 | goto next_iter; |
| 6843 | } |
| 6844 | |
| 6845 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6846 | if (!rc) { |
| 6847 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6848 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6849 | if (idx > start_idx) { |
| 6850 | /* we will process it later in the set */ |
| 6851 | goto skip_children; |
| 6852 | } |
| 6853 | } else { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 6854 | 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] | 6855 | } |
| 6856 | } else if (rc == LY_EINVAL) { |
| 6857 | goto skip_children; |
| 6858 | } |
| 6859 | |
| 6860 | next_iter: |
| 6861 | /* TREE DFS NEXT ELEM */ |
| 6862 | /* select element for the next run - children first */ |
| 6863 | next = lysc_node_child(elem); |
| 6864 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6865 | next = next->next; |
| 6866 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6867 | next = next->next; |
| 6868 | } |
| 6869 | if (!next) { |
| 6870 | skip_children: |
| 6871 | /* no children, so try siblings, but only if it's not the start, |
| 6872 | * that is considered to be the root and it's siblings are not traversed */ |
| 6873 | if (elem != start) { |
| 6874 | next = elem->next; |
| 6875 | } else { |
| 6876 | break; |
| 6877 | } |
| 6878 | } |
| 6879 | while (!next) { |
| 6880 | /* no siblings, go back through the parents */ |
| 6881 | if (elem->parent == start) { |
| 6882 | /* we are done, no next element to process */ |
| 6883 | break; |
| 6884 | } |
| 6885 | /* parent is already processed, go to its sibling */ |
| 6886 | elem = elem->parent; |
| 6887 | next = elem->next; |
| 6888 | } |
| 6889 | } |
| 6890 | |
| 6891 | return LY_SUCCESS; |
| 6892 | } |
| 6893 | |
| 6894 | /** |
| 6895 | * @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] | 6896 | * |
| 6897 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6898 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6899 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6900 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6901 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6902 | */ |
| 6903 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6904 | 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] | 6905 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6906 | uint32_t i, orig_used, mod_idx; |
| 6907 | const struct lys_module *mod; |
| 6908 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6909 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6910 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6911 | return LY_SUCCESS; |
| 6912 | } |
| 6913 | |
| 6914 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6915 | 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] | 6916 | return LY_EVALID; |
| 6917 | } |
| 6918 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6919 | orig_used = set->used; |
| 6920 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6921 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6922 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6923 | continue; |
| 6924 | } |
| 6925 | |
| 6926 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6927 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6928 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6929 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6930 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6931 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6932 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6933 | /* traverse all top-level nodes in all the modules */ |
| 6934 | mod_idx = 0; |
| 6935 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6936 | /* module may not be implemented or not compiled yet */ |
| 6937 | if (!mod->compiled) { |
| 6938 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6939 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6940 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6941 | root = NULL; |
| 6942 | /* no getnext opts needed */ |
| 6943 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 6944 | 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] | 6945 | } |
| 6946 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6947 | |
| 6948 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6949 | /* add all the descendants recursively */ |
| 6950 | 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] | 6951 | } |
| 6952 | } |
| 6953 | |
| 6954 | return LY_SUCCESS; |
| 6955 | } |
| 6956 | |
| 6957 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6958 | * @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] | 6959 | * Indirectly context position aware. |
| 6960 | * |
| 6961 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6962 | * @param[in] mod Matching metadata module, NULL for any. |
| 6963 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6964 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6965 | * @return LY_ERR |
| 6966 | */ |
| 6967 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6968 | 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] | 6969 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6970 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6971 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6972 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6973 | return LY_SUCCESS; |
| 6974 | } |
| 6975 | |
| 6976 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6977 | 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] | 6978 | return LY_EVALID; |
| 6979 | } |
| 6980 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6981 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6982 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6983 | |
| 6984 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 6985 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6986 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6987 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6988 | |
| 6989 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6990 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6991 | continue; |
| 6992 | } |
| 6993 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6994 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6995 | /* match */ |
| 6996 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6997 | set->val.meta[i].meta = sub; |
| 6998 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6999 | /* pos does not change */ |
| 7000 | replaced = 1; |
| 7001 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7002 | 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] | 7003 | } |
| 7004 | ++i; |
| 7005 | } |
| 7006 | } |
| 7007 | } |
| 7008 | |
| 7009 | if (!replaced) { |
| 7010 | /* no match */ |
| 7011 | set_remove_node(set, i); |
| 7012 | } |
| 7013 | } |
| 7014 | |
| 7015 | return LY_SUCCESS; |
| 7016 | } |
| 7017 | |
| 7018 | /** |
| 7019 | * @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] | 7020 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7021 | * |
| 7022 | * @param[in,out] set1 Set to use for the result. |
| 7023 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7024 | * @return LY_ERR |
| 7025 | */ |
| 7026 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7027 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7028 | { |
| 7029 | LY_ERR rc; |
| 7030 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7031 | 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] | 7032 | 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] | 7033 | return LY_EVALID; |
| 7034 | } |
| 7035 | |
| 7036 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7037 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7038 | return LY_SUCCESS; |
| 7039 | } |
| 7040 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7041 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 7042 | /* release hidden allocated data (lyxp_set.size) */ |
| 7043 | lyxp_set_free_content(set1); |
| 7044 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7045 | memcpy(set1, set2, sizeof *set1); |
| 7046 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7047 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7048 | return LY_SUCCESS; |
| 7049 | } |
| 7050 | |
| 7051 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7052 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7053 | |
| 7054 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7055 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7056 | LY_CHECK_RET(rc); |
| 7057 | |
| 7058 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7059 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7060 | |
| 7061 | return LY_SUCCESS; |
| 7062 | } |
| 7063 | |
| 7064 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7065 | * @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] | 7066 | * Context position aware. |
| 7067 | * |
| 7068 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7069 | * @param[in] mod Matching metadata module, NULL for any. |
| 7070 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7071 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7072 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7073 | */ |
| 7074 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7075 | 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] | 7076 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7077 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7078 | struct lyxp_set *set_all_desc = NULL; |
| 7079 | LY_ERR rc; |
| 7080 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7081 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7082 | return LY_SUCCESS; |
| 7083 | } |
| 7084 | |
| 7085 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7086 | 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] | 7087 | return LY_EVALID; |
| 7088 | } |
| 7089 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7090 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7091 | * but it likely won't be used much, so it's a waste of time */ |
| 7092 | /* copy the context */ |
| 7093 | set_all_desc = set_copy(set); |
| 7094 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7095 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7096 | if (rc != LY_SUCCESS) { |
| 7097 | lyxp_set_free(set_all_desc); |
| 7098 | return rc; |
| 7099 | } |
| 7100 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7101 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7102 | if (rc != LY_SUCCESS) { |
| 7103 | lyxp_set_free(set_all_desc); |
| 7104 | return rc; |
| 7105 | } |
| 7106 | lyxp_set_free(set_all_desc); |
| 7107 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7108 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7109 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7110 | |
| 7111 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7112 | * we have all attributes qualified in lyd tree */ |
| 7113 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7114 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7115 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7116 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7117 | continue; |
| 7118 | } |
| 7119 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7120 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7121 | /* match */ |
| 7122 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7123 | set->val.meta[i].meta = sub; |
| 7124 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7125 | /* pos does not change */ |
| 7126 | replaced = 1; |
| 7127 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7128 | 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] | 7129 | } |
| 7130 | ++i; |
| 7131 | } |
| 7132 | } |
| 7133 | } |
| 7134 | |
| 7135 | if (!replaced) { |
| 7136 | /* no match */ |
| 7137 | set_remove_node(set, i); |
| 7138 | } |
| 7139 | } |
| 7140 | |
| 7141 | return LY_SUCCESS; |
| 7142 | } |
| 7143 | |
| 7144 | /** |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7145 | * @brief Move context @p set1 single item to the result of a comparison. |
| 7146 | * |
| 7147 | * @param[in] set1 First set with the item to compare. |
| 7148 | * @param[in] idx1 Index of the item in @p set1. |
| 7149 | * @param[in] set2 Second set. |
| 7150 | * @param[in] op Comparison operator to process. |
| 7151 | * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`. |
| 7152 | * @param[out] result Result of the comparison. |
| 7153 | * @return LY_ERR value. |
| 7154 | */ |
| 7155 | static LY_ERR |
| 7156 | moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op, |
| 7157 | ly_bool switch_operands, ly_bool *result) |
| 7158 | { |
| 7159 | struct lyxp_set tmp1 = {0}; |
| 7160 | LY_ERR rc = LY_SUCCESS; |
| 7161 | |
| 7162 | assert(set1->type == LYXP_SET_NODE_SET); |
| 7163 | |
| 7164 | /* cast set1 */ |
| 7165 | switch (set2->type) { |
| 7166 | case LYXP_SET_NUMBER: |
| 7167 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); |
| 7168 | break; |
| 7169 | case LYXP_SET_BOOLEAN: |
| 7170 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); |
| 7171 | break; |
| 7172 | default: |
| 7173 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); |
| 7174 | break; |
| 7175 | } |
| 7176 | LY_CHECK_GOTO(rc, cleanup); |
| 7177 | |
| 7178 | /* canonize set2 */ |
| 7179 | LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup); |
| 7180 | |
| 7181 | /* compare recursively and store the result */ |
| 7182 | if (switch_operands) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7183 | LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7184 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7185 | LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7186 | } |
| 7187 | |
| 7188 | cleanup: |
| 7189 | lyxp_set_free_content(&tmp1); |
| 7190 | return rc; |
| 7191 | } |
| 7192 | |
| 7193 | /** |
| 7194 | * @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] | 7195 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7196 | * |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7197 | * @param[in] set1 Set acting as the first operand for @p op. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7198 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7199 | * @param[in] op Comparison operator to process. |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7200 | * @param[out] result Result of the comparison. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7201 | * @return LY_ERR |
| 7202 | */ |
| 7203 | static LY_ERR |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7204 | 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] | 7205 | { |
| 7206 | /* |
| 7207 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7208 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7209 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7210 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7211 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7212 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7213 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7214 | * |
| 7215 | * '=' or '!=' |
| 7216 | * BOOLEAN + BOOLEAN |
| 7217 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7218 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7219 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7220 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7221 | * NUMBER + NUMBER |
| 7222 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7223 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7224 | * STRING + STRING |
| 7225 | * |
| 7226 | * '<=', '<', '>=', '>' |
| 7227 | * NUMBER + NUMBER |
| 7228 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7229 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7230 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7231 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7232 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7233 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7234 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7235 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7236 | */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7237 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7238 | LY_ERR rc; |
| 7239 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7240 | /* iterative evaluation with node-sets */ |
| 7241 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7242 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7243 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7244 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7245 | 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] | 7246 | |
| 7247 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7248 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7249 | return LY_SUCCESS; |
| 7250 | } |
| 7251 | } |
| 7252 | } else { |
| 7253 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7254 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7255 | 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] | 7256 | |
| 7257 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7258 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7259 | return LY_SUCCESS; |
| 7260 | } |
| 7261 | } |
| 7262 | } |
| 7263 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7264 | /* false for all the nodes */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7265 | *result = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7266 | return LY_SUCCESS; |
| 7267 | } |
| 7268 | |
| 7269 | /* first convert properly */ |
| 7270 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7271 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7272 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7273 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7274 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7275 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7276 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7277 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7278 | LY_CHECK_RET(rc); |
| 7279 | } /* else we have 2 strings */ |
| 7280 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7281 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7282 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7283 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7284 | LY_CHECK_RET(rc); |
| 7285 | } |
| 7286 | |
| 7287 | assert(set1->type == set2->type); |
| 7288 | |
| 7289 | /* compute result */ |
| 7290 | if (op[0] == '=') { |
| 7291 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7292 | *result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7293 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7294 | *result = (set1->val.num == set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7295 | } else { |
| 7296 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7297 | *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7298 | } |
| 7299 | } else if (op[0] == '!') { |
| 7300 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7301 | *result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7302 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7303 | *result = (set1->val.num != set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7304 | } else { |
| 7305 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7306 | *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7307 | } |
| 7308 | } else { |
| 7309 | assert(set1->type == LYXP_SET_NUMBER); |
| 7310 | if (op[0] == '<') { |
| 7311 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7312 | *result = (set1->val.num <= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7313 | } else { |
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 | } |
| 7316 | } else { |
| 7317 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7318 | *result = (set1->val.num >= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7319 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7320 | *result = (set1->val.num > set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7321 | } |
| 7322 | } |
| 7323 | } |
| 7324 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7325 | return LY_SUCCESS; |
| 7326 | } |
| 7327 | |
| 7328 | /** |
| 7329 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7330 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7331 | * |
| 7332 | * @param[in,out] set1 Set to use for the result. |
| 7333 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7334 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7335 | * @return LY_ERR |
| 7336 | */ |
| 7337 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7338 | 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] | 7339 | { |
| 7340 | LY_ERR rc; |
| 7341 | |
| 7342 | /* unary '-' */ |
| 7343 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7344 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7345 | LY_CHECK_RET(rc); |
| 7346 | set1->val.num *= -1; |
| 7347 | lyxp_set_free(set2); |
| 7348 | return LY_SUCCESS; |
| 7349 | } |
| 7350 | |
| 7351 | assert(set1 && set2); |
| 7352 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7353 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7354 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7355 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7356 | LY_CHECK_RET(rc); |
| 7357 | |
| 7358 | switch (op[0]) { |
| 7359 | /* '+' */ |
| 7360 | case '+': |
| 7361 | set1->val.num += set2->val.num; |
| 7362 | break; |
| 7363 | |
| 7364 | /* '-' */ |
| 7365 | case '-': |
| 7366 | set1->val.num -= set2->val.num; |
| 7367 | break; |
| 7368 | |
| 7369 | /* '*' */ |
| 7370 | case '*': |
| 7371 | set1->val.num *= set2->val.num; |
| 7372 | break; |
| 7373 | |
| 7374 | /* 'div' */ |
| 7375 | case 'd': |
| 7376 | set1->val.num /= set2->val.num; |
| 7377 | break; |
| 7378 | |
| 7379 | /* 'mod' */ |
| 7380 | case 'm': |
| 7381 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7382 | break; |
| 7383 | |
| 7384 | default: |
| 7385 | LOGINT_RET(set1->ctx); |
| 7386 | } |
| 7387 | |
| 7388 | return LY_SUCCESS; |
| 7389 | } |
| 7390 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7391 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7392 | * @brief Evaluate Predicate. Logs directly on error. |
| 7393 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7394 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7395 | * |
| 7396 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7397 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7398 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7399 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7400 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7401 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7402 | */ |
| 7403 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7404 | 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] | 7405 | { |
| 7406 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7407 | uint32_t i, orig_exp, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7408 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7409 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7410 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7411 | |
| 7412 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7413 | 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] | 7414 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7415 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7416 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7417 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7418 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7419 | 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] | 7420 | LY_CHECK_RET(rc); |
| 7421 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7422 | /* 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] | 7423 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7424 | |
| 7425 | /* empty set, nothing to evaluate */ |
| 7426 | if (!set->used) { |
| 7427 | goto only_parse; |
| 7428 | } |
| 7429 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7430 | /* decide forward or reverse axis */ |
| 7431 | switch (axis) { |
| 7432 | case LYXP_AXIS_ANCESTOR: |
| 7433 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7434 | case LYXP_AXIS_PRECEDING: |
| 7435 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7436 | reverse_axis = 1; |
| 7437 | break; |
| 7438 | case LYXP_AXIS_DESCENDANT: |
| 7439 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7440 | case LYXP_AXIS_FOLLOWING: |
| 7441 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7442 | case LYXP_AXIS_PARENT: |
| 7443 | case LYXP_AXIS_CHILD: |
| 7444 | case LYXP_AXIS_SELF: |
| 7445 | case LYXP_AXIS_ATTRIBUTE: |
| 7446 | reverse_axis = 0; |
| 7447 | break; |
| 7448 | } |
| 7449 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7450 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7451 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7452 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7453 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7454 | set_init(&set2, set); |
| 7455 | 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] | 7456 | |
| 7457 | /* remember the node context position for position() and context size for last() */ |
| 7458 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7459 | |
| 7460 | set2.ctx_pos = orig_pos; |
| 7461 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7462 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7463 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7464 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7465 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7466 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7467 | return rc; |
| 7468 | } |
| 7469 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7470 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | if (set2.type == LYXP_SET_NUMBER) { |
| 7472 | if ((long long)set2.val.num == orig_pos) { |
| 7473 | set2.val.num = 1; |
| 7474 | } else { |
| 7475 | set2.val.num = 0; |
| 7476 | } |
| 7477 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7478 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7479 | |
| 7480 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7481 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7482 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7483 | } |
| 7484 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7485 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7486 | |
| 7487 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7488 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7489 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7490 | /* there is a currently-valid node */ |
| 7491 | break; |
| 7492 | } |
| 7493 | } |
| 7494 | /* empty set, nothing to evaluate */ |
| 7495 | if (i == set->used) { |
| 7496 | goto only_parse; |
| 7497 | } |
| 7498 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7499 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7500 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7501 | /* set special in_ctx to all the valid snodes */ |
| 7502 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7503 | |
| 7504 | /* use the valid snodes one-by-one */ |
| 7505 | for (i = 0; i < set->used; ++i) { |
| 7506 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7507 | continue; |
| 7508 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7509 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7510 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7511 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7512 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7513 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7514 | LY_CHECK_RET(rc); |
| 7515 | |
| 7516 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7517 | } |
| 7518 | |
| 7519 | /* restore the state as it was before the predicate */ |
| 7520 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7521 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7522 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7523 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7524 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7525 | } |
| 7526 | } |
| 7527 | |
| 7528 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7529 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7530 | set_fill_set(&set2, set); |
| 7531 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7532 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7533 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7534 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7535 | return rc; |
| 7536 | } |
| 7537 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7538 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7539 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7540 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7541 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7542 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7543 | } |
| 7544 | |
| 7545 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7546 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7547 | 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] | 7548 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7549 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7550 | |
| 7551 | return LY_SUCCESS; |
| 7552 | } |
| 7553 | |
| 7554 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7555 | * @brief Evaluate Literal. Logs directly on error. |
| 7556 | * |
| 7557 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7558 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7559 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7560 | */ |
| 7561 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7562 | 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] | 7563 | { |
| 7564 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7565 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7566 | set_fill_string(set, "", 0); |
| 7567 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7568 | 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] | 7569 | } |
| 7570 | } |
| 7571 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7572 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7573 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7574 | } |
| 7575 | |
| 7576 | /** |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7577 | * @brief Check that a nametest in a predicate matches a key node. |
| 7578 | * |
| 7579 | * @param[in] nametest Nametest to check. |
| 7580 | * @param[in] len Length of @p nametest. |
| 7581 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7582 | * @param[in] set Context set. |
| 7583 | * @param[in] key Expected key node. |
| 7584 | * @return LY_SUCCESS on success, |
| 7585 | * @return LY_ENOT if a predicate could not be compiled. |
| 7586 | * @return LY_ERR on any error. |
| 7587 | */ |
| 7588 | static LY_ERR |
| 7589 | eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode, |
| 7590 | const struct lyxp_set *set, const struct lysc_node *key) |
| 7591 | { |
| 7592 | const struct lys_module *mod; |
| 7593 | |
| 7594 | /* prefix (module) */ |
| 7595 | LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod)); |
| 7596 | if (mod != key->module) { |
| 7597 | return LY_ENOT; |
| 7598 | } |
| 7599 | |
| 7600 | /* node name */ |
| 7601 | if (ly_strncmp(key->name, nametest, len)) { |
| 7602 | return LY_ENOT; |
| 7603 | } |
| 7604 | |
| 7605 | return LY_SUCCESS; |
| 7606 | } |
| 7607 | |
| 7608 | /** |
| 7609 | * @brief Append a simple predicate for the node. |
| 7610 | * |
| 7611 | * @param[in] exp Full parsed XPath expression. |
| 7612 | * @param[in] tok_idx Predicate start index in @p exp. |
| 7613 | * @param[in] end_tok_idx Predicate end index in @p exp. |
| 7614 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7615 | * @param[in] set Context set. |
| 7616 | * @param[in] pred_node Node with the value referenced in the predicate. |
| 7617 | * @param[in,out] pred Predicate to append to. |
| 7618 | * @param[in,out] pred_len Length of @p pred, is updated. |
| 7619 | * @return LY_SUCCESS on success, |
| 7620 | * @return LY_ENOT if a predicate could not be compiled. |
| 7621 | * @return LY_ERR on any error. |
| 7622 | */ |
| 7623 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7624 | 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] | 7625 | const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred, |
| 7626 | uint32_t *pred_len) |
| 7627 | { |
| 7628 | LY_ERR rc = LY_SUCCESS; |
| 7629 | uint32_t i; |
| 7630 | const struct lyd_node *siblings; |
| 7631 | struct lyd_node *ctx_node; |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7632 | const struct lysc_node *sparent, *cur_scnode; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7633 | struct lyxp_expr *val_exp = NULL; |
| 7634 | struct lyxp_set set2 = {0}; |
| 7635 | char quot; |
| 7636 | |
| 7637 | /* duplicate the value expression */ |
| 7638 | LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup); |
| 7639 | |
| 7640 | /* get its atoms */ |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7641 | cur_scnode = set->cur_node ? set->cur_node->schema : NULL; |
| 7642 | LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode, |
| 7643 | ctx_scnode, &set2, LYXP_SCNODE), cleanup); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7644 | |
| 7645 | /* check whether we can compile a single predicate (evaluation result value is always the same) */ |
| 7646 | for (i = 0; i < set2.used; ++i) { |
| 7647 | if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) { |
| 7648 | /* skip root and context node */ |
| 7649 | continue; |
| 7650 | } |
| 7651 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7652 | /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the |
| 7653 | * context node instance */ |
| 7654 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) { |
| 7655 | /* 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] | 7656 | rc = LY_ENOT; |
| 7657 | goto cleanup; |
| 7658 | } |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7659 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) { |
| 7660 | for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {} |
| 7661 | if (sparent) { |
| 7662 | /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */ |
| 7663 | rc = LY_ENOT; |
| 7664 | goto cleanup; |
| 7665 | } |
| 7666 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7667 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7668 | /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered, |
| 7669 | * but the current node can be safely ignored, it is always the same data instance */ |
| 7670 | 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] | 7671 | rc = LY_ENOT; |
| 7672 | goto cleanup; |
| 7673 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7674 | } |
| 7675 | |
| 7676 | /* get any data instance of the context node, we checked it makes no difference */ |
| 7677 | siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree; |
| 7678 | LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup); |
| 7679 | |
| 7680 | /* evaluate the value subexpression with the root context node */ |
| 7681 | lyxp_set_free_content(&set2); |
| 7682 | LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node, |
| 7683 | ctx_node, set->tree, NULL, &set2, 0), cleanup); |
| 7684 | |
| 7685 | /* cast it into a string */ |
| 7686 | LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup); |
| 7687 | |
| 7688 | /* append the JSON predicate */ |
| 7689 | *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3); |
| 7690 | LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7691 | quot = strchr(set2.val.str, '\'') ? '\"' : '\''; |
| 7692 | *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot); |
| 7693 | |
| 7694 | cleanup: |
| 7695 | lyxp_expr_free(set->ctx, val_exp); |
| 7696 | lyxp_set_free_content(&set2); |
| 7697 | return rc; |
| 7698 | } |
| 7699 | |
| 7700 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7701 | * @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] | 7702 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7703 | * @param[in] exp Full parsed XPath expression. |
| 7704 | * @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] | 7705 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7706 | * @param[in] set Context set. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7707 | * @param[out] predicates Parsed predicates. |
| 7708 | * @param[out] pred_type Type of @p predicates. |
| 7709 | * @return LY_SUCCESS on success, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7710 | * @return LY_ENOT if a predicate could not be compiled. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7711 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7712 | */ |
| 7713 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7714 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7715 | const struct lyxp_set *set, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7716 | { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7717 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7718 | 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] | 7719 | const struct lysc_node *key; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7720 | char *pred = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7721 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7722 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7723 | assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7724 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7725 | /* turn logging off */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7726 | ly_temp_log_options(&temp_lo); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7727 | |
| 7728 | if (ctx_scnode->nodetype == LYS_LIST) { |
| 7729 | /* check for predicates "[key1=...][key2=...]..." */ |
| 7730 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7731 | /* get key count */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7732 | if (ctx_scnode->flags & LYS_KEYLESS) { |
| 7733 | rc = LY_ENOT; |
| 7734 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7735 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7736 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7737 | /* learn where the predicates end */ |
| 7738 | e_idx = *tok_idx; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7739 | 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] | 7740 | /* '[' */ |
| 7741 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7742 | rc = LY_ENOT; |
| 7743 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7744 | } |
| 7745 | ++e_idx; |
| 7746 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7747 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7748 | /* not a key */ |
| 7749 | rc = LY_ENOT; |
| 7750 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7751 | } |
| 7752 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7753 | /* check key */ |
| 7754 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx], |
| 7755 | exp->tok_len[e_idx], ctx_scnode, set, key), cleanup); |
| 7756 | |
| 7757 | ++e_idx; |
| 7758 | |
| 7759 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7760 | /* not '=' */ |
| 7761 | rc = LY_ENOT; |
| 7762 | goto cleanup; |
| 7763 | } |
| 7764 | ++e_idx; |
| 7765 | |
| 7766 | /* value start */ |
| 7767 | val_start_idx = e_idx; |
| 7768 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7769 | /* ']' */ |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7770 | nested_pred = 1; |
| 7771 | do { |
| 7772 | ++e_idx; |
| 7773 | |
| 7774 | 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] | 7775 | /* higher priority than '=' */ |
| 7776 | rc = LY_ENOT; |
| 7777 | goto cleanup; |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7778 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7779 | /* nested predicate */ |
| 7780 | ++nested_pred; |
| 7781 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7782 | /* predicate end */ |
| 7783 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7784 | } |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7785 | } while (nested_pred); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7786 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7787 | /* try to evaluate the value */ |
| 7788 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, |
| 7789 | set, key, &pred, &pred_len), cleanup); |
| 7790 | |
| 7791 | ++e_idx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7792 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7793 | } else { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7794 | /* check for predicate "[.=...]" */ |
| 7795 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7796 | /* learn just where this single predicate ends */ |
| 7797 | e_idx = *tok_idx; |
| 7798 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7799 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7800 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7801 | rc = LY_ENOT; |
| 7802 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7803 | } |
| 7804 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7805 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7806 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7807 | /* not the node value */ |
| 7808 | rc = LY_ENOT; |
| 7809 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7810 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7811 | ++e_idx; |
| 7812 | |
| 7813 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7814 | /* not '=' */ |
| 7815 | rc = LY_ENOT; |
| 7816 | goto cleanup; |
| 7817 | } |
| 7818 | ++e_idx; |
| 7819 | |
| 7820 | /* value start */ |
| 7821 | val_start_idx = e_idx; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7822 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7823 | /* ']' */ |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7824 | nested_pred = 1; |
| 7825 | do { |
| 7826 | ++e_idx; |
| 7827 | |
| 7828 | 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] | 7829 | /* higher priority than '=' */ |
| 7830 | rc = LY_ENOT; |
| 7831 | goto cleanup; |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7832 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7833 | /* nested predicate */ |
| 7834 | ++nested_pred; |
| 7835 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7836 | /* predicate end */ |
| 7837 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7838 | } |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7839 | } while (nested_pred); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7840 | |
| 7841 | /* try to evaluate the value */ |
| 7842 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set, |
| 7843 | ctx_scnode, &pred, &pred_len), cleanup); |
| 7844 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7845 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7846 | } |
| 7847 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7848 | /* parse the predicate(s) */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7849 | LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL, |
| 7850 | LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7851 | |
| 7852 | /* compile */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7853 | rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2, |
| 7854 | &pred_idx, LY_VALUE_JSON, NULL, predicates, pred_type); |
| 7855 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7856 | |
| 7857 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7858 | *tok_idx = e_idx; |
| 7859 | |
| 7860 | cleanup: |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7861 | ly_temp_log_options(NULL); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7862 | lyxp_expr_free(set->ctx, exp2); |
| 7863 | free(pred); |
| 7864 | return rc; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7865 | } |
| 7866 | |
| 7867 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7868 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7869 | * data node(s) could be found using a single hash-based search. |
| 7870 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7871 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7872 | * @param[in] node Next context node to check. |
| 7873 | * @param[in] name Expected node name. |
| 7874 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7875 | * @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] | 7876 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7877 | * @param[in] format Prefix format. |
| 7878 | * @param[in,out] found Previously found node, is updated. |
| 7879 | * @return LY_SUCCESS on success, |
| 7880 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7881 | */ |
| 7882 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7883 | 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] | 7884 | 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] | 7885 | const struct lysc_node **found) |
| 7886 | { |
| 7887 | const struct lysc_node *scnode; |
| 7888 | const struct lys_module *mod; |
| 7889 | uint32_t idx = 0; |
| 7890 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7891 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7892 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7893 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7894 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7895 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7896 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7897 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7898 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7899 | if (!mod->implemented) { |
| 7900 | continue; |
| 7901 | } |
| 7902 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7903 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7904 | if (scnode) { |
| 7905 | /* we have found a match */ |
| 7906 | break; |
| 7907 | } |
| 7908 | } |
| 7909 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7910 | if (!scnode) { |
| 7911 | /* all modules searched */ |
| 7912 | idx = 0; |
| 7913 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7914 | } else { |
| 7915 | /* search in top-level */ |
| 7916 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7917 | } |
| 7918 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7919 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7920 | /* we must adjust the module to inherit the one from the context node */ |
| 7921 | moveto_mod = node->schema->module; |
| 7922 | } |
| 7923 | |
| 7924 | /* search in children, do not repeat the same search */ |
| 7925 | 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] | 7926 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7927 | |
| 7928 | /* additional context check */ |
| 7929 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7930 | scnode = NULL; |
| 7931 | } |
| 7932 | |
| 7933 | if (scnode) { |
| 7934 | if (*found) { |
| 7935 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7936 | * (more hash-based searches would be required, not supported) */ |
| 7937 | return LY_ENOT; |
| 7938 | } else { |
| 7939 | /* remember the found schema node and continue to make sure it can be used */ |
| 7940 | *found = scnode; |
| 7941 | } |
| 7942 | } |
| 7943 | |
| 7944 | if (idx) { |
| 7945 | /* continue searching all the following models */ |
| 7946 | goto continue_search; |
| 7947 | } |
| 7948 | |
| 7949 | return LY_SUCCESS; |
| 7950 | } |
| 7951 | |
| 7952 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7953 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7954 | * |
| 7955 | * @param[in] set XPath set. |
| 7956 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7957 | * @param[in] ncname XPath NCName being evaluated. |
| 7958 | * @param[in] ncname_len Length of @p ncname. |
| 7959 | * @param[in] expr Whole XPath expression. |
| 7960 | * @param[in] options XPath options. |
| 7961 | */ |
| 7962 | static void |
| 7963 | 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] | 7964 | uint32_t ncname_len, const char *expr, uint32_t options) |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7965 | { |
| 7966 | const char *format; |
| 7967 | char *path = NULL, *ppath = NULL; |
| 7968 | |
| 7969 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7970 | if (scparent) { |
| 7971 | /* generate path for the parent */ |
| 7972 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7973 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7974 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7975 | ppath = strdup("<root>"); |
| 7976 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7977 | ppath = strdup("<config-root>"); |
| 7978 | } |
| 7979 | } |
| 7980 | if (ppath) { |
| 7981 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7982 | if (options & LYXP_SCNODE_ERROR) { |
| 7983 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7984 | } else { |
| 7985 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7986 | } |
| 7987 | } else { |
| 7988 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7989 | if (options & LYXP_SCNODE_ERROR) { |
| 7990 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7991 | } else { |
| 7992 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7993 | } |
| 7994 | } |
| 7995 | free(path); |
| 7996 | free(ppath); |
| 7997 | } |
| 7998 | |
| 7999 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8000 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 8001 | * |
| 8002 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8003 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8004 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 8005 | * |
| 8006 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8007 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8008 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8009 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8010 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8011 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8012 | * @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] | 8013 | */ |
| 8014 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8015 | 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] | 8016 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8017 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8018 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8019 | const char *ncname, *ncname_dict = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8020 | uint32_t i, ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8021 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8022 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8023 | struct ly_path_predicate *predicates = NULL; |
| 8024 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8025 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8026 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8027 | 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] | 8028 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8029 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8030 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8031 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8032 | goto moveto; |
| 8033 | } |
| 8034 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8035 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 8036 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8037 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8038 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8039 | /* all nodes will match */ |
| 8040 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8041 | } |
| 8042 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8043 | /* parse (and skip) module name */ |
| 8044 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8045 | LY_CHECK_GOTO(rc, cleanup); |
| 8046 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8047 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8048 | /* all nodes from the module will match */ |
| 8049 | goto moveto; |
| 8050 | } |
| 8051 | |
| 8052 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 8053 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8054 | /* find the matching schema node in some parent in the context */ |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8055 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8056 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 8057 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8058 | /* check failed */ |
| 8059 | scnode = NULL; |
| 8060 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8061 | } |
| 8062 | } |
| 8063 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8064 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 8065 | /* try to create the predicates */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 8066 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates, &pred_type)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8067 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8068 | scnode = NULL; |
| 8069 | } |
| 8070 | } |
| 8071 | } |
| 8072 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8073 | if (!scnode) { |
| 8074 | /* 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] | 8075 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 8076 | 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] | 8077 | } |
| 8078 | |
| 8079 | moveto: |
| 8080 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8081 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8082 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8083 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8084 | } else { |
| 8085 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 8086 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8087 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8088 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8089 | } |
| 8090 | LY_CHECK_GOTO(rc, cleanup); |
| 8091 | } |
| 8092 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8093 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8094 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8095 | ly_bool found = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8096 | |
| 8097 | /* remember parent if there is only one, to print in the warning */ |
| 8098 | for (i = 0; i < set->used; ++i) { |
| 8099 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 8100 | if (!scparent) { |
| 8101 | /* remember the context node */ |
| 8102 | scparent = &set->val.scnodes[i]; |
| 8103 | } else { |
| 8104 | /* several context nodes, no reasonable error possible */ |
| 8105 | scparent = NULL; |
| 8106 | break; |
| 8107 | } |
| 8108 | } |
| 8109 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8110 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8111 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8112 | /* efficient evaluation that does not add all the descendants into the set */ |
| 8113 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8114 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8115 | if (all_desc) { |
| 8116 | /* "//" == "/descendant-or-self::node()/" */ |
| 8117 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8118 | LY_CHECK_GOTO(rc, cleanup); |
| 8119 | } |
| 8120 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8121 | } |
| 8122 | LY_CHECK_GOTO(rc, cleanup); |
| 8123 | |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8124 | i = set->used; |
| 8125 | do { |
| 8126 | --i; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8127 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8128 | found = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8129 | break; |
| 8130 | } |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8131 | } while (i); |
| 8132 | if (!found) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8133 | /* generate message */ |
| 8134 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 8135 | |
| 8136 | if (options & LYXP_SCNODE_ERROR) { |
| 8137 | /* error */ |
| 8138 | rc = LY_EVALID; |
| 8139 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8140 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8141 | |
| 8142 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 8143 | rc = LY_ENOT; |
| 8144 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8145 | } |
| 8146 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8147 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8148 | /* efficient evaluation */ |
| 8149 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 8150 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 8151 | /* we can find the child nodes using hashes */ |
| 8152 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8153 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8154 | if (all_desc) { |
| 8155 | /* "//" == "/descendant-or-self::node()/" */ |
| 8156 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8157 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8158 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8159 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8160 | } |
| 8161 | LY_CHECK_GOTO(rc, cleanup); |
| 8162 | } |
| 8163 | } |
| 8164 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8165 | if (scnode_skip_pred) { |
| 8166 | /* skip predicates */ |
| 8167 | options |= LYXP_SKIP_EXPR; |
| 8168 | } |
| 8169 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8170 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8171 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8172 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8173 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8174 | } |
| 8175 | |
| 8176 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8177 | if (scnode_skip_pred) { |
| 8178 | /* restore options */ |
| 8179 | options &= ~LYXP_SKIP_EXPR; |
| 8180 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8181 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8182 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 8183 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 8184 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8185 | return rc; |
| 8186 | } |
| 8187 | |
| 8188 | /** |
| 8189 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 8190 | * |
| 8191 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8192 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8193 | * [8] NodeType ::= 'text' | 'node' |
| 8194 | * |
| 8195 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8196 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8197 | * @param[in] axis Axis to search on. |
| 8198 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8199 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8200 | * @param[in] options XPath options. |
| 8201 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8202 | */ |
| 8203 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8204 | 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] | 8205 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8206 | { |
| 8207 | LY_ERR rc; |
| 8208 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8209 | (void)all_desc; |
| 8210 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8211 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8212 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8213 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 8214 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8215 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8216 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 8217 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8218 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8219 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8220 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8221 | 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] | 8222 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8223 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8224 | |
| 8225 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8226 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8227 | 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] | 8228 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8229 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8230 | |
| 8231 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8232 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8233 | 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] | 8234 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8235 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8236 | |
| 8237 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8238 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8239 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8240 | LY_CHECK_RET(rc); |
| 8241 | } |
| 8242 | |
| 8243 | return LY_SUCCESS; |
| 8244 | } |
| 8245 | |
| 8246 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8247 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8248 | * |
| 8249 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8250 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8251 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8252 | * |
| 8253 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8254 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8255 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8256 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8257 | * @param[in] options XPath options. |
| 8258 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8259 | */ |
| 8260 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8261 | 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] | 8262 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8264 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8265 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8266 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8267 | |
| 8268 | goto step; |
| 8269 | do { |
| 8270 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8271 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8272 | all_desc = 0; |
| 8273 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8274 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8275 | all_desc = 1; |
| 8276 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8277 | 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] | 8278 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8279 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8280 | |
| 8281 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8282 | /* AxisSpecifier */ |
| 8283 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8284 | 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] | 8285 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8286 | 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] | 8287 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8288 | ++(*tok_idx); |
| 8289 | |
| 8290 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8291 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8292 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8293 | ++(*tok_idx); |
| 8294 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8295 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8296 | |
| 8297 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8298 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8299 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8300 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8301 | /* default */ |
| 8302 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8303 | } |
| 8304 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8305 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8306 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8307 | case LYXP_TOKEN_DOT: |
| 8308 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8309 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8310 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8311 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8312 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8313 | rc = LY_EVALID; |
| 8314 | goto cleanup; |
| 8315 | } |
| 8316 | |
| 8317 | if (all_desc) { |
| 8318 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8319 | LY_CHECK_GOTO(rc, cleanup); |
| 8320 | } |
| 8321 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8322 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8323 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8324 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8325 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8326 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8327 | break; |
| 8328 | |
| 8329 | case LYXP_TOKEN_DDOT: |
| 8330 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8331 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8332 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8333 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8334 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8335 | rc = LY_EVALID; |
| 8336 | goto cleanup; |
| 8337 | } |
| 8338 | |
| 8339 | if (all_desc) { |
| 8340 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8341 | LY_CHECK_GOTO(rc, cleanup); |
| 8342 | } |
| 8343 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8344 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8345 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8346 | 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] | 8347 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8348 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8349 | break; |
| 8350 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8352 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8353 | 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] | 8354 | if (rc == LY_ENOT) { |
| 8355 | assert(options & LYXP_SCNODE_ALL); |
| 8356 | /* skip the rest of this path */ |
| 8357 | rc = LY_SUCCESS; |
| 8358 | scnode_skip_path = 1; |
| 8359 | options |= LYXP_SKIP_EXPR; |
| 8360 | } |
| 8361 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8362 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8363 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8364 | case LYXP_TOKEN_NODETYPE: |
| 8365 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8366 | 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] | 8367 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8368 | break; |
| 8369 | |
| 8370 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8371 | LOGINT(set->ctx); |
| 8372 | rc = LY_EINT; |
| 8373 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8374 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8375 | } 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] | 8376 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8377 | cleanup: |
| 8378 | if (scnode_skip_path) { |
| 8379 | options &= ~LYXP_SKIP_EXPR; |
| 8380 | } |
| 8381 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8382 | } |
| 8383 | |
| 8384 | /** |
| 8385 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8386 | * |
| 8387 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8388 | * |
| 8389 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8390 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8391 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8392 | * @param[in] options XPath options. |
| 8393 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8394 | */ |
| 8395 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8396 | 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] | 8397 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8398 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8399 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8400 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8401 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8402 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8403 | } |
| 8404 | |
| 8405 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8406 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8407 | /* evaluate '/' - deferred */ |
| 8408 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8409 | 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] | 8410 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8411 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8412 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8413 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8414 | return LY_SUCCESS; |
| 8415 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8416 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8417 | case LYXP_TOKEN_DOT: |
| 8418 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8419 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8420 | case LYXP_TOKEN_AT: |
| 8421 | case LYXP_TOKEN_NAMETEST: |
| 8422 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8423 | 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] | 8424 | break; |
| 8425 | default: |
| 8426 | break; |
| 8427 | } |
| 8428 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8429 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8430 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8431 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8432 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8433 | 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] | 8434 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8435 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8436 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8437 | 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] | 8438 | } |
| 8439 | |
| 8440 | return LY_SUCCESS; |
| 8441 | } |
| 8442 | |
| 8443 | /** |
| 8444 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8445 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8446 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8447 | * |
| 8448 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8449 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8450 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8451 | * @param[in] options XPath options. |
| 8452 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8453 | */ |
| 8454 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8455 | 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] | 8456 | { |
| 8457 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8458 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8459 | LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL; |
| 8460 | uint32_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8461 | struct lyxp_set **args = NULL, **args_aux; |
| 8462 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8463 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8464 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8465 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8466 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8467 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8468 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8469 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8470 | xpath_func = &xpath_sum; |
| 8471 | } |
| 8472 | break; |
| 8473 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8474 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8475 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8476 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8477 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8478 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8479 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8480 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8481 | xpath_func = &xpath_true; |
| 8482 | } |
| 8483 | break; |
| 8484 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8485 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8486 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8487 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8488 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8489 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8490 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8491 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8492 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8493 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8494 | xpath_func = &xpath_deref; |
| 8495 | } |
| 8496 | break; |
| 8497 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8498 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8499 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8500 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8501 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8502 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8503 | xpath_func = &xpath_string; |
| 8504 | } |
| 8505 | break; |
| 8506 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8507 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8508 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8509 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8510 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8511 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8512 | xpath_func = &xpath_current; |
| 8513 | } |
| 8514 | break; |
| 8515 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8516 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8517 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8518 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8519 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8520 | } 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] | 8521 | xpath_func = &xpath_re_match; |
| 8522 | } |
| 8523 | break; |
| 8524 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8525 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8526 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8527 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8528 | xpath_func = &xpath_translate; |
| 8529 | } |
| 8530 | break; |
| 8531 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8532 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8534 | } 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] | 8535 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8536 | } 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] | 8537 | xpath_func = &xpath_bit_is_set; |
| 8538 | } |
| 8539 | break; |
| 8540 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8541 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8542 | xpath_func = &xpath_starts_with; |
| 8543 | } |
| 8544 | break; |
| 8545 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8546 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8547 | xpath_func = &xpath_derived_from; |
| 8548 | } |
| 8549 | break; |
| 8550 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8551 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8552 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8553 | } 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] | 8554 | xpath_func = &xpath_string_length; |
| 8555 | } |
| 8556 | break; |
| 8557 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8558 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8559 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8560 | } 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] | 8561 | xpath_func = &xpath_substring_after; |
| 8562 | } |
| 8563 | break; |
| 8564 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8565 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8566 | xpath_func = &xpath_substring_before; |
| 8567 | } |
| 8568 | break; |
| 8569 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8570 | 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] | 8571 | xpath_func = &xpath_derived_from_or_self; |
| 8572 | } |
| 8573 | break; |
| 8574 | } |
| 8575 | |
| 8576 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8577 | 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] | 8578 | return LY_EVALID; |
| 8579 | } |
| 8580 | } |
| 8581 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8582 | 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] | 8583 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8584 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8585 | |
| 8586 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8587 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8588 | 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] | 8589 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8590 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8591 | |
| 8592 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8593 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8594 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8595 | args = malloc(sizeof *args); |
| 8596 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8597 | arg_count = 1; |
| 8598 | args[0] = set_copy(set); |
| 8599 | if (!args[0]) { |
| 8600 | rc = LY_EMEM; |
| 8601 | goto cleanup; |
| 8602 | } |
| 8603 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8604 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8605 | LY_CHECK_GOTO(rc, cleanup); |
| 8606 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8607 | 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] | 8608 | LY_CHECK_GOTO(rc, cleanup); |
| 8609 | } |
| 8610 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8611 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8612 | 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] | 8613 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8614 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8615 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8616 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8617 | ++arg_count; |
| 8618 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8619 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8620 | args = args_aux; |
| 8621 | args[arg_count - 1] = set_copy(set); |
| 8622 | if (!args[arg_count - 1]) { |
| 8623 | rc = LY_EMEM; |
| 8624 | goto cleanup; |
| 8625 | } |
| 8626 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8627 | 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] | 8628 | LY_CHECK_GOTO(rc, cleanup); |
| 8629 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8630 | 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] | 8631 | LY_CHECK_GOTO(rc, cleanup); |
| 8632 | } |
| 8633 | } |
| 8634 | |
| 8635 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8636 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8637 | 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] | 8638 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8639 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8640 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8641 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8642 | /* evaluate function */ |
| 8643 | rc = xpath_func(args, arg_count, set, options); |
| 8644 | |
| 8645 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8646 | /* merge all nodes from arg evaluations */ |
| 8647 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8648 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8649 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8650 | } |
| 8651 | } |
| 8652 | } else { |
| 8653 | rc = LY_SUCCESS; |
| 8654 | } |
| 8655 | |
| 8656 | cleanup: |
| 8657 | for (i = 0; i < arg_count; ++i) { |
| 8658 | lyxp_set_free(args[i]); |
| 8659 | } |
| 8660 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8661 | return rc; |
| 8662 | } |
| 8663 | |
| 8664 | /** |
| 8665 | * @brief Evaluate Number. Logs directly on error. |
| 8666 | * |
| 8667 | * @param[in] ctx Context for errors. |
| 8668 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8669 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8670 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8671 | * @return LY_ERR |
| 8672 | */ |
| 8673 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8674 | 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] | 8675 | { |
| 8676 | long double num; |
| 8677 | char *endptr; |
| 8678 | |
| 8679 | if (set) { |
| 8680 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8681 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8682 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8683 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8684 | 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] | 8685 | 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] | 8686 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8687 | } 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] | 8688 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8689 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8690 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8691 | return LY_EVALID; |
| 8692 | } |
| 8693 | |
| 8694 | set_fill_number(set, num); |
| 8695 | } |
| 8696 | |
| 8697 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8698 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8699 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8700 | return LY_SUCCESS; |
| 8701 | } |
| 8702 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8703 | LY_ERR |
| 8704 | lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var) |
| 8705 | { |
| 8706 | LY_ERR ret = LY_ENOTFOUND; |
| 8707 | LY_ARRAY_COUNT_TYPE u; |
| 8708 | |
| 8709 | assert(vars && name); |
| 8710 | |
| 8711 | name_len = name_len ? name_len : strlen(name); |
| 8712 | |
| 8713 | LY_ARRAY_FOR(vars, u) { |
| 8714 | if (!strncmp(vars[u].name, name, name_len)) { |
| 8715 | ret = LY_SUCCESS; |
| 8716 | break; |
| 8717 | } |
| 8718 | } |
| 8719 | |
| 8720 | if (var && !ret) { |
| 8721 | *var = &vars[u]; |
| 8722 | } |
| 8723 | |
| 8724 | return ret; |
| 8725 | } |
| 8726 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8727 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8728 | * @brief Evaluate VariableReference. |
| 8729 | * |
| 8730 | * @param[in] exp Parsed XPath expression. |
| 8731 | * @param[in] tok_idx Position in the expression @p exp. |
| 8732 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8733 | * @param[in,out] set Context and result set. |
| 8734 | * @param[in] options XPath options. |
| 8735 | * @return LY_ERR value. |
| 8736 | */ |
| 8737 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8738 | 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] | 8739 | { |
| 8740 | LY_ERR ret; |
| 8741 | const char *name; |
| 8742 | struct lyxp_var *var; |
| 8743 | const struct lyxp_var *vars; |
| 8744 | struct lyxp_expr *tokens = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8745 | uint32_t token_index, name_len; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8746 | |
| 8747 | vars = set->vars; |
| 8748 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8749 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8750 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
Michal Vasko | e68b540 | 2022-07-29 14:58:15 +0200 | [diff] [blame] | 8751 | name_len = exp->tok_len[*tok_idx]; |
| 8752 | ret = lyxp_vars_find((struct lyxp_var *)vars, name, name_len, &var); |
| 8753 | LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret, "XPath variable \"%.*s\" not defined.", (int)name_len, name), ret); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8754 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8755 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8756 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8757 | LY_CHECK_GOTO(ret, cleanup); |
| 8758 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8759 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8760 | token_index = 0; |
| 8761 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8762 | LY_CHECK_GOTO(ret, cleanup); |
| 8763 | |
| 8764 | cleanup: |
| 8765 | lyxp_expr_free(set->ctx, tokens); |
| 8766 | |
| 8767 | return ret; |
| 8768 | } |
| 8769 | |
| 8770 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8771 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8772 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8773 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8774 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8775 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8776 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8777 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8778 | * |
| 8779 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8780 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8781 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8782 | * @param[in] options XPath options. |
| 8783 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8784 | */ |
| 8785 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8786 | 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] | 8787 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8788 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8789 | LY_ERR rc; |
| 8790 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8791 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8792 | case LYXP_TOKEN_PAR1: |
| 8793 | /* '(' Expr ')' */ |
| 8794 | |
| 8795 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8796 | 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] | 8797 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8798 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8799 | |
| 8800 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8801 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8802 | LY_CHECK_RET(rc); |
| 8803 | |
| 8804 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8805 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8806 | 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] | 8807 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8808 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8809 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8810 | goto predicate; |
| 8811 | |
| 8812 | case LYXP_TOKEN_DOT: |
| 8813 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8814 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8815 | case LYXP_TOKEN_AT: |
| 8816 | case LYXP_TOKEN_NAMETEST: |
| 8817 | case LYXP_TOKEN_NODETYPE: |
| 8818 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8819 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8820 | LY_CHECK_RET(rc); |
| 8821 | break; |
| 8822 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8823 | case LYXP_TOKEN_VARREF: |
| 8824 | /* VariableReference */ |
| 8825 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8826 | LY_CHECK_RET(rc); |
| 8827 | ++(*tok_idx); |
| 8828 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8829 | goto predicate; |
| 8830 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8831 | case LYXP_TOKEN_FUNCNAME: |
| 8832 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8833 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8834 | LY_CHECK_RET(rc); |
| 8835 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8836 | goto predicate; |
| 8837 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8838 | case LYXP_TOKEN_OPER_PATH: |
| 8839 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8840 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8841 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8842 | LY_CHECK_RET(rc); |
| 8843 | break; |
| 8844 | |
| 8845 | case LYXP_TOKEN_LITERAL: |
| 8846 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8847 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8848 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8849 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8850 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8851 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8852 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8853 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8854 | } |
| 8855 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8856 | goto predicate; |
| 8857 | |
| 8858 | case LYXP_TOKEN_NUMBER: |
| 8859 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8860 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8861 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8862 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8863 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8864 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8865 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8866 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8867 | } |
| 8868 | LY_CHECK_RET(rc); |
| 8869 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8870 | goto predicate; |
| 8871 | |
| 8872 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8873 | 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] | 8874 | return LY_EVALID; |
| 8875 | } |
| 8876 | |
| 8877 | return LY_SUCCESS; |
| 8878 | |
| 8879 | predicate: |
| 8880 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8881 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8882 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8883 | LY_CHECK_RET(rc); |
| 8884 | } |
| 8885 | |
| 8886 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8887 | 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] | 8888 | |
| 8889 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8890 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8891 | all_desc = 0; |
| 8892 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8893 | all_desc = 1; |
| 8894 | } |
| 8895 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8896 | 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] | 8897 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8898 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8899 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8900 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8901 | LY_CHECK_RET(rc); |
| 8902 | } |
| 8903 | |
| 8904 | return LY_SUCCESS; |
| 8905 | } |
| 8906 | |
| 8907 | /** |
| 8908 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8909 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8910 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8911 | * |
| 8912 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8913 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8914 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8915 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8916 | * @param[in] options XPath options. |
| 8917 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8918 | */ |
| 8919 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8920 | 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] | 8921 | { |
| 8922 | LY_ERR rc = LY_SUCCESS; |
| 8923 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8924 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8925 | |
| 8926 | assert(repeat); |
| 8927 | |
| 8928 | set_init(&orig_set, set); |
| 8929 | set_init(&set2, set); |
| 8930 | |
| 8931 | set_fill_set(&orig_set, set); |
| 8932 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8933 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8934 | LY_CHECK_GOTO(rc, cleanup); |
| 8935 | |
| 8936 | /* ('|' PathExpr)* */ |
| 8937 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8938 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8939 | 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] | 8940 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8941 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8942 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8943 | if (options & LYXP_SKIP_EXPR) { |
| 8944 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8945 | LY_CHECK_GOTO(rc, cleanup); |
| 8946 | continue; |
| 8947 | } |
| 8948 | |
| 8949 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8950 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8951 | LY_CHECK_GOTO(rc, cleanup); |
| 8952 | |
| 8953 | /* eval */ |
| 8954 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8955 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8956 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8957 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8958 | LY_CHECK_GOTO(rc, cleanup); |
| 8959 | } |
| 8960 | } |
| 8961 | |
| 8962 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8963 | lyxp_set_free_content(&orig_set); |
| 8964 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8965 | return rc; |
| 8966 | } |
| 8967 | |
| 8968 | /** |
| 8969 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8970 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8971 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8972 | * |
| 8973 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8974 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8975 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8976 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8977 | * @param[in] options XPath options. |
| 8978 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8979 | */ |
| 8980 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8981 | 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] | 8982 | { |
| 8983 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8984 | uint32_t this_op, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8985 | |
| 8986 | assert(repeat); |
| 8987 | |
| 8988 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8989 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8990 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8991 | 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] | 8992 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8993 | 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] | 8994 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8995 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8996 | } |
| 8997 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8998 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8999 | LY_CHECK_RET(rc); |
| 9000 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9001 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9002 | if (options & LYXP_SCNODE_ALL) { |
| 9003 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 9004 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9005 | 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] | 9006 | LY_CHECK_RET(rc); |
| 9007 | } |
| 9008 | } |
| 9009 | |
| 9010 | return LY_SUCCESS; |
| 9011 | } |
| 9012 | |
| 9013 | /** |
| 9014 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 9015 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9016 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9017 | * | MultiplicativeExpr '*' UnaryExpr |
| 9018 | * | MultiplicativeExpr 'div' UnaryExpr |
| 9019 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 9020 | * |
| 9021 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9022 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9023 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9024 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9025 | * @param[in] options XPath options. |
| 9026 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9027 | */ |
| 9028 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9029 | 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] | 9030 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9031 | { |
| 9032 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9033 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9034 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9035 | |
| 9036 | assert(repeat); |
| 9037 | |
| 9038 | set_init(&orig_set, set); |
| 9039 | set_init(&set2, set); |
| 9040 | |
| 9041 | set_fill_set(&orig_set, set); |
| 9042 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9043 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9044 | LY_CHECK_GOTO(rc, cleanup); |
| 9045 | |
| 9046 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 9047 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9048 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9049 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9050 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9051 | 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] | 9052 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9053 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9054 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9055 | if (options & LYXP_SKIP_EXPR) { |
| 9056 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9057 | LY_CHECK_GOTO(rc, cleanup); |
| 9058 | continue; |
| 9059 | } |
| 9060 | |
| 9061 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9062 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9063 | LY_CHECK_GOTO(rc, cleanup); |
| 9064 | |
| 9065 | /* eval */ |
| 9066 | if (options & LYXP_SCNODE_ALL) { |
| 9067 | 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] | 9068 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9069 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9070 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9071 | 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] | 9072 | LY_CHECK_GOTO(rc, cleanup); |
| 9073 | } |
| 9074 | } |
| 9075 | |
| 9076 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9077 | lyxp_set_free_content(&orig_set); |
| 9078 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9079 | return rc; |
| 9080 | } |
| 9081 | |
| 9082 | /** |
| 9083 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 9084 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9085 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9086 | * | AdditiveExpr '+' MultiplicativeExpr |
| 9087 | * | AdditiveExpr '-' MultiplicativeExpr |
| 9088 | * |
| 9089 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9090 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9091 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9092 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9093 | * @param[in] options XPath options. |
| 9094 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9095 | */ |
| 9096 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9097 | 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] | 9098 | { |
| 9099 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9100 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9101 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9102 | |
| 9103 | assert(repeat); |
| 9104 | |
| 9105 | set_init(&orig_set, set); |
| 9106 | set_init(&set2, set); |
| 9107 | |
| 9108 | set_fill_set(&orig_set, set); |
| 9109 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9110 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9111 | LY_CHECK_GOTO(rc, cleanup); |
| 9112 | |
| 9113 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 9114 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9115 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9116 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9117 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9118 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9119 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9120 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9121 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9122 | if (options & LYXP_SKIP_EXPR) { |
| 9123 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9124 | LY_CHECK_GOTO(rc, cleanup); |
| 9125 | continue; |
| 9126 | } |
| 9127 | |
| 9128 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9129 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9130 | LY_CHECK_GOTO(rc, cleanup); |
| 9131 | |
| 9132 | /* eval */ |
| 9133 | if (options & LYXP_SCNODE_ALL) { |
| 9134 | 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] | 9135 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9136 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9137 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9138 | 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] | 9139 | LY_CHECK_GOTO(rc, cleanup); |
| 9140 | } |
| 9141 | } |
| 9142 | |
| 9143 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9144 | lyxp_set_free_content(&orig_set); |
| 9145 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9146 | return rc; |
| 9147 | } |
| 9148 | |
| 9149 | /** |
| 9150 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 9151 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9152 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9153 | * | RelationalExpr '<' AdditiveExpr |
| 9154 | * | RelationalExpr '>' AdditiveExpr |
| 9155 | * | RelationalExpr '<=' AdditiveExpr |
| 9156 | * | RelationalExpr '>=' AdditiveExpr |
| 9157 | * |
| 9158 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9159 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9160 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9161 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9162 | * @param[in] options XPath options. |
| 9163 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9164 | */ |
| 9165 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9166 | 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] | 9167 | { |
| 9168 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9169 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9170 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9171 | |
| 9172 | assert(repeat); |
| 9173 | |
| 9174 | set_init(&orig_set, set); |
| 9175 | set_init(&set2, set); |
| 9176 | |
| 9177 | set_fill_set(&orig_set, set); |
| 9178 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9179 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9180 | LY_CHECK_GOTO(rc, cleanup); |
| 9181 | |
| 9182 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 9183 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9184 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9185 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9186 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9187 | 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] | 9188 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9189 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9190 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9191 | if (options & LYXP_SKIP_EXPR) { |
| 9192 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9193 | LY_CHECK_GOTO(rc, cleanup); |
| 9194 | continue; |
| 9195 | } |
| 9196 | |
| 9197 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9198 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9199 | LY_CHECK_GOTO(rc, cleanup); |
| 9200 | |
| 9201 | /* eval */ |
| 9202 | if (options & LYXP_SCNODE_ALL) { |
| 9203 | 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] | 9204 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9205 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9206 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9207 | ly_bool result; |
| 9208 | |
| 9209 | 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] | 9210 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9211 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9212 | } |
| 9213 | } |
| 9214 | |
| 9215 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9216 | lyxp_set_free_content(&orig_set); |
| 9217 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9218 | return rc; |
| 9219 | } |
| 9220 | |
| 9221 | /** |
| 9222 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 9223 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9224 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9225 | * | EqualityExpr '!=' RelationalExpr |
| 9226 | * |
| 9227 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9228 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9229 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9230 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9231 | * @param[in] options XPath options. |
| 9232 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9233 | */ |
| 9234 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9235 | 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] | 9236 | { |
| 9237 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9238 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9239 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9240 | |
| 9241 | assert(repeat); |
| 9242 | |
| 9243 | set_init(&orig_set, set); |
| 9244 | set_init(&set2, set); |
| 9245 | |
| 9246 | set_fill_set(&orig_set, set); |
| 9247 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9248 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9249 | LY_CHECK_GOTO(rc, cleanup); |
| 9250 | |
| 9251 | /* ('=' / '!=' RelationalExpr)* */ |
| 9252 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9253 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9254 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9255 | 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] | 9256 | 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] | 9257 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9258 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9259 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9260 | if (options & LYXP_SKIP_EXPR) { |
| 9261 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9262 | LY_CHECK_GOTO(rc, cleanup); |
| 9263 | continue; |
| 9264 | } |
| 9265 | |
| 9266 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9267 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9268 | LY_CHECK_GOTO(rc, cleanup); |
| 9269 | |
| 9270 | /* eval */ |
| 9271 | if (options & LYXP_SCNODE_ALL) { |
| 9272 | 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] | 9273 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9274 | 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] | 9275 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9276 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9277 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9278 | ly_bool result; |
| 9279 | |
| 9280 | 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] | 9281 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9282 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9283 | } |
| 9284 | } |
| 9285 | |
| 9286 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9287 | lyxp_set_free_content(&orig_set); |
| 9288 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9289 | return rc; |
| 9290 | } |
| 9291 | |
| 9292 | /** |
| 9293 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9294 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9295 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9296 | * |
| 9297 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9298 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9299 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9300 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9301 | * @param[in] options XPath options. |
| 9302 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9303 | */ |
| 9304 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9305 | 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] | 9306 | { |
| 9307 | LY_ERR rc; |
| 9308 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9309 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9310 | |
| 9311 | assert(repeat); |
| 9312 | |
| 9313 | set_init(&orig_set, set); |
| 9314 | set_init(&set2, set); |
| 9315 | |
| 9316 | set_fill_set(&orig_set, set); |
| 9317 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9318 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9319 | LY_CHECK_GOTO(rc, cleanup); |
| 9320 | |
| 9321 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9322 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9323 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9324 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9325 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9326 | } |
| 9327 | |
| 9328 | /* ('and' EqualityExpr)* */ |
| 9329 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9330 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9331 | 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] | 9332 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9333 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9334 | |
| 9335 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9336 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9337 | 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] | 9338 | LY_CHECK_GOTO(rc, cleanup); |
| 9339 | continue; |
| 9340 | } |
| 9341 | |
| 9342 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9343 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9344 | LY_CHECK_GOTO(rc, cleanup); |
| 9345 | |
| 9346 | /* eval - just get boolean value actually */ |
| 9347 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9348 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9349 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9350 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9351 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9352 | set_fill_set(set, &set2); |
| 9353 | } |
| 9354 | } |
| 9355 | |
| 9356 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9357 | lyxp_set_free_content(&orig_set); |
| 9358 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9359 | return rc; |
| 9360 | } |
| 9361 | |
| 9362 | /** |
| 9363 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9364 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9365 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9366 | * |
| 9367 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9368 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9369 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9370 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9371 | * @param[in] options XPath options. |
| 9372 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9373 | */ |
| 9374 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9375 | 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] | 9376 | { |
| 9377 | LY_ERR rc; |
| 9378 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9379 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9380 | |
| 9381 | assert(repeat); |
| 9382 | |
| 9383 | set_init(&orig_set, set); |
| 9384 | set_init(&set2, set); |
| 9385 | |
| 9386 | set_fill_set(&orig_set, set); |
| 9387 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9388 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9389 | LY_CHECK_GOTO(rc, cleanup); |
| 9390 | |
| 9391 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9392 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9393 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9394 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9395 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9396 | } |
| 9397 | |
| 9398 | /* ('or' AndExpr)* */ |
| 9399 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9400 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9401 | 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] | 9402 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9403 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9404 | |
| 9405 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9406 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9407 | 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] | 9408 | LY_CHECK_GOTO(rc, cleanup); |
| 9409 | continue; |
| 9410 | } |
| 9411 | |
| 9412 | set_fill_set(&set2, &orig_set); |
| 9413 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9414 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9415 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9416 | LY_CHECK_GOTO(rc, cleanup); |
| 9417 | |
| 9418 | /* eval - just get boolean value actually */ |
| 9419 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9420 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9421 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9422 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9423 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9424 | set_fill_set(set, &set2); |
| 9425 | } |
| 9426 | } |
| 9427 | |
| 9428 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9429 | lyxp_set_free_content(&orig_set); |
| 9430 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9431 | return rc; |
| 9432 | } |
| 9433 | |
| 9434 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9435 | * @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] | 9436 | * |
| 9437 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9438 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9439 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9440 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9441 | * @param[in] options XPath options. |
| 9442 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9443 | */ |
| 9444 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9445 | 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] | 9446 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9447 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9448 | uint32_t i, count; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9449 | enum lyxp_expr_type next_etype; |
| 9450 | LY_ERR rc; |
| 9451 | |
| 9452 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9453 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9454 | next_etype = LYXP_EXPR_NONE; |
| 9455 | } else { |
| 9456 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9457 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9458 | |
| 9459 | /* select one-priority lower because etype expression called us */ |
| 9460 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9461 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9462 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9463 | 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] | 9464 | } else { |
| 9465 | next_etype = LYXP_EXPR_NONE; |
| 9466 | } |
| 9467 | } |
| 9468 | |
| 9469 | /* decide what expression are we parsing based on the repeat */ |
| 9470 | switch (next_etype) { |
| 9471 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9472 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9473 | break; |
| 9474 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9475 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9476 | break; |
| 9477 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9478 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9479 | break; |
| 9480 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9481 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9482 | break; |
| 9483 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9484 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9485 | break; |
| 9486 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9487 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9488 | break; |
| 9489 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9490 | rc = eval_unary_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_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9493 | rc = eval_union_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_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9496 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9497 | break; |
| 9498 | default: |
| 9499 | LOGINT_RET(set->ctx); |
| 9500 | } |
| 9501 | |
| 9502 | return rc; |
| 9503 | } |
| 9504 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9505 | /** |
| 9506 | * @brief Get root type. |
| 9507 | * |
| 9508 | * @param[in] ctx_node Context node. |
| 9509 | * @param[in] ctx_scnode Schema context node. |
| 9510 | * @param[in] options XPath options. |
| 9511 | * @return Root type. |
| 9512 | */ |
| 9513 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9514 | 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] | 9515 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9516 | const struct lysc_node *op; |
| 9517 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9518 | /* explicit */ |
| 9519 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9520 | return LYXP_NODE_ROOT; |
| 9521 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9522 | return LYXP_NODE_ROOT_CONFIG; |
| 9523 | } |
| 9524 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9525 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9526 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9527 | 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] | 9528 | |
| 9529 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9530 | /* general root that can access everything */ |
| 9531 | return LYXP_NODE_ROOT; |
| 9532 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9533 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9534 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9535 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9536 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9537 | } |
| 9538 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9539 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9540 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9541 | 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] | 9542 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9543 | if (op || !(options & LYXP_SCHEMA)) { |
| 9544 | /* general root that can access everything */ |
| 9545 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9546 | } 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] | 9547 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9548 | return LYXP_NODE_ROOT_CONFIG; |
| 9549 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9550 | return LYXP_NODE_ROOT; |
| 9551 | } |
| 9552 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9553 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9554 | 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] | 9555 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node, |
| 9556 | 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] | 9557 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9558 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9559 | LY_ERR rc; |
| 9560 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9561 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9562 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9563 | 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] | 9564 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9565 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9566 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9567 | if (tree) { |
| 9568 | /* adjust the pointer to be the first top-level sibling */ |
| 9569 | while (tree->parent) { |
| 9570 | tree = lyd_parent(tree); |
| 9571 | } |
| 9572 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9573 | |
Michal Vasko | 6f78a77 | 2022-11-10 08:13:52 +0100 | [diff] [blame] | 9574 | if (lysc_data_parent(tree->schema)) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9575 | /* unable to evaluate absolute paths */ |
| 9576 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9577 | LYD_NAME(tree)); |
| 9578 | return LY_EINVAL; |
| 9579 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9580 | } |
| 9581 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9582 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9583 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9584 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9585 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9586 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9587 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9588 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9589 | set->cur_node = cur_node; |
| 9590 | for (set->context_op = cur_node ? cur_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9591 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9592 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9593 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9594 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9595 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9596 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9597 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9598 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9599 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9600 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9601 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9602 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9603 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9604 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9605 | } |
| 9606 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9607 | if (set->cur_node) { |
| 9608 | LOG_LOCBACK(0, 1, 0, 0); |
| 9609 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9610 | return rc; |
| 9611 | } |
| 9612 | |
| 9613 | #if 0 |
| 9614 | |
| 9615 | /* full xml printing of set elements, not used currently */ |
| 9616 | |
| 9617 | void |
| 9618 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9619 | { |
| 9620 | uint32_t i; |
| 9621 | char *str_num; |
| 9622 | struct lyout out; |
| 9623 | |
| 9624 | memset(&out, 0, sizeof out); |
| 9625 | |
| 9626 | out.type = LYOUT_STREAM; |
| 9627 | out.method.f = f; |
| 9628 | |
| 9629 | switch (set->type) { |
| 9630 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9631 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9632 | break; |
| 9633 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9634 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9635 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9636 | break; |
| 9637 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9638 | ly_print_(&out, "String XPath set:\n"); |
| 9639 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9640 | break; |
| 9641 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9642 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9643 | |
| 9644 | if (isnan(set->value.num)) { |
| 9645 | str_num = strdup("NaN"); |
| 9646 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9647 | str_num = strdup("0"); |
| 9648 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9649 | str_num = strdup("Infinity"); |
| 9650 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9651 | str_num = strdup("-Infinity"); |
| 9652 | } else if ((long long)set->value.num == set->value.num) { |
| 9653 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9654 | str_num = NULL; |
| 9655 | } |
| 9656 | } else { |
| 9657 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9658 | str_num = NULL; |
| 9659 | } |
| 9660 | } |
| 9661 | if (!str_num) { |
| 9662 | LOGMEM; |
| 9663 | return; |
| 9664 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9665 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9666 | free(str_num); |
| 9667 | break; |
| 9668 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9669 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9670 | |
| 9671 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9672 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9673 | switch (set->node_type[i]) { |
| 9674 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9675 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9676 | break; |
| 9677 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9678 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9679 | break; |
| 9680 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9681 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9682 | break; |
| 9683 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9684 | 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] | 9685 | break; |
| 9686 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9687 | 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] | 9688 | break; |
| 9689 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9690 | 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] | 9691 | break; |
| 9692 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9693 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9694 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9695 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9696 | break; |
| 9697 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9698 | 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] | 9699 | break; |
| 9700 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9701 | 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] | 9702 | break; |
| 9703 | } |
| 9704 | } |
| 9705 | break; |
| 9706 | } |
| 9707 | } |
| 9708 | |
| 9709 | #endif |
| 9710 | |
| 9711 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9712 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9713 | { |
| 9714 | long double num; |
| 9715 | char *str; |
| 9716 | LY_ERR rc; |
| 9717 | |
| 9718 | if (!set || (set->type == target)) { |
| 9719 | return LY_SUCCESS; |
| 9720 | } |
| 9721 | |
| 9722 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9723 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9724 | |
| 9725 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9726 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9727 | return LY_EINVAL; |
| 9728 | } |
| 9729 | |
| 9730 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9731 | 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] | 9732 | switch (set->type) { |
| 9733 | case LYXP_SET_NUMBER: |
| 9734 | if (isnan(set->val.num)) { |
| 9735 | set->val.str = strdup("NaN"); |
| 9736 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9737 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9738 | set->val.str = strdup("0"); |
| 9739 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9740 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9741 | set->val.str = strdup("Infinity"); |
| 9742 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9743 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9744 | set->val.str = strdup("-Infinity"); |
| 9745 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9746 | } else if ((long long)set->val.num == set->val.num) { |
| 9747 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9748 | LOGMEM_RET(set->ctx); |
| 9749 | } |
| 9750 | set->val.str = str; |
| 9751 | } else { |
| 9752 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9753 | LOGMEM_RET(set->ctx); |
| 9754 | } |
| 9755 | set->val.str = str; |
| 9756 | } |
| 9757 | break; |
| 9758 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9759 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9760 | set->val.str = strdup("true"); |
| 9761 | } else { |
| 9762 | set->val.str = strdup("false"); |
| 9763 | } |
| 9764 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9765 | break; |
| 9766 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9767 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9768 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9769 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9770 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9771 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9772 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9773 | set->val.str = str; |
| 9774 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9775 | default: |
| 9776 | LOGINT_RET(set->ctx); |
| 9777 | } |
| 9778 | set->type = LYXP_SET_STRING; |
| 9779 | } |
| 9780 | |
| 9781 | /* to NUMBER */ |
| 9782 | if (target == LYXP_SET_NUMBER) { |
| 9783 | switch (set->type) { |
| 9784 | case LYXP_SET_STRING: |
| 9785 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9786 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9787 | set->val.num = num; |
| 9788 | break; |
| 9789 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9790 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9791 | set->val.num = 1; |
| 9792 | } else { |
| 9793 | set->val.num = 0; |
| 9794 | } |
| 9795 | break; |
| 9796 | default: |
| 9797 | LOGINT_RET(set->ctx); |
| 9798 | } |
| 9799 | set->type = LYXP_SET_NUMBER; |
| 9800 | } |
| 9801 | |
| 9802 | /* to BOOLEAN */ |
| 9803 | if (target == LYXP_SET_BOOLEAN) { |
| 9804 | switch (set->type) { |
| 9805 | case LYXP_SET_NUMBER: |
| 9806 | 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] | 9807 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9808 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9809 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9810 | } |
| 9811 | break; |
| 9812 | case LYXP_SET_STRING: |
| 9813 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9814 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9815 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9816 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9817 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9818 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9819 | } |
| 9820 | break; |
| 9821 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9822 | if (set->used) { |
| 9823 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9824 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9825 | } else { |
| 9826 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9827 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9828 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9829 | break; |
| 9830 | default: |
| 9831 | LOGINT_RET(set->ctx); |
| 9832 | } |
| 9833 | set->type = LYXP_SET_BOOLEAN; |
| 9834 | } |
| 9835 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9836 | return LY_SUCCESS; |
| 9837 | } |
| 9838 | |
| 9839 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9840 | 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] | 9841 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode, |
| 9842 | 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] | 9843 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9844 | LY_ERR ret; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9845 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9846 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9847 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9848 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9849 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9850 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9851 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9852 | |
| 9853 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9854 | memset(set, 0, sizeof *set); |
| 9855 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9856 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 9857 | 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] | 9858 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9859 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9860 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9861 | set->cur_scnode = cur_scnode; |
| 9862 | for (set->context_op = cur_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9863 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9864 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9865 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9866 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9867 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9868 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9869 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9870 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9871 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9872 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9873 | |
Michal Vasko | f69e455 | 2022-12-02 10:27:42 +0100 | [diff] [blame] | 9874 | LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9875 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9876 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9877 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9878 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9879 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9880 | { |
| 9881 | if (!path) { |
| 9882 | return NULL; |
| 9883 | } |
| 9884 | |
| 9885 | return path->expr; |
| 9886 | } |