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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 35 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "printer_data.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 37 | #include "schema_compile_node.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 39 | #include "tree_data.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 40 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 41 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 42 | #include "tree_schema_internal.h" |
| 43 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 44 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 45 | static LY_ERR set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
| 46 | enum lyxp_axis axis, uint32_t *index_p); |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 47 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth); |
| 48 | 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] | 49 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 50 | 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] | 51 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 52 | static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, |
| 53 | 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] | 54 | static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 55 | enum lyxp_axis axis, uint32_t options); |
| 56 | static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 57 | enum lyxp_axis axis, uint32_t options); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 58 | static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 59 | |
aPiecek | 96dc1e3 | 2021-10-08 15:45:59 +0200 | [diff] [blame] | 60 | /* Functions are divided into the following basic classes: |
| 61 | * |
| 62 | * (re)parse functions: |
| 63 | * Parse functions parse the expression into |
| 64 | * tokens (syntactic analysis). |
| 65 | * Reparse functions perform semantic analysis |
| 66 | * (do not save the result, just a check) of |
| 67 | * the expression and fill repeat indices. |
| 68 | * |
| 69 | * warn functions: |
| 70 | * Warn functions check specific reasonable conditions for schema XPath |
| 71 | * and print a warning if they are not satisfied. |
| 72 | * |
| 73 | * moveto functions: |
| 74 | * They and only they actually change the context (set). |
| 75 | * |
| 76 | * eval functions: |
| 77 | * They execute a parsed XPath expression on some data subtree. |
| 78 | */ |
| 79 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 80 | /** |
| 81 | * @brief Print the type of an XPath \p set. |
| 82 | * |
| 83 | * @param[in] set Set to use. |
| 84 | * @return Set type string. |
| 85 | */ |
| 86 | static const char * |
| 87 | print_set_type(struct lyxp_set *set) |
| 88 | { |
| 89 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 90 | case LYXP_SET_NODE_SET: |
| 91 | return "node set"; |
| 92 | case LYXP_SET_SCNODE_SET: |
| 93 | return "schema node set"; |
| 94 | case LYXP_SET_BOOLEAN: |
| 95 | return "boolean"; |
| 96 | case LYXP_SET_NUMBER: |
| 97 | return "number"; |
| 98 | case LYXP_SET_STRING: |
| 99 | return "string"; |
| 100 | } |
| 101 | |
| 102 | return NULL; |
| 103 | } |
| 104 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 105 | const char * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 106 | lyxp_token2str(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 107 | { |
| 108 | switch (tok) { |
| 109 | case LYXP_TOKEN_PAR1: |
| 110 | return "("; |
| 111 | case LYXP_TOKEN_PAR2: |
| 112 | return ")"; |
| 113 | case LYXP_TOKEN_BRACK1: |
| 114 | return "["; |
| 115 | case LYXP_TOKEN_BRACK2: |
| 116 | return "]"; |
| 117 | case LYXP_TOKEN_DOT: |
| 118 | return "."; |
| 119 | case LYXP_TOKEN_DDOT: |
| 120 | return ".."; |
| 121 | case LYXP_TOKEN_AT: |
| 122 | return "@"; |
| 123 | case LYXP_TOKEN_COMMA: |
| 124 | return ","; |
| 125 | case LYXP_TOKEN_NAMETEST: |
| 126 | return "NameTest"; |
| 127 | case LYXP_TOKEN_NODETYPE: |
| 128 | return "NodeType"; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 129 | case LYXP_TOKEN_VARREF: |
| 130 | return "VariableReference"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 131 | case LYXP_TOKEN_FUNCNAME: |
| 132 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 133 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 134 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 135 | case LYXP_TOKEN_OPER_EQUAL: |
| 136 | return "Operator(Equal)"; |
| 137 | case LYXP_TOKEN_OPER_NEQUAL: |
| 138 | return "Operator(Non-equal)"; |
| 139 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 140 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 141 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 142 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 143 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 144 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 145 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 146 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 147 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 148 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 149 | case LYXP_TOKEN_LITERAL: |
| 150 | return "Literal"; |
| 151 | case LYXP_TOKEN_NUMBER: |
| 152 | return "Number"; |
| 153 | default: |
| 154 | LOGINT(NULL); |
| 155 | return ""; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 160 | * @brief Transform string into an axis. |
| 161 | * |
| 162 | * @param[in] str String to transform. |
| 163 | * @param[in] str_len Length of @p str. |
| 164 | * @return Transformed axis. |
| 165 | */ |
| 166 | static enum lyxp_axis |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 167 | str2axis(const char *str, uint32_t str_len) |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 168 | { |
| 169 | switch (str_len) { |
| 170 | case 4: |
| 171 | assert(!strncmp("self", str, str_len)); |
| 172 | return LYXP_AXIS_SELF; |
| 173 | case 5: |
| 174 | assert(!strncmp("child", str, str_len)); |
| 175 | return LYXP_AXIS_CHILD; |
| 176 | case 6: |
| 177 | assert(!strncmp("parent", str, str_len)); |
| 178 | return LYXP_AXIS_PARENT; |
| 179 | case 8: |
| 180 | assert(!strncmp("ancestor", str, str_len)); |
| 181 | return LYXP_AXIS_ANCESTOR; |
| 182 | case 9: |
| 183 | if (str[0] == 'a') { |
| 184 | assert(!strncmp("attribute", str, str_len)); |
| 185 | return LYXP_AXIS_ATTRIBUTE; |
| 186 | } else if (str[0] == 'f') { |
| 187 | assert(!strncmp("following", str, str_len)); |
| 188 | return LYXP_AXIS_FOLLOWING; |
| 189 | } else { |
| 190 | assert(!strncmp("preceding", str, str_len)); |
| 191 | return LYXP_AXIS_PRECEDING; |
| 192 | } |
| 193 | break; |
| 194 | case 10: |
| 195 | assert(!strncmp("descendant", str, str_len)); |
| 196 | return LYXP_AXIS_DESCENDANT; |
| 197 | case 16: |
| 198 | assert(!strncmp("ancestor-or-self", str, str_len)); |
| 199 | return LYXP_AXIS_ANCESTOR_OR_SELF; |
| 200 | case 17: |
| 201 | if (str[0] == 'f') { |
| 202 | assert(!strncmp("following-sibling", str, str_len)); |
| 203 | return LYXP_AXIS_FOLLOWING_SIBLING; |
| 204 | } else { |
| 205 | assert(!strncmp("preceding-sibling", str, str_len)); |
| 206 | return LYXP_AXIS_PRECEDING_SIBLING; |
| 207 | } |
| 208 | break; |
| 209 | case 18: |
| 210 | assert(!strncmp("descendant-or-self", str, str_len)); |
| 211 | return LYXP_AXIS_DESCENDANT_OR_SELF; |
| 212 | } |
| 213 | |
| 214 | LOGINT(NULL); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 219 | * @brief Print the whole expression \p exp to debug output. |
| 220 | * |
| 221 | * @param[in] exp Expression to use. |
| 222 | */ |
| 223 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 224 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 225 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 226 | #define MSG_BUFFER_SIZE 128 |
| 227 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 228 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 229 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 230 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| 234 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 235 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 236 | 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] | 237 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 238 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 239 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 240 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 241 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 242 | } |
| 243 | strcat(tmp, ")"); |
| 244 | } |
| 245 | LOGDBG(LY_LDGXPATH, tmp); |
| 246 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 247 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | #ifndef NDEBUG |
| 251 | |
| 252 | /** |
| 253 | * @brief Print XPath set content to debug output. |
| 254 | * |
| 255 | * @param[in] set Set to print. |
| 256 | */ |
| 257 | static void |
| 258 | print_set_debug(struct lyxp_set *set) |
| 259 | { |
| 260 | uint32_t i; |
| 261 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 262 | struct lyxp_set_node *item; |
| 263 | struct lyxp_set_scnode *sitem; |
| 264 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 265 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
| 269 | switch (set->type) { |
| 270 | case LYXP_SET_NODE_SET: |
| 271 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 272 | for (i = 0; i < set->used; ++i) { |
| 273 | item = &set->val.nodes[i]; |
| 274 | |
| 275 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 276 | case LYXP_NODE_NONE: |
| 277 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 278 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 279 | case LYXP_NODE_ROOT: |
| 280 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 281 | break; |
| 282 | case LYXP_NODE_ROOT_CONFIG: |
| 283 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 284 | break; |
| 285 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 286 | 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] | 287 | 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] | 288 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 289 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 290 | 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] | 291 | item->node->schema->name, lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 292 | } else { |
| 293 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 294 | } |
| 295 | break; |
| 296 | case LYXP_NODE_TEXT: |
| 297 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 298 | 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] | 299 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 300 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 301 | 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] | 302 | } |
| 303 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 304 | case LYXP_NODE_META: |
| 305 | 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] | 306 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 307 | break; |
| 308 | } |
| 309 | } |
| 310 | break; |
| 311 | |
| 312 | case LYXP_SET_SCNODE_SET: |
| 313 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 314 | for (i = 0; i < set->used; ++i) { |
| 315 | sitem = &set->val.scnodes[i]; |
| 316 | |
| 317 | switch (sitem->type) { |
| 318 | case LYXP_NODE_ROOT: |
| 319 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 320 | break; |
| 321 | case LYXP_NODE_ROOT_CONFIG: |
| 322 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 323 | break; |
| 324 | case LYXP_NODE_ELEM: |
| 325 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 326 | break; |
| 327 | default: |
| 328 | LOGINT(NULL); |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | break; |
| 333 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 334 | case LYXP_SET_BOOLEAN: |
| 335 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 336 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 337 | break; |
| 338 | |
| 339 | case LYXP_SET_STRING: |
| 340 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 341 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 342 | break; |
| 343 | |
| 344 | case LYXP_SET_NUMBER: |
| 345 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 346 | |
| 347 | if (isnan(set->val.num)) { |
| 348 | str = strdup("NaN"); |
| 349 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 350 | str = strdup("0"); |
| 351 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 352 | str = strdup("Infinity"); |
| 353 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 354 | str = strdup("-Infinity"); |
| 355 | } else if ((long long)set->val.num == set->val.num) { |
| 356 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 357 | str = NULL; |
| 358 | } |
| 359 | } else { |
| 360 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 361 | str = NULL; |
| 362 | } |
| 363 | } |
| 364 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 365 | |
| 366 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 367 | free(str); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | #endif |
| 372 | |
| 373 | /** |
| 374 | * @brief Realloc the string \p str. |
| 375 | * |
| 376 | * @param[in] ctx libyang context for logging. |
| 377 | * @param[in] needed How much free space is required. |
| 378 | * @param[in,out] str Pointer to the string to use. |
| 379 | * @param[in,out] used Used bytes in \p str. |
| 380 | * @param[in,out] size Allocated bytes in \p str. |
| 381 | * @return LY_ERR |
| 382 | */ |
| 383 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 384 | 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] | 385 | { |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 386 | if (*size - (unsigned)*used < needed) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 387 | do { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 388 | if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 389 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 390 | return LY_EINVAL; |
| 391 | } |
| 392 | *size += LYXP_STRING_CAST_SIZE_STEP; |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 393 | } while (*size - (unsigned)*used < needed); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 394 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 395 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 396 | } |
| 397 | |
| 398 | return LY_SUCCESS; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * @brief Cast nodes recursively to one string @p str. |
| 403 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 404 | * @param[in] node Node to cast, NULL if root. |
| 405 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 406 | * @param[in] indent Current indent. |
| 407 | * @param[in,out] str Resulting string. |
| 408 | * @param[in,out] used Used bytes in @p str. |
| 409 | * @param[in,out] size Allocated bytes in @p str. |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 410 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 411 | */ |
| 412 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 413 | cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used, |
| 414 | uint32_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 415 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 416 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 417 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | const struct lyd_node *child; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 419 | enum lyxp_node_type child_type; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 420 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 421 | struct lyd_node_any *any; |
| 422 | LY_ERR rc; |
| 423 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 424 | 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] | 425 | return LY_SUCCESS; |
| 426 | } |
| 427 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 428 | if (!node) { |
| 429 | /* fake container */ |
| 430 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 431 | strcpy(*str + (*used - 1), "\n"); |
| 432 | ++(*used); |
| 433 | |
| 434 | ++indent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 435 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 436 | /* print all the top-level nodes */ |
| 437 | child = NULL; |
| 438 | child_type = 0; |
| 439 | while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) { |
| 440 | LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 443 | /* end fake container */ |
| 444 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 445 | strcpy(*str + (*used - 1), "\n"); |
| 446 | ++(*used); |
| 447 | |
| 448 | --indent; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 449 | } else { |
| 450 | switch (node->schema->nodetype) { |
| 451 | case LYS_CONTAINER: |
| 452 | case LYS_LIST: |
| 453 | case LYS_RPC: |
| 454 | case LYS_NOTIF: |
| 455 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
| 456 | strcpy(*str + (*used - 1), "\n"); |
| 457 | ++(*used); |
| 458 | |
| 459 | for (child = lyd_child(node); child; child = child->next) { |
| 460 | LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size)); |
| 461 | } |
| 462 | |
| 463 | break; |
| 464 | |
| 465 | case LYS_LEAF: |
| 466 | case LYS_LEAFLIST: |
| 467 | value_str = lyd_get_value(node); |
| 468 | |
| 469 | /* print indent */ |
| 470 | LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size)); |
| 471 | memset(*str + (*used - 1), ' ', indent * 2); |
| 472 | *used += indent * 2; |
| 473 | |
| 474 | /* print value */ |
| 475 | if (*used == 1) { |
| 476 | sprintf(*str + (*used - 1), "%s", value_str); |
| 477 | *used += strlen(value_str); |
| 478 | } else { |
| 479 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 480 | *used += strlen(value_str) + 1; |
| 481 | } |
| 482 | |
| 483 | break; |
| 484 | |
| 485 | case LYS_ANYXML: |
| 486 | case LYS_ANYDATA: |
| 487 | any = (struct lyd_node_any *)node; |
| 488 | if (!(void *)any->value.tree) { |
| 489 | /* no content */ |
| 490 | buf = strdup(""); |
| 491 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 492 | } else { |
| 493 | struct ly_out *out; |
| 494 | |
| 495 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 496 | /* try to parse it into a data tree */ |
| 497 | if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB, |
| 498 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
| 499 | /* successfully parsed */ |
| 500 | free(any->value.mem); |
| 501 | any->value.tree = tree; |
| 502 | any->value_type = LYD_ANYDATA_DATATREE; |
| 503 | } |
| 504 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
| 505 | } |
| 506 | |
| 507 | switch (any->value_type) { |
| 508 | case LYD_ANYDATA_STRING: |
| 509 | case LYD_ANYDATA_XML: |
| 510 | case LYD_ANYDATA_JSON: |
| 511 | buf = strdup(any->value.json); |
| 512 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 513 | break; |
| 514 | case LYD_ANYDATA_DATATREE: |
| 515 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
| 516 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
| 517 | ly_out_free(out, NULL, 0); |
| 518 | LY_CHECK_RET(rc < 0, -rc); |
| 519 | break; |
| 520 | case LYD_ANYDATA_LYB: |
| 521 | LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 522 | return LY_EINVAL; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | line = strtok_r(buf, "\n", &ptr); |
| 527 | do { |
| 528 | rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size); |
| 529 | if (rc != LY_SUCCESS) { |
| 530 | free(buf); |
| 531 | return rc; |
| 532 | } |
| 533 | memset(*str + (*used - 1), ' ', indent * 2); |
| 534 | *used += indent * 2; |
| 535 | |
| 536 | strcpy(*str + (*used - 1), line); |
| 537 | *used += strlen(line); |
| 538 | |
| 539 | strcpy(*str + (*used - 1), "\n"); |
| 540 | *used += 1; |
| 541 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 542 | |
| 543 | free(buf); |
| 544 | break; |
| 545 | |
| 546 | default: |
| 547 | LOGINT_RET(set->ctx); |
| 548 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | return LY_SUCCESS; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * @brief Cast an element into a string. |
| 556 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 557 | * @param[in] node Node to cast, NULL if root. |
| 558 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 559 | * @param[out] str Element cast to dynamically-allocated string. |
| 560 | * @return LY_ERR |
| 561 | */ |
| 562 | static LY_ERR |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 563 | 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] | 564 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 565 | uint32_t used, size; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 566 | LY_ERR rc; |
| 567 | |
| 568 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 569 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 570 | (*str)[0] = '\0'; |
| 571 | used = 1; |
| 572 | size = LYXP_STRING_CAST_SIZE_START; |
| 573 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 574 | rc = cast_string_recursive(node, set, 0, str, &used, &size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 575 | if (rc != LY_SUCCESS) { |
| 576 | free(*str); |
| 577 | return rc; |
| 578 | } |
| 579 | |
| 580 | if (size > used) { |
| 581 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 582 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 583 | } |
| 584 | return LY_SUCCESS; |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 589 | * Context position aware. |
| 590 | * |
| 591 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 592 | * @param[out] str Cast dynamically-allocated string. |
| 593 | * @return LY_ERR |
| 594 | */ |
| 595 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 596 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 597 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 598 | if (!set->used) { |
| 599 | *str = strdup(""); |
| 600 | if (!*str) { |
| 601 | LOGMEM_RET(set->ctx); |
| 602 | } |
| 603 | return LY_SUCCESS; |
| 604 | } |
| 605 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 606 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 607 | case LYXP_NODE_NONE: |
| 608 | /* invalid */ |
| 609 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 610 | case LYXP_NODE_ROOT: |
| 611 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 612 | case LYXP_NODE_ELEM: |
| 613 | case LYXP_NODE_TEXT: |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 614 | return cast_string_elem(set->val.nodes[0].node, set, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 615 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 616 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 617 | if (!*str) { |
| 618 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 619 | } |
| 620 | return LY_SUCCESS; |
| 621 | } |
| 622 | |
| 623 | LOGINT_RET(set->ctx); |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * @brief Cast a string into an XPath number. |
| 628 | * |
| 629 | * @param[in] str String to use. |
| 630 | * @return Cast number. |
| 631 | */ |
| 632 | static long double |
| 633 | cast_string_to_number(const char *str) |
| 634 | { |
| 635 | long double num; |
| 636 | char *ptr; |
| 637 | |
| 638 | errno = 0; |
| 639 | num = strtold(str, &ptr); |
| 640 | if (errno || *ptr) { |
| 641 | num = NAN; |
| 642 | } |
| 643 | return num; |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * @brief Callback for checking value equality. |
| 648 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 649 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 650 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 651 | * @param[in] val1_p First value. |
| 652 | * @param[in] val2_p Second value. |
| 653 | * @param[in] mod Whether hash table is being modified. |
| 654 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 655 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 656 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 657 | static ly_bool |
| 658 | 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] | 659 | { |
| 660 | struct lyxp_set_hash_node *val1, *val2; |
| 661 | |
| 662 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 663 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 664 | |
| 665 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 666 | return 1; |
| 667 | } |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * @brief Insert node and its hash into set. |
| 674 | * |
| 675 | * @param[in] set et to insert to. |
| 676 | * @param[in] node Node with hash. |
| 677 | * @param[in] type Node type. |
| 678 | */ |
| 679 | static void |
| 680 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 681 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 682 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 683 | uint32_t i, hash; |
| 684 | struct lyxp_set_hash_node hnode; |
| 685 | |
| 686 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 687 | /* create hash table and add all the nodes */ |
| 688 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 689 | for (i = 0; i < set->used; ++i) { |
| 690 | hnode.node = set->val.nodes[i].node; |
| 691 | hnode.type = set->val.nodes[i].type; |
| 692 | |
| 693 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 694 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 695 | hash = dict_hash_multi(hash, NULL, 0); |
| 696 | |
| 697 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 698 | assert(!r); |
| 699 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 700 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 701 | if (hnode.node == node) { |
| 702 | /* it was just added, do not add it twice */ |
| 703 | node = NULL; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 709 | /* add the new node into hash table */ |
| 710 | hnode.node = node; |
| 711 | hnode.type = type; |
| 712 | |
| 713 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 714 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 715 | hash = dict_hash_multi(hash, NULL, 0); |
| 716 | |
| 717 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 718 | assert(!r); |
| 719 | (void)r; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * @brief Remove node and its hash from set. |
| 725 | * |
| 726 | * @param[in] set Set to remove from. |
| 727 | * @param[in] node Node to remove. |
| 728 | * @param[in] type Node type. |
| 729 | */ |
| 730 | static void |
| 731 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 732 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 733 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 734 | struct lyxp_set_hash_node hnode; |
| 735 | uint32_t hash; |
| 736 | |
| 737 | if (set->ht) { |
| 738 | hnode.node = node; |
| 739 | hnode.type = type; |
| 740 | |
| 741 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 742 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 743 | hash = dict_hash_multi(hash, NULL, 0); |
| 744 | |
| 745 | r = lyht_remove(set->ht, &hnode, hash); |
| 746 | assert(!r); |
| 747 | (void)r; |
| 748 | |
| 749 | if (!set->ht->used) { |
| 750 | lyht_free(set->ht); |
| 751 | set->ht = NULL; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * @brief Check whether node is in set based on its hash. |
| 758 | * |
| 759 | * @param[in] set Set to search in. |
| 760 | * @param[in] node Node to search for. |
| 761 | * @param[in] type Node type. |
| 762 | * @param[in] skip_idx Index in @p set to skip. |
| 763 | * @return LY_ERR |
| 764 | */ |
| 765 | static LY_ERR |
| 766 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 767 | { |
| 768 | struct lyxp_set_hash_node hnode, *match_p; |
| 769 | uint32_t hash; |
| 770 | |
| 771 | hnode.node = node; |
| 772 | hnode.type = type; |
| 773 | |
| 774 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 775 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 776 | hash = dict_hash_multi(hash, NULL, 0); |
| 777 | |
| 778 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 779 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 780 | /* we found it on the index that should be skipped, find another */ |
| 781 | hnode = *match_p; |
| 782 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 783 | /* none other found */ |
| 784 | return LY_SUCCESS; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return LY_EEXIST; |
| 789 | } |
| 790 | |
| 791 | /* not found */ |
| 792 | return LY_SUCCESS; |
| 793 | } |
| 794 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 795 | void |
| 796 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 797 | { |
| 798 | if (!set) { |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | if (set->type == LYXP_SET_NODE_SET) { |
| 803 | free(set->val.nodes); |
| 804 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 805 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 806 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 807 | lyht_free(set->ht); |
| 808 | } else { |
| 809 | if (set->type == LYXP_SET_STRING) { |
| 810 | free(set->val.str); |
| 811 | } |
| 812 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 813 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 814 | |
| 815 | set->val.nodes = NULL; |
| 816 | set->used = 0; |
| 817 | set->size = 0; |
| 818 | set->ht = NULL; |
| 819 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 820 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 821 | } |
| 822 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 823 | /** |
| 824 | * @brief Free dynamically-allocated set. |
| 825 | * |
| 826 | * @param[in] set Set to free. |
| 827 | */ |
| 828 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 829 | lyxp_set_free(struct lyxp_set *set) |
| 830 | { |
| 831 | if (!set) { |
| 832 | return; |
| 833 | } |
| 834 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 835 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 836 | free(set); |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * @brief Initialize set context. |
| 841 | * |
| 842 | * @param[in] new Set to initialize. |
| 843 | * @param[in] set Arbitrary initialized set. |
| 844 | */ |
| 845 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 846 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 847 | { |
| 848 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 849 | if (set) { |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 850 | new->non_child_axis = set->non_child_axis; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 851 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 852 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 853 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 854 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 855 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 856 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 857 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 858 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 859 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 860 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /** |
| 864 | * @brief Create a deep copy of a set. |
| 865 | * |
| 866 | * @param[in] set Set to copy. |
| 867 | * @return Copy of @p set. |
| 868 | */ |
| 869 | static struct lyxp_set * |
| 870 | set_copy(struct lyxp_set *set) |
| 871 | { |
| 872 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 873 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 874 | |
| 875 | if (!set) { |
| 876 | return NULL; |
| 877 | } |
| 878 | |
| 879 | ret = malloc(sizeof *ret); |
| 880 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 881 | set_init(ret, set); |
| 882 | |
| 883 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 884 | ret->type = set->type; |
| 885 | |
| 886 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 887 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 888 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 889 | uint32_t idx; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 890 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 891 | LY_CHECK_ERR_RET(set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, |
| 892 | set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 893 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 894 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 895 | lyxp_set_free(ret); |
| 896 | return NULL; |
| 897 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 898 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 902 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 903 | if (set->used) { |
| 904 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 905 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 906 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 907 | } else { |
| 908 | ret->val.nodes = NULL; |
| 909 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 910 | |
| 911 | ret->used = ret->size = set->used; |
| 912 | ret->ctx_pos = set->ctx_pos; |
| 913 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 914 | if (set->ht) { |
| 915 | ret->ht = lyht_dup(set->ht); |
| 916 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 917 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 918 | memcpy(ret, set, sizeof *ret); |
| 919 | if (set->type == LYXP_SET_STRING) { |
| 920 | ret->val.str = strdup(set->val.str); |
| 921 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 922 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | return ret; |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 930 | * |
| 931 | * @param[in] set Set to fill. |
| 932 | * @param[in] string String to fill into \p set. |
| 933 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 934 | */ |
| 935 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 936 | 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] | 937 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 938 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 939 | |
| 940 | set->type = LYXP_SET_STRING; |
| 941 | if ((str_len == 0) && (string[0] != '\0')) { |
| 942 | string = ""; |
| 943 | } |
| 944 | set->val.str = strndup(string, str_len); |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 949 | * |
| 950 | * @param[in] set Set to fill. |
| 951 | * @param[in] number Number to fill into \p set. |
| 952 | */ |
| 953 | static void |
| 954 | set_fill_number(struct lyxp_set *set, long double number) |
| 955 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 956 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 957 | |
| 958 | set->type = LYXP_SET_NUMBER; |
| 959 | set->val.num = number; |
| 960 | } |
| 961 | |
| 962 | /** |
| 963 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 964 | * |
| 965 | * @param[in] set Set to fill. |
| 966 | * @param[in] boolean Boolean to fill into \p set. |
| 967 | */ |
| 968 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 969 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 970 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 971 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 972 | |
| 973 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 974 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /** |
| 978 | * @brief Fill XPath set with the value from another set (deep assign). |
| 979 | * Any current data are disposed of. |
| 980 | * |
| 981 | * @param[in] trg Set to fill. |
| 982 | * @param[in] src Source set to copy into \p trg. |
| 983 | */ |
| 984 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 985 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 986 | { |
| 987 | if (!trg || !src) { |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | if (trg->type == LYXP_SET_NODE_SET) { |
| 992 | free(trg->val.nodes); |
| 993 | } else if (trg->type == LYXP_SET_STRING) { |
| 994 | free(trg->val.str); |
| 995 | } |
| 996 | set_init(trg, src); |
| 997 | |
| 998 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 999 | trg->type = LYXP_SET_SCNODE_SET; |
| 1000 | trg->used = src->used; |
| 1001 | trg->size = src->used; |
| 1002 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1003 | if (trg->size) { |
| 1004 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 1005 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1006 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 1007 | } else { |
| 1008 | trg->val.scnodes = NULL; |
| 1009 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1010 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1011 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1012 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1013 | set_fill_number(trg, src->val.num); |
| 1014 | } else if (src->type == LYXP_SET_STRING) { |
| 1015 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 1016 | } else { |
| 1017 | if (trg->type == LYXP_SET_NODE_SET) { |
| 1018 | free(trg->val.nodes); |
| 1019 | } else if (trg->type == LYXP_SET_STRING) { |
| 1020 | free(trg->val.str); |
| 1021 | } |
| 1022 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1023 | assert(src->type == LYXP_SET_NODE_SET); |
| 1024 | |
| 1025 | trg->type = LYXP_SET_NODE_SET; |
| 1026 | trg->used = src->used; |
| 1027 | trg->size = src->used; |
| 1028 | trg->ctx_pos = src->ctx_pos; |
| 1029 | trg->ctx_size = src->ctx_size; |
| 1030 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1031 | if (trg->size) { |
| 1032 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 1033 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1034 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 1035 | } else { |
| 1036 | trg->val.nodes = NULL; |
| 1037 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1038 | if (src->ht) { |
| 1039 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1040 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1041 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * @brief Clear context of all schema nodes. |
| 1048 | * |
| 1049 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1050 | * @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] | 1051 | */ |
| 1052 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1053 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1054 | { |
| 1055 | uint32_t i; |
| 1056 | |
| 1057 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1058 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1059 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1060 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 1061 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | /** |
| 1067 | * @brief Remove a node from a set. Removing last node changes |
| 1068 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1069 | * |
| 1070 | * @param[in] set Set to use. |
| 1071 | * @param[in] idx Index from @p set of the node to be removed. |
| 1072 | */ |
| 1073 | static void |
| 1074 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1075 | { |
| 1076 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1077 | assert(idx < set->used); |
| 1078 | |
| 1079 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1080 | |
| 1081 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1082 | if (idx < set->used) { |
| 1083 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1084 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1085 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1090 | * @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] | 1091 | * |
| 1092 | * @param[in] set Set to use. |
| 1093 | * @param[in] idx Index from @p set of the node to be removed. |
| 1094 | */ |
| 1095 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1096 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1097 | { |
| 1098 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1099 | assert(idx < set->used); |
| 1100 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1101 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1102 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1103 | } |
| 1104 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1108 | * @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] | 1109 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1110 | * |
| 1111 | * @param[in] set Set to consolidate. |
| 1112 | */ |
| 1113 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1114 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1115 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1116 | uint32_t i, orig_used, end = 0; |
| 1117 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1118 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1119 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1120 | |
| 1121 | orig_used = set->used; |
| 1122 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1123 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1124 | start = -1; |
| 1125 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1126 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1127 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1128 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1129 | end = i; |
| 1130 | ++i; |
| 1131 | break; |
| 1132 | } |
| 1133 | |
| 1134 | ++i; |
| 1135 | if (i == orig_used) { |
| 1136 | end = i; |
| 1137 | } |
| 1138 | } while (i < orig_used); |
| 1139 | |
| 1140 | if (start > -1) { |
| 1141 | /* move the whole chunk of valid nodes together */ |
| 1142 | if (set->used != (unsigned)start) { |
| 1143 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1144 | } |
| 1145 | set->used += end - start; |
| 1146 | } |
| 1147 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1151 | * @brief Check for duplicates in a node set. |
| 1152 | * |
| 1153 | * @param[in] set Set to check. |
| 1154 | * @param[in] node Node to look for in @p set. |
| 1155 | * @param[in] node_type Type of @p node. |
| 1156 | * @param[in] skip_idx Index from @p set to skip. |
| 1157 | * @return LY_ERR |
| 1158 | */ |
| 1159 | static LY_ERR |
| 1160 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1161 | { |
| 1162 | uint32_t i; |
| 1163 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1164 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1165 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1166 | } |
| 1167 | |
| 1168 | for (i = 0; i < set->used; ++i) { |
| 1169 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1170 | continue; |
| 1171 | } |
| 1172 | |
| 1173 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1174 | return LY_EEXIST; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | return LY_SUCCESS; |
| 1179 | } |
| 1180 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1181 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1182 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1183 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1184 | { |
| 1185 | uint32_t i; |
| 1186 | |
| 1187 | for (i = 0; i < set->used; ++i) { |
| 1188 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | 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] | 1193 | if (index_p) { |
| 1194 | *index_p = i; |
| 1195 | } |
| 1196 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1200 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1201 | } |
| 1202 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1203 | void |
| 1204 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1205 | { |
| 1206 | uint32_t orig_used, i, j; |
| 1207 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1208 | 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] | 1209 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1210 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1211 | return; |
| 1212 | } |
| 1213 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1214 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1215 | /* release hidden allocated data (lyxp_set.size) */ |
| 1216 | lyxp_set_free_content(set1); |
| 1217 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1218 | memcpy(set1, set2, sizeof *set1); |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | if (set1->used + set2->used > set1->size) { |
| 1223 | set1->size = set1->used + set2->used; |
| 1224 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1225 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1226 | } |
| 1227 | |
| 1228 | orig_used = set1->used; |
| 1229 | |
| 1230 | for (i = 0; i < set2->used; ++i) { |
| 1231 | for (j = 0; j < orig_used; ++j) { |
| 1232 | /* detect duplicities */ |
| 1233 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1234 | break; |
| 1235 | } |
| 1236 | } |
| 1237 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1238 | if (j < orig_used) { |
| 1239 | /* node is there, but update its status if needed */ |
| 1240 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1241 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1242 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1243 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1244 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1245 | } |
| 1246 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1247 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1248 | ++set1->used; |
| 1249 | } |
| 1250 | } |
| 1251 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1252 | lyxp_set_free_content(set2); |
| 1253 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /** |
| 1257 | * @brief Insert a node into a set. Context position aware. |
| 1258 | * |
| 1259 | * @param[in] set Set to use. |
| 1260 | * @param[in] node Node to insert to @p set. |
| 1261 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1262 | * @param[in] node_type Node type of @p node. |
| 1263 | * @param[in] idx Index in @p set to insert into. |
| 1264 | */ |
| 1265 | static void |
| 1266 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1267 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1268 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1269 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1270 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1271 | /* first item */ |
| 1272 | if (idx) { |
| 1273 | /* no real harm done, but it is a bug */ |
| 1274 | LOGINT(set->ctx); |
| 1275 | idx = 0; |
| 1276 | } |
| 1277 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1278 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1279 | set->type = LYXP_SET_NODE_SET; |
| 1280 | set->used = 0; |
| 1281 | set->size = LYXP_SET_SIZE_START; |
| 1282 | set->ctx_pos = 1; |
| 1283 | set->ctx_size = 1; |
| 1284 | set->ht = NULL; |
| 1285 | } else { |
| 1286 | /* not an empty set */ |
| 1287 | if (set->used == set->size) { |
| 1288 | |
| 1289 | /* set is full */ |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1290 | 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] | 1291 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1292 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | if (idx > set->used) { |
| 1296 | LOGINT(set->ctx); |
| 1297 | idx = set->used; |
| 1298 | } |
| 1299 | |
| 1300 | /* make space for the new node */ |
| 1301 | if (idx < set->used) { |
| 1302 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | /* finally assign the value */ |
| 1307 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1308 | set->val.nodes[idx].type = node_type; |
| 1309 | set->val.nodes[idx].pos = pos; |
| 1310 | ++set->used; |
| 1311 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1312 | /* add into hash table */ |
| 1313 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1314 | } |
| 1315 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1316 | /** |
| 1317 | * @brief Insert schema node into set. |
| 1318 | * |
| 1319 | * @param[in] set Set to insert into. |
| 1320 | * @param[in] node Node to insert. |
| 1321 | * @param[in] node_type Node type of @p node. |
| 1322 | * @param[in] axis Axis that @p node was reached on. |
| 1323 | * @param[out] index_p Optional pointer to store index if the inserted @p node. |
| 1324 | * @return LY_SUCCESS on success. |
| 1325 | * @return LY_EMEM on memory allocation failure. |
| 1326 | */ |
| 1327 | static LY_ERR |
| 1328 | set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
| 1329 | enum lyxp_axis axis, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1330 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1331 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1332 | |
| 1333 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1334 | |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1335 | if (!set->size) { |
| 1336 | /* first item */ |
| 1337 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
| 1338 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
| 1339 | set->type = LYXP_SET_SCNODE_SET; |
| 1340 | set->used = 0; |
| 1341 | set->size = LYXP_SET_SIZE_START; |
| 1342 | set->ctx_pos = 1; |
| 1343 | set->ctx_size = 1; |
| 1344 | set->ht = NULL; |
| 1345 | } |
| 1346 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1347 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1348 | /* BUG if axes differs, this new one is thrown away */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1349 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1350 | } else { |
| 1351 | if (set->used == set->size) { |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1352 | 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] | 1353 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1354 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1355 | } |
| 1356 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1357 | index = set->used; |
| 1358 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1359 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1360 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1361 | set->val.scnodes[index].axis = axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1362 | ++set->used; |
| 1363 | } |
| 1364 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1365 | if (index_p) { |
| 1366 | *index_p = index; |
| 1367 | } |
| 1368 | |
| 1369 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1373 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1374 | * |
| 1375 | * @param[in] set Set to modify. |
| 1376 | * @return New context value. |
| 1377 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1378 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1379 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1380 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1381 | uint32_t i; |
| 1382 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1383 | |
| 1384 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1385 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1386 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1387 | retry: |
| 1388 | for (i = 0; i < set->used; ++i) { |
| 1389 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1390 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1391 | goto retry; |
| 1392 | } |
| 1393 | } |
| 1394 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1395 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1396 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | return ret_ctx; |
| 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * @brief Get unique @p node position in the data. |
| 1405 | * |
| 1406 | * @param[in] node Node to find. |
| 1407 | * @param[in] node_type Node type of @p node. |
| 1408 | * @param[in] root Root node. |
| 1409 | * @param[in] root_type Type of the XPath @p root node. |
| 1410 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1411 | * be used to increase efficiency and start the DFS from this node. |
| 1412 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1413 | * @return Node position. |
| 1414 | */ |
| 1415 | static uint32_t |
| 1416 | 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] | 1417 | 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] | 1418 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1419 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1420 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1421 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1422 | |
| 1423 | assert(prev && prev_pos && !root->prev->next); |
| 1424 | |
| 1425 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
| 1429 | if (*prev) { |
| 1430 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1431 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1432 | 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] | 1433 | goto dfs_search; |
| 1434 | } |
| 1435 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1436 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1437 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1438 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1439 | LYD_TREE_DFS_continue = 0; |
| 1440 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1441 | if (*prev && !elem) { |
| 1442 | /* resume previous DFS */ |
| 1443 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1444 | LYD_TREE_DFS_continue = 0; |
| 1445 | } |
| 1446 | |
Michal Vasko | 482a682 | 2022-05-06 08:56:12 +0200 | [diff] [blame] | 1447 | 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] | 1448 | /* skip */ |
| 1449 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1450 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1451 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1452 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1453 | break; |
| 1454 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1455 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1456 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1457 | |
| 1458 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1462 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1463 | break; |
| 1464 | } |
| 1465 | } |
| 1466 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1467 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1468 | if (!(*prev)) { |
| 1469 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1470 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1471 | return 0; |
| 1472 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1473 | /* start the search again from the beginning */ |
| 1474 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1475 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1476 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1477 | pos = 1; |
| 1478 | goto dfs_search; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | /* remember the last found node for next time */ |
| 1483 | *prev = node; |
| 1484 | *prev_pos = pos; |
| 1485 | |
| 1486 | return pos; |
| 1487 | } |
| 1488 | |
| 1489 | /** |
| 1490 | * @brief Assign (fill) missing node positions. |
| 1491 | * |
| 1492 | * @param[in] set Set to fill positions in. |
| 1493 | * @param[in] root Context root node. |
| 1494 | * @param[in] root_type Context root type. |
| 1495 | * @return LY_ERR |
| 1496 | */ |
| 1497 | static LY_ERR |
| 1498 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1499 | { |
| 1500 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1501 | uint32_t i, tmp_pos = 0; |
| 1502 | |
| 1503 | for (i = 0; i < set->used; ++i) { |
| 1504 | if (!set->val.nodes[i].pos) { |
| 1505 | tmp_node = NULL; |
| 1506 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1507 | case LYXP_NODE_META: |
| 1508 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1509 | if (!tmp_node) { |
| 1510 | LOGINT_RET(root->schema->module->ctx); |
| 1511 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1512 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1513 | case LYXP_NODE_ELEM: |
| 1514 | case LYXP_NODE_TEXT: |
| 1515 | if (!tmp_node) { |
| 1516 | tmp_node = set->val.nodes[i].node; |
| 1517 | } |
| 1518 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1519 | break; |
| 1520 | default: |
| 1521 | /* all roots have position 0 */ |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | return LY_SUCCESS; |
| 1528 | } |
| 1529 | |
| 1530 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1531 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1532 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1533 | * @param[in] meta Metadata to use. |
| 1534 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1535 | */ |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1536 | static uint32_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1537 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1538 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1539 | uint32_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1540 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1541 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1542 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1543 | ++pos; |
| 1544 | } |
| 1545 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1546 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1547 | return pos; |
| 1548 | } |
| 1549 | |
| 1550 | /** |
| 1551 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1552 | * |
| 1553 | * @param[in] item1 1st node. |
| 1554 | * @param[in] item2 2nd node. |
| 1555 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1556 | */ |
| 1557 | static int |
| 1558 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1559 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1560 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1561 | |
| 1562 | if (item1->pos < item2->pos) { |
| 1563 | return -1; |
| 1564 | } |
| 1565 | |
| 1566 | if (item1->pos > item2->pos) { |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
| 1570 | /* node positions are equal, the fun case */ |
| 1571 | |
| 1572 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1573 | /* special case since text nodes are actually saved as their parents */ |
| 1574 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1575 | if (item1->type == LYXP_NODE_ELEM) { |
| 1576 | assert(item2->type == LYXP_NODE_TEXT); |
| 1577 | return -1; |
| 1578 | } else { |
| 1579 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1580 | return 1; |
| 1581 | } |
| 1582 | } |
| 1583 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1584 | /* we need meta positions now */ |
| 1585 | if (item1->type == LYXP_NODE_META) { |
| 1586 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1587 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1588 | if (item2->type == LYXP_NODE_META) { |
| 1589 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1592 | /* 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] | 1593 | /* check for duplicates */ |
| 1594 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1595 | 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] | 1596 | return 0; |
| 1597 | } |
| 1598 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1599 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1600 | /* elem is always first, 2nd node is after it */ |
| 1601 | if (item1->type == LYXP_NODE_ELEM) { |
| 1602 | assert(item2->type != LYXP_NODE_ELEM); |
| 1603 | return -1; |
| 1604 | } |
| 1605 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1606 | /* 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] | 1607 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1608 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1609 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1610 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1611 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1612 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1613 | return 1; |
| 1614 | } |
| 1615 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1616 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1617 | /* 2nd is after 1st */ |
| 1618 | return -1; |
| 1619 | } |
| 1620 | |
| 1621 | /** |
| 1622 | * @brief Set cast for comparisons. |
| 1623 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1624 | * @param[in,out] trg Target set to cast source into. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1625 | * @param[in] src Source set. |
| 1626 | * @param[in] type Target set type. |
| 1627 | * @param[in] src_idx Source set node index. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1628 | * @return LY_SUCCESS on success. |
| 1629 | * @return LY_ERR value on error. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1630 | */ |
| 1631 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1632 | 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] | 1633 | { |
| 1634 | assert(src->type == LYXP_SET_NODE_SET); |
| 1635 | |
| 1636 | set_init(trg, src); |
| 1637 | |
| 1638 | /* insert node into target set */ |
| 1639 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1640 | |
| 1641 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1642 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1643 | } |
| 1644 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1645 | /** |
| 1646 | * @brief Set content canonization for comparisons. |
| 1647 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1648 | * @param[in,out] set Set to canonize. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1649 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1650 | * @return LY_SUCCESS on success. |
| 1651 | * @return LY_ERR value on error. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1652 | */ |
| 1653 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1654 | 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] | 1655 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1656 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1657 | struct lyd_value val; |
| 1658 | struct ly_err_item *err = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1659 | LY_ERR rc; |
| 1660 | |
| 1661 | /* is there anything to canonize even? */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1662 | if (set->type == LYXP_SET_STRING) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1663 | /* do we have a type to use for canonization? */ |
| 1664 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1665 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1666 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1667 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1668 | } |
| 1669 | } |
| 1670 | if (!type) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1671 | /* no canonization needed/possible */ |
| 1672 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1673 | } |
| 1674 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1675 | /* check for built-in types without required canonization */ |
| 1676 | if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) { |
| 1677 | /* string */ |
| 1678 | return LY_SUCCESS; |
| 1679 | } |
| 1680 | if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) { |
| 1681 | /* boolean */ |
| 1682 | return LY_SUCCESS; |
| 1683 | } |
| 1684 | if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) { |
| 1685 | /* enumeration */ |
| 1686 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1687 | } |
| 1688 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1689 | /* print canonized string, ignore errors, the value may not satisfy schema constraints */ |
| 1690 | 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] | 1691 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1692 | ly_err_free(err); |
| 1693 | if (rc) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1694 | /* invalid value, function store automaticaly dealloc value when fail */ |
| 1695 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1696 | } |
| 1697 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1698 | /* use the canonized string value */ |
| 1699 | free(set->val.str); |
| 1700 | set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val)); |
| 1701 | type->plugin->free(set->ctx, &val); |
| 1702 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1703 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1704 | return LY_SUCCESS; |
| 1705 | } |
| 1706 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1707 | /** |
| 1708 | * @brief Bubble sort @p set into XPath document order. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1709 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1710 | * |
| 1711 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1712 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1713 | */ |
| 1714 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1715 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1716 | { |
| 1717 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1718 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1719 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1720 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1721 | struct lyxp_set_node item; |
| 1722 | struct lyxp_set_hash_node hnode; |
| 1723 | uint64_t hash; |
| 1724 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1725 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1726 | return 0; |
| 1727 | } |
| 1728 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1729 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1730 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1731 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1732 | |
| 1733 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1734 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1735 | return -1; |
| 1736 | } |
| 1737 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1738 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1739 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1740 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1741 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1742 | |
| 1743 | for (i = 0; i < set->used; ++i) { |
| 1744 | inverted = 0; |
| 1745 | change = 0; |
| 1746 | |
| 1747 | for (j = 1; j < set->used - i; ++j) { |
| 1748 | /* compare node positions */ |
| 1749 | if (inverted) { |
| 1750 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1751 | } else { |
| 1752 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1753 | } |
| 1754 | |
| 1755 | /* swap if needed */ |
| 1756 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1757 | change = 1; |
| 1758 | |
| 1759 | item = set->val.nodes[j - 1]; |
| 1760 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1761 | set->val.nodes[j] = item; |
| 1762 | } else { |
| 1763 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1764 | inverted = !inverted; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | ++ret; |
| 1769 | |
| 1770 | if (!change) { |
| 1771 | break; |
| 1772 | } |
| 1773 | } |
| 1774 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1775 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1776 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1777 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1778 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1779 | |
| 1780 | /* check node hashes */ |
| 1781 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1782 | assert(set->ht); |
| 1783 | for (i = 0; i < set->used; ++i) { |
| 1784 | hnode.node = set->val.nodes[i].node; |
| 1785 | hnode.type = set->val.nodes[i].type; |
| 1786 | |
| 1787 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1788 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1789 | hash = dict_hash_multi(hash, NULL, 0); |
| 1790 | |
| 1791 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | return ret - 1; |
| 1796 | } |
| 1797 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1798 | /** |
| 1799 | * @brief Merge 2 sorted sets into one. |
| 1800 | * |
| 1801 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1802 | * @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] | 1803 | * @return LY_ERR |
| 1804 | */ |
| 1805 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1806 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1807 | { |
| 1808 | uint32_t i, j, k, count, dup_count; |
| 1809 | int cmp; |
| 1810 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1811 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1812 | 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] | 1813 | return LY_EINVAL; |
| 1814 | } |
| 1815 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1816 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1817 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1818 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1819 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1820 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1821 | return LY_SUCCESS; |
| 1822 | } |
| 1823 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1824 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0143b68 | 2021-12-13 17:13:09 +0100 | [diff] [blame] | 1825 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1826 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1827 | |
| 1828 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1829 | 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] | 1830 | return LY_EINT; |
| 1831 | } |
| 1832 | |
| 1833 | #ifndef NDEBUG |
| 1834 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1835 | print_set_debug(trg); |
| 1836 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1837 | print_set_debug(src); |
| 1838 | #endif |
| 1839 | |
| 1840 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1841 | * will likely be wasted on them, too bad) */ |
| 1842 | if (trg->size - trg->used < src->used) { |
| 1843 | trg->size = trg->used + src->used; |
| 1844 | |
| 1845 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1846 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1847 | } |
| 1848 | |
| 1849 | i = 0; |
| 1850 | j = 0; |
| 1851 | count = 0; |
| 1852 | dup_count = 0; |
| 1853 | do { |
| 1854 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1855 | if (!cmp) { |
| 1856 | if (!count) { |
| 1857 | /* duplicate, just skip it */ |
| 1858 | ++i; |
| 1859 | ++j; |
| 1860 | } else { |
| 1861 | /* we are copying something already, so let's copy the duplicate too, |
| 1862 | * we are hoping that afterwards there are some more nodes to |
| 1863 | * copy and this way we can copy them all together */ |
| 1864 | ++count; |
| 1865 | ++dup_count; |
| 1866 | ++i; |
| 1867 | ++j; |
| 1868 | } |
| 1869 | } else if (cmp < 0) { |
| 1870 | /* inserting src node into trg, just remember it for now */ |
| 1871 | ++count; |
| 1872 | ++i; |
| 1873 | |
| 1874 | /* insert the hash now */ |
| 1875 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1876 | } else if (count) { |
| 1877 | copy_nodes: |
| 1878 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1879 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1880 | &trg->val.nodes[j], |
| 1881 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1882 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1883 | |
| 1884 | trg->used += count - dup_count; |
| 1885 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1886 | j += count - dup_count; |
| 1887 | |
| 1888 | count = 0; |
| 1889 | dup_count = 0; |
| 1890 | } else { |
| 1891 | ++j; |
| 1892 | } |
| 1893 | } while ((i < src->used) && (j < trg->used)); |
| 1894 | |
| 1895 | if ((i < src->used) || count) { |
| 1896 | /* insert all the hashes first */ |
| 1897 | for (k = i; k < src->used; ++k) { |
| 1898 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1899 | } |
| 1900 | |
| 1901 | /* loop ended, but we need to copy something at trg end */ |
| 1902 | count += src->used - i; |
| 1903 | i = src->used; |
| 1904 | goto copy_nodes; |
| 1905 | } |
| 1906 | |
| 1907 | /* we are inserting hashes before the actual node insert, which causes |
| 1908 | * situations when there were initially not enough items for a hash table, |
| 1909 | * but even after some were inserted, hash table was not created (during |
| 1910 | * insertion the number of items is not updated yet) */ |
| 1911 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1912 | set_insert_node_hash(trg, NULL, 0); |
| 1913 | } |
| 1914 | |
| 1915 | #ifndef NDEBUG |
| 1916 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1917 | print_set_debug(trg); |
| 1918 | #endif |
| 1919 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1920 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1921 | return LY_SUCCESS; |
| 1922 | } |
| 1923 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1924 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1925 | 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] | 1926 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1927 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1928 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1929 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1930 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1931 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1932 | } |
| 1933 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1934 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1935 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1936 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]), |
| 1937 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1938 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1939 | return LY_ENOT; |
| 1940 | } |
| 1941 | |
| 1942 | return LY_SUCCESS; |
| 1943 | } |
| 1944 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1945 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1946 | 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] | 1947 | { |
| 1948 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1949 | |
| 1950 | /* skip the token */ |
| 1951 | ++(*tok_idx); |
| 1952 | |
| 1953 | return LY_SUCCESS; |
| 1954 | } |
| 1955 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1956 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1957 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1958 | 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] | 1959 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1960 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1961 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1962 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1963 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1964 | } |
| 1965 | return LY_EINCOMPLETE; |
| 1966 | } |
| 1967 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1968 | 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] | 1969 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1970 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1971 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1972 | } |
| 1973 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | return LY_SUCCESS; |
| 1977 | } |
| 1978 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 1979 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1980 | 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] | 1981 | enum lyxp_token want_tok2) |
| 1982 | { |
| 1983 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 1984 | |
| 1985 | /* skip the token */ |
| 1986 | ++(*tok_idx); |
| 1987 | |
| 1988 | return LY_SUCCESS; |
| 1989 | } |
| 1990 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1991 | /** |
| 1992 | * @brief Stack operation push on the repeat array. |
| 1993 | * |
| 1994 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1995 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1996 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1997 | */ |
| 1998 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1999 | 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] | 2000 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2001 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2002 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2003 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2004 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2005 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 2006 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 2007 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 2008 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2009 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2010 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 2011 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 2012 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | /** |
| 2017 | * @brief Reparse Predicate. Logs directly on error. |
| 2018 | * |
| 2019 | * [7] Predicate ::= '[' Expr ']' |
| 2020 | * |
| 2021 | * @param[in] ctx Context for logging. |
| 2022 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2023 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2024 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2025 | * @return LY_ERR |
| 2026 | */ |
| 2027 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2028 | 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] | 2029 | { |
| 2030 | LY_ERR rc; |
| 2031 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2032 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2033 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2034 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2035 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2036 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2037 | LY_CHECK_RET(rc); |
| 2038 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2039 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2040 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2041 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2042 | |
| 2043 | return LY_SUCCESS; |
| 2044 | } |
| 2045 | |
| 2046 | /** |
| 2047 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 2048 | * |
| 2049 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 2050 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 2051 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 2052 | * |
| 2053 | * @param[in] ctx Context for logging. |
| 2054 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2055 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2056 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2057 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 2058 | */ |
| 2059 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2060 | 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] | 2061 | { |
| 2062 | LY_ERR rc; |
| 2063 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2064 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2065 | LY_CHECK_RET(rc); |
| 2066 | |
| 2067 | goto step; |
| 2068 | do { |
| 2069 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2070 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2072 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2073 | LY_CHECK_RET(rc); |
| 2074 | step: |
| 2075 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2076 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2077 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2078 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2079 | break; |
| 2080 | |
| 2081 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2082 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2083 | break; |
| 2084 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2085 | case LYXP_TOKEN_AXISNAME: |
| 2086 | ++(*tok_idx); |
| 2087 | |
| 2088 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON); |
| 2089 | LY_CHECK_RET(rc); |
| 2090 | |
| 2091 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2092 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2093 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2094 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2097 | 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] | 2098 | 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] | 2099 | return LY_EVALID; |
| 2100 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2101 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) { |
| 2102 | goto reparse_nodetype; |
| 2103 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2104 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2107 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | |
| 2109 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2110 | reparse_nodetype: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2111 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2112 | |
| 2113 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2114 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2115 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | |
| 2118 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | |
| 2123 | reparse_predicate: |
| 2124 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2125 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2126 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2127 | LY_CHECK_RET(rc); |
| 2128 | } |
| 2129 | break; |
| 2130 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2131 | 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] | 2132 | return LY_EVALID; |
| 2133 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | } 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] | 2135 | |
| 2136 | return LY_SUCCESS; |
| 2137 | } |
| 2138 | |
| 2139 | /** |
| 2140 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2141 | * |
| 2142 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2143 | * |
| 2144 | * @param[in] ctx Context for logging. |
| 2145 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2147 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2148 | * @return LY_ERR |
| 2149 | */ |
| 2150 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2151 | 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] | 2152 | { |
| 2153 | LY_ERR rc; |
| 2154 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2155 | 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] | 2156 | |
| 2157 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2158 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2159 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2162 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2163 | return LY_SUCCESS; |
| 2164 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2165 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2166 | case LYXP_TOKEN_DOT: |
| 2167 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2168 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2169 | case LYXP_TOKEN_AT: |
| 2170 | case LYXP_TOKEN_NAMETEST: |
| 2171 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2172 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2173 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2174 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2175 | default: |
| 2176 | break; |
| 2177 | } |
| 2178 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2179 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2180 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2181 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2182 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2183 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2184 | LY_CHECK_RET(rc); |
| 2185 | } |
| 2186 | |
| 2187 | return LY_SUCCESS; |
| 2188 | } |
| 2189 | |
| 2190 | /** |
| 2191 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2192 | * |
| 2193 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2194 | * |
| 2195 | * @param[in] ctx Context for logging. |
| 2196 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2197 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2198 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2199 | * @return LY_ERR |
| 2200 | */ |
| 2201 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2202 | 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] | 2203 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2204 | int8_t min_arg_count = -1; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2205 | uint32_t arg_count, max_arg_count = 0, func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2206 | LY_ERR rc; |
| 2207 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2208 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2209 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2210 | func_tok_idx = *tok_idx; |
| 2211 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2213 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2214 | min_arg_count = 1; |
| 2215 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2216 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2217 | min_arg_count = 1; |
| 2218 | max_arg_count = 1; |
| 2219 | } |
| 2220 | break; |
| 2221 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2222 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2223 | min_arg_count = 1; |
| 2224 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2225 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2226 | min_arg_count = 0; |
| 2227 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2228 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2229 | min_arg_count = 0; |
| 2230 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2231 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2232 | min_arg_count = 0; |
| 2233 | max_arg_count = 0; |
| 2234 | } |
| 2235 | break; |
| 2236 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2237 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2238 | min_arg_count = 1; |
| 2239 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2240 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2241 | min_arg_count = 0; |
| 2242 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2243 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2244 | min_arg_count = 1; |
| 2245 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2246 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2247 | min_arg_count = 1; |
| 2248 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2249 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2250 | min_arg_count = 1; |
| 2251 | max_arg_count = 1; |
| 2252 | } |
| 2253 | break; |
| 2254 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2255 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2256 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2257 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2258 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2259 | min_arg_count = 0; |
| 2260 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2261 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2262 | min_arg_count = 0; |
| 2263 | max_arg_count = 1; |
| 2264 | } |
| 2265 | break; |
| 2266 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2267 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2268 | min_arg_count = 1; |
| 2269 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2270 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2271 | min_arg_count = 1; |
| 2272 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2273 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2274 | min_arg_count = 0; |
| 2275 | max_arg_count = 0; |
| 2276 | } |
| 2277 | break; |
| 2278 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2279 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2280 | min_arg_count = 2; |
| 2281 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2282 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2283 | min_arg_count = 0; |
| 2284 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2285 | } 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] | 2286 | min_arg_count = 2; |
| 2287 | max_arg_count = 2; |
| 2288 | } |
| 2289 | break; |
| 2290 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2291 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2292 | min_arg_count = 2; |
| 2293 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2294 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2295 | min_arg_count = 3; |
| 2296 | max_arg_count = 3; |
| 2297 | } |
| 2298 | break; |
| 2299 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2300 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2301 | min_arg_count = 0; |
| 2302 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2303 | } 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] | 2304 | min_arg_count = 1; |
| 2305 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2306 | } 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] | 2307 | min_arg_count = 2; |
| 2308 | max_arg_count = 2; |
| 2309 | } |
| 2310 | break; |
| 2311 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2312 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2313 | min_arg_count = 2; |
| 2314 | max_arg_count = 2; |
| 2315 | } |
| 2316 | break; |
| 2317 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2318 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2319 | min_arg_count = 2; |
| 2320 | max_arg_count = 2; |
| 2321 | } |
| 2322 | break; |
| 2323 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2324 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2325 | min_arg_count = 0; |
| 2326 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2327 | } 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] | 2328 | min_arg_count = 0; |
| 2329 | max_arg_count = 1; |
| 2330 | } |
| 2331 | break; |
| 2332 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2333 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2334 | min_arg_count = 0; |
| 2335 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2336 | } 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] | 2337 | min_arg_count = 2; |
| 2338 | max_arg_count = 2; |
| 2339 | } |
| 2340 | break; |
| 2341 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2342 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2343 | min_arg_count = 2; |
| 2344 | max_arg_count = 2; |
| 2345 | } |
| 2346 | break; |
| 2347 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2348 | 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] | 2349 | min_arg_count = 2; |
| 2350 | max_arg_count = 2; |
| 2351 | } |
| 2352 | break; |
| 2353 | } |
| 2354 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2355 | 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] | 2356 | return LY_EINVAL; |
| 2357 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2358 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2359 | |
| 2360 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2361 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2362 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2363 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2364 | |
| 2365 | /* ( Expr ( ',' Expr )* )? */ |
| 2366 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2367 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2368 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2369 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2370 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2371 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2372 | LY_CHECK_RET(rc); |
| 2373 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2374 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2375 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2376 | |
| 2377 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2378 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2379 | LY_CHECK_RET(rc); |
| 2380 | } |
| 2381 | |
| 2382 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2383 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2384 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2385 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2386 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2387 | 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] | 2388 | 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] | 2389 | return LY_EVALID; |
| 2390 | } |
| 2391 | |
| 2392 | return LY_SUCCESS; |
| 2393 | } |
| 2394 | |
| 2395 | /** |
| 2396 | * @brief Reparse PathExpr. Logs directly on error. |
| 2397 | * |
| 2398 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2399 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2400 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2401 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2402 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2403 | * |
| 2404 | * @param[in] ctx Context for logging. |
| 2405 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2406 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2407 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2408 | * @return LY_ERR |
| 2409 | */ |
| 2410 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2411 | 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] | 2412 | { |
| 2413 | LY_ERR rc; |
| 2414 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2415 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2416 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2419 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2420 | case LYXP_TOKEN_PAR1: |
| 2421 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2422 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2423 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2424 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2425 | LY_CHECK_RET(rc); |
| 2426 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2427 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2428 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2429 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2430 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2431 | case LYXP_TOKEN_DOT: |
| 2432 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2433 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2434 | case LYXP_TOKEN_AT: |
| 2435 | case LYXP_TOKEN_NAMETEST: |
| 2436 | case LYXP_TOKEN_NODETYPE: |
| 2437 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2438 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2439 | LY_CHECK_RET(rc); |
| 2440 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2441 | case LYXP_TOKEN_VARREF: |
| 2442 | /* VariableReference */ |
| 2443 | ++(*tok_idx); |
| 2444 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2445 | case LYXP_TOKEN_FUNCNAME: |
| 2446 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2447 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2448 | LY_CHECK_RET(rc); |
| 2449 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2450 | case LYXP_TOKEN_OPER_PATH: |
| 2451 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2452 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2453 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2454 | LY_CHECK_RET(rc); |
| 2455 | break; |
| 2456 | case LYXP_TOKEN_LITERAL: |
| 2457 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2458 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2459 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2460 | case LYXP_TOKEN_NUMBER: |
| 2461 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2462 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2463 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2465 | 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] | 2466 | return LY_EVALID; |
| 2467 | } |
| 2468 | |
| 2469 | return LY_SUCCESS; |
| 2470 | |
| 2471 | predicate: |
| 2472 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2473 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2474 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2475 | LY_CHECK_RET(rc); |
| 2476 | } |
| 2477 | |
| 2478 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2479 | 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] | 2480 | |
| 2481 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2482 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2483 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2484 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2485 | LY_CHECK_RET(rc); |
| 2486 | } |
| 2487 | |
| 2488 | return LY_SUCCESS; |
| 2489 | } |
| 2490 | |
| 2491 | /** |
| 2492 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2493 | * |
| 2494 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2495 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2496 | * |
| 2497 | * @param[in] ctx Context for logging. |
| 2498 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2499 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2500 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2501 | * @return LY_ERR |
| 2502 | */ |
| 2503 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2504 | 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] | 2505 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2506 | uint32_t prev_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2507 | LY_ERR rc; |
| 2508 | |
| 2509 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2510 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2511 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2512 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2513 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2514 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2518 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2519 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2520 | LY_CHECK_RET(rc); |
| 2521 | |
| 2522 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2523 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2524 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2525 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2526 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2527 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2528 | LY_CHECK_RET(rc); |
| 2529 | } |
| 2530 | |
| 2531 | return LY_SUCCESS; |
| 2532 | } |
| 2533 | |
| 2534 | /** |
| 2535 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2536 | * |
| 2537 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2538 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2539 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2540 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2541 | * | MultiplicativeExpr '*' UnaryExpr |
| 2542 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2543 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2544 | * |
| 2545 | * @param[in] ctx Context for logging. |
| 2546 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2547 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2548 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2549 | * @return LY_ERR |
| 2550 | */ |
| 2551 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2552 | 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] | 2553 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2554 | uint32_t prev_add_exp, prev_mul_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2555 | LY_ERR rc; |
| 2556 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2557 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2558 | goto reparse_multiplicative_expr; |
| 2559 | |
| 2560 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2561 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2562 | ((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] | 2563 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2564 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2565 | |
| 2566 | reparse_multiplicative_expr: |
| 2567 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2568 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2569 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2570 | LY_CHECK_RET(rc); |
| 2571 | |
| 2572 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2573 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2574 | ((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] | 2575 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2576 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2577 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2578 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2579 | LY_CHECK_RET(rc); |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | return LY_SUCCESS; |
| 2584 | } |
| 2585 | |
| 2586 | /** |
| 2587 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2588 | * |
| 2589 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2590 | * | EqualityExpr '!=' RelationalExpr |
| 2591 | * [14] RelationalExpr ::= AdditiveExpr |
| 2592 | * | RelationalExpr '<' AdditiveExpr |
| 2593 | * | RelationalExpr '>' AdditiveExpr |
| 2594 | * | RelationalExpr '<=' AdditiveExpr |
| 2595 | * | RelationalExpr '>=' AdditiveExpr |
| 2596 | * |
| 2597 | * @param[in] ctx Context for logging. |
| 2598 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2599 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2600 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2601 | * @return LY_ERR |
| 2602 | */ |
| 2603 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2604 | 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] | 2605 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2606 | uint32_t prev_eq_exp, prev_rel_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2607 | LY_ERR rc; |
| 2608 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2609 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2610 | goto reparse_additive_expr; |
| 2611 | |
| 2612 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2613 | 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] | 2614 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2615 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2616 | |
| 2617 | reparse_additive_expr: |
| 2618 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2619 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2620 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2621 | LY_CHECK_RET(rc); |
| 2622 | |
| 2623 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2624 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2625 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2626 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2627 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2628 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2629 | LY_CHECK_RET(rc); |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | return LY_SUCCESS; |
| 2634 | } |
| 2635 | |
| 2636 | /** |
| 2637 | * @brief Reparse OrExpr. Logs directly on error. |
| 2638 | * |
| 2639 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2640 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2641 | * |
| 2642 | * @param[in] ctx Context for logging. |
| 2643 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2644 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2645 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2646 | * @return LY_ERR |
| 2647 | */ |
| 2648 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2649 | 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] | 2650 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2651 | uint32_t prev_or_exp, prev_and_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2652 | LY_ERR rc; |
| 2653 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2654 | ++depth; |
| 2655 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2656 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2657 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2658 | goto reparse_equality_expr; |
| 2659 | |
| 2660 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2661 | 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] | 2662 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2663 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2664 | |
| 2665 | reparse_equality_expr: |
| 2666 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2667 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2668 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2669 | LY_CHECK_RET(rc); |
| 2670 | |
| 2671 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2672 | 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] | 2673 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2674 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2675 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2676 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2677 | LY_CHECK_RET(rc); |
| 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | return LY_SUCCESS; |
| 2682 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2683 | |
| 2684 | /** |
| 2685 | * @brief Parse NCName. |
| 2686 | * |
| 2687 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2688 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2689 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2690 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2691 | parse_ncname(const char *ncname) |
| 2692 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2693 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2694 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2695 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2696 | |
| 2697 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2698 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2699 | return len; |
| 2700 | } |
| 2701 | |
| 2702 | do { |
| 2703 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2704 | if (!*ncname) { |
| 2705 | break; |
| 2706 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2707 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2708 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2709 | |
| 2710 | return len; |
| 2711 | } |
| 2712 | |
| 2713 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2714 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2715 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2716 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2717 | * @param[in] exp Expression to use. |
| 2718 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2719 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2720 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2721 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2722 | */ |
| 2723 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2724 | 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] | 2725 | { |
| 2726 | uint32_t prev; |
| 2727 | |
| 2728 | if (exp->used == exp->size) { |
| 2729 | prev = exp->size; |
| 2730 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2731 | if (prev > exp->size) { |
| 2732 | LOGINT(ctx); |
| 2733 | return LY_EINT; |
| 2734 | } |
| 2735 | |
| 2736 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2737 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2738 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2739 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2740 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2741 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2742 | } |
| 2743 | |
| 2744 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2745 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2746 | exp->tok_len[exp->used] = tok_len; |
| 2747 | ++exp->used; |
| 2748 | return LY_SUCCESS; |
| 2749 | } |
| 2750 | |
| 2751 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2752 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2753 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2754 | uint32_t i; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2755 | |
| 2756 | if (!expr) { |
| 2757 | return; |
| 2758 | } |
| 2759 | |
| 2760 | lydict_remove(ctx, expr->expr); |
| 2761 | free(expr->tokens); |
| 2762 | free(expr->tok_pos); |
| 2763 | free(expr->tok_len); |
| 2764 | if (expr->repeat) { |
| 2765 | for (i = 0; i < expr->used; ++i) { |
| 2766 | free(expr->repeat[i]); |
| 2767 | } |
| 2768 | } |
| 2769 | free(expr->repeat); |
| 2770 | free(expr); |
| 2771 | } |
| 2772 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2773 | /** |
| 2774 | * @brief Parse Axis name. |
| 2775 | * |
| 2776 | * @param[in] str String to parse. |
| 2777 | * @param[in] str_len Length of @p str. |
| 2778 | * @return LY_SUCCESS if an axis. |
| 2779 | * @return LY_ENOT otherwise. |
| 2780 | */ |
| 2781 | static LY_ERR |
| 2782 | expr_parse_axis(const char *str, size_t str_len) |
| 2783 | { |
| 2784 | switch (str_len) { |
| 2785 | case 4: |
| 2786 | if (!strncmp("self", str, str_len)) { |
| 2787 | return LY_SUCCESS; |
| 2788 | } |
| 2789 | break; |
| 2790 | case 5: |
| 2791 | if (!strncmp("child", str, str_len)) { |
| 2792 | return LY_SUCCESS; |
| 2793 | } |
| 2794 | break; |
| 2795 | case 6: |
| 2796 | if (!strncmp("parent", str, str_len)) { |
| 2797 | return LY_SUCCESS; |
| 2798 | } |
| 2799 | break; |
| 2800 | case 8: |
| 2801 | if (!strncmp("ancestor", str, str_len)) { |
| 2802 | return LY_SUCCESS; |
| 2803 | } |
| 2804 | break; |
| 2805 | case 9: |
| 2806 | if (!strncmp("attribute", str, str_len)) { |
| 2807 | return LY_SUCCESS; |
| 2808 | } else if (!strncmp("following", str, str_len)) { |
| 2809 | return LY_SUCCESS; |
| 2810 | } else if (!strncmp("namespace", str, str_len)) { |
| 2811 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2812 | return LY_ENOT; |
| 2813 | } else if (!strncmp("preceding", str, str_len)) { |
| 2814 | return LY_SUCCESS; |
| 2815 | } |
| 2816 | break; |
| 2817 | case 10: |
| 2818 | if (!strncmp("descendant", str, str_len)) { |
| 2819 | return LY_SUCCESS; |
| 2820 | } |
| 2821 | break; |
| 2822 | case 16: |
| 2823 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2824 | return LY_SUCCESS; |
| 2825 | } |
| 2826 | break; |
| 2827 | case 17: |
| 2828 | if (!strncmp("following-sibling", str, str_len)) { |
| 2829 | return LY_SUCCESS; |
| 2830 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2831 | return LY_SUCCESS; |
| 2832 | } |
| 2833 | break; |
| 2834 | case 18: |
| 2835 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2836 | return LY_SUCCESS; |
| 2837 | } |
| 2838 | break; |
| 2839 | } |
| 2840 | |
| 2841 | return LY_ENOT; |
| 2842 | } |
| 2843 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2844 | LY_ERR |
| 2845 | 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] | 2846 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2847 | LY_ERR ret = LY_SUCCESS; |
| 2848 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2849 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2850 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2851 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2852 | uint32_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2853 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2854 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2855 | assert(expr_p); |
| 2856 | |
| 2857 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2858 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2859 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2863 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2864 | } |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2865 | if (expr_len > UINT32_MAX) { |
| 2866 | 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] | 2867 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2871 | expr = calloc(1, sizeof *expr); |
| 2872 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2873 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2874 | expr->used = 0; |
| 2875 | expr->size = LYXP_EXPR_SIZE_START; |
| 2876 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2877 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2878 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2879 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2880 | 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] | 2881 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2882 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2883 | 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] | 2884 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2885 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2886 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2887 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2888 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2889 | ++parsed; |
| 2890 | } |
| 2891 | |
| 2892 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2893 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2894 | |
| 2895 | /* '(' */ |
| 2896 | tok_len = 1; |
| 2897 | tok_type = LYXP_TOKEN_PAR1; |
| 2898 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2899 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2900 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2901 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2902 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2903 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2904 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2905 | /* it is NodeType after all */ |
| 2906 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2907 | |
| 2908 | prev_ntype_check = 0; |
| 2909 | prev_func_check = 0; |
| 2910 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2911 | /* it is FunctionName after all */ |
| 2912 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2913 | |
| 2914 | prev_ntype_check = 0; |
| 2915 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2916 | } |
| 2917 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2918 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2919 | |
| 2920 | /* ')' */ |
| 2921 | tok_len = 1; |
| 2922 | tok_type = LYXP_TOKEN_PAR2; |
| 2923 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2924 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2925 | |
| 2926 | /* '[' */ |
| 2927 | tok_len = 1; |
| 2928 | tok_type = LYXP_TOKEN_BRACK1; |
| 2929 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2930 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2931 | |
| 2932 | /* ']' */ |
| 2933 | tok_len = 1; |
| 2934 | tok_type = LYXP_TOKEN_BRACK2; |
| 2935 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2936 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2937 | |
| 2938 | /* '..' */ |
| 2939 | tok_len = 2; |
| 2940 | tok_type = LYXP_TOKEN_DDOT; |
| 2941 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2942 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2943 | |
| 2944 | /* '.' */ |
| 2945 | tok_len = 1; |
| 2946 | tok_type = LYXP_TOKEN_DOT; |
| 2947 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2948 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2949 | |
| 2950 | /* '@' */ |
| 2951 | tok_len = 1; |
| 2952 | tok_type = LYXP_TOKEN_AT; |
| 2953 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2954 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2955 | |
| 2956 | /* ',' */ |
| 2957 | tok_len = 1; |
| 2958 | tok_type = LYXP_TOKEN_COMMA; |
| 2959 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2960 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2961 | |
| 2962 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2963 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2964 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2965 | 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] | 2966 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2967 | ++tok_len; |
| 2968 | tok_type = LYXP_TOKEN_LITERAL; |
| 2969 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2970 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2971 | |
| 2972 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2973 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2974 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2975 | 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] | 2976 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2977 | ++tok_len; |
| 2978 | tok_type = LYXP_TOKEN_LITERAL; |
| 2979 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2980 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2981 | |
| 2982 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2983 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2984 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2985 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2986 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2987 | } |
| 2988 | tok_type = LYXP_TOKEN_NUMBER; |
| 2989 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2990 | } else if (expr_str[parsed] == '$') { |
| 2991 | |
| 2992 | /* VariableReference */ |
| 2993 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2994 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2995 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 2996 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 2997 | tok_len = ncname_len; |
| 2998 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 2999 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 3000 | error); |
| 3001 | tok_type = LYXP_TOKEN_VARREF; |
| 3002 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3003 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3004 | |
| 3005 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3006 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3007 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3008 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3009 | } else { |
| 3010 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3011 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3012 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3013 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3014 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3015 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3016 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3017 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3018 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 3019 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3020 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3021 | |
| 3022 | /* Operator '<=', '>=' */ |
| 3023 | tok_len = 2; |
| 3024 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3025 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3026 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3027 | |
| 3028 | /* Operator '|' */ |
| 3029 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3030 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3031 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3032 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3033 | |
| 3034 | /* Operator '+', '-' */ |
| 3035 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3036 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3037 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3038 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3039 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3040 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3041 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3042 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3043 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3044 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3045 | |
| 3046 | /* Operator '<', '>' */ |
| 3047 | tok_len = 1; |
| 3048 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3049 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3050 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3051 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3052 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3053 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3054 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3055 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3056 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3057 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3058 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3059 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3060 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3061 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3062 | |
| 3063 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3064 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3065 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3066 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3067 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3068 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3069 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3070 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3071 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3072 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3073 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3074 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3075 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3076 | } 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] | 3077 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3078 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3079 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3080 | } else if (prev_ntype_check || prev_func_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3081 | 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] | 3082 | 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] | 3083 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3084 | goto error; |
| 3085 | } else { |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3086 | 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] | 3087 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3088 | goto error; |
| 3089 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3090 | } else { |
| 3091 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3092 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3093 | if (expr_str[parsed] == '*') { |
| 3094 | ncname_len = 1; |
| 3095 | } else { |
| 3096 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3097 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3098 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3099 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3100 | tok_len = ncname_len; |
| 3101 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3102 | has_axis = 0; |
| 3103 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3104 | /* axis */ |
| 3105 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
| 3106 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error); |
| 3107 | tok_type = LYXP_TOKEN_AXISNAME; |
| 3108 | |
| 3109 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3110 | parsed += tok_len; |
| 3111 | |
| 3112 | /* '::' */ |
| 3113 | tok_len = 2; |
| 3114 | tok_type = LYXP_TOKEN_DCOLON; |
| 3115 | |
| 3116 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3117 | parsed += tok_len; |
| 3118 | |
| 3119 | if (expr_str[parsed] == '*') { |
| 3120 | ncname_len = 1; |
| 3121 | } else { |
| 3122 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3123 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3124 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3125 | } |
| 3126 | tok_len = ncname_len; |
| 3127 | |
| 3128 | has_axis = 1; |
| 3129 | } |
| 3130 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3131 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3132 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3133 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3134 | ++tok_len; |
| 3135 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3136 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 3137 | 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] | 3138 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3139 | tok_len += ncname_len; |
| 3140 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3141 | /* remove old flags to prevent ambiguities */ |
| 3142 | prev_ntype_check = 0; |
| 3143 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3144 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3145 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3146 | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 3147 | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
| 3148 | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3149 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3154 | 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] | 3155 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3156 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3157 | ++parsed; |
| 3158 | } |
| 3159 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3160 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3161 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3162 | if (reparse) { |
| 3163 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3164 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 3165 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3166 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3167 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 3168 | 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] | 3169 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3170 | 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] | 3171 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3172 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3173 | goto error; |
| 3174 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3175 | } |
| 3176 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3177 | print_expr_struct_debug(expr); |
| 3178 | *expr_p = expr; |
| 3179 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3180 | |
| 3181 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3182 | lyxp_expr_free(ctx, expr); |
| 3183 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3184 | } |
| 3185 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3186 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3187 | 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] | 3188 | struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3189 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3190 | LY_ERR ret = LY_SUCCESS; |
| 3191 | struct lyxp_expr *dup = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3192 | uint32_t used = 0, i, j, k, expr_len; |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3193 | const char *expr_start; |
| 3194 | |
| 3195 | 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] | 3196 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3197 | if (!exp) { |
| 3198 | goto cleanup; |
| 3199 | } |
| 3200 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3201 | if (!start_idx && !end_idx) { |
| 3202 | end_idx = exp->used - 1; |
| 3203 | } |
| 3204 | |
| 3205 | expr_start = exp->expr + exp->tok_pos[start_idx]; |
| 3206 | expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx]; |
| 3207 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3208 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3209 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3210 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3211 | if (exp->used) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3212 | used = (end_idx - start_idx) + 1; |
| 3213 | |
| 3214 | dup->tokens = malloc(used * sizeof *dup->tokens); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3215 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3216 | memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3217 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3218 | dup->tok_pos = malloc(used * sizeof *dup->tok_pos); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3219 | 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] | 3220 | 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] | 3221 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3222 | if (start_idx) { |
| 3223 | /* fix the indices in the expression */ |
| 3224 | for (i = 0; i < used; ++i) { |
| 3225 | dup->tok_pos[i] -= expr_start - exp->expr; |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | dup->tok_len = malloc(used * sizeof *dup->tok_len); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3230 | 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] | 3231 | 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] | 3232 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3233 | if (exp->repeat) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3234 | dup->repeat = malloc(used * sizeof *dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3235 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3236 | for (i = start_idx; i <= end_idx; ++i) { |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3237 | if (!exp->repeat[i]) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3238 | dup->repeat[i - start_idx] = NULL; |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3239 | } else { |
| 3240 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3241 | /* the ending 0 as well */ |
| 3242 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3243 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3244 | dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat); |
| 3245 | LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3246 | memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat); |
| 3247 | dup->repeat[i - start_idx][j - 1] = 0; |
| 3248 | |
| 3249 | if (start_idx) { |
| 3250 | /* fix the indices in the tokens */ |
| 3251 | for (k = 0; k < j; ++k) { |
| 3252 | dup->repeat[i - start_idx][k] -= start_idx; |
| 3253 | } |
| 3254 | } |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3255 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3256 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3257 | } |
| 3258 | } |
| 3259 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3260 | dup->used = used; |
| 3261 | dup->size = used; |
| 3262 | |
| 3263 | /* copy only subexpression */ |
| 3264 | 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] | 3265 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3266 | cleanup: |
| 3267 | if (ret) { |
| 3268 | lyxp_expr_free(ctx, dup); |
| 3269 | } else { |
| 3270 | *dup_p = dup; |
| 3271 | } |
| 3272 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3273 | } |
| 3274 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3275 | /** |
| 3276 | * @brief Get the last-added schema node that is currently in the context. |
| 3277 | * |
| 3278 | * @param[in] set Set to search in. |
| 3279 | * @return Last-added schema context node, NULL if no node is in context. |
| 3280 | */ |
| 3281 | static struct lysc_node * |
| 3282 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3283 | { |
| 3284 | uint32_t i; |
| 3285 | |
| 3286 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3287 | return NULL; |
| 3288 | } |
| 3289 | |
| 3290 | i = set->used; |
| 3291 | do { |
| 3292 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3293 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3294 | /* if there are more, simply return the first found (last added) */ |
| 3295 | return set->val.scnodes[i].scnode; |
| 3296 | } |
| 3297 | } while (i); |
| 3298 | |
| 3299 | return NULL; |
| 3300 | } |
| 3301 | |
| 3302 | /** |
| 3303 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3304 | * |
| 3305 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3306 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3307 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3308 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3309 | warn_is_numeric_type(struct lysc_type *type) |
| 3310 | { |
| 3311 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3312 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3313 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3314 | |
| 3315 | switch (type->basetype) { |
| 3316 | case LY_TYPE_DEC64: |
| 3317 | case LY_TYPE_INT8: |
| 3318 | case LY_TYPE_UINT8: |
| 3319 | case LY_TYPE_INT16: |
| 3320 | case LY_TYPE_UINT16: |
| 3321 | case LY_TYPE_INT32: |
| 3322 | case LY_TYPE_UINT32: |
| 3323 | case LY_TYPE_INT64: |
| 3324 | case LY_TYPE_UINT64: |
| 3325 | return 1; |
| 3326 | case LY_TYPE_UNION: |
| 3327 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3328 | LY_ARRAY_FOR(uni->types, u) { |
| 3329 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3330 | if (ret) { |
| 3331 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3332 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3333 | } |
| 3334 | } |
| 3335 | /* did not find any suitable type */ |
| 3336 | return 0; |
| 3337 | case LY_TYPE_LEAFREF: |
| 3338 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3339 | default: |
| 3340 | return 0; |
| 3341 | } |
| 3342 | } |
| 3343 | |
| 3344 | /** |
| 3345 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3346 | * |
| 3347 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3348 | * @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] | 3349 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3350 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3351 | warn_is_string_type(struct lysc_type *type) |
| 3352 | { |
| 3353 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3354 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3355 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3356 | |
| 3357 | switch (type->basetype) { |
| 3358 | case LY_TYPE_BITS: |
| 3359 | case LY_TYPE_ENUM: |
| 3360 | case LY_TYPE_IDENT: |
| 3361 | case LY_TYPE_INST: |
| 3362 | case LY_TYPE_STRING: |
| 3363 | return 1; |
| 3364 | case LY_TYPE_UNION: |
| 3365 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3366 | LY_ARRAY_FOR(uni->types, u) { |
| 3367 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3368 | if (ret) { |
| 3369 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3370 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3371 | } |
| 3372 | } |
| 3373 | /* did not find any suitable type */ |
| 3374 | return 0; |
| 3375 | case LY_TYPE_LEAFREF: |
| 3376 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3377 | default: |
| 3378 | return 0; |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | /** |
| 3383 | * @brief Test whether a type is one specific type. |
| 3384 | * |
| 3385 | * @param[in] type Type to test. |
| 3386 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3387 | * @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] | 3388 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3389 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3390 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3391 | { |
| 3392 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3393 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3394 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3395 | |
| 3396 | if (type->basetype == base) { |
| 3397 | return 1; |
| 3398 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3399 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3400 | LY_ARRAY_FOR(uni->types, u) { |
| 3401 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3402 | if (ret) { |
| 3403 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3404 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3405 | } |
| 3406 | } |
| 3407 | /* did not find any suitable type */ |
| 3408 | return 0; |
| 3409 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3410 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3411 | } |
| 3412 | |
| 3413 | return 0; |
| 3414 | } |
| 3415 | |
| 3416 | /** |
| 3417 | * @brief Get next type of a (union) type. |
| 3418 | * |
| 3419 | * @param[in] type Base type. |
| 3420 | * @param[in] prev_type Previously returned type. |
| 3421 | * @return Next type or NULL. |
| 3422 | */ |
| 3423 | static struct lysc_type * |
| 3424 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3425 | { |
| 3426 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3427 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3428 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3429 | |
| 3430 | switch (type->basetype) { |
| 3431 | case LY_TYPE_UNION: |
| 3432 | uni = (struct lysc_type_union *)type; |
| 3433 | if (!prev_type) { |
| 3434 | return uni->types[0]; |
| 3435 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3436 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3437 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3438 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3439 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3440 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3441 | found = 1; |
| 3442 | } |
| 3443 | } |
| 3444 | return NULL; |
| 3445 | default: |
| 3446 | if (prev_type) { |
| 3447 | assert(type == prev_type); |
| 3448 | return NULL; |
| 3449 | } else { |
| 3450 | return type; |
| 3451 | } |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | /** |
| 3456 | * @brief Test whether 2 types have a common type. |
| 3457 | * |
| 3458 | * @param[in] type1 First type. |
| 3459 | * @param[in] type2 Second type. |
| 3460 | * @return 1 if they do, 0 otherwise. |
| 3461 | */ |
| 3462 | static int |
| 3463 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3464 | { |
| 3465 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3466 | |
| 3467 | t1 = NULL; |
| 3468 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3469 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3470 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3471 | } else { |
| 3472 | rt1 = t1; |
| 3473 | } |
| 3474 | |
| 3475 | t2 = NULL; |
| 3476 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3477 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3478 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3479 | } else { |
| 3480 | rt2 = t2; |
| 3481 | } |
| 3482 | |
| 3483 | if (rt2->basetype == rt1->basetype) { |
| 3484 | /* match found */ |
| 3485 | return 1; |
| 3486 | } |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | return 0; |
| 3491 | } |
| 3492 | |
| 3493 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3494 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3495 | * |
| 3496 | * @param[in] ctx Context for logging. |
| 3497 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3498 | * @param[in] subexpr Subexpression start. |
| 3499 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3500 | * @param[in] cur_scnode Expression context node. |
| 3501 | */ |
| 3502 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3503 | 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] | 3504 | const struct lysc_node *cur_scnode) |
| 3505 | { |
| 3506 | char *path; |
| 3507 | |
| 3508 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3509 | 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] | 3510 | tok_pos, subexpr_len, subexpr, path); |
| 3511 | free(path); |
| 3512 | } |
| 3513 | |
| 3514 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3515 | * @brief Check both operands of comparison operators. |
| 3516 | * |
| 3517 | * @param[in] ctx Context for errors. |
| 3518 | * @param[in] set1 First operand set. |
| 3519 | * @param[in] set2 Second operand set. |
| 3520 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3521 | * @param[in] expr Start of the expression to print with the warning. |
| 3522 | * @param[in] tok_pos Token position. |
| 3523 | */ |
| 3524 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3525 | 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] | 3526 | uint32_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3527 | { |
| 3528 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3529 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3530 | |
| 3531 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3532 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3533 | |
| 3534 | if (!node1 && !node2) { |
| 3535 | /* no node-sets involved, nothing to do */ |
| 3536 | return; |
| 3537 | } |
| 3538 | |
| 3539 | if (node1) { |
| 3540 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3541 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3542 | warning = 1; |
| 3543 | leaves = 0; |
| 3544 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3545 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3546 | warning = 1; |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | if (node2) { |
| 3551 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3552 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3553 | warning = 1; |
| 3554 | leaves = 0; |
| 3555 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3556 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3557 | warning = 1; |
| 3558 | } |
| 3559 | } |
| 3560 | |
| 3561 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3562 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3563 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3564 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3565 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3566 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3567 | warning = 1; |
| 3568 | } |
| 3569 | } |
| 3570 | |
| 3571 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3572 | 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] | 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | /** |
| 3577 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3578 | * |
| 3579 | * @param[in] exp Parsed XPath expression. |
| 3580 | * @param[in] set Set with the leaf/leaf-list. |
| 3581 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3582 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3583 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3584 | */ |
| 3585 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3586 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp, |
| 3587 | uint32_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3588 | { |
| 3589 | struct lysc_node *scnode; |
| 3590 | struct lysc_type *type; |
| 3591 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3592 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3593 | LY_ERR rc; |
| 3594 | struct ly_err_item *err = NULL; |
| 3595 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3596 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3597 | ((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] | 3598 | /* check that the node can have the specified value */ |
| 3599 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3600 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3601 | } else { |
| 3602 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3603 | } |
| 3604 | if (!value) { |
| 3605 | LOGMEM(set->ctx); |
| 3606 | return; |
| 3607 | } |
| 3608 | |
| 3609 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3610 | 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] | 3611 | " 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] | 3612 | 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] | 3613 | (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] | 3614 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3618 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3619 | 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] | 3620 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3621 | if (rc == LY_EINCOMPLETE) { |
| 3622 | rc = LY_SUCCESS; |
| 3623 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3624 | |
| 3625 | if (err) { |
| 3626 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3627 | ly_err_free(err); |
| 3628 | } else if (rc != LY_SUCCESS) { |
| 3629 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3630 | } |
| 3631 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3632 | 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] | 3633 | (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] | 3634 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3635 | } else { |
| 3636 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | free(value); |
| 3640 | } |
| 3641 | } |
| 3642 | |
| 3643 | /* |
| 3644 | * XPath functions |
| 3645 | */ |
| 3646 | |
| 3647 | /** |
| 3648 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3649 | * depending on whether the first node bit value from the second argument is set. |
| 3650 | * |
| 3651 | * @param[in] args Array of arguments. |
| 3652 | * @param[in] arg_count Count of elements in @p args. |
| 3653 | * @param[in,out] set Context and result set at the same time. |
| 3654 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3655 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3656 | */ |
| 3657 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3658 | 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] | 3659 | { |
| 3660 | struct lyd_node_term *leaf; |
| 3661 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3662 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3663 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3664 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3665 | |
| 3666 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3667 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3668 | 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] | 3669 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3670 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3671 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3672 | sleaf->name); |
| 3673 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3674 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3675 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3679 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3680 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3681 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3682 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3683 | 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] | 3684 | } |
| 3685 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3686 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3687 | return rc; |
| 3688 | } |
| 3689 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3690 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3691 | 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] | 3692 | return LY_EVALID; |
| 3693 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3694 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3695 | LY_CHECK_RET(rc); |
| 3696 | |
| 3697 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3698 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3699 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3700 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3701 | LYD_VALUE_GET(&leaf->value, bits); |
| 3702 | LY_ARRAY_FOR(bits->items, u) { |
| 3703 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3704 | set_fill_boolean(set, 1); |
| 3705 | break; |
| 3706 | } |
| 3707 | } |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | return LY_SUCCESS; |
| 3712 | } |
| 3713 | |
| 3714 | /** |
| 3715 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3716 | * with the argument converted to boolean. |
| 3717 | * |
| 3718 | * @param[in] args Array of arguments. |
| 3719 | * @param[in] arg_count Count of elements in @p args. |
| 3720 | * @param[in,out] set Context and result set at the same time. |
| 3721 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3722 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3723 | */ |
| 3724 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3725 | 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] | 3726 | { |
| 3727 | LY_ERR rc; |
| 3728 | |
| 3729 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3730 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3731 | return LY_SUCCESS; |
| 3732 | } |
| 3733 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3734 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3735 | LY_CHECK_RET(rc); |
| 3736 | set_fill_set(set, args[0]); |
| 3737 | |
| 3738 | return LY_SUCCESS; |
| 3739 | } |
| 3740 | |
| 3741 | /** |
| 3742 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3743 | * with the first argument rounded up to the nearest integer. |
| 3744 | * |
| 3745 | * @param[in] args Array of arguments. |
| 3746 | * @param[in] arg_count Count of elements in @p args. |
| 3747 | * @param[in,out] set Context and result set at the same time. |
| 3748 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3749 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3750 | */ |
| 3751 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3752 | 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] | 3753 | { |
| 3754 | struct lysc_node_leaf *sleaf; |
| 3755 | LY_ERR rc = LY_SUCCESS; |
| 3756 | |
| 3757 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3758 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3759 | 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] | 3760 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3761 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3762 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3763 | sleaf->name); |
| 3764 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3765 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3766 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3767 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3768 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3769 | return rc; |
| 3770 | } |
| 3771 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3772 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3773 | LY_CHECK_RET(rc); |
| 3774 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3775 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3776 | } else { |
| 3777 | set_fill_number(set, args[0]->val.num); |
| 3778 | } |
| 3779 | |
| 3780 | return LY_SUCCESS; |
| 3781 | } |
| 3782 | |
| 3783 | /** |
| 3784 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3785 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3786 | * |
| 3787 | * @param[in] args Array of arguments. |
| 3788 | * @param[in] arg_count Count of elements in @p args. |
| 3789 | * @param[in,out] set Context and result set at the same time. |
| 3790 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3791 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3792 | */ |
| 3793 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3794 | 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] | 3795 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3796 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3797 | char *str = NULL; |
| 3798 | size_t used = 1; |
| 3799 | LY_ERR rc = LY_SUCCESS; |
| 3800 | struct lysc_node_leaf *sleaf; |
| 3801 | |
| 3802 | if (options & LYXP_SCNODE_ALL) { |
| 3803 | for (i = 0; i < arg_count; ++i) { |
| 3804 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3805 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3806 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3807 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3808 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3809 | 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] | 3810 | } |
| 3811 | } |
| 3812 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3813 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3814 | return rc; |
| 3815 | } |
| 3816 | |
| 3817 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3818 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3819 | if (rc != LY_SUCCESS) { |
| 3820 | free(str); |
| 3821 | return rc; |
| 3822 | } |
| 3823 | |
| 3824 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3825 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3826 | strcpy(str + used - 1, args[i]->val.str); |
| 3827 | used += strlen(args[i]->val.str); |
| 3828 | } |
| 3829 | |
| 3830 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3831 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3832 | set->type = LYXP_SET_STRING; |
| 3833 | set->val.str = str; |
| 3834 | |
| 3835 | return LY_SUCCESS; |
| 3836 | } |
| 3837 | |
| 3838 | /** |
| 3839 | * @brief Execute the XPath contains(string, string) function. |
| 3840 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3841 | * be found in the first or not. |
| 3842 | * |
| 3843 | * @param[in] args Array of arguments. |
| 3844 | * @param[in] arg_count Count of elements in @p args. |
| 3845 | * @param[in,out] set Context and result set at the same time. |
| 3846 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3847 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3848 | */ |
| 3849 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3850 | 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] | 3851 | { |
| 3852 | struct lysc_node_leaf *sleaf; |
| 3853 | LY_ERR rc = LY_SUCCESS; |
| 3854 | |
| 3855 | if (options & LYXP_SCNODE_ALL) { |
| 3856 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3857 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3858 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3859 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3860 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3861 | 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] | 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3866 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3867 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3868 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3869 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3870 | 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] | 3871 | } |
| 3872 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3873 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3874 | return rc; |
| 3875 | } |
| 3876 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3877 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3878 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3879 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3880 | LY_CHECK_RET(rc); |
| 3881 | |
| 3882 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3883 | set_fill_boolean(set, 1); |
| 3884 | } else { |
| 3885 | set_fill_boolean(set, 0); |
| 3886 | } |
| 3887 | |
| 3888 | return LY_SUCCESS; |
| 3889 | } |
| 3890 | |
| 3891 | /** |
| 3892 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3893 | * with the size of the node-set from the argument. |
| 3894 | * |
| 3895 | * @param[in] args Array of arguments. |
| 3896 | * @param[in] arg_count Count of elements in @p args. |
| 3897 | * @param[in,out] set Context and result set at the same time. |
| 3898 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3899 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3900 | */ |
| 3901 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3902 | 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] | 3903 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3904 | LY_ERR rc = LY_SUCCESS; |
| 3905 | |
| 3906 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3907 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3908 | 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] | 3909 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3910 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3911 | return rc; |
| 3912 | } |
| 3913 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3914 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3915 | 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] | 3916 | return LY_EVALID; |
| 3917 | } |
| 3918 | |
| 3919 | set_fill_number(set, args[0]->used); |
| 3920 | return LY_SUCCESS; |
| 3921 | } |
| 3922 | |
| 3923 | /** |
| 3924 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3925 | * with the context with the intial node. |
| 3926 | * |
| 3927 | * @param[in] args Array of arguments. |
| 3928 | * @param[in] arg_count Count of elements in @p args. |
| 3929 | * @param[in,out] set Context and result set at the same time. |
| 3930 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3931 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3932 | */ |
| 3933 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3934 | 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] | 3935 | { |
| 3936 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3937 | 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] | 3938 | return LY_EVALID; |
| 3939 | } |
| 3940 | |
| 3941 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3942 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3943 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3944 | if (set->cur_scnode) { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 3945 | 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] | 3946 | } else { |
| 3947 | /* root node */ |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 3948 | 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] | 3949 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3950 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3951 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3952 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3953 | if (set->cur_node) { |
| 3954 | /* position is filled later */ |
| 3955 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3956 | } else { |
| 3957 | /* root node */ |
| 3958 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3959 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | return LY_SUCCESS; |
| 3963 | } |
| 3964 | |
| 3965 | /** |
| 3966 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3967 | * leafref or instance-identifier target node(s). |
| 3968 | * |
| 3969 | * @param[in] args Array of arguments. |
| 3970 | * @param[in] arg_count Count of elements in @p args. |
| 3971 | * @param[in,out] set Context and result set at the same time. |
| 3972 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3973 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3974 | */ |
| 3975 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3976 | 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] | 3977 | { |
| 3978 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3979 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3980 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3981 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3982 | struct ly_path *p; |
| 3983 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3984 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3985 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3986 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3987 | |
| 3988 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3989 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3990 | 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] | 3991 | } 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] | 3992 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3993 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3994 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3995 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3996 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3997 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3998 | __func__, sleaf->name); |
| 3999 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4000 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4001 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 4002 | 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] | 4003 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 4004 | 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] | 4005 | |
| 4006 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4007 | 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] | 4008 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4009 | if (!r) { |
| 4010 | /* get the target node */ |
| 4011 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 4012 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4013 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 4014 | 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] | 4015 | } /* 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] | 4016 | } |
| 4017 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4018 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4019 | } |
| 4020 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4021 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4022 | 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] | 4023 | return LY_EVALID; |
| 4024 | } |
| 4025 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4026 | lyxp_set_free_content(set); |
| 4027 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4028 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4029 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4030 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4031 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 4032 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4033 | 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] | 4034 | &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4035 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4036 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4037 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4038 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4039 | } else { |
| 4040 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4041 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4042 | 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] | 4043 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4044 | return LY_EVALID; |
| 4045 | } |
| 4046 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4047 | |
| 4048 | /* insert it */ |
| 4049 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4050 | } |
| 4051 | } |
| 4052 | |
| 4053 | return LY_SUCCESS; |
| 4054 | } |
| 4055 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4056 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4057 | 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] | 4058 | { |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 4059 | uint32_t i, id_len; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4060 | LY_ARRAY_COUNT_TYPE u; |
| 4061 | struct lyd_node_term *leaf; |
| 4062 | struct lysc_node_leaf *sleaf; |
| 4063 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4064 | struct lyd_value *val; |
| 4065 | const struct lys_module *mod; |
| 4066 | const char *id_name; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4067 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4068 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4069 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4070 | |
| 4071 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4072 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4073 | 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] | 4074 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4075 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4076 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4077 | sleaf->name); |
| 4078 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4079 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4080 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4084 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4085 | 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] | 4086 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4087 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4088 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4089 | } |
| 4090 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4091 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4092 | return rc; |
| 4093 | } |
| 4094 | |
| 4095 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4096 | 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] | 4097 | return LY_EVALID; |
| 4098 | } |
| 4099 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4100 | LY_CHECK_RET(rc); |
| 4101 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4102 | /* parse the identity */ |
| 4103 | id_name = args[1]->val.str; |
| 4104 | id_len = strlen(id_name); |
| 4105 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4106 | LY_CHECK_RET(rc); |
| 4107 | if (!mod) { |
| 4108 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4109 | return LY_EVALID; |
| 4110 | } |
| 4111 | |
| 4112 | /* find the identity */ |
| 4113 | found = 0; |
| 4114 | LY_ARRAY_FOR(mod->identities, u) { |
| 4115 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4116 | /* we have match */ |
| 4117 | found = 1; |
| 4118 | break; |
| 4119 | } |
| 4120 | } |
| 4121 | if (!found) { |
| 4122 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4123 | return LY_EVALID; |
| 4124 | } |
| 4125 | id = &mod->identities[u]; |
| 4126 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4127 | set_fill_boolean(set, 0); |
| 4128 | found = 0; |
| 4129 | for (i = 0; i < args[0]->used; ++i) { |
| 4130 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4131 | continue; |
| 4132 | } |
| 4133 | |
| 4134 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4135 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4136 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4137 | val = &leaf->value; |
| 4138 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 4139 | /* uninteresting */ |
| 4140 | continue; |
| 4141 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4142 | } else { |
| 4143 | meta = args[0]->val.meta[i].meta; |
| 4144 | val = &meta->value; |
| 4145 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4146 | /* uninteresting */ |
| 4147 | continue; |
| 4148 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4149 | } |
| 4150 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4151 | /* check the identity itself */ |
| 4152 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4153 | set_fill_boolean(set, 1); |
| 4154 | found = 1; |
| 4155 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4156 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4157 | set_fill_boolean(set, 1); |
| 4158 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4159 | } |
| 4160 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4161 | if (found) { |
| 4162 | break; |
| 4163 | } |
| 4164 | } |
| 4165 | |
| 4166 | return LY_SUCCESS; |
| 4167 | } |
| 4168 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4169 | /** |
| 4170 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4171 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4172 | * argument identity. |
| 4173 | * |
| 4174 | * @param[in] args Array of arguments. |
| 4175 | * @param[in] arg_count Count of elements in @p args. |
| 4176 | * @param[in,out] set Context and result set at the same time. |
| 4177 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4178 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4179 | */ |
| 4180 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4181 | xpath_derived_from(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4182 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4183 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | /** |
| 4187 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4188 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4189 | * the second argument identity. |
| 4190 | * |
| 4191 | * @param[in] args Array of arguments. |
| 4192 | * @param[in] arg_count Count of elements in @p args. |
| 4193 | * @param[in,out] set Context and result set at the same time. |
| 4194 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4195 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4196 | */ |
| 4197 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4198 | 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] | 4199 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4200 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | /** |
| 4204 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4205 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4206 | * |
| 4207 | * @param[in] args Array of arguments. |
| 4208 | * @param[in] arg_count Count of elements in @p args. |
| 4209 | * @param[in,out] set Context and result set at the same time. |
| 4210 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4211 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4212 | */ |
| 4213 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4214 | 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] | 4215 | { |
| 4216 | struct lyd_node_term *leaf; |
| 4217 | struct lysc_node_leaf *sleaf; |
| 4218 | LY_ERR rc = LY_SUCCESS; |
| 4219 | |
| 4220 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4221 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | 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] | 4223 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4224 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4225 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4226 | sleaf->name); |
| 4227 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4228 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4229 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4230 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4231 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4232 | return rc; |
| 4233 | } |
| 4234 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4235 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4236 | 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] | 4237 | return LY_EVALID; |
| 4238 | } |
| 4239 | |
| 4240 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4241 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4242 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4243 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4244 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4245 | set_fill_number(set, leaf->value.enum_item->value); |
| 4246 | } |
| 4247 | } |
| 4248 | |
| 4249 | return LY_SUCCESS; |
| 4250 | } |
| 4251 | |
| 4252 | /** |
| 4253 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4254 | * with false value. |
| 4255 | * |
| 4256 | * @param[in] args Array of arguments. |
| 4257 | * @param[in] arg_count Count of elements in @p args. |
| 4258 | * @param[in,out] set Context and result set at the same time. |
| 4259 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4260 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4261 | */ |
| 4262 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4263 | 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] | 4264 | { |
| 4265 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4266 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4267 | return LY_SUCCESS; |
| 4268 | } |
| 4269 | |
| 4270 | set_fill_boolean(set, 0); |
| 4271 | return LY_SUCCESS; |
| 4272 | } |
| 4273 | |
| 4274 | /** |
| 4275 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4276 | * with the first argument floored (truncated). |
| 4277 | * |
| 4278 | * @param[in] args Array of arguments. |
| 4279 | * @param[in] arg_count Count of elements in @p args. |
| 4280 | * @param[in,out] set Context and result set at the same time. |
| 4281 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4282 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4283 | */ |
| 4284 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4285 | 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] | 4286 | { |
| 4287 | LY_ERR rc; |
| 4288 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4289 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4290 | LY_CHECK_RET(rc); |
| 4291 | if (isfinite(args[0]->val.num)) { |
| 4292 | set_fill_number(set, (long long)args[0]->val.num); |
| 4293 | } |
| 4294 | |
| 4295 | return LY_SUCCESS; |
| 4296 | } |
| 4297 | |
| 4298 | /** |
| 4299 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4300 | * whether the language of the text matches the one from the argument. |
| 4301 | * |
| 4302 | * @param[in] args Array of arguments. |
| 4303 | * @param[in] arg_count Count of elements in @p args. |
| 4304 | * @param[in,out] set Context and result set at the same time. |
| 4305 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4306 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4307 | */ |
| 4308 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4309 | 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] | 4310 | { |
| 4311 | const struct lyd_node *node; |
| 4312 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4313 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4314 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4315 | LY_ERR rc = LY_SUCCESS; |
| 4316 | |
| 4317 | if (options & LYXP_SCNODE_ALL) { |
| 4318 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4319 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4320 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4321 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4322 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4323 | 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] | 4324 | } |
| 4325 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4326 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4327 | return rc; |
| 4328 | } |
| 4329 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4330 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4331 | LY_CHECK_RET(rc); |
| 4332 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4333 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4334 | 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] | 4335 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4336 | } else if (!set->used) { |
| 4337 | set_fill_boolean(set, 0); |
| 4338 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | switch (set->val.nodes[0].type) { |
| 4342 | case LYXP_NODE_ELEM: |
| 4343 | case LYXP_NODE_TEXT: |
| 4344 | node = set->val.nodes[0].node; |
| 4345 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4346 | case LYXP_NODE_META: |
| 4347 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4348 | break; |
| 4349 | default: |
| 4350 | /* nothing to do with roots */ |
| 4351 | set_fill_boolean(set, 0); |
| 4352 | return LY_SUCCESS; |
| 4353 | } |
| 4354 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4355 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4356 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4357 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4358 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4359 | 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] | 4360 | break; |
| 4361 | } |
| 4362 | } |
| 4363 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4364 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4365 | break; |
| 4366 | } |
| 4367 | } |
| 4368 | |
| 4369 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4370 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4371 | set_fill_boolean(set, 0); |
| 4372 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4373 | uint64_t i; |
| 4374 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4375 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4376 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4377 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4378 | set_fill_boolean(set, 0); |
| 4379 | break; |
| 4380 | } |
| 4381 | } |
| 4382 | if (!args[0]->val.str[i]) { |
| 4383 | if (!val[i] || (val[i] == '-')) { |
| 4384 | set_fill_boolean(set, 1); |
| 4385 | } else { |
| 4386 | set_fill_boolean(set, 0); |
| 4387 | } |
| 4388 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | return LY_SUCCESS; |
| 4392 | } |
| 4393 | |
| 4394 | /** |
| 4395 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4396 | * with the context size. |
| 4397 | * |
| 4398 | * @param[in] args Array of arguments. |
| 4399 | * @param[in] arg_count Count of elements in @p args. |
| 4400 | * @param[in,out] set Context and result set at the same time. |
| 4401 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4402 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4403 | */ |
| 4404 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4405 | 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] | 4406 | { |
| 4407 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4408 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4409 | return LY_SUCCESS; |
| 4410 | } |
| 4411 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4412 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4413 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4414 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4415 | } else if (!set->used) { |
| 4416 | set_fill_number(set, 0); |
| 4417 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | set_fill_number(set, set->ctx_size); |
| 4421 | return LY_SUCCESS; |
| 4422 | } |
| 4423 | |
| 4424 | /** |
| 4425 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4426 | * with the node name without namespace from the argument or the context. |
| 4427 | * |
| 4428 | * @param[in] args Array of arguments. |
| 4429 | * @param[in] arg_count Count of elements in @p args. |
| 4430 | * @param[in,out] set Context and result set at the same time. |
| 4431 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4432 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4433 | */ |
| 4434 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4435 | 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] | 4436 | { |
| 4437 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4438 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4439 | /* suppress unused variable warning */ |
| 4440 | (void)options; |
| 4441 | |
| 4442 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4443 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4444 | return LY_SUCCESS; |
| 4445 | } |
| 4446 | |
| 4447 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4448 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4449 | 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] | 4450 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4451 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4452 | } else if (!args[0]->used) { |
| 4453 | set_fill_string(set, "", 0); |
| 4454 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4458 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4459 | |
| 4460 | item = &args[0]->val.nodes[0]; |
| 4461 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4462 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4463 | 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] | 4464 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4465 | } else if (!set->used) { |
| 4466 | set_fill_string(set, "", 0); |
| 4467 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4471 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4472 | |
| 4473 | item = &set->val.nodes[0]; |
| 4474 | } |
| 4475 | |
| 4476 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4477 | case LYXP_NODE_NONE: |
| 4478 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4479 | case LYXP_NODE_ROOT: |
| 4480 | case LYXP_NODE_ROOT_CONFIG: |
| 4481 | case LYXP_NODE_TEXT: |
| 4482 | set_fill_string(set, "", 0); |
| 4483 | break; |
| 4484 | case LYXP_NODE_ELEM: |
| 4485 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4486 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4487 | case LYXP_NODE_META: |
| 4488 | 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] | 4489 | break; |
| 4490 | } |
| 4491 | |
| 4492 | return LY_SUCCESS; |
| 4493 | } |
| 4494 | |
| 4495 | /** |
| 4496 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4497 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4498 | * |
| 4499 | * @param[in] args Array of arguments. |
| 4500 | * @param[in] arg_count Count of elements in @p args. |
| 4501 | * @param[in,out] set Context and result set at the same time. |
| 4502 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4503 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4504 | */ |
| 4505 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4506 | 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] | 4507 | { |
| 4508 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4509 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4510 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4511 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4512 | |
| 4513 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4514 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4515 | return LY_SUCCESS; |
| 4516 | } |
| 4517 | |
| 4518 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4519 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4520 | 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] | 4521 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4522 | } else if (!args[0]->used) { |
| 4523 | set_fill_string(set, "", 0); |
| 4524 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4528 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4529 | |
| 4530 | item = &args[0]->val.nodes[0]; |
| 4531 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4532 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4533 | 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] | 4534 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4535 | } else if (!set->used) { |
| 4536 | set_fill_string(set, "", 0); |
| 4537 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4541 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4542 | |
| 4543 | item = &set->val.nodes[0]; |
| 4544 | } |
| 4545 | |
| 4546 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4547 | case LYXP_NODE_NONE: |
| 4548 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4549 | case LYXP_NODE_ROOT: |
| 4550 | case LYXP_NODE_ROOT_CONFIG: |
| 4551 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4552 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4553 | break; |
| 4554 | case LYXP_NODE_ELEM: |
| 4555 | mod = item->node->schema->module; |
| 4556 | name = item->node->schema->name; |
| 4557 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4558 | case LYXP_NODE_META: |
| 4559 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4560 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4561 | break; |
| 4562 | } |
| 4563 | |
| 4564 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4565 | 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] | 4566 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4567 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4568 | set_fill_string(set, str, strlen(str)); |
| 4569 | free(str); |
| 4570 | } else { |
| 4571 | set_fill_string(set, "", 0); |
| 4572 | } |
| 4573 | |
| 4574 | return LY_SUCCESS; |
| 4575 | } |
| 4576 | |
| 4577 | /** |
| 4578 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4579 | * with the namespace of the node from the argument or the context. |
| 4580 | * |
| 4581 | * @param[in] args Array of arguments. |
| 4582 | * @param[in] arg_count Count of elements in @p args. |
| 4583 | * @param[in,out] set Context and result set at the same time. |
| 4584 | * @param[in] options XPath options. |
| 4585 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4586 | */ |
| 4587 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4588 | 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] | 4589 | { |
| 4590 | struct lyxp_set_node *item; |
| 4591 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4592 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4593 | /* suppress unused variable warning */ |
| 4594 | (void)options; |
| 4595 | |
| 4596 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4597 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4598 | return LY_SUCCESS; |
| 4599 | } |
| 4600 | |
| 4601 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4602 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4603 | 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] | 4604 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4605 | return LY_EVALID; |
| 4606 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4607 | set_fill_string(set, "", 0); |
| 4608 | return LY_SUCCESS; |
| 4609 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4610 | |
| 4611 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4612 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4613 | |
| 4614 | item = &args[0]->val.nodes[0]; |
| 4615 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4616 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4617 | 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] | 4618 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4619 | } else if (!set->used) { |
| 4620 | set_fill_string(set, "", 0); |
| 4621 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4625 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4626 | |
| 4627 | item = &set->val.nodes[0]; |
| 4628 | } |
| 4629 | |
| 4630 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4631 | case LYXP_NODE_NONE: |
| 4632 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4633 | case LYXP_NODE_ROOT: |
| 4634 | case LYXP_NODE_ROOT_CONFIG: |
| 4635 | case LYXP_NODE_TEXT: |
| 4636 | set_fill_string(set, "", 0); |
| 4637 | break; |
| 4638 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4639 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4640 | if (item->type == LYXP_NODE_ELEM) { |
| 4641 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4642 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4643 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4644 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4648 | break; |
| 4649 | } |
| 4650 | |
| 4651 | return LY_SUCCESS; |
| 4652 | } |
| 4653 | |
| 4654 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4655 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4656 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4657 | * from the argument or the context. |
| 4658 | * |
| 4659 | * @param[in] args Array of arguments. |
| 4660 | * @param[in] arg_count Count of elements in @p args. |
| 4661 | * @param[in,out] set Context and result set at the same time. |
| 4662 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4663 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4664 | */ |
| 4665 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4666 | 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] | 4667 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4668 | uint32_t i, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4669 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4670 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4671 | struct lysc_node_leaf *sleaf; |
| 4672 | LY_ERR rc = LY_SUCCESS; |
| 4673 | |
| 4674 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4675 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4676 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4677 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4678 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4679 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4680 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4681 | 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] | 4682 | } |
| 4683 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4684 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4685 | return rc; |
| 4686 | } |
| 4687 | |
| 4688 | if (arg_count) { |
| 4689 | set_fill_set(set, args[0]); |
| 4690 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4691 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4692 | LY_CHECK_RET(rc); |
| 4693 | |
| 4694 | /* is there any normalization necessary? */ |
| 4695 | for (i = 0; set->val.str[i]; ++i) { |
| 4696 | if (is_xmlws(set->val.str[i])) { |
| 4697 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4698 | have_spaces = 1; |
| 4699 | break; |
| 4700 | } |
| 4701 | space_before = 1; |
| 4702 | } else { |
| 4703 | space_before = 0; |
| 4704 | } |
| 4705 | } |
| 4706 | |
| 4707 | /* yep, there is */ |
| 4708 | if (have_spaces) { |
| 4709 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4710 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4711 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4712 | new_used = 0; |
| 4713 | |
| 4714 | space_before = 0; |
| 4715 | for (i = 0; set->val.str[i]; ++i) { |
| 4716 | if (is_xmlws(set->val.str[i])) { |
| 4717 | if ((i == 0) || space_before) { |
| 4718 | space_before = 1; |
| 4719 | continue; |
| 4720 | } else { |
| 4721 | space_before = 1; |
| 4722 | } |
| 4723 | } else { |
| 4724 | space_before = 0; |
| 4725 | } |
| 4726 | |
| 4727 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4728 | ++new_used; |
| 4729 | } |
| 4730 | |
| 4731 | /* at worst there is one trailing space now */ |
| 4732 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4733 | --new_used; |
| 4734 | } |
| 4735 | |
| 4736 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4737 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4738 | new[new_used] = '\0'; |
| 4739 | |
| 4740 | free(set->val.str); |
| 4741 | set->val.str = new; |
| 4742 | } |
| 4743 | |
| 4744 | return LY_SUCCESS; |
| 4745 | } |
| 4746 | |
| 4747 | /** |
| 4748 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4749 | * with the argument converted to boolean and logically inverted. |
| 4750 | * |
| 4751 | * @param[in] args Array of arguments. |
| 4752 | * @param[in] arg_count Count of elements in @p args. |
| 4753 | * @param[in,out] set Context and result set at the same time. |
| 4754 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4755 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4756 | */ |
| 4757 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4758 | 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] | 4759 | { |
| 4760 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4761 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4762 | return LY_SUCCESS; |
| 4763 | } |
| 4764 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4765 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4766 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4767 | set_fill_boolean(set, 0); |
| 4768 | } else { |
| 4769 | set_fill_boolean(set, 1); |
| 4770 | } |
| 4771 | |
| 4772 | return LY_SUCCESS; |
| 4773 | } |
| 4774 | |
| 4775 | /** |
| 4776 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4777 | * with the number representation of either the argument or the context. |
| 4778 | * |
| 4779 | * @param[in] args Array of arguments. |
| 4780 | * @param[in] arg_count Count of elements in @p args. |
| 4781 | * @param[in,out] set Context and result set at the same time. |
| 4782 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4783 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4784 | */ |
| 4785 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4786 | 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] | 4787 | { |
| 4788 | LY_ERR rc; |
| 4789 | |
| 4790 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4791 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4792 | return LY_SUCCESS; |
| 4793 | } |
| 4794 | |
| 4795 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4796 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4797 | LY_CHECK_RET(rc); |
| 4798 | set_fill_set(set, args[0]); |
| 4799 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4800 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4801 | LY_CHECK_RET(rc); |
| 4802 | } |
| 4803 | |
| 4804 | return LY_SUCCESS; |
| 4805 | } |
| 4806 | |
| 4807 | /** |
| 4808 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4809 | * with the context position. |
| 4810 | * |
| 4811 | * @param[in] args Array of arguments. |
| 4812 | * @param[in] arg_count Count of elements in @p args. |
| 4813 | * @param[in,out] set Context and result set at the same time. |
| 4814 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4815 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4816 | */ |
| 4817 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4818 | 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] | 4819 | { |
| 4820 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4821 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4822 | return LY_SUCCESS; |
| 4823 | } |
| 4824 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4825 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4826 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4827 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4828 | } else if (!set->used) { |
| 4829 | set_fill_number(set, 0); |
| 4830 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | set_fill_number(set, set->ctx_pos); |
| 4834 | |
| 4835 | /* UNUSED in 'Release' build type */ |
| 4836 | (void)options; |
| 4837 | return LY_SUCCESS; |
| 4838 | } |
| 4839 | |
| 4840 | /** |
| 4841 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4842 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4843 | * YANG 1.1 RFC section 10.2.1. |
| 4844 | * |
| 4845 | * @param[in] args Array of arguments. |
| 4846 | * @param[in] arg_count Count of elements in @p args. |
| 4847 | * @param[in,out] set Context and result set at the same time. |
| 4848 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4849 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4850 | */ |
| 4851 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4852 | 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] | 4853 | { |
| 4854 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4855 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4856 | LY_ERR rc = LY_SUCCESS; |
| 4857 | struct ly_err_item *err; |
| 4858 | |
| 4859 | if (options & LYXP_SCNODE_ALL) { |
| 4860 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4861 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4862 | 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] | 4863 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4864 | 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] | 4865 | } |
| 4866 | } |
| 4867 | |
| 4868 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4869 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4870 | 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] | 4871 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4872 | 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] | 4873 | } |
| 4874 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4875 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4876 | return rc; |
| 4877 | } |
| 4878 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4879 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4880 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4881 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4882 | LY_CHECK_RET(rc); |
| 4883 | |
| 4884 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4885 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4886 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4887 | 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] | 4888 | if (set->cur_node) { |
| 4889 | LOG_LOCBACK(0, 1, 0, 0); |
| 4890 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4891 | if (rc != LY_SUCCESS) { |
| 4892 | LY_ARRAY_FREE(patterns); |
| 4893 | return rc; |
| 4894 | } |
| 4895 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4896 | 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] | 4897 | pcre2_code_free((*pattern)->code); |
| 4898 | free(*pattern); |
| 4899 | LY_ARRAY_FREE(patterns); |
| 4900 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4901 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4902 | ly_err_free(err); |
| 4903 | return rc; |
| 4904 | } |
| 4905 | |
| 4906 | if (rc == LY_EVALID) { |
| 4907 | ly_err_free(err); |
| 4908 | set_fill_boolean(set, 0); |
| 4909 | } else { |
| 4910 | set_fill_boolean(set, 1); |
| 4911 | } |
| 4912 | |
| 4913 | return LY_SUCCESS; |
| 4914 | } |
| 4915 | |
| 4916 | /** |
| 4917 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4918 | * with the rounded first argument. For details refer to |
| 4919 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4920 | * |
| 4921 | * @param[in] args Array of arguments. |
| 4922 | * @param[in] arg_count Count of elements in @p args. |
| 4923 | * @param[in,out] set Context and result set at the same time. |
| 4924 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4925 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4926 | */ |
| 4927 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4928 | 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] | 4929 | { |
| 4930 | struct lysc_node_leaf *sleaf; |
| 4931 | LY_ERR rc = LY_SUCCESS; |
| 4932 | |
| 4933 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4934 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4935 | 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] | 4936 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4937 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4938 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4939 | sleaf->name); |
| 4940 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4941 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4942 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4943 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4944 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4945 | return rc; |
| 4946 | } |
| 4947 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4948 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4949 | LY_CHECK_RET(rc); |
| 4950 | |
| 4951 | /* cover only the cases where floor can't be used */ |
| 4952 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4953 | set_fill_number(set, -0.0f); |
| 4954 | } else { |
| 4955 | args[0]->val.num += 0.5; |
| 4956 | rc = xpath_floor(args, 1, args[0], options); |
| 4957 | LY_CHECK_RET(rc); |
| 4958 | set_fill_number(set, args[0]->val.num); |
| 4959 | } |
| 4960 | |
| 4961 | return LY_SUCCESS; |
| 4962 | } |
| 4963 | |
| 4964 | /** |
| 4965 | * @brief Execute the XPath starts-with(string, string) function. |
| 4966 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4967 | * the prefix of the first or not. |
| 4968 | * |
| 4969 | * @param[in] args Array of arguments. |
| 4970 | * @param[in] arg_count Count of elements in @p args. |
| 4971 | * @param[in,out] set Context and result set at the same time. |
| 4972 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4973 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4974 | */ |
| 4975 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4976 | 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] | 4977 | { |
| 4978 | struct lysc_node_leaf *sleaf; |
| 4979 | LY_ERR rc = LY_SUCCESS; |
| 4980 | |
| 4981 | if (options & LYXP_SCNODE_ALL) { |
| 4982 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4983 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4984 | 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] | 4985 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4986 | 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] | 4987 | } |
| 4988 | } |
| 4989 | |
| 4990 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4991 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4992 | 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] | 4993 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4994 | 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] | 4995 | } |
| 4996 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4997 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4998 | return rc; |
| 4999 | } |
| 5000 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5001 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5002 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5003 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5004 | LY_CHECK_RET(rc); |
| 5005 | |
| 5006 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 5007 | set_fill_boolean(set, 0); |
| 5008 | } else { |
| 5009 | set_fill_boolean(set, 1); |
| 5010 | } |
| 5011 | |
| 5012 | return LY_SUCCESS; |
| 5013 | } |
| 5014 | |
| 5015 | /** |
| 5016 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 5017 | * with the string representation of either the argument or the context. |
| 5018 | * |
| 5019 | * @param[in] args Array of arguments. |
| 5020 | * @param[in] arg_count Count of elements in @p args. |
| 5021 | * @param[in,out] set Context and result set at the same time. |
| 5022 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5023 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5024 | */ |
| 5025 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5026 | 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] | 5027 | { |
| 5028 | LY_ERR rc; |
| 5029 | |
| 5030 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5031 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5032 | return LY_SUCCESS; |
| 5033 | } |
| 5034 | |
| 5035 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5036 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5037 | LY_CHECK_RET(rc); |
| 5038 | set_fill_set(set, args[0]); |
| 5039 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5040 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5041 | LY_CHECK_RET(rc); |
| 5042 | } |
| 5043 | |
| 5044 | return LY_SUCCESS; |
| 5045 | } |
| 5046 | |
| 5047 | /** |
| 5048 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5049 | * with the length of the string in either the argument or the context. |
| 5050 | * |
| 5051 | * @param[in] args Array of arguments. |
| 5052 | * @param[in] arg_count Count of elements in @p args. |
| 5053 | * @param[in,out] set Context and result set at the same time. |
| 5054 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5055 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5056 | */ |
| 5057 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5058 | 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] | 5059 | { |
| 5060 | struct lysc_node_leaf *sleaf; |
| 5061 | LY_ERR rc = LY_SUCCESS; |
| 5062 | |
| 5063 | if (options & LYXP_SCNODE_ALL) { |
| 5064 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5065 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5066 | 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] | 5067 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5068 | 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] | 5069 | } |
| 5070 | } |
| 5071 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5072 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5073 | 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] | 5074 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5075 | 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] | 5076 | } |
| 5077 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5078 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5079 | return rc; |
| 5080 | } |
| 5081 | |
| 5082 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5083 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5084 | LY_CHECK_RET(rc); |
| 5085 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5086 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5087 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5088 | LY_CHECK_RET(rc); |
| 5089 | set_fill_number(set, strlen(set->val.str)); |
| 5090 | } |
| 5091 | |
| 5092 | return LY_SUCCESS; |
| 5093 | } |
| 5094 | |
| 5095 | /** |
| 5096 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5097 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5098 | * on the second argument index ending on the third argument index, |
| 5099 | * indexed from 1. For exact definition refer to |
| 5100 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5101 | * |
| 5102 | * @param[in] args Array of arguments. |
| 5103 | * @param[in] arg_count Count of elements in @p args. |
| 5104 | * @param[in,out] set Context and result set at the same time. |
| 5105 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5106 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5107 | */ |
| 5108 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5109 | 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] | 5110 | { |
Michal Vasko | 6db996e | 2022-07-28 10:28:04 +0200 | [diff] [blame] | 5111 | int64_t start; |
| 5112 | int32_t len; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5113 | uint32_t str_start, str_len, pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5114 | struct lysc_node_leaf *sleaf; |
| 5115 | LY_ERR rc = LY_SUCCESS; |
| 5116 | |
| 5117 | if (options & LYXP_SCNODE_ALL) { |
| 5118 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5119 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5120 | 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] | 5121 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5122 | 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] | 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5127 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5128 | 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] | 5129 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5130 | 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] | 5131 | } |
| 5132 | } |
| 5133 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5134 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5135 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5136 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5137 | 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] | 5138 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5139 | 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] | 5140 | } |
| 5141 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5142 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5143 | return rc; |
| 5144 | } |
| 5145 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5146 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5147 | LY_CHECK_RET(rc); |
| 5148 | |
| 5149 | /* start */ |
| 5150 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5151 | return -1; |
| 5152 | } |
| 5153 | if (isfinite(args[1]->val.num)) { |
| 5154 | start = args[1]->val.num - 1; |
| 5155 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5156 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5157 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5158 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5159 | } |
| 5160 | |
| 5161 | /* len */ |
| 5162 | if (arg_count == 3) { |
| 5163 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5164 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5165 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5166 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5167 | } else if (isfinite(args[2]->val.num)) { |
| 5168 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5169 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5170 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5171 | } |
| 5172 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5173 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | /* find matching character positions */ |
| 5177 | str_start = 0; |
| 5178 | str_len = 0; |
| 5179 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5180 | if (pos < start) { |
| 5181 | ++str_start; |
| 5182 | } else if (pos < start + len) { |
| 5183 | ++str_len; |
| 5184 | } else { |
| 5185 | break; |
| 5186 | } |
| 5187 | } |
| 5188 | |
| 5189 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5190 | return LY_SUCCESS; |
| 5191 | } |
| 5192 | |
| 5193 | /** |
| 5194 | * @brief Execute the XPath substring-after(string, string) function. |
| 5195 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5196 | * of the second argument in the first or an empty string. |
| 5197 | * |
| 5198 | * @param[in] args Array of arguments. |
| 5199 | * @param[in] arg_count Count of elements in @p args. |
| 5200 | * @param[in,out] set Context and result set at the same time. |
| 5201 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5202 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5203 | */ |
| 5204 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5205 | 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] | 5206 | { |
| 5207 | char *ptr; |
| 5208 | struct lysc_node_leaf *sleaf; |
| 5209 | LY_ERR rc = LY_SUCCESS; |
| 5210 | |
| 5211 | if (options & LYXP_SCNODE_ALL) { |
| 5212 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5213 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5214 | 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] | 5215 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5216 | 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] | 5217 | } |
| 5218 | } |
| 5219 | |
| 5220 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5221 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5222 | 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] | 5223 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5224 | 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] | 5225 | } |
| 5226 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5227 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5228 | return rc; |
| 5229 | } |
| 5230 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5231 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5232 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5233 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5234 | LY_CHECK_RET(rc); |
| 5235 | |
| 5236 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5237 | if (ptr) { |
| 5238 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5239 | } else { |
| 5240 | set_fill_string(set, "", 0); |
| 5241 | } |
| 5242 | |
| 5243 | return LY_SUCCESS; |
| 5244 | } |
| 5245 | |
| 5246 | /** |
| 5247 | * @brief Execute the XPath substring-before(string, string) function. |
| 5248 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5249 | * of the second argument in the first or an empty string. |
| 5250 | * |
| 5251 | * @param[in] args Array of arguments. |
| 5252 | * @param[in] arg_count Count of elements in @p args. |
| 5253 | * @param[in,out] set Context and result set at the same time. |
| 5254 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5255 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5256 | */ |
| 5257 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5258 | 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] | 5259 | { |
| 5260 | char *ptr; |
| 5261 | struct lysc_node_leaf *sleaf; |
| 5262 | LY_ERR rc = LY_SUCCESS; |
| 5263 | |
| 5264 | if (options & LYXP_SCNODE_ALL) { |
| 5265 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5266 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5267 | 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] | 5268 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5269 | 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] | 5270 | } |
| 5271 | } |
| 5272 | |
| 5273 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5274 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5275 | 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] | 5276 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5277 | 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] | 5278 | } |
| 5279 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5280 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5281 | return rc; |
| 5282 | } |
| 5283 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5284 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5285 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5286 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5287 | LY_CHECK_RET(rc); |
| 5288 | |
| 5289 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5290 | if (ptr) { |
| 5291 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5292 | } else { |
| 5293 | set_fill_string(set, "", 0); |
| 5294 | } |
| 5295 | |
| 5296 | return LY_SUCCESS; |
| 5297 | } |
| 5298 | |
| 5299 | /** |
| 5300 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5301 | * with the sum of all the nodes in the context. |
| 5302 | * |
| 5303 | * @param[in] args Array of arguments. |
| 5304 | * @param[in] arg_count Count of elements in @p args. |
| 5305 | * @param[in,out] set Context and result set at the same time. |
| 5306 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5307 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5308 | */ |
| 5309 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5310 | 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] | 5311 | { |
| 5312 | long double num; |
| 5313 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5314 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5315 | struct lyxp_set set_item; |
| 5316 | struct lysc_node_leaf *sleaf; |
| 5317 | LY_ERR rc = LY_SUCCESS; |
| 5318 | |
| 5319 | if (options & LYXP_SCNODE_ALL) { |
| 5320 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5321 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5322 | 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] | 5323 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5324 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5325 | 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] | 5326 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5328 | 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] | 5329 | } |
| 5330 | } |
| 5331 | } |
| 5332 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5333 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5334 | return rc; |
| 5335 | } |
| 5336 | |
| 5337 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5338 | |
| 5339 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5340 | 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] | 5341 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5342 | } else if (!args[0]->used) { |
| 5343 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5344 | } |
| 5345 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5346 | set_init(&set_item, set); |
| 5347 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5348 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5349 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5350 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5351 | |
| 5352 | set_item.used = 1; |
| 5353 | set_item.size = 1; |
| 5354 | |
| 5355 | for (i = 0; i < args[0]->used; ++i) { |
| 5356 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5357 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5358 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5359 | LY_CHECK_RET(rc); |
| 5360 | num = cast_string_to_number(str); |
| 5361 | free(str); |
| 5362 | set->val.num += num; |
| 5363 | } |
| 5364 | |
| 5365 | free(set_item.val.nodes); |
| 5366 | |
| 5367 | return LY_SUCCESS; |
| 5368 | } |
| 5369 | |
| 5370 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5371 | * @brief Execute the XPath translate(string, string, string) function. |
| 5372 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5373 | * from the second argument replaced by those on the corresponding |
| 5374 | * positions in the third argument. |
| 5375 | * |
| 5376 | * @param[in] args Array of arguments. |
| 5377 | * @param[in] arg_count Count of elements in @p args. |
| 5378 | * @param[in,out] set Context and result set at the same time. |
| 5379 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5380 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5381 | */ |
| 5382 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5383 | 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] | 5384 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5385 | uint32_t i, j, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5386 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5387 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5388 | struct lysc_node_leaf *sleaf; |
| 5389 | LY_ERR rc = LY_SUCCESS; |
| 5390 | |
| 5391 | if (options & LYXP_SCNODE_ALL) { |
| 5392 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5393 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5394 | 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] | 5395 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5396 | 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] | 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5401 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5402 | 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] | 5403 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5404 | 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] | 5405 | } |
| 5406 | } |
| 5407 | |
| 5408 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5409 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5410 | 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] | 5411 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5412 | 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] | 5413 | } |
| 5414 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5415 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5416 | return rc; |
| 5417 | } |
| 5418 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5419 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5420 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5421 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5422 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5423 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5424 | LY_CHECK_RET(rc); |
| 5425 | |
| 5426 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5427 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5428 | new_used = 0; |
| 5429 | |
| 5430 | have_removed = 0; |
| 5431 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5432 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5433 | |
| 5434 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5435 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5436 | /* removing this char */ |
| 5437 | if (j >= strlen(args[2]->val.str)) { |
| 5438 | have_removed = 1; |
| 5439 | found = 1; |
| 5440 | break; |
| 5441 | } |
| 5442 | /* replacing this char */ |
| 5443 | new[new_used] = args[2]->val.str[j]; |
| 5444 | ++new_used; |
| 5445 | found = 1; |
| 5446 | break; |
| 5447 | } |
| 5448 | } |
| 5449 | |
| 5450 | /* copying this char */ |
| 5451 | if (!found) { |
| 5452 | new[new_used] = args[0]->val.str[i]; |
| 5453 | ++new_used; |
| 5454 | } |
| 5455 | } |
| 5456 | |
| 5457 | if (have_removed) { |
| 5458 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5459 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5460 | } |
| 5461 | new[new_used] = '\0'; |
| 5462 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5463 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5464 | set->type = LYXP_SET_STRING; |
| 5465 | set->val.str = new; |
| 5466 | |
| 5467 | return LY_SUCCESS; |
| 5468 | } |
| 5469 | |
| 5470 | /** |
| 5471 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5472 | * with true value. |
| 5473 | * |
| 5474 | * @param[in] args Array of arguments. |
| 5475 | * @param[in] arg_count Count of elements in @p args. |
| 5476 | * @param[in,out] set Context and result set at the same time. |
| 5477 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5478 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5479 | */ |
| 5480 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5481 | 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] | 5482 | { |
| 5483 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5484 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5485 | return LY_SUCCESS; |
| 5486 | } |
| 5487 | |
| 5488 | set_fill_boolean(set, 1); |
| 5489 | return LY_SUCCESS; |
| 5490 | } |
| 5491 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5492 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5493 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5494 | * with only nodes from the context. |
| 5495 | * |
| 5496 | * @param[in,out] set Context and result set at the same time. |
| 5497 | * @param[in] axis Axis to search on. |
| 5498 | * @param[in] options XPath options. |
| 5499 | * @return LY_ERR |
| 5500 | */ |
| 5501 | static LY_ERR |
| 5502 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5503 | { |
| 5504 | if (options & LYXP_SCNODE_ALL) { |
| 5505 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5506 | } |
| 5507 | |
| 5508 | if (set->type != LYXP_SET_NODE_SET) { |
| 5509 | lyxp_set_free_content(set); |
| 5510 | return LY_SUCCESS; |
| 5511 | } |
| 5512 | |
| 5513 | /* just like moving to a node with no restrictions */ |
| 5514 | return moveto_node(set, NULL, NULL, axis, options); |
| 5515 | } |
| 5516 | |
| 5517 | /** |
| 5518 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5519 | * with the text content of the nodes in the context. |
| 5520 | * |
| 5521 | * @param[in,out] set Context and result set at the same time. |
| 5522 | * @param[in] axis Axis to search on. |
| 5523 | * @param[in] options XPath options. |
| 5524 | * @return LY_ERR |
| 5525 | */ |
| 5526 | static LY_ERR |
| 5527 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5528 | { |
| 5529 | uint32_t i; |
| 5530 | |
| 5531 | if (options & LYXP_SCNODE_ALL) { |
| 5532 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5533 | return LY_SUCCESS; |
| 5534 | } |
| 5535 | |
| 5536 | if (set->type != LYXP_SET_NODE_SET) { |
| 5537 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5538 | return LY_EVALID; |
| 5539 | } |
| 5540 | |
| 5541 | if (axis != LYXP_AXIS_CHILD) { |
| 5542 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5543 | lyxp_set_free_content(set); |
| 5544 | return LY_SUCCESS; |
| 5545 | } |
| 5546 | |
| 5547 | for (i = 0; i < set->used; ++i) { |
| 5548 | switch (set->val.nodes[i].type) { |
| 5549 | case LYXP_NODE_NONE: |
| 5550 | LOGINT_RET(set->ctx); |
| 5551 | case LYXP_NODE_ELEM: |
| 5552 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5553 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5554 | break; |
| 5555 | } |
| 5556 | /* fall through */ |
| 5557 | case LYXP_NODE_ROOT: |
| 5558 | case LYXP_NODE_ROOT_CONFIG: |
| 5559 | case LYXP_NODE_TEXT: |
| 5560 | case LYXP_NODE_META: |
| 5561 | set_remove_node_none(set, i); |
| 5562 | break; |
| 5563 | } |
| 5564 | } |
| 5565 | set_remove_nodes_none(set); |
| 5566 | |
| 5567 | return LY_SUCCESS; |
| 5568 | } |
| 5569 | |
| 5570 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5571 | * @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] | 5572 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5573 | * XPath @p set is expected to be a (sc)node set! |
| 5574 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5575 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5576 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5577 | * @param[in] set Set with general XPath context. |
| 5578 | * @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] | 5579 | * @param[out] moveto_mod Expected module of a matching node. |
| 5580 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5581 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5582 | static LY_ERR |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 5583 | 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] | 5584 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5585 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5586 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5587 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5588 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5589 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5590 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5591 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5592 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5593 | /* specific module */ |
| 5594 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5595 | 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] | 5596 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5597 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5598 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5599 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5600 | return LY_EVALID; |
| 5601 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5602 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5603 | *qname += pref_len + 1; |
| 5604 | *qname_len -= pref_len + 1; |
| 5605 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5606 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5607 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5608 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5609 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5610 | case LY_VALUE_SCHEMA: |
| 5611 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5612 | /* current module */ |
| 5613 | mod = set->cur_mod; |
| 5614 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5615 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5616 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5617 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5618 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5619 | /* inherit parent (context node) module */ |
| 5620 | if (ctx_scnode) { |
| 5621 | mod = ctx_scnode->module; |
| 5622 | } else { |
| 5623 | mod = NULL; |
| 5624 | } |
| 5625 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5626 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5627 | /* all nodes need to be prefixed */ |
| 5628 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5629 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5630 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5631 | } |
| 5632 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5633 | *moveto_mod = mod; |
| 5634 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5635 | } |
| 5636 | |
| 5637 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5638 | * @brief Move context @p set to the root. Handles absolute path. |
| 5639 | * Result is LYXP_SET_NODE_SET. |
| 5640 | * |
| 5641 | * @param[in,out] set Set to use. |
| 5642 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5643 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5644 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5645 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5646 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5647 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5648 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5649 | |
| 5650 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5651 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 5652 | 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] | 5653 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5654 | set->type = LYXP_SET_NODE_SET; |
| 5655 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5656 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 5657 | set->non_child_axis = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5658 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5659 | |
| 5660 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5661 | } |
| 5662 | |
| 5663 | /** |
| 5664 | * @brief Check @p node as a part of NameTest processing. |
| 5665 | * |
| 5666 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5667 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5668 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5669 | * @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] | 5670 | * @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] | 5671 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5672 | * @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] | 5673 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5674 | */ |
| 5675 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5676 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5677 | 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] | 5678 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5679 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5680 | assert(node_type == set->root_type); |
| 5681 | |
| 5682 | if (node_name || moveto_mod) { |
| 5683 | /* root will not match a specific node */ |
| 5684 | return LY_ENOT; |
| 5685 | } |
| 5686 | return LY_SUCCESS; |
| 5687 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5688 | /* other types will not match */ |
| 5689 | return LY_ENOT; |
| 5690 | } |
| 5691 | |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5692 | if (!node->schema) { |
| 5693 | /* opaque node never matches */ |
| 5694 | return LY_ENOT; |
| 5695 | } |
| 5696 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5697 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5698 | if (moveto_mod) { |
| 5699 | if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) { |
| 5700 | return LY_ENOT; |
| 5701 | } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) { |
| 5702 | return LY_ENOT; |
| 5703 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5704 | } |
| 5705 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5706 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5707 | 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] | 5708 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5709 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5710 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5711 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5715 | if (node_name) { |
| 5716 | if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) { |
| 5717 | return LY_ENOT; |
| 5718 | } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) { |
| 5719 | return LY_ENOT; |
| 5720 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5721 | } |
| 5722 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5723 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5724 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5725 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5726 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5727 | |
| 5728 | /* match */ |
| 5729 | return LY_SUCCESS; |
| 5730 | } |
| 5731 | |
| 5732 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5733 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5734 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5735 | * @param[in] iter Last returned node. |
| 5736 | * @param[in] stop Node to stop the search on and not return. |
| 5737 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5738 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5739 | static const struct lyd_node * |
| 5740 | moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5741 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5742 | const struct lyd_node *next = NULL; |
| 5743 | |
| 5744 | /* 1) child */ |
| 5745 | next = lyd_child(iter); |
| 5746 | if (!next) { |
| 5747 | if (iter == stop) { |
| 5748 | /* reached stop, no more descendants */ |
| 5749 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5750 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5751 | /* 2) child next sibling */ |
| 5752 | next = iter->next; |
| 5753 | } |
| 5754 | while (!next) { |
| 5755 | iter = lyd_parent(iter); |
| 5756 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5757 | return NULL; |
| 5758 | } |
| 5759 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5760 | } |
| 5761 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5762 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5763 | } |
| 5764 | |
| 5765 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5766 | * @brief Get the next node in a backward DFS. |
| 5767 | * |
| 5768 | * @param[in] iter Last returned node. |
| 5769 | * @param[in] stop Node to stop the search on and not return. |
| 5770 | * @return Next node, NULL if there are no more. |
| 5771 | */ |
| 5772 | static const struct lyd_node * |
| 5773 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5774 | { |
| 5775 | const struct lyd_node *next = NULL; |
| 5776 | |
| 5777 | /* 1) previous sibling innermost last child */ |
| 5778 | next = iter->prev->next ? iter->prev : NULL; |
| 5779 | while (next && lyd_child(next)) { |
| 5780 | next = lyd_child(next); |
| 5781 | next = next->prev; |
| 5782 | } |
| 5783 | |
| 5784 | if (!next) { |
| 5785 | /* 2) parent */ |
| 5786 | iter = lyd_parent(iter); |
| 5787 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5788 | return NULL; |
| 5789 | } |
| 5790 | next = iter; |
| 5791 | } |
| 5792 | |
| 5793 | return next; |
| 5794 | } |
| 5795 | |
| 5796 | /** |
| 5797 | * @brief Get the first node on an axis for a context node. |
| 5798 | * |
| 5799 | * @param[in,out] iter NULL, updated to the next node. |
| 5800 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5801 | * @param[in] node Context node. |
| 5802 | * @param[in] node_type Type of @p node. |
| 5803 | * @param[in] axis Axis to use. |
| 5804 | * @param[in] set XPath set with the general context. |
| 5805 | * @return LY_SUCCESS on success. |
| 5806 | * @return LY_ENOTFOUND if no next node found. |
| 5807 | */ |
| 5808 | static LY_ERR |
| 5809 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5810 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5811 | { |
| 5812 | const struct lyd_node *next = NULL; |
| 5813 | enum lyxp_node_type next_type = 0; |
| 5814 | |
| 5815 | assert(!*iter); |
| 5816 | assert(!*iter_type); |
| 5817 | |
| 5818 | switch (axis) { |
| 5819 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5820 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5821 | case LYXP_AXIS_SELF: |
| 5822 | /* return the context node */ |
| 5823 | next = node; |
| 5824 | next_type = node_type; |
| 5825 | break; |
| 5826 | |
| 5827 | case LYXP_AXIS_ANCESTOR: |
| 5828 | case LYXP_AXIS_PARENT: |
| 5829 | if (node_type == LYXP_NODE_ELEM) { |
| 5830 | next = lyd_parent(node); |
| 5831 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5832 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5833 | next = node; |
| 5834 | next_type = LYXP_NODE_ELEM; |
| 5835 | } else if (node_type == LYXP_NODE_META) { |
| 5836 | next = ((struct lyd_meta *)node)->parent; |
| 5837 | next_type = LYXP_NODE_ELEM; |
| 5838 | } /* else root does not have a parent */ |
| 5839 | break; |
| 5840 | |
| 5841 | case LYXP_AXIS_CHILD: |
| 5842 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5843 | assert(!node); |
| 5844 | |
| 5845 | /* search in all the trees */ |
| 5846 | next = set->tree; |
| 5847 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5848 | } else { |
| 5849 | /* search in children */ |
| 5850 | next = lyd_child(node); |
| 5851 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5852 | } |
| 5853 | break; |
| 5854 | |
| 5855 | case LYXP_AXIS_DESCENDANT: |
| 5856 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5857 | /* top-level nodes */ |
| 5858 | next = set->tree; |
| 5859 | next_type = LYXP_NODE_ELEM; |
| 5860 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5861 | /* start from the context node */ |
| 5862 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5863 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5864 | } /* else no children */ |
| 5865 | break; |
| 5866 | |
| 5867 | case LYXP_AXIS_FOLLOWING: |
| 5868 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5869 | if (node_type == LYXP_NODE_ELEM) { |
| 5870 | /* first next sibling */ |
| 5871 | next = node->next; |
| 5872 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5873 | } /* else no sibling */ |
| 5874 | break; |
| 5875 | |
| 5876 | case LYXP_AXIS_PRECEDING: |
| 5877 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5878 | /* skip ancestors */ |
| 5879 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5880 | assert(next); |
| 5881 | next_type = LYXP_NODE_ELEM; |
| 5882 | } /* else no sibling */ |
| 5883 | break; |
| 5884 | |
| 5885 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5886 | if (node_type == LYXP_NODE_ELEM) { |
| 5887 | /* first previous sibling */ |
| 5888 | next = node->prev->next ? node->prev : NULL; |
| 5889 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5890 | } /* else no sibling */ |
| 5891 | break; |
| 5892 | |
| 5893 | case LYXP_AXIS_ATTRIBUTE: |
| 5894 | /* handled specially */ |
| 5895 | assert(0); |
| 5896 | LOGINT(set->ctx); |
| 5897 | break; |
| 5898 | } |
| 5899 | |
| 5900 | *iter = next; |
| 5901 | *iter_type = next_type; |
| 5902 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5903 | } |
| 5904 | |
| 5905 | /** |
| 5906 | * @brief Iterate over all nodes on an axis for a context node. |
| 5907 | * |
| 5908 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5909 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5910 | * @param[in] node Context node. |
| 5911 | * @param[in] node_type Type of @p node. |
| 5912 | * @param[in] axis Axis to use. |
| 5913 | * @param[in] set XPath set with the general context. |
| 5914 | * @return LY_SUCCESS on success. |
| 5915 | * @return LY_ENOTFOUND if no next node found. |
| 5916 | */ |
| 5917 | static LY_ERR |
| 5918 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5919 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5920 | { |
| 5921 | const struct lyd_node *next = NULL; |
| 5922 | enum lyxp_node_type next_type = 0; |
| 5923 | |
| 5924 | if (!*iter_type) { |
| 5925 | /* first returned node */ |
| 5926 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5927 | } |
| 5928 | |
| 5929 | switch (axis) { |
| 5930 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5931 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5932 | /* fake first ancestor, we returned self before */ |
| 5933 | *iter = NULL; |
| 5934 | *iter_type = 0; |
| 5935 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 5936 | } /* else continue ancestor */ |
| 5937 | |
| 5938 | /* fallthrough */ |
| 5939 | case LYXP_AXIS_ANCESTOR: |
| 5940 | if (*iter_type == LYXP_NODE_ELEM) { |
| 5941 | /* iter parent */ |
| 5942 | next = lyd_parent(*iter); |
| 5943 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5944 | } /* else root, no ancestors */ |
| 5945 | break; |
| 5946 | |
| 5947 | case LYXP_AXIS_CHILD: |
| 5948 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5949 | |
| 5950 | /* next sibling (child) */ |
| 5951 | next = (*iter)->next; |
| 5952 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5953 | break; |
| 5954 | |
| 5955 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5956 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5957 | /* fake first descendant, we returned self before */ |
| 5958 | *iter = NULL; |
| 5959 | *iter_type = 0; |
| 5960 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 5961 | } /* else continue descendant */ |
| 5962 | |
| 5963 | /* fallthrough */ |
| 5964 | case LYXP_AXIS_DESCENDANT: |
| 5965 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5966 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 5967 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5968 | break; |
| 5969 | |
| 5970 | case LYXP_AXIS_FOLLOWING: |
| 5971 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5972 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 5973 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5974 | break; |
| 5975 | |
| 5976 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5977 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5978 | |
| 5979 | /* next sibling */ |
| 5980 | next = (*iter)->next; |
| 5981 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5982 | break; |
| 5983 | |
| 5984 | case LYXP_AXIS_PARENT: |
| 5985 | case LYXP_AXIS_SELF: |
| 5986 | /* parent/self was returned before */ |
| 5987 | break; |
| 5988 | |
| 5989 | case LYXP_AXIS_PRECEDING: |
| 5990 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5991 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 5992 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5993 | break; |
| 5994 | |
| 5995 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5996 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5997 | |
| 5998 | /* previous sibling */ |
| 5999 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 6000 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6001 | break; |
| 6002 | |
| 6003 | case LYXP_AXIS_ATTRIBUTE: |
| 6004 | /* handled specially */ |
| 6005 | assert(0); |
| 6006 | LOGINT(set->ctx); |
| 6007 | break; |
| 6008 | } |
| 6009 | |
| 6010 | *iter = next; |
| 6011 | *iter_type = next_type; |
| 6012 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6013 | } |
| 6014 | |
| 6015 | /** |
| 6016 | * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6017 | * |
| 6018 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6019 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6020 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6021 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6022 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6023 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6024 | */ |
| 6025 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6026 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6027 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6028 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6029 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6030 | const struct lyd_node *iter; |
| 6031 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6032 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6033 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6034 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6035 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6036 | return LY_SUCCESS; |
| 6037 | } |
| 6038 | |
| 6039 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6040 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6041 | return LY_EVALID; |
| 6042 | } |
| 6043 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6044 | /* init result set */ |
| 6045 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6047 | for (i = 0; i < set->used; ++i) { |
| 6048 | /* iterate over all the nodes on the axis of the node */ |
| 6049 | iter = NULL; |
| 6050 | iter_type = 0; |
| 6051 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6052 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6053 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6054 | rc = r; |
| 6055 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6056 | } else if (r) { |
| 6057 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6058 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6059 | |
| 6060 | /* check for duplicates if they are possible */ |
| 6061 | switch (axis) { |
| 6062 | case LYXP_AXIS_ANCESTOR: |
| 6063 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6064 | case LYXP_AXIS_DESCENDANT: |
| 6065 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6066 | case LYXP_AXIS_FOLLOWING: |
| 6067 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6068 | case LYXP_AXIS_PARENT: |
| 6069 | case LYXP_AXIS_PRECEDING: |
| 6070 | case LYXP_AXIS_PRECEDING_SIBLING: |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6071 | result.non_child_axis = 1; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6072 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6073 | continue; |
| 6074 | } |
| 6075 | break; |
| 6076 | case LYXP_AXIS_CHILD: |
| 6077 | case LYXP_AXIS_SELF: |
| 6078 | break; |
| 6079 | case LYXP_AXIS_ATTRIBUTE: |
| 6080 | /* handled specially */ |
| 6081 | assert(0); |
| 6082 | LOGINT(set->ctx); |
| 6083 | break; |
| 6084 | } |
| 6085 | |
| 6086 | /* matching node */ |
| 6087 | set_insert_node(&result, iter, 0, iter_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6088 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6089 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6090 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6091 | /* move result to the set */ |
| 6092 | lyxp_set_free_content(set); |
| 6093 | *set = result; |
| 6094 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6095 | |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6096 | /* sort the final set if the document order could have been broken */ |
| 6097 | if (set->non_child_axis) { |
| 6098 | set_sort(set); |
| 6099 | } else { |
| 6100 | assert(!set_sort(set)); |
| 6101 | } |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6102 | |
| 6103 | cleanup: |
| 6104 | lyxp_set_free_content(&result); |
| 6105 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6106 | } |
| 6107 | |
| 6108 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6109 | * @brief Move context @p set to child nodes using hashes. Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6110 | * |
| 6111 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6112 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6113 | * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6114 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6115 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6116 | */ |
| 6117 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6118 | moveto_node_hash_child(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates, |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6119 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6120 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6121 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6122 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6123 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6124 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6125 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6126 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6127 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6128 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6129 | /* init result set */ |
| 6130 | set_init(&result, set); |
| 6131 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6132 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6133 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6134 | } |
| 6135 | |
| 6136 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6137 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6138 | ret = LY_EVALID; |
| 6139 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | /* context check for all the nodes since we have the schema node */ |
| 6143 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6144 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6145 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6146 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6147 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6148 | lyxp_set_free_content(set); |
| 6149 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6150 | } |
| 6151 | |
| 6152 | /* create specific data instance if needed */ |
| 6153 | if (scnode->nodetype == LYS_LIST) { |
| 6154 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 6155 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6156 | LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6157 | } |
| 6158 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6159 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6160 | siblings = NULL; |
| 6161 | |
| 6162 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6163 | assert(!set->val.nodes[i].node); |
| 6164 | |
| 6165 | /* search in all the trees */ |
| 6166 | siblings = set->tree; |
| 6167 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6168 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6169 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6170 | } |
| 6171 | |
| 6172 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6173 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6174 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6175 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6176 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6177 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6178 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6179 | |
| 6180 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6181 | if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6182 | ret = LY_EINCOMPLETE; |
| 6183 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | if (sub) { |
| 6187 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6188 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6189 | } |
| 6190 | } |
| 6191 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6192 | /* move result to the set */ |
| 6193 | lyxp_set_free_content(set); |
| 6194 | *set = result; |
| 6195 | result.type = LYXP_SET_NUMBER; |
| 6196 | assert(!set_sort(set)); |
| 6197 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6198 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6199 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6200 | lyd_free_tree(inst); |
| 6201 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6202 | } |
| 6203 | |
| 6204 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6205 | * @brief Check @p node as a part of schema NameTest processing. |
| 6206 | * |
| 6207 | * @param[in] node Schema node to check. |
| 6208 | * @param[in] ctx_scnode Context node. |
| 6209 | * @param[in] set Set to read general context from. |
| 6210 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6211 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6212 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6213 | */ |
| 6214 | static LY_ERR |
| 6215 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6216 | const char *node_name, const struct lys_module *moveto_mod) |
| 6217 | { |
| 6218 | if (!moveto_mod && node_name) { |
| 6219 | switch (set->format) { |
| 6220 | case LY_VALUE_SCHEMA: |
| 6221 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6222 | /* use current module */ |
| 6223 | moveto_mod = set->cur_mod; |
| 6224 | break; |
| 6225 | case LY_VALUE_JSON: |
| 6226 | case LY_VALUE_LYB: |
| 6227 | case LY_VALUE_STR_NS: |
| 6228 | /* inherit module of the context node, if any */ |
| 6229 | if (ctx_scnode) { |
| 6230 | moveto_mod = ctx_scnode->module; |
| 6231 | } |
| 6232 | break; |
| 6233 | case LY_VALUE_CANON: |
| 6234 | case LY_VALUE_XML: |
| 6235 | /* not defined */ |
| 6236 | LOGINT(set->ctx); |
| 6237 | return LY_EINVAL; |
| 6238 | } |
| 6239 | } |
| 6240 | |
| 6241 | if (!node) { |
| 6242 | /* root will not match a specific node */ |
| 6243 | if (node_name || moveto_mod) { |
| 6244 | return LY_ENOT; |
| 6245 | } |
| 6246 | return LY_SUCCESS; |
| 6247 | } |
| 6248 | |
| 6249 | /* module check */ |
| 6250 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6251 | return LY_ENOT; |
| 6252 | } |
| 6253 | |
| 6254 | /* context check */ |
| 6255 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6256 | return LY_EINVAL; |
| 6257 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6258 | return LY_EINVAL; |
| 6259 | } |
| 6260 | |
| 6261 | /* name check */ |
| 6262 | if (node_name && (node->name != node_name)) { |
| 6263 | return LY_ENOT; |
| 6264 | } |
| 6265 | |
| 6266 | /* match */ |
| 6267 | return LY_SUCCESS; |
| 6268 | } |
| 6269 | |
| 6270 | /** |
| 6271 | * @brief Get the next node in a forward schema node DFS. |
| 6272 | * |
| 6273 | * @param[in] iter Last returned node. |
| 6274 | * @param[in] stop Node to stop the search on and not return. |
| 6275 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6276 | * @return Next node, NULL if there are no more. |
| 6277 | */ |
| 6278 | static const struct lysc_node * |
| 6279 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6280 | { |
| 6281 | const struct lysc_node *next = NULL; |
| 6282 | |
| 6283 | next = lysc_node_child(iter); |
| 6284 | if (!next) { |
| 6285 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6286 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6287 | /* we are done, no next element to process */ |
| 6288 | return NULL; |
| 6289 | } |
| 6290 | |
| 6291 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6292 | } |
| 6293 | while (!next && iter) { |
| 6294 | /* parent is already processed, go to its sibling */ |
| 6295 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6296 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6297 | /* we are done, no next element to process */ |
| 6298 | return NULL; |
| 6299 | } |
| 6300 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6301 | } |
| 6302 | |
| 6303 | return next; |
| 6304 | } |
| 6305 | |
| 6306 | /** |
| 6307 | * @brief Consider schema node based on its in_ctx enum value. |
| 6308 | * |
| 6309 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6310 | * @param[in] axis Axis to use. |
| 6311 | * @return LY_SUCCESS on success. |
| 6312 | * @return LY_ENOT if the node should not be returned. |
| 6313 | */ |
| 6314 | static LY_ERR |
| 6315 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6316 | { |
| 6317 | switch (axis) { |
| 6318 | case LYXP_AXIS_SELF: |
| 6319 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6320 | /* additionally put the start node into context */ |
| 6321 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6322 | return LY_SUCCESS; |
| 6323 | } |
| 6324 | break; |
| 6325 | case LYXP_AXIS_PARENT: |
| 6326 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6327 | case LYXP_AXIS_ANCESTOR: |
| 6328 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6329 | case LYXP_AXIS_DESCENDANT: |
| 6330 | case LYXP_AXIS_FOLLOWING: |
| 6331 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6332 | case LYXP_AXIS_PRECEDING: |
| 6333 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6334 | case LYXP_AXIS_CHILD: |
| 6335 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6336 | /* remember that context node was used */ |
| 6337 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6338 | return LY_SUCCESS; |
| 6339 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6340 | /* traversed */ |
| 6341 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6342 | return LY_SUCCESS; |
| 6343 | } |
| 6344 | break; |
| 6345 | case LYXP_AXIS_ATTRIBUTE: |
| 6346 | /* unreachable */ |
| 6347 | assert(0); |
| 6348 | LOGINT(NULL); |
| 6349 | break; |
| 6350 | } |
| 6351 | |
| 6352 | return LY_ENOT; |
| 6353 | } |
| 6354 | |
| 6355 | /** |
| 6356 | * @brief Get previous sibling for a schema node. |
| 6357 | * |
| 6358 | * @param[in] scnode Schema node. |
| 6359 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6360 | * @return Previous sibling, NULL if none. |
| 6361 | */ |
| 6362 | static const struct lysc_node * |
| 6363 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6364 | { |
| 6365 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6366 | |
| 6367 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6368 | if (next == scnode) { |
| 6369 | break; |
| 6370 | } |
| 6371 | |
| 6372 | prev = next; |
| 6373 | } |
| 6374 | |
| 6375 | return prev; |
| 6376 | } |
| 6377 | |
| 6378 | /** |
| 6379 | * @brief Get the first schema node on an axis for a context node. |
| 6380 | * |
| 6381 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6382 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6383 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6384 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6385 | * @param[in] scnode Context node. |
| 6386 | * @param[in] node_type Type of @p scnode. |
| 6387 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6388 | * @param[in] axis Axis to use. |
| 6389 | * @param[in] set XPath set with the general context. |
| 6390 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6391 | * @return LY_SUCCESS on success. |
| 6392 | * @return LY_ENOTFOUND if no next node found. |
| 6393 | */ |
| 6394 | static LY_ERR |
| 6395 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6396 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6397 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6398 | { |
| 6399 | const struct lysc_node *next = NULL; |
| 6400 | enum lyxp_node_type next_type = 0; |
| 6401 | |
| 6402 | assert(!*iter); |
| 6403 | assert(!*iter_type); |
| 6404 | |
| 6405 | *iter_mod = NULL; |
| 6406 | *iter_mod_idx = 0; |
| 6407 | |
| 6408 | switch (axis) { |
| 6409 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6410 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6411 | case LYXP_AXIS_SELF: |
| 6412 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6413 | /* just return the node */ |
| 6414 | next = scnode; |
| 6415 | next_type = node_type; |
| 6416 | } |
| 6417 | break; |
| 6418 | |
| 6419 | case LYXP_AXIS_ANCESTOR: |
| 6420 | case LYXP_AXIS_PARENT: |
| 6421 | if (node_type == LYXP_NODE_ELEM) { |
| 6422 | next = lysc_data_parent(scnode); |
| 6423 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6424 | } /* else no parent */ |
| 6425 | break; |
| 6426 | |
| 6427 | case LYXP_AXIS_DESCENDANT: |
| 6428 | case LYXP_AXIS_CHILD: |
| 6429 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6430 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6431 | * it can be in a top-level augment */ |
| 6432 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6433 | /* module may not be implemented or not compiled yet */ |
| 6434 | if (!(*iter_mod)->compiled) { |
| 6435 | continue; |
| 6436 | } |
| 6437 | |
| 6438 | /* get next node */ |
| 6439 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6440 | next_type = LYXP_NODE_ELEM; |
| 6441 | break; |
| 6442 | } |
| 6443 | } |
| 6444 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6445 | /* get next node */ |
| 6446 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6447 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6448 | } |
| 6449 | break; |
| 6450 | |
| 6451 | case LYXP_AXIS_FOLLOWING: |
| 6452 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6453 | if (node_type == LYXP_NODE_ELEM) { |
| 6454 | /* first next sibling */ |
| 6455 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6456 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6457 | } /* else no sibling */ |
| 6458 | break; |
| 6459 | |
| 6460 | case LYXP_AXIS_PRECEDING: |
| 6461 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6462 | if (node_type == LYXP_NODE_ELEM) { |
| 6463 | /* first parent sibling */ |
| 6464 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6465 | if (next == scnode) { |
| 6466 | /* no preceding sibling */ |
| 6467 | next = NULL; |
| 6468 | } |
| 6469 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6470 | } /* else no sibling */ |
| 6471 | break; |
| 6472 | |
| 6473 | case LYXP_AXIS_ATTRIBUTE: |
| 6474 | /* unreachable */ |
| 6475 | assert(0); |
| 6476 | LOGINT(set->ctx); |
| 6477 | break; |
| 6478 | } |
| 6479 | |
| 6480 | *iter = next; |
| 6481 | *iter_type = next_type; |
| 6482 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6483 | } |
| 6484 | |
| 6485 | /** |
| 6486 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6487 | * |
| 6488 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6489 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6490 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6491 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6492 | * @param[in] scnode Context node. |
| 6493 | * @param[in] node_type Type of @p scnode. |
| 6494 | * @param[in] axis Axis to use. |
| 6495 | * @param[in] set XPath set with the general context. |
| 6496 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6497 | * @return LY_SUCCESS on success. |
| 6498 | * @return LY_ENOTFOUND if no next node found. |
| 6499 | */ |
| 6500 | static LY_ERR |
| 6501 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6502 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6503 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6504 | { |
| 6505 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6506 | enum lyxp_node_type next_type = 0; |
| 6507 | |
| 6508 | if (!*iter_type) { |
| 6509 | /* first returned node */ |
| 6510 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6511 | getnext_opts); |
| 6512 | } |
| 6513 | |
| 6514 | switch (axis) { |
| 6515 | case LYXP_AXIS_PARENT: |
| 6516 | case LYXP_AXIS_SELF: |
| 6517 | /* parent/self was returned before */ |
| 6518 | break; |
| 6519 | |
| 6520 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6521 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6522 | /* fake first ancestor, we returned self before */ |
| 6523 | *iter = NULL; |
| 6524 | *iter_type = 0; |
| 6525 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6526 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
| 6527 | } /* else continue ancestor */ |
| 6528 | |
| 6529 | /* fallthrough */ |
| 6530 | case LYXP_AXIS_ANCESTOR: |
| 6531 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6532 | next = lysc_data_parent(*iter); |
| 6533 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6534 | } /* else no ancestor */ |
| 6535 | break; |
| 6536 | |
| 6537 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6538 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6539 | /* fake first descendant, we returned self before */ |
| 6540 | *iter = NULL; |
| 6541 | *iter_type = 0; |
| 6542 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6543 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6544 | } /* else DFS until context node */ |
| 6545 | dfs_stop = scnode; |
| 6546 | |
| 6547 | /* fallthrough */ |
| 6548 | case LYXP_AXIS_DESCENDANT: |
| 6549 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6550 | /* DFS until the context node */ |
| 6551 | dfs_stop = scnode; |
| 6552 | } |
| 6553 | |
| 6554 | /* fallthrough */ |
| 6555 | case LYXP_AXIS_PRECEDING: |
| 6556 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6557 | /* DFS until the previous sibling */ |
| 6558 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6559 | assert(dfs_stop); |
| 6560 | |
| 6561 | if (*iter == dfs_stop) { |
| 6562 | /* we are done */ |
| 6563 | break; |
| 6564 | } |
| 6565 | } |
| 6566 | |
| 6567 | /* fallthrough */ |
| 6568 | case LYXP_AXIS_FOLLOWING: |
| 6569 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6570 | /* DFS through the whole module */ |
| 6571 | dfs_stop = NULL; |
| 6572 | } |
| 6573 | |
| 6574 | /* nested nodes */ |
| 6575 | assert(*iter); |
| 6576 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6577 | if (next) { |
| 6578 | next_type = LYXP_NODE_ELEM; |
| 6579 | break; |
| 6580 | } /* else get next top-level node just like a child */ |
| 6581 | |
| 6582 | /* fallthrough */ |
| 6583 | case LYXP_AXIS_CHILD: |
| 6584 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6585 | if (!*iter_mod) { |
| 6586 | /* nodes from a single module */ |
| 6587 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6588 | next_type = LYXP_NODE_ELEM; |
| 6589 | break; |
| 6590 | } |
| 6591 | |
| 6592 | assert(scnode); |
| 6593 | if (!lysc_data_parent(scnode)) { |
| 6594 | /* iterating over top-level nodes, find next */ |
| 6595 | while (lysc_data_parent(*iter)) { |
| 6596 | *iter = lysc_data_parent(*iter); |
| 6597 | } |
| 6598 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6599 | next_type = LYXP_NODE_ELEM; |
| 6600 | break; |
| 6601 | } |
| 6602 | } |
| 6603 | } |
| 6604 | |
| 6605 | while (*iter_mod) { |
| 6606 | /* module top-level nodes */ |
| 6607 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6608 | next_type = LYXP_NODE_ELEM; |
| 6609 | break; |
| 6610 | } |
| 6611 | |
| 6612 | /* get next module */ |
| 6613 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6614 | /* module may not be implemented or not compiled yet */ |
| 6615 | if ((*iter_mod)->compiled) { |
| 6616 | break; |
| 6617 | } |
| 6618 | } |
| 6619 | |
| 6620 | /* new module, start over */ |
| 6621 | *iter = NULL; |
| 6622 | } |
| 6623 | break; |
| 6624 | |
| 6625 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6626 | assert(*iter); |
| 6627 | |
| 6628 | /* next parent sibling until scnode */ |
| 6629 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6630 | if (next == scnode) { |
| 6631 | /* no previous sibling */ |
| 6632 | next = NULL; |
| 6633 | } |
| 6634 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6635 | break; |
| 6636 | |
| 6637 | case LYXP_AXIS_ATTRIBUTE: |
| 6638 | /* unreachable */ |
| 6639 | assert(0); |
| 6640 | LOGINT(set->ctx); |
| 6641 | break; |
| 6642 | } |
| 6643 | |
| 6644 | *iter = next; |
| 6645 | *iter_type = next_type; |
| 6646 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6647 | } |
| 6648 | |
| 6649 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6650 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6651 | * |
| 6652 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6653 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6654 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6655 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6656 | * @param[in] options XPath options. |
| 6657 | * @return LY_ERR |
| 6658 | */ |
| 6659 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6660 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6661 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6662 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6663 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6664 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6665 | const struct lys_module *mod; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6666 | const struct lysc_node *iter; |
| 6667 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6668 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6669 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6670 | return LY_SUCCESS; |
| 6671 | } |
| 6672 | |
| 6673 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6674 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6675 | return LY_EVALID; |
| 6676 | } |
| 6677 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6678 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6679 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6680 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6681 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6682 | } |
| 6683 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6684 | orig_used = set->used; |
| 6685 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6686 | /* update in_ctx first */ |
| 6687 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6688 | /* not usable, skip */ |
| 6689 | continue; |
| 6690 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6691 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6692 | iter = NULL; |
| 6693 | iter_type = 0; |
| 6694 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6695 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6696 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6697 | continue; |
| 6698 | } |
| 6699 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6700 | /* insert */ |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 6701 | 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] | 6702 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6703 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6704 | if ((idx < orig_used) && (idx > i)) { |
| 6705 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6706 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6707 | } |
| 6708 | } |
| 6709 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6710 | |
| 6711 | /* correct temporary in_ctx values */ |
| 6712 | if (temp_ctx) { |
| 6713 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6714 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6715 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6716 | } |
| 6717 | } |
| 6718 | } |
| 6719 | |
| 6720 | return LY_SUCCESS; |
| 6721 | } |
| 6722 | |
| 6723 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6724 | * @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] | 6725 | * Context position aware. |
| 6726 | * |
| 6727 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6728 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6729 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6730 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6731 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6732 | */ |
| 6733 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6734 | 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] | 6735 | { |
| 6736 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6737 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6738 | struct lyxp_set ret_set; |
| 6739 | LY_ERR rc; |
| 6740 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6741 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6742 | return LY_SUCCESS; |
| 6743 | } |
| 6744 | |
| 6745 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6746 | 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] | 6747 | return LY_EVALID; |
| 6748 | } |
| 6749 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6750 | /* 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] | 6751 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6752 | LY_CHECK_RET(rc); |
| 6753 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6754 | /* 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] | 6755 | set_init(&ret_set, set); |
| 6756 | for (i = 0; i < set->used; ++i) { |
| 6757 | |
| 6758 | /* TREE DFS */ |
| 6759 | start = set->val.nodes[i].node; |
| 6760 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6761 | 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] | 6762 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6763 | /* add matching node into result set */ |
| 6764 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6765 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6766 | /* the node is a duplicate, we'll process it later in the set */ |
| 6767 | goto skip_children; |
| 6768 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6769 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6770 | return rc; |
| 6771 | } else if (rc == LY_EINVAL) { |
| 6772 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6773 | } |
| 6774 | |
| 6775 | /* TREE DFS NEXT ELEM */ |
| 6776 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6777 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6778 | if (!next) { |
| 6779 | skip_children: |
| 6780 | /* no children, so try siblings, but only if it's not the start, |
| 6781 | * that is considered to be the root and it's siblings are not traversed */ |
| 6782 | if (elem != start) { |
| 6783 | next = elem->next; |
| 6784 | } else { |
| 6785 | break; |
| 6786 | } |
| 6787 | } |
| 6788 | while (!next) { |
| 6789 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6790 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6791 | /* we are done, no next element to process */ |
| 6792 | break; |
| 6793 | } |
| 6794 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6795 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6796 | next = elem->next; |
| 6797 | } |
| 6798 | } |
| 6799 | } |
| 6800 | |
| 6801 | /* make the temporary set the current one */ |
| 6802 | ret_set.ctx_pos = set->ctx_pos; |
| 6803 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6804 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6805 | memcpy(set, &ret_set, sizeof *set); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6806 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6807 | |
| 6808 | return LY_SUCCESS; |
| 6809 | } |
| 6810 | |
| 6811 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6812 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6813 | * Result is LYXP_SET_NODE_SET. |
| 6814 | * |
| 6815 | * @param[in] set Set to use. |
| 6816 | * @param[in] start Start node whose subtree to add. |
| 6817 | * @param[in] start_idx Index of @p start in @p set. |
| 6818 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6819 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6820 | * @param[in] options XPath options. |
| 6821 | * @return LY_ERR value. |
| 6822 | */ |
| 6823 | static LY_ERR |
| 6824 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6825 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6826 | { |
| 6827 | const struct lysc_node *next, *elem; |
| 6828 | uint32_t idx; |
| 6829 | LY_ERR rc; |
| 6830 | |
| 6831 | /* TREE DFS */ |
| 6832 | for (elem = next = start; elem; elem = next) { |
| 6833 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6834 | /* schema-only nodes, skip root */ |
| 6835 | goto next_iter; |
| 6836 | } |
| 6837 | |
| 6838 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6839 | if (!rc) { |
| 6840 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6841 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6842 | if (idx > start_idx) { |
| 6843 | /* we will process it later in the set */ |
| 6844 | goto skip_children; |
| 6845 | } |
| 6846 | } else { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 6847 | 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] | 6848 | } |
| 6849 | } else if (rc == LY_EINVAL) { |
| 6850 | goto skip_children; |
| 6851 | } |
| 6852 | |
| 6853 | next_iter: |
| 6854 | /* TREE DFS NEXT ELEM */ |
| 6855 | /* select element for the next run - children first */ |
| 6856 | next = lysc_node_child(elem); |
| 6857 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6858 | next = next->next; |
| 6859 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6860 | next = next->next; |
| 6861 | } |
| 6862 | if (!next) { |
| 6863 | skip_children: |
| 6864 | /* no children, so try siblings, but only if it's not the start, |
| 6865 | * that is considered to be the root and it's siblings are not traversed */ |
| 6866 | if (elem != start) { |
| 6867 | next = elem->next; |
| 6868 | } else { |
| 6869 | break; |
| 6870 | } |
| 6871 | } |
| 6872 | while (!next) { |
| 6873 | /* no siblings, go back through the parents */ |
| 6874 | if (elem->parent == start) { |
| 6875 | /* we are done, no next element to process */ |
| 6876 | break; |
| 6877 | } |
| 6878 | /* parent is already processed, go to its sibling */ |
| 6879 | elem = elem->parent; |
| 6880 | next = elem->next; |
| 6881 | } |
| 6882 | } |
| 6883 | |
| 6884 | return LY_SUCCESS; |
| 6885 | } |
| 6886 | |
| 6887 | /** |
| 6888 | * @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] | 6889 | * |
| 6890 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6891 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6892 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6893 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6894 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6895 | */ |
| 6896 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6897 | 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] | 6898 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6899 | uint32_t i, orig_used, mod_idx; |
| 6900 | const struct lys_module *mod; |
| 6901 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6902 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6903 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6904 | return LY_SUCCESS; |
| 6905 | } |
| 6906 | |
| 6907 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6908 | 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] | 6909 | return LY_EVALID; |
| 6910 | } |
| 6911 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6912 | orig_used = set->used; |
| 6913 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6914 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6915 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6916 | continue; |
| 6917 | } |
| 6918 | |
| 6919 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6920 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6921 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6922 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6923 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6924 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6925 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6926 | /* traverse all top-level nodes in all the modules */ |
| 6927 | mod_idx = 0; |
| 6928 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6929 | /* module may not be implemented or not compiled yet */ |
| 6930 | if (!mod->compiled) { |
| 6931 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6932 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6933 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6934 | root = NULL; |
| 6935 | /* no getnext opts needed */ |
| 6936 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 6937 | 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] | 6938 | } |
| 6939 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6940 | |
| 6941 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6942 | /* add all the descendants recursively */ |
| 6943 | 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] | 6944 | } |
| 6945 | } |
| 6946 | |
| 6947 | return LY_SUCCESS; |
| 6948 | } |
| 6949 | |
| 6950 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6951 | * @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] | 6952 | * Indirectly context position aware. |
| 6953 | * |
| 6954 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6955 | * @param[in] mod Matching metadata module, NULL for any. |
| 6956 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6957 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6958 | * @return LY_ERR |
| 6959 | */ |
| 6960 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6961 | 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] | 6962 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6963 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6964 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6965 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6966 | return LY_SUCCESS; |
| 6967 | } |
| 6968 | |
| 6969 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6970 | 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] | 6971 | return LY_EVALID; |
| 6972 | } |
| 6973 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6974 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6975 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6976 | |
| 6977 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 6978 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6979 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6980 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6981 | |
| 6982 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6983 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6984 | continue; |
| 6985 | } |
| 6986 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6987 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6988 | /* match */ |
| 6989 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6990 | set->val.meta[i].meta = sub; |
| 6991 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6992 | /* pos does not change */ |
| 6993 | replaced = 1; |
| 6994 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6995 | 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] | 6996 | } |
| 6997 | ++i; |
| 6998 | } |
| 6999 | } |
| 7000 | } |
| 7001 | |
| 7002 | if (!replaced) { |
| 7003 | /* no match */ |
| 7004 | set_remove_node(set, i); |
| 7005 | } |
| 7006 | } |
| 7007 | |
| 7008 | return LY_SUCCESS; |
| 7009 | } |
| 7010 | |
| 7011 | /** |
| 7012 | * @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] | 7013 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7014 | * |
| 7015 | * @param[in,out] set1 Set to use for the result. |
| 7016 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7017 | * @return LY_ERR |
| 7018 | */ |
| 7019 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7020 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7021 | { |
| 7022 | LY_ERR rc; |
| 7023 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7024 | 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] | 7025 | 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] | 7026 | return LY_EVALID; |
| 7027 | } |
| 7028 | |
| 7029 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7030 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7031 | return LY_SUCCESS; |
| 7032 | } |
| 7033 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7034 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 7035 | /* release hidden allocated data (lyxp_set.size) */ |
| 7036 | lyxp_set_free_content(set1); |
| 7037 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7038 | memcpy(set1, set2, sizeof *set1); |
| 7039 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7040 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7041 | return LY_SUCCESS; |
| 7042 | } |
| 7043 | |
| 7044 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7045 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7046 | |
| 7047 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7048 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7049 | LY_CHECK_RET(rc); |
| 7050 | |
| 7051 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7052 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7053 | |
| 7054 | return LY_SUCCESS; |
| 7055 | } |
| 7056 | |
| 7057 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7058 | * @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] | 7059 | * Context position aware. |
| 7060 | * |
| 7061 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7062 | * @param[in] mod Matching metadata module, NULL for any. |
| 7063 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7064 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7065 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7066 | */ |
| 7067 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7068 | 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] | 7069 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7070 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7071 | struct lyxp_set *set_all_desc = NULL; |
| 7072 | LY_ERR rc; |
| 7073 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7074 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7075 | return LY_SUCCESS; |
| 7076 | } |
| 7077 | |
| 7078 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7079 | 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] | 7080 | return LY_EVALID; |
| 7081 | } |
| 7082 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7083 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7084 | * but it likely won't be used much, so it's a waste of time */ |
| 7085 | /* copy the context */ |
| 7086 | set_all_desc = set_copy(set); |
| 7087 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7088 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7089 | if (rc != LY_SUCCESS) { |
| 7090 | lyxp_set_free(set_all_desc); |
| 7091 | return rc; |
| 7092 | } |
| 7093 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7094 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7095 | if (rc != LY_SUCCESS) { |
| 7096 | lyxp_set_free(set_all_desc); |
| 7097 | return rc; |
| 7098 | } |
| 7099 | lyxp_set_free(set_all_desc); |
| 7100 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7101 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7102 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7103 | |
| 7104 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7105 | * we have all attributes qualified in lyd tree */ |
| 7106 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7107 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7108 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7109 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7110 | continue; |
| 7111 | } |
| 7112 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7113 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7114 | /* match */ |
| 7115 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7116 | set->val.meta[i].meta = sub; |
| 7117 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7118 | /* pos does not change */ |
| 7119 | replaced = 1; |
| 7120 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7121 | 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] | 7122 | } |
| 7123 | ++i; |
| 7124 | } |
| 7125 | } |
| 7126 | } |
| 7127 | |
| 7128 | if (!replaced) { |
| 7129 | /* no match */ |
| 7130 | set_remove_node(set, i); |
| 7131 | } |
| 7132 | } |
| 7133 | |
| 7134 | return LY_SUCCESS; |
| 7135 | } |
| 7136 | |
| 7137 | /** |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7138 | * @brief Move context @p set1 single item to the result of a comparison. |
| 7139 | * |
| 7140 | * @param[in] set1 First set with the item to compare. |
| 7141 | * @param[in] idx1 Index of the item in @p set1. |
| 7142 | * @param[in] set2 Second set. |
| 7143 | * @param[in] op Comparison operator to process. |
| 7144 | * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`. |
| 7145 | * @param[out] result Result of the comparison. |
| 7146 | * @return LY_ERR value. |
| 7147 | */ |
| 7148 | static LY_ERR |
| 7149 | moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op, |
| 7150 | ly_bool switch_operands, ly_bool *result) |
| 7151 | { |
| 7152 | struct lyxp_set tmp1 = {0}; |
| 7153 | LY_ERR rc = LY_SUCCESS; |
| 7154 | |
| 7155 | assert(set1->type == LYXP_SET_NODE_SET); |
| 7156 | |
| 7157 | /* cast set1 */ |
| 7158 | switch (set2->type) { |
| 7159 | case LYXP_SET_NUMBER: |
| 7160 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); |
| 7161 | break; |
| 7162 | case LYXP_SET_BOOLEAN: |
| 7163 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); |
| 7164 | break; |
| 7165 | default: |
| 7166 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); |
| 7167 | break; |
| 7168 | } |
| 7169 | LY_CHECK_GOTO(rc, cleanup); |
| 7170 | |
| 7171 | /* canonize set2 */ |
| 7172 | LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup); |
| 7173 | |
| 7174 | /* compare recursively and store the result */ |
| 7175 | if (switch_operands) { |
| 7176 | LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op), cleanup); |
| 7177 | *result = set2->val.bln; |
| 7178 | } else { |
| 7179 | LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op), cleanup); |
| 7180 | *result = tmp1.val.bln; |
| 7181 | } |
| 7182 | |
| 7183 | cleanup: |
| 7184 | lyxp_set_free_content(&tmp1); |
| 7185 | return rc; |
| 7186 | } |
| 7187 | |
| 7188 | /** |
| 7189 | * @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] | 7190 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7191 | * |
| 7192 | * @param[in,out] set1 Set to use for the result. |
| 7193 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7194 | * @param[in] op Comparison operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7195 | * @return LY_ERR |
| 7196 | */ |
| 7197 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7198 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7199 | { |
| 7200 | /* |
| 7201 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7202 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7203 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7204 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7205 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7206 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7207 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7208 | * |
| 7209 | * '=' or '!=' |
| 7210 | * BOOLEAN + BOOLEAN |
| 7211 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7212 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7213 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7214 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7215 | * NUMBER + NUMBER |
| 7216 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7217 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7218 | * STRING + STRING |
| 7219 | * |
| 7220 | * '<=', '<', '>=', '>' |
| 7221 | * NUMBER + NUMBER |
| 7222 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7223 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7224 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7225 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7226 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7227 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7228 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7229 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7230 | */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7231 | ly_bool result; |
| 7232 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7233 | LY_ERR rc; |
| 7234 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7235 | /* iterative evaluation with node-sets */ |
| 7236 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7237 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7238 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7239 | /* evaluate for the single item */ |
| 7240 | 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] | 7241 | |
| 7242 | /* lazy evaluation until true */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7243 | if (result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7244 | set_fill_boolean(set1, 1); |
| 7245 | return LY_SUCCESS; |
| 7246 | } |
| 7247 | } |
| 7248 | } else { |
| 7249 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7250 | /* evaluate for the single item */ |
| 7251 | 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] | 7252 | |
| 7253 | /* lazy evaluation until true */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7254 | if (result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7255 | set_fill_boolean(set1, 1); |
| 7256 | return LY_SUCCESS; |
| 7257 | } |
| 7258 | } |
| 7259 | } |
| 7260 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7261 | /* false for all the nodes */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7262 | set_fill_boolean(set1, 0); |
| 7263 | return LY_SUCCESS; |
| 7264 | } |
| 7265 | |
| 7266 | /* first convert properly */ |
| 7267 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7268 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7269 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7270 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7271 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7272 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7273 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7274 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7275 | LY_CHECK_RET(rc); |
| 7276 | } /* else we have 2 strings */ |
| 7277 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7278 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7279 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7280 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7281 | LY_CHECK_RET(rc); |
| 7282 | } |
| 7283 | |
| 7284 | assert(set1->type == set2->type); |
| 7285 | |
| 7286 | /* compute result */ |
| 7287 | if (op[0] == '=') { |
| 7288 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7289 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7290 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 7291 | result = (set1->val.num == set2->val.num); |
| 7292 | } else { |
| 7293 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | f60d178 | 2022-08-02 11:00:32 +0200 | [diff] [blame] | 7294 | result = strcmp(set1->val.str, set2->val.str) ? 0 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7295 | } |
| 7296 | } else if (op[0] == '!') { |
| 7297 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7298 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7299 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 7300 | result = (set1->val.num != set2->val.num); |
| 7301 | } else { |
| 7302 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | f60d178 | 2022-08-02 11:00:32 +0200 | [diff] [blame] | 7303 | result = strcmp(set1->val.str, set2->val.str) ? 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7304 | } |
| 7305 | } else { |
| 7306 | assert(set1->type == LYXP_SET_NUMBER); |
| 7307 | if (op[0] == '<') { |
| 7308 | if (op[1] == '=') { |
| 7309 | result = (set1->val.num <= set2->val.num); |
| 7310 | } else { |
| 7311 | result = (set1->val.num < set2->val.num); |
| 7312 | } |
| 7313 | } else { |
| 7314 | if (op[1] == '=') { |
| 7315 | result = (set1->val.num >= set2->val.num); |
| 7316 | } else { |
| 7317 | result = (set1->val.num > set2->val.num); |
| 7318 | } |
| 7319 | } |
| 7320 | } |
| 7321 | |
| 7322 | /* assign result */ |
| 7323 | if (result) { |
| 7324 | set_fill_boolean(set1, 1); |
| 7325 | } else { |
| 7326 | set_fill_boolean(set1, 0); |
| 7327 | } |
| 7328 | |
| 7329 | return LY_SUCCESS; |
| 7330 | } |
| 7331 | |
| 7332 | /** |
| 7333 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7334 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7335 | * |
| 7336 | * @param[in,out] set1 Set to use for the result. |
| 7337 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7338 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7339 | * @return LY_ERR |
| 7340 | */ |
| 7341 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7342 | 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] | 7343 | { |
| 7344 | LY_ERR rc; |
| 7345 | |
| 7346 | /* unary '-' */ |
| 7347 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7348 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7349 | LY_CHECK_RET(rc); |
| 7350 | set1->val.num *= -1; |
| 7351 | lyxp_set_free(set2); |
| 7352 | return LY_SUCCESS; |
| 7353 | } |
| 7354 | |
| 7355 | assert(set1 && set2); |
| 7356 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7357 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7358 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7359 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7360 | LY_CHECK_RET(rc); |
| 7361 | |
| 7362 | switch (op[0]) { |
| 7363 | /* '+' */ |
| 7364 | case '+': |
| 7365 | set1->val.num += set2->val.num; |
| 7366 | break; |
| 7367 | |
| 7368 | /* '-' */ |
| 7369 | case '-': |
| 7370 | set1->val.num -= set2->val.num; |
| 7371 | break; |
| 7372 | |
| 7373 | /* '*' */ |
| 7374 | case '*': |
| 7375 | set1->val.num *= set2->val.num; |
| 7376 | break; |
| 7377 | |
| 7378 | /* 'div' */ |
| 7379 | case 'd': |
| 7380 | set1->val.num /= set2->val.num; |
| 7381 | break; |
| 7382 | |
| 7383 | /* 'mod' */ |
| 7384 | case 'm': |
| 7385 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7386 | break; |
| 7387 | |
| 7388 | default: |
| 7389 | LOGINT_RET(set1->ctx); |
| 7390 | } |
| 7391 | |
| 7392 | return LY_SUCCESS; |
| 7393 | } |
| 7394 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7395 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7396 | * @brief Evaluate Predicate. Logs directly on error. |
| 7397 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7398 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7399 | * |
| 7400 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7401 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7402 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7403 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7404 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7405 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7406 | */ |
| 7407 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7408 | 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] | 7409 | { |
| 7410 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7411 | uint32_t i, orig_exp, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7412 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7413 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7414 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7415 | |
| 7416 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7417 | 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] | 7418 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7419 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7420 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7421 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7422 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7423 | 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] | 7424 | LY_CHECK_RET(rc); |
| 7425 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7426 | /* 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] | 7427 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7428 | |
| 7429 | /* empty set, nothing to evaluate */ |
| 7430 | if (!set->used) { |
| 7431 | goto only_parse; |
| 7432 | } |
| 7433 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7434 | /* decide forward or reverse axis */ |
| 7435 | switch (axis) { |
| 7436 | case LYXP_AXIS_ANCESTOR: |
| 7437 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7438 | case LYXP_AXIS_PRECEDING: |
| 7439 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7440 | reverse_axis = 1; |
| 7441 | break; |
| 7442 | case LYXP_AXIS_DESCENDANT: |
| 7443 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7444 | case LYXP_AXIS_FOLLOWING: |
| 7445 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7446 | case LYXP_AXIS_PARENT: |
| 7447 | case LYXP_AXIS_CHILD: |
| 7448 | case LYXP_AXIS_SELF: |
| 7449 | case LYXP_AXIS_ATTRIBUTE: |
| 7450 | reverse_axis = 0; |
| 7451 | break; |
| 7452 | } |
| 7453 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7454 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7455 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7457 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7458 | set_init(&set2, set); |
| 7459 | 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] | 7460 | |
| 7461 | /* remember the node context position for position() and context size for last() */ |
| 7462 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7463 | |
| 7464 | set2.ctx_pos = orig_pos; |
| 7465 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7466 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7467 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7468 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7469 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7470 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | return rc; |
| 7472 | } |
| 7473 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7474 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | if (set2.type == LYXP_SET_NUMBER) { |
| 7476 | if ((long long)set2.val.num == orig_pos) { |
| 7477 | set2.val.num = 1; |
| 7478 | } else { |
| 7479 | set2.val.num = 0; |
| 7480 | } |
| 7481 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7482 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7483 | |
| 7484 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7485 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7486 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7487 | } |
| 7488 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7489 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7490 | |
| 7491 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7492 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7493 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7494 | /* there is a currently-valid node */ |
| 7495 | break; |
| 7496 | } |
| 7497 | } |
| 7498 | /* empty set, nothing to evaluate */ |
| 7499 | if (i == set->used) { |
| 7500 | goto only_parse; |
| 7501 | } |
| 7502 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7503 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7504 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7505 | /* set special in_ctx to all the valid snodes */ |
| 7506 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7507 | |
| 7508 | /* use the valid snodes one-by-one */ |
| 7509 | for (i = 0; i < set->used; ++i) { |
| 7510 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7511 | continue; |
| 7512 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7513 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7514 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7515 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7516 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7517 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7518 | LY_CHECK_RET(rc); |
| 7519 | |
| 7520 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7521 | } |
| 7522 | |
| 7523 | /* restore the state as it was before the predicate */ |
| 7524 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7525 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7526 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7527 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7528 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7529 | } |
| 7530 | } |
| 7531 | |
| 7532 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7533 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7534 | set_fill_set(&set2, set); |
| 7535 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7536 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7537 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7538 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7539 | return rc; |
| 7540 | } |
| 7541 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7542 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7543 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7544 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7546 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7547 | } |
| 7548 | |
| 7549 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7550 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7551 | 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] | 7552 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7553 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7554 | |
| 7555 | return LY_SUCCESS; |
| 7556 | } |
| 7557 | |
| 7558 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7559 | * @brief Evaluate Literal. Logs directly on error. |
| 7560 | * |
| 7561 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7562 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7563 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7564 | */ |
| 7565 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7566 | 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] | 7567 | { |
| 7568 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7569 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7570 | set_fill_string(set, "", 0); |
| 7571 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7572 | 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] | 7573 | } |
| 7574 | } |
| 7575 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7576 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7577 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7578 | } |
| 7579 | |
| 7580 | /** |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7581 | * @brief Check that a nametest in a predicate matches a key node. |
| 7582 | * |
| 7583 | * @param[in] nametest Nametest to check. |
| 7584 | * @param[in] len Length of @p nametest. |
| 7585 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7586 | * @param[in] set Context set. |
| 7587 | * @param[in] key Expected key node. |
| 7588 | * @return LY_SUCCESS on success, |
| 7589 | * @return LY_ENOT if a predicate could not be compiled. |
| 7590 | * @return LY_ERR on any error. |
| 7591 | */ |
| 7592 | static LY_ERR |
| 7593 | eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode, |
| 7594 | const struct lyxp_set *set, const struct lysc_node *key) |
| 7595 | { |
| 7596 | const struct lys_module *mod; |
| 7597 | |
| 7598 | /* prefix (module) */ |
| 7599 | LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod)); |
| 7600 | if (mod != key->module) { |
| 7601 | return LY_ENOT; |
| 7602 | } |
| 7603 | |
| 7604 | /* node name */ |
| 7605 | if (ly_strncmp(key->name, nametest, len)) { |
| 7606 | return LY_ENOT; |
| 7607 | } |
| 7608 | |
| 7609 | return LY_SUCCESS; |
| 7610 | } |
| 7611 | |
| 7612 | /** |
| 7613 | * @brief Append a simple predicate for the node. |
| 7614 | * |
| 7615 | * @param[in] exp Full parsed XPath expression. |
| 7616 | * @param[in] tok_idx Predicate start index in @p exp. |
| 7617 | * @param[in] end_tok_idx Predicate end index in @p exp. |
| 7618 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7619 | * @param[in] set Context set. |
| 7620 | * @param[in] pred_node Node with the value referenced in the predicate. |
| 7621 | * @param[in,out] pred Predicate to append to. |
| 7622 | * @param[in,out] pred_len Length of @p pred, is updated. |
| 7623 | * @return LY_SUCCESS on success, |
| 7624 | * @return LY_ENOT if a predicate could not be compiled. |
| 7625 | * @return LY_ERR on any error. |
| 7626 | */ |
| 7627 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7628 | 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] | 7629 | const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred, |
| 7630 | uint32_t *pred_len) |
| 7631 | { |
| 7632 | LY_ERR rc = LY_SUCCESS; |
| 7633 | uint32_t i; |
| 7634 | const struct lyd_node *siblings; |
| 7635 | struct lyd_node *ctx_node; |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7636 | const struct lysc_node *sparent, *cur_scnode; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7637 | struct lyxp_expr *val_exp = NULL; |
| 7638 | struct lyxp_set set2 = {0}; |
| 7639 | char quot; |
| 7640 | |
| 7641 | /* duplicate the value expression */ |
| 7642 | LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup); |
| 7643 | |
| 7644 | /* get its atoms */ |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7645 | cur_scnode = set->cur_node ? set->cur_node->schema : NULL; |
| 7646 | LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode, |
| 7647 | ctx_scnode, &set2, LYXP_SCNODE), cleanup); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7648 | |
| 7649 | /* check whether we can compile a single predicate (evaluation result value is always the same) */ |
| 7650 | for (i = 0; i < set2.used; ++i) { |
| 7651 | if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) { |
| 7652 | /* skip root and context node */ |
| 7653 | continue; |
| 7654 | } |
| 7655 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7656 | /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the |
| 7657 | * context node instance */ |
| 7658 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) { |
| 7659 | /* 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] | 7660 | rc = LY_ENOT; |
| 7661 | goto cleanup; |
| 7662 | } |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7663 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) { |
| 7664 | for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {} |
| 7665 | if (sparent) { |
| 7666 | /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */ |
| 7667 | rc = LY_ENOT; |
| 7668 | goto cleanup; |
| 7669 | } |
| 7670 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7671 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7672 | /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered, |
| 7673 | * but the current node can be safely ignored, it is always the same data instance */ |
| 7674 | 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] | 7675 | rc = LY_ENOT; |
| 7676 | goto cleanup; |
| 7677 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7678 | } |
| 7679 | |
| 7680 | /* get any data instance of the context node, we checked it makes no difference */ |
| 7681 | siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree; |
| 7682 | LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup); |
| 7683 | |
| 7684 | /* evaluate the value subexpression with the root context node */ |
| 7685 | lyxp_set_free_content(&set2); |
| 7686 | LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node, |
| 7687 | ctx_node, set->tree, NULL, &set2, 0), cleanup); |
| 7688 | |
| 7689 | /* cast it into a string */ |
| 7690 | LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup); |
| 7691 | |
| 7692 | /* append the JSON predicate */ |
| 7693 | *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3); |
| 7694 | LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7695 | quot = strchr(set2.val.str, '\'') ? '\"' : '\''; |
| 7696 | *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot); |
| 7697 | |
| 7698 | cleanup: |
| 7699 | lyxp_expr_free(set->ctx, val_exp); |
| 7700 | lyxp_set_free_content(&set2); |
| 7701 | return rc; |
| 7702 | } |
| 7703 | |
| 7704 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7705 | * @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] | 7706 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7707 | * @param[in] exp Full parsed XPath expression. |
| 7708 | * @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] | 7709 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7710 | * @param[in] set Context set. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7711 | * @param[out] predicates Parsed predicates. |
| 7712 | * @param[out] pred_type Type of @p predicates. |
| 7713 | * @return LY_SUCCESS on success, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7714 | * @return LY_ENOT if a predicate could not be compiled. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7715 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7716 | */ |
| 7717 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7718 | 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] | 7719 | 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] | 7720 | { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7721 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7722 | uint32_t e_idx, val_start_idx, pred_idx = 0, prev_lo, pred_len = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7723 | const struct lysc_node *key; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7724 | char *pred = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7725 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7726 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7727 | assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7728 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7729 | /* turn logging off */ |
| 7730 | prev_lo = ly_log_options(0); |
| 7731 | |
| 7732 | if (ctx_scnode->nodetype == LYS_LIST) { |
| 7733 | /* check for predicates "[key1=...][key2=...]..." */ |
| 7734 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7735 | /* get key count */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7736 | if (ctx_scnode->flags & LYS_KEYLESS) { |
| 7737 | rc = LY_ENOT; |
| 7738 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7739 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7740 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7741 | /* learn where the predicates end */ |
| 7742 | e_idx = *tok_idx; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7743 | 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] | 7744 | /* '[' */ |
| 7745 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7746 | rc = LY_ENOT; |
| 7747 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7748 | } |
| 7749 | ++e_idx; |
| 7750 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7751 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7752 | /* not a key */ |
| 7753 | rc = LY_ENOT; |
| 7754 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7755 | } |
| 7756 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7757 | /* check key */ |
| 7758 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx], |
| 7759 | exp->tok_len[e_idx], ctx_scnode, set, key), cleanup); |
| 7760 | |
| 7761 | ++e_idx; |
| 7762 | |
| 7763 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7764 | /* not '=' */ |
| 7765 | rc = LY_ENOT; |
| 7766 | goto cleanup; |
| 7767 | } |
| 7768 | ++e_idx; |
| 7769 | |
| 7770 | /* value start */ |
| 7771 | val_start_idx = e_idx; |
| 7772 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7773 | /* ']' */ |
| 7774 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7775 | if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
| 7776 | /* higher priority than '=' */ |
| 7777 | rc = LY_ENOT; |
| 7778 | goto cleanup; |
| 7779 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7780 | ++e_idx; |
| 7781 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7782 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7783 | /* try to evaluate the value */ |
| 7784 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, |
| 7785 | set, key, &pred, &pred_len), cleanup); |
| 7786 | |
| 7787 | ++e_idx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7788 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7789 | } else { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7790 | /* check for predicate "[.=...]" */ |
| 7791 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7792 | /* learn just where this single predicate ends */ |
| 7793 | e_idx = *tok_idx; |
| 7794 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7795 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7796 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7797 | rc = LY_ENOT; |
| 7798 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7799 | } |
| 7800 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7801 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7802 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7803 | /* not the node value */ |
| 7804 | rc = LY_ENOT; |
| 7805 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7806 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7807 | ++e_idx; |
| 7808 | |
| 7809 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7810 | /* not '=' */ |
| 7811 | rc = LY_ENOT; |
| 7812 | goto cleanup; |
| 7813 | } |
| 7814 | ++e_idx; |
| 7815 | |
| 7816 | /* value start */ |
| 7817 | val_start_idx = e_idx; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7818 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7819 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7820 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7821 | if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
| 7822 | /* higher priority than '=' */ |
| 7823 | rc = LY_ENOT; |
| 7824 | goto cleanup; |
| 7825 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7826 | ++e_idx; |
| 7827 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7828 | |
| 7829 | /* try to evaluate the value */ |
| 7830 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set, |
| 7831 | ctx_scnode, &pred, &pred_len), cleanup); |
| 7832 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7833 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7834 | } |
| 7835 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7836 | /* parse the predicate(s) */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7837 | LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL, |
| 7838 | LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7839 | |
| 7840 | /* compile */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7841 | rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2, |
| 7842 | &pred_idx, LY_VALUE_JSON, NULL, predicates, pred_type); |
| 7843 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7844 | |
| 7845 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7846 | *tok_idx = e_idx; |
| 7847 | |
| 7848 | cleanup: |
| 7849 | ly_log_options(prev_lo); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7850 | lyxp_expr_free(set->ctx, exp2); |
| 7851 | free(pred); |
| 7852 | return rc; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7853 | } |
| 7854 | |
| 7855 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7856 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7857 | * data node(s) could be found using a single hash-based search. |
| 7858 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7859 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7860 | * @param[in] node Next context node to check. |
| 7861 | * @param[in] name Expected node name. |
| 7862 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7863 | * @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] | 7864 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7865 | * @param[in] format Prefix format. |
| 7866 | * @param[in,out] found Previously found node, is updated. |
| 7867 | * @return LY_SUCCESS on success, |
| 7868 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7869 | */ |
| 7870 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7871 | 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] | 7872 | 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] | 7873 | const struct lysc_node **found) |
| 7874 | { |
| 7875 | const struct lysc_node *scnode; |
| 7876 | const struct lys_module *mod; |
| 7877 | uint32_t idx = 0; |
| 7878 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7879 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7880 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7881 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7882 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7883 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7884 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7885 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7886 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7887 | if (!mod->implemented) { |
| 7888 | continue; |
| 7889 | } |
| 7890 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7891 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7892 | if (scnode) { |
| 7893 | /* we have found a match */ |
| 7894 | break; |
| 7895 | } |
| 7896 | } |
| 7897 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7898 | if (!scnode) { |
| 7899 | /* all modules searched */ |
| 7900 | idx = 0; |
| 7901 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7902 | } else { |
| 7903 | /* search in top-level */ |
| 7904 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7905 | } |
| 7906 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7907 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7908 | /* we must adjust the module to inherit the one from the context node */ |
| 7909 | moveto_mod = node->schema->module; |
| 7910 | } |
| 7911 | |
| 7912 | /* search in children, do not repeat the same search */ |
| 7913 | 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] | 7914 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7915 | |
| 7916 | /* additional context check */ |
| 7917 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7918 | scnode = NULL; |
| 7919 | } |
| 7920 | |
| 7921 | if (scnode) { |
| 7922 | if (*found) { |
| 7923 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7924 | * (more hash-based searches would be required, not supported) */ |
| 7925 | return LY_ENOT; |
| 7926 | } else { |
| 7927 | /* remember the found schema node and continue to make sure it can be used */ |
| 7928 | *found = scnode; |
| 7929 | } |
| 7930 | } |
| 7931 | |
| 7932 | if (idx) { |
| 7933 | /* continue searching all the following models */ |
| 7934 | goto continue_search; |
| 7935 | } |
| 7936 | |
| 7937 | return LY_SUCCESS; |
| 7938 | } |
| 7939 | |
| 7940 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7941 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7942 | * |
| 7943 | * @param[in] set XPath set. |
| 7944 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7945 | * @param[in] ncname XPath NCName being evaluated. |
| 7946 | * @param[in] ncname_len Length of @p ncname. |
| 7947 | * @param[in] expr Whole XPath expression. |
| 7948 | * @param[in] options XPath options. |
| 7949 | */ |
| 7950 | static void |
| 7951 | 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] | 7952 | uint32_t ncname_len, const char *expr, uint32_t options) |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7953 | { |
| 7954 | const char *format; |
| 7955 | char *path = NULL, *ppath = NULL; |
| 7956 | |
| 7957 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7958 | if (scparent) { |
| 7959 | /* generate path for the parent */ |
| 7960 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7961 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7962 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7963 | ppath = strdup("<root>"); |
| 7964 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7965 | ppath = strdup("<config-root>"); |
| 7966 | } |
| 7967 | } |
| 7968 | if (ppath) { |
| 7969 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7970 | if (options & LYXP_SCNODE_ERROR) { |
| 7971 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7972 | } else { |
| 7973 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7974 | } |
| 7975 | } else { |
| 7976 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7977 | if (options & LYXP_SCNODE_ERROR) { |
| 7978 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7979 | } else { |
| 7980 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7981 | } |
| 7982 | } |
| 7983 | free(path); |
| 7984 | free(ppath); |
| 7985 | } |
| 7986 | |
| 7987 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7988 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7989 | * |
| 7990 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7991 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7992 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7993 | * |
| 7994 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7995 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7996 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7997 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7998 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7999 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8000 | * @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] | 8001 | */ |
| 8002 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8003 | 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] | 8004 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8005 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8006 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8007 | const char *ncname, *ncname_dict = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8008 | uint32_t i, ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8009 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8010 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8011 | struct ly_path_predicate *predicates = NULL; |
| 8012 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8013 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8014 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8015 | 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] | 8016 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8017 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8018 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8019 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8020 | goto moveto; |
| 8021 | } |
| 8022 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8023 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 8024 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8025 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8026 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8027 | /* all nodes will match */ |
| 8028 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8029 | } |
| 8030 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8031 | /* parse (and skip) module name */ |
| 8032 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8033 | LY_CHECK_GOTO(rc, cleanup); |
| 8034 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8035 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8036 | /* all nodes from the module will match */ |
| 8037 | goto moveto; |
| 8038 | } |
| 8039 | |
| 8040 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 8041 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8042 | /* find the matching schema node in some parent in the context */ |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8043 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8044 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 8045 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8046 | /* check failed */ |
| 8047 | scnode = NULL; |
| 8048 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8049 | } |
| 8050 | } |
| 8051 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8052 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 8053 | /* try to create the predicates */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 8054 | 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] | 8055 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8056 | scnode = NULL; |
| 8057 | } |
| 8058 | } |
| 8059 | } |
| 8060 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8061 | if (!scnode) { |
| 8062 | /* 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] | 8063 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 8064 | 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] | 8065 | } |
| 8066 | |
| 8067 | moveto: |
| 8068 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8069 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8070 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8071 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8072 | } else { |
| 8073 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 8074 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8075 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8076 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8077 | } |
| 8078 | LY_CHECK_GOTO(rc, cleanup); |
| 8079 | } |
| 8080 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8081 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8082 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8083 | ly_bool found = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8084 | |
| 8085 | /* remember parent if there is only one, to print in the warning */ |
| 8086 | for (i = 0; i < set->used; ++i) { |
| 8087 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 8088 | if (!scparent) { |
| 8089 | /* remember the context node */ |
| 8090 | scparent = &set->val.scnodes[i]; |
| 8091 | } else { |
| 8092 | /* several context nodes, no reasonable error possible */ |
| 8093 | scparent = NULL; |
| 8094 | break; |
| 8095 | } |
| 8096 | } |
| 8097 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8098 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8099 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8100 | /* efficient evaluation that does not add all the descendants into the set */ |
| 8101 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8102 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8103 | if (all_desc) { |
| 8104 | /* "//" == "/descendant-or-self::node()/" */ |
| 8105 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8106 | LY_CHECK_GOTO(rc, cleanup); |
| 8107 | } |
| 8108 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8109 | } |
| 8110 | LY_CHECK_GOTO(rc, cleanup); |
| 8111 | |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8112 | i = set->used; |
| 8113 | do { |
| 8114 | --i; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8115 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8116 | found = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8117 | break; |
| 8118 | } |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8119 | } while (i); |
| 8120 | if (!found) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8121 | /* generate message */ |
| 8122 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 8123 | |
| 8124 | if (options & LYXP_SCNODE_ERROR) { |
| 8125 | /* error */ |
| 8126 | rc = LY_EVALID; |
| 8127 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8128 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8129 | |
| 8130 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 8131 | rc = LY_ENOT; |
| 8132 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8133 | } |
| 8134 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8135 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8136 | /* efficient evaluation */ |
| 8137 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 8138 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 8139 | /* we can find the child nodes using hashes */ |
| 8140 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8141 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8142 | if (all_desc) { |
| 8143 | /* "//" == "/descendant-or-self::node()/" */ |
| 8144 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8145 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8146 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8147 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8148 | } |
| 8149 | LY_CHECK_GOTO(rc, cleanup); |
| 8150 | } |
| 8151 | } |
| 8152 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8153 | if (scnode_skip_pred) { |
| 8154 | /* skip predicates */ |
| 8155 | options |= LYXP_SKIP_EXPR; |
| 8156 | } |
| 8157 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8158 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8159 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8160 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8161 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8162 | } |
| 8163 | |
| 8164 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8165 | if (scnode_skip_pred) { |
| 8166 | /* restore options */ |
| 8167 | options &= ~LYXP_SKIP_EXPR; |
| 8168 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8169 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8170 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 8171 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 8172 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8173 | return rc; |
| 8174 | } |
| 8175 | |
| 8176 | /** |
| 8177 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 8178 | * |
| 8179 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8180 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8181 | * [8] NodeType ::= 'text' | 'node' |
| 8182 | * |
| 8183 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8184 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8185 | * @param[in] axis Axis to search on. |
| 8186 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8187 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8188 | * @param[in] options XPath options. |
| 8189 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8190 | */ |
| 8191 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8192 | 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] | 8193 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8194 | { |
| 8195 | LY_ERR rc; |
| 8196 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8197 | (void)all_desc; |
| 8198 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8199 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8200 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8201 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 8202 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8203 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8204 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 8205 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8206 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8207 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8208 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8209 | 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] | 8210 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8211 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8212 | |
| 8213 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8214 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8215 | 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] | 8216 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8217 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8218 | |
| 8219 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8220 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
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 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8226 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8227 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8228 | LY_CHECK_RET(rc); |
| 8229 | } |
| 8230 | |
| 8231 | return LY_SUCCESS; |
| 8232 | } |
| 8233 | |
| 8234 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8235 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8236 | * |
| 8237 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8238 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8239 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8240 | * |
| 8241 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8242 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8243 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8244 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8245 | * @param[in] options XPath options. |
| 8246 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8247 | */ |
| 8248 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8249 | 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] | 8250 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8251 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8252 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8253 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8254 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8255 | |
| 8256 | goto step; |
| 8257 | do { |
| 8258 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8259 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8260 | all_desc = 0; |
| 8261 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8262 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | all_desc = 1; |
| 8264 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8265 | 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] | 8266 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8267 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8268 | |
| 8269 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8270 | /* AxisSpecifier */ |
| 8271 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8272 | 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] | 8273 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8274 | 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] | 8275 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8276 | ++(*tok_idx); |
| 8277 | |
| 8278 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8279 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8280 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8281 | ++(*tok_idx); |
| 8282 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8283 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8284 | |
| 8285 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8286 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8287 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8288 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8289 | /* default */ |
| 8290 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8291 | } |
| 8292 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8293 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8294 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8295 | case LYXP_TOKEN_DOT: |
| 8296 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8297 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8298 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8299 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8300 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8301 | rc = LY_EVALID; |
| 8302 | goto cleanup; |
| 8303 | } |
| 8304 | |
| 8305 | if (all_desc) { |
| 8306 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8307 | LY_CHECK_GOTO(rc, cleanup); |
| 8308 | } |
| 8309 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8310 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8311 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8312 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8313 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8314 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8315 | break; |
| 8316 | |
| 8317 | case LYXP_TOKEN_DDOT: |
| 8318 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8319 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8320 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8321 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8322 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8323 | rc = LY_EVALID; |
| 8324 | goto cleanup; |
| 8325 | } |
| 8326 | |
| 8327 | if (all_desc) { |
| 8328 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8329 | LY_CHECK_GOTO(rc, cleanup); |
| 8330 | } |
| 8331 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8332 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8333 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8334 | 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] | 8335 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8336 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8337 | break; |
| 8338 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8339 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8340 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8341 | 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] | 8342 | if (rc == LY_ENOT) { |
| 8343 | assert(options & LYXP_SCNODE_ALL); |
| 8344 | /* skip the rest of this path */ |
| 8345 | rc = LY_SUCCESS; |
| 8346 | scnode_skip_path = 1; |
| 8347 | options |= LYXP_SKIP_EXPR; |
| 8348 | } |
| 8349 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8350 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8352 | case LYXP_TOKEN_NODETYPE: |
| 8353 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8354 | 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] | 8355 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8356 | break; |
| 8357 | |
| 8358 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8359 | LOGINT(set->ctx); |
| 8360 | rc = LY_EINT; |
| 8361 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8362 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8363 | } 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] | 8364 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8365 | cleanup: |
| 8366 | if (scnode_skip_path) { |
| 8367 | options &= ~LYXP_SKIP_EXPR; |
| 8368 | } |
| 8369 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8370 | } |
| 8371 | |
| 8372 | /** |
| 8373 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8374 | * |
| 8375 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8376 | * |
| 8377 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8378 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8379 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8380 | * @param[in] options XPath options. |
| 8381 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8382 | */ |
| 8383 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8384 | 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] | 8385 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8386 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8387 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8388 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8389 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8390 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8391 | } |
| 8392 | |
| 8393 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8394 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8395 | /* evaluate '/' - deferred */ |
| 8396 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8397 | 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] | 8398 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8399 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8400 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8401 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8402 | return LY_SUCCESS; |
| 8403 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8404 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8405 | case LYXP_TOKEN_DOT: |
| 8406 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8407 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8408 | case LYXP_TOKEN_AT: |
| 8409 | case LYXP_TOKEN_NAMETEST: |
| 8410 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8411 | 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] | 8412 | break; |
| 8413 | default: |
| 8414 | break; |
| 8415 | } |
| 8416 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8417 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8418 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8419 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8420 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8421 | 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] | 8422 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8423 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8424 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8425 | 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] | 8426 | } |
| 8427 | |
| 8428 | return LY_SUCCESS; |
| 8429 | } |
| 8430 | |
| 8431 | /** |
| 8432 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8433 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8434 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8435 | * |
| 8436 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8437 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8438 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8439 | * @param[in] options XPath options. |
| 8440 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8441 | */ |
| 8442 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8443 | 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] | 8444 | { |
| 8445 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8446 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8447 | LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL; |
| 8448 | uint32_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8449 | struct lyxp_set **args = NULL, **args_aux; |
| 8450 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8451 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8452 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8453 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8454 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8455 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8456 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8457 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8458 | xpath_func = &xpath_sum; |
| 8459 | } |
| 8460 | break; |
| 8461 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8462 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8463 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8464 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8465 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8466 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8467 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8468 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8469 | xpath_func = &xpath_true; |
| 8470 | } |
| 8471 | break; |
| 8472 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8473 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8474 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8475 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8476 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8477 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8478 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8479 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8480 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8481 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8482 | xpath_func = &xpath_deref; |
| 8483 | } |
| 8484 | break; |
| 8485 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8486 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8487 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8488 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8489 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8490 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8491 | xpath_func = &xpath_string; |
| 8492 | } |
| 8493 | break; |
| 8494 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8495 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8496 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8497 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8498 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8499 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8500 | xpath_func = &xpath_current; |
| 8501 | } |
| 8502 | break; |
| 8503 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8504 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8505 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8506 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8507 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8508 | } 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] | 8509 | xpath_func = &xpath_re_match; |
| 8510 | } |
| 8511 | break; |
| 8512 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8513 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8514 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8515 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8516 | xpath_func = &xpath_translate; |
| 8517 | } |
| 8518 | break; |
| 8519 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8520 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8521 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8522 | } 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] | 8523 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8524 | } 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] | 8525 | xpath_func = &xpath_bit_is_set; |
| 8526 | } |
| 8527 | break; |
| 8528 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8529 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8530 | xpath_func = &xpath_starts_with; |
| 8531 | } |
| 8532 | break; |
| 8533 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8534 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8535 | xpath_func = &xpath_derived_from; |
| 8536 | } |
| 8537 | break; |
| 8538 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8539 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8540 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8541 | } 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] | 8542 | xpath_func = &xpath_string_length; |
| 8543 | } |
| 8544 | break; |
| 8545 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8546 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8547 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8548 | } 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] | 8549 | xpath_func = &xpath_substring_after; |
| 8550 | } |
| 8551 | break; |
| 8552 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8553 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8554 | xpath_func = &xpath_substring_before; |
| 8555 | } |
| 8556 | break; |
| 8557 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8558 | 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] | 8559 | xpath_func = &xpath_derived_from_or_self; |
| 8560 | } |
| 8561 | break; |
| 8562 | } |
| 8563 | |
| 8564 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8565 | 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] | 8566 | return LY_EVALID; |
| 8567 | } |
| 8568 | } |
| 8569 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8570 | 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] | 8571 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8572 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8573 | |
| 8574 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8575 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8576 | 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] | 8577 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8578 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8579 | |
| 8580 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8581 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8582 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8583 | args = malloc(sizeof *args); |
| 8584 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8585 | arg_count = 1; |
| 8586 | args[0] = set_copy(set); |
| 8587 | if (!args[0]) { |
| 8588 | rc = LY_EMEM; |
| 8589 | goto cleanup; |
| 8590 | } |
| 8591 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8592 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8593 | LY_CHECK_GOTO(rc, cleanup); |
| 8594 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8595 | 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] | 8596 | LY_CHECK_GOTO(rc, cleanup); |
| 8597 | } |
| 8598 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8599 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8600 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8601 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8602 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8603 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8604 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8605 | ++arg_count; |
| 8606 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8607 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8608 | args = args_aux; |
| 8609 | args[arg_count - 1] = set_copy(set); |
| 8610 | if (!args[arg_count - 1]) { |
| 8611 | rc = LY_EMEM; |
| 8612 | goto cleanup; |
| 8613 | } |
| 8614 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8615 | 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] | 8616 | LY_CHECK_GOTO(rc, cleanup); |
| 8617 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8618 | 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] | 8619 | LY_CHECK_GOTO(rc, cleanup); |
| 8620 | } |
| 8621 | } |
| 8622 | |
| 8623 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8624 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8625 | 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] | 8626 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8627 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8628 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8629 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8630 | /* evaluate function */ |
| 8631 | rc = xpath_func(args, arg_count, set, options); |
| 8632 | |
| 8633 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8634 | /* merge all nodes from arg evaluations */ |
| 8635 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8636 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8637 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8638 | } |
| 8639 | } |
| 8640 | } else { |
| 8641 | rc = LY_SUCCESS; |
| 8642 | } |
| 8643 | |
| 8644 | cleanup: |
| 8645 | for (i = 0; i < arg_count; ++i) { |
| 8646 | lyxp_set_free(args[i]); |
| 8647 | } |
| 8648 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8649 | return rc; |
| 8650 | } |
| 8651 | |
| 8652 | /** |
| 8653 | * @brief Evaluate Number. Logs directly on error. |
| 8654 | * |
| 8655 | * @param[in] ctx Context for errors. |
| 8656 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8657 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8658 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8659 | * @return LY_ERR |
| 8660 | */ |
| 8661 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8662 | 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] | 8663 | { |
| 8664 | long double num; |
| 8665 | char *endptr; |
| 8666 | |
| 8667 | if (set) { |
| 8668 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8669 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8670 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8671 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8672 | 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] | 8673 | 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] | 8674 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8675 | } 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] | 8676 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8677 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8678 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8679 | return LY_EVALID; |
| 8680 | } |
| 8681 | |
| 8682 | set_fill_number(set, num); |
| 8683 | } |
| 8684 | |
| 8685 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8686 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8687 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8688 | return LY_SUCCESS; |
| 8689 | } |
| 8690 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8691 | LY_ERR |
| 8692 | lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var) |
| 8693 | { |
| 8694 | LY_ERR ret = LY_ENOTFOUND; |
| 8695 | LY_ARRAY_COUNT_TYPE u; |
| 8696 | |
| 8697 | assert(vars && name); |
| 8698 | |
| 8699 | name_len = name_len ? name_len : strlen(name); |
| 8700 | |
| 8701 | LY_ARRAY_FOR(vars, u) { |
| 8702 | if (!strncmp(vars[u].name, name, name_len)) { |
| 8703 | ret = LY_SUCCESS; |
| 8704 | break; |
| 8705 | } |
| 8706 | } |
| 8707 | |
| 8708 | if (var && !ret) { |
| 8709 | *var = &vars[u]; |
| 8710 | } |
| 8711 | |
| 8712 | return ret; |
| 8713 | } |
| 8714 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8715 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8716 | * @brief Evaluate VariableReference. |
| 8717 | * |
| 8718 | * @param[in] exp Parsed XPath expression. |
| 8719 | * @param[in] tok_idx Position in the expression @p exp. |
| 8720 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8721 | * @param[in,out] set Context and result set. |
| 8722 | * @param[in] options XPath options. |
| 8723 | * @return LY_ERR value. |
| 8724 | */ |
| 8725 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8726 | 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] | 8727 | { |
| 8728 | LY_ERR ret; |
| 8729 | const char *name; |
| 8730 | struct lyxp_var *var; |
| 8731 | const struct lyxp_var *vars; |
| 8732 | struct lyxp_expr *tokens = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8733 | uint32_t token_index, name_len; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8734 | |
| 8735 | vars = set->vars; |
| 8736 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8737 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8738 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
Michal Vasko | e68b540 | 2022-07-29 14:58:15 +0200 | [diff] [blame] | 8739 | name_len = exp->tok_len[*tok_idx]; |
| 8740 | ret = lyxp_vars_find((struct lyxp_var *)vars, name, name_len, &var); |
| 8741 | 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] | 8742 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8743 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8744 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8745 | LY_CHECK_GOTO(ret, cleanup); |
| 8746 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8747 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8748 | token_index = 0; |
| 8749 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8750 | LY_CHECK_GOTO(ret, cleanup); |
| 8751 | |
| 8752 | cleanup: |
| 8753 | lyxp_expr_free(set->ctx, tokens); |
| 8754 | |
| 8755 | return ret; |
| 8756 | } |
| 8757 | |
| 8758 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8759 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8760 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8761 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8762 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8763 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8764 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8765 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8766 | * |
| 8767 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8768 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8769 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8770 | * @param[in] options XPath options. |
| 8771 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8772 | */ |
| 8773 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8774 | 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] | 8775 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8776 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8777 | LY_ERR rc; |
| 8778 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8779 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8780 | case LYXP_TOKEN_PAR1: |
| 8781 | /* '(' Expr ')' */ |
| 8782 | |
| 8783 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8784 | 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] | 8785 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8786 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8787 | |
| 8788 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8789 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8790 | LY_CHECK_RET(rc); |
| 8791 | |
| 8792 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8793 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8794 | 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] | 8795 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8796 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8797 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8798 | goto predicate; |
| 8799 | |
| 8800 | case LYXP_TOKEN_DOT: |
| 8801 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8802 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8803 | case LYXP_TOKEN_AT: |
| 8804 | case LYXP_TOKEN_NAMETEST: |
| 8805 | case LYXP_TOKEN_NODETYPE: |
| 8806 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8807 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8808 | LY_CHECK_RET(rc); |
| 8809 | break; |
| 8810 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8811 | case LYXP_TOKEN_VARREF: |
| 8812 | /* VariableReference */ |
| 8813 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8814 | LY_CHECK_RET(rc); |
| 8815 | ++(*tok_idx); |
| 8816 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8817 | goto predicate; |
| 8818 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8819 | case LYXP_TOKEN_FUNCNAME: |
| 8820 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8821 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8822 | LY_CHECK_RET(rc); |
| 8823 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8824 | goto predicate; |
| 8825 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8826 | case LYXP_TOKEN_OPER_PATH: |
| 8827 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8828 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8829 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8830 | LY_CHECK_RET(rc); |
| 8831 | break; |
| 8832 | |
| 8833 | case LYXP_TOKEN_LITERAL: |
| 8834 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8835 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8836 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8837 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8838 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8839 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8840 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8841 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8842 | } |
| 8843 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8844 | goto predicate; |
| 8845 | |
| 8846 | case LYXP_TOKEN_NUMBER: |
| 8847 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8848 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8849 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8850 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8851 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8852 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8853 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8854 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8855 | } |
| 8856 | LY_CHECK_RET(rc); |
| 8857 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8858 | goto predicate; |
| 8859 | |
| 8860 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8861 | 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] | 8862 | return LY_EVALID; |
| 8863 | } |
| 8864 | |
| 8865 | return LY_SUCCESS; |
| 8866 | |
| 8867 | predicate: |
| 8868 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8869 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8870 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8871 | LY_CHECK_RET(rc); |
| 8872 | } |
| 8873 | |
| 8874 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8875 | 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] | 8876 | |
| 8877 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8878 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8879 | all_desc = 0; |
| 8880 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8881 | all_desc = 1; |
| 8882 | } |
| 8883 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8884 | 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] | 8885 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8886 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8887 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8888 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8889 | LY_CHECK_RET(rc); |
| 8890 | } |
| 8891 | |
| 8892 | return LY_SUCCESS; |
| 8893 | } |
| 8894 | |
| 8895 | /** |
| 8896 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8897 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8898 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8899 | * |
| 8900 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8901 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8902 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8903 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8904 | * @param[in] options XPath options. |
| 8905 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8906 | */ |
| 8907 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8908 | 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] | 8909 | { |
| 8910 | LY_ERR rc = LY_SUCCESS; |
| 8911 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8912 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8913 | |
| 8914 | assert(repeat); |
| 8915 | |
| 8916 | set_init(&orig_set, set); |
| 8917 | set_init(&set2, set); |
| 8918 | |
| 8919 | set_fill_set(&orig_set, set); |
| 8920 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8921 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8922 | LY_CHECK_GOTO(rc, cleanup); |
| 8923 | |
| 8924 | /* ('|' PathExpr)* */ |
| 8925 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8926 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8927 | 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] | 8928 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8929 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8930 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8931 | if (options & LYXP_SKIP_EXPR) { |
| 8932 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8933 | LY_CHECK_GOTO(rc, cleanup); |
| 8934 | continue; |
| 8935 | } |
| 8936 | |
| 8937 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8938 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8939 | LY_CHECK_GOTO(rc, cleanup); |
| 8940 | |
| 8941 | /* eval */ |
| 8942 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8943 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8944 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8945 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8946 | LY_CHECK_GOTO(rc, cleanup); |
| 8947 | } |
| 8948 | } |
| 8949 | |
| 8950 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8951 | lyxp_set_free_content(&orig_set); |
| 8952 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8953 | return rc; |
| 8954 | } |
| 8955 | |
| 8956 | /** |
| 8957 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8958 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8959 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8960 | * |
| 8961 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8962 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8963 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8964 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8965 | * @param[in] options XPath options. |
| 8966 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8967 | */ |
| 8968 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8969 | 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] | 8970 | { |
| 8971 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8972 | uint32_t this_op, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8973 | |
| 8974 | assert(repeat); |
| 8975 | |
| 8976 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8977 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8978 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8979 | 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] | 8980 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8981 | 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] | 8982 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8983 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8984 | } |
| 8985 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8986 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8987 | LY_CHECK_RET(rc); |
| 8988 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8989 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8990 | if (options & LYXP_SCNODE_ALL) { |
| 8991 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 8992 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8993 | 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] | 8994 | LY_CHECK_RET(rc); |
| 8995 | } |
| 8996 | } |
| 8997 | |
| 8998 | return LY_SUCCESS; |
| 8999 | } |
| 9000 | |
| 9001 | /** |
| 9002 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 9003 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9004 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9005 | * | MultiplicativeExpr '*' UnaryExpr |
| 9006 | * | MultiplicativeExpr 'div' UnaryExpr |
| 9007 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 9008 | * |
| 9009 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9010 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9011 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9012 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9013 | * @param[in] options XPath options. |
| 9014 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9015 | */ |
| 9016 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9017 | 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] | 9018 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9019 | { |
| 9020 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9021 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9022 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9023 | |
| 9024 | assert(repeat); |
| 9025 | |
| 9026 | set_init(&orig_set, set); |
| 9027 | set_init(&set2, set); |
| 9028 | |
| 9029 | set_fill_set(&orig_set, set); |
| 9030 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9031 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9032 | LY_CHECK_GOTO(rc, cleanup); |
| 9033 | |
| 9034 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 9035 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9036 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9037 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9038 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9039 | 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] | 9040 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9041 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9042 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9043 | if (options & LYXP_SKIP_EXPR) { |
| 9044 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9045 | LY_CHECK_GOTO(rc, cleanup); |
| 9046 | continue; |
| 9047 | } |
| 9048 | |
| 9049 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9050 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9051 | LY_CHECK_GOTO(rc, cleanup); |
| 9052 | |
| 9053 | /* eval */ |
| 9054 | if (options & LYXP_SCNODE_ALL) { |
| 9055 | 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] | 9056 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9057 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9058 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9059 | 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] | 9060 | LY_CHECK_GOTO(rc, cleanup); |
| 9061 | } |
| 9062 | } |
| 9063 | |
| 9064 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9065 | lyxp_set_free_content(&orig_set); |
| 9066 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9067 | return rc; |
| 9068 | } |
| 9069 | |
| 9070 | /** |
| 9071 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 9072 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9073 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9074 | * | AdditiveExpr '+' MultiplicativeExpr |
| 9075 | * | AdditiveExpr '-' MultiplicativeExpr |
| 9076 | * |
| 9077 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9078 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9079 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9080 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9081 | * @param[in] options XPath options. |
| 9082 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9083 | */ |
| 9084 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9085 | 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] | 9086 | { |
| 9087 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9088 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9089 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9090 | |
| 9091 | assert(repeat); |
| 9092 | |
| 9093 | set_init(&orig_set, set); |
| 9094 | set_init(&set2, set); |
| 9095 | |
| 9096 | set_fill_set(&orig_set, set); |
| 9097 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9098 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9099 | LY_CHECK_GOTO(rc, cleanup); |
| 9100 | |
| 9101 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 9102 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9103 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9104 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9105 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9106 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9107 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9108 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9109 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9110 | if (options & LYXP_SKIP_EXPR) { |
| 9111 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9112 | LY_CHECK_GOTO(rc, cleanup); |
| 9113 | continue; |
| 9114 | } |
| 9115 | |
| 9116 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9117 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9118 | LY_CHECK_GOTO(rc, cleanup); |
| 9119 | |
| 9120 | /* eval */ |
| 9121 | if (options & LYXP_SCNODE_ALL) { |
| 9122 | 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] | 9123 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9124 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9125 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9126 | 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] | 9127 | LY_CHECK_GOTO(rc, cleanup); |
| 9128 | } |
| 9129 | } |
| 9130 | |
| 9131 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9132 | lyxp_set_free_content(&orig_set); |
| 9133 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9134 | return rc; |
| 9135 | } |
| 9136 | |
| 9137 | /** |
| 9138 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 9139 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9140 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9141 | * | RelationalExpr '<' AdditiveExpr |
| 9142 | * | RelationalExpr '>' AdditiveExpr |
| 9143 | * | RelationalExpr '<=' AdditiveExpr |
| 9144 | * | RelationalExpr '>=' AdditiveExpr |
| 9145 | * |
| 9146 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9147 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9148 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9149 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9150 | * @param[in] options XPath options. |
| 9151 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9152 | */ |
| 9153 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9154 | 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] | 9155 | { |
| 9156 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9157 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9158 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9159 | |
| 9160 | assert(repeat); |
| 9161 | |
| 9162 | set_init(&orig_set, set); |
| 9163 | set_init(&set2, set); |
| 9164 | |
| 9165 | set_fill_set(&orig_set, set); |
| 9166 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9167 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9168 | LY_CHECK_GOTO(rc, cleanup); |
| 9169 | |
| 9170 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 9171 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9172 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9173 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9174 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9175 | 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] | 9176 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9177 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9178 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9179 | if (options & LYXP_SKIP_EXPR) { |
| 9180 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9181 | LY_CHECK_GOTO(rc, cleanup); |
| 9182 | continue; |
| 9183 | } |
| 9184 | |
| 9185 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9186 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9187 | LY_CHECK_GOTO(rc, cleanup); |
| 9188 | |
| 9189 | /* eval */ |
| 9190 | if (options & LYXP_SCNODE_ALL) { |
| 9191 | 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] | 9192 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9193 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9194 | } else { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 9195 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9196 | LY_CHECK_GOTO(rc, cleanup); |
| 9197 | } |
| 9198 | } |
| 9199 | |
| 9200 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9201 | lyxp_set_free_content(&orig_set); |
| 9202 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9203 | return rc; |
| 9204 | } |
| 9205 | |
| 9206 | /** |
| 9207 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 9208 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9209 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9210 | * | EqualityExpr '!=' RelationalExpr |
| 9211 | * |
| 9212 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9213 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9214 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9215 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9216 | * @param[in] options XPath options. |
| 9217 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9218 | */ |
| 9219 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9220 | 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] | 9221 | { |
| 9222 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9223 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9224 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9225 | |
| 9226 | assert(repeat); |
| 9227 | |
| 9228 | set_init(&orig_set, set); |
| 9229 | set_init(&set2, set); |
| 9230 | |
| 9231 | set_fill_set(&orig_set, set); |
| 9232 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9233 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9234 | LY_CHECK_GOTO(rc, cleanup); |
| 9235 | |
| 9236 | /* ('=' / '!=' RelationalExpr)* */ |
| 9237 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9238 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9239 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9240 | 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] | 9241 | 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] | 9242 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9243 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9244 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9245 | if (options & LYXP_SKIP_EXPR) { |
| 9246 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9247 | LY_CHECK_GOTO(rc, cleanup); |
| 9248 | continue; |
| 9249 | } |
| 9250 | |
| 9251 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9252 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9253 | LY_CHECK_GOTO(rc, cleanup); |
| 9254 | |
| 9255 | /* eval */ |
| 9256 | if (options & LYXP_SCNODE_ALL) { |
| 9257 | 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] | 9258 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9259 | 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] | 9260 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9261 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9262 | } else { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 9263 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9264 | LY_CHECK_GOTO(rc, cleanup); |
| 9265 | } |
| 9266 | } |
| 9267 | |
| 9268 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9269 | lyxp_set_free_content(&orig_set); |
| 9270 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9271 | return rc; |
| 9272 | } |
| 9273 | |
| 9274 | /** |
| 9275 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9276 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9277 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9278 | * |
| 9279 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9280 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9281 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9282 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9283 | * @param[in] options XPath options. |
| 9284 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9285 | */ |
| 9286 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9287 | 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] | 9288 | { |
| 9289 | LY_ERR rc; |
| 9290 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9291 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9292 | |
| 9293 | assert(repeat); |
| 9294 | |
| 9295 | set_init(&orig_set, set); |
| 9296 | set_init(&set2, set); |
| 9297 | |
| 9298 | set_fill_set(&orig_set, set); |
| 9299 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9300 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9301 | LY_CHECK_GOTO(rc, cleanup); |
| 9302 | |
| 9303 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9304 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9305 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9306 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9307 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9308 | } |
| 9309 | |
| 9310 | /* ('and' EqualityExpr)* */ |
| 9311 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9312 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9313 | 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] | 9314 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9315 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9316 | |
| 9317 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9318 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9319 | 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] | 9320 | LY_CHECK_GOTO(rc, cleanup); |
| 9321 | continue; |
| 9322 | } |
| 9323 | |
| 9324 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9325 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9326 | LY_CHECK_GOTO(rc, cleanup); |
| 9327 | |
| 9328 | /* eval - just get boolean value actually */ |
| 9329 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9330 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9331 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9332 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9333 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9334 | set_fill_set(set, &set2); |
| 9335 | } |
| 9336 | } |
| 9337 | |
| 9338 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9339 | lyxp_set_free_content(&orig_set); |
| 9340 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9341 | return rc; |
| 9342 | } |
| 9343 | |
| 9344 | /** |
| 9345 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9346 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9347 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9348 | * |
| 9349 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9350 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9351 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9352 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9353 | * @param[in] options XPath options. |
| 9354 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9355 | */ |
| 9356 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9357 | 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] | 9358 | { |
| 9359 | LY_ERR rc; |
| 9360 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9361 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9362 | |
| 9363 | assert(repeat); |
| 9364 | |
| 9365 | set_init(&orig_set, set); |
| 9366 | set_init(&set2, set); |
| 9367 | |
| 9368 | set_fill_set(&orig_set, set); |
| 9369 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9370 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9371 | LY_CHECK_GOTO(rc, cleanup); |
| 9372 | |
| 9373 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9374 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9375 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9376 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9377 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9378 | } |
| 9379 | |
| 9380 | /* ('or' AndExpr)* */ |
| 9381 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9382 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9383 | 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] | 9384 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9385 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9386 | |
| 9387 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9388 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9389 | 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] | 9390 | LY_CHECK_GOTO(rc, cleanup); |
| 9391 | continue; |
| 9392 | } |
| 9393 | |
| 9394 | set_fill_set(&set2, &orig_set); |
| 9395 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9396 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9397 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9398 | LY_CHECK_GOTO(rc, cleanup); |
| 9399 | |
| 9400 | /* eval - just get boolean value actually */ |
| 9401 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9402 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9403 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9404 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9405 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9406 | set_fill_set(set, &set2); |
| 9407 | } |
| 9408 | } |
| 9409 | |
| 9410 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9411 | lyxp_set_free_content(&orig_set); |
| 9412 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9413 | return rc; |
| 9414 | } |
| 9415 | |
| 9416 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9417 | * @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] | 9418 | * |
| 9419 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9420 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9421 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9422 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9423 | * @param[in] options XPath options. |
| 9424 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9425 | */ |
| 9426 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9427 | 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] | 9428 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9429 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9430 | uint32_t i, count; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9431 | enum lyxp_expr_type next_etype; |
| 9432 | LY_ERR rc; |
| 9433 | |
| 9434 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9435 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9436 | next_etype = LYXP_EXPR_NONE; |
| 9437 | } else { |
| 9438 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9439 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9440 | |
| 9441 | /* select one-priority lower because etype expression called us */ |
| 9442 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9443 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9444 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9445 | 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] | 9446 | } else { |
| 9447 | next_etype = LYXP_EXPR_NONE; |
| 9448 | } |
| 9449 | } |
| 9450 | |
| 9451 | /* decide what expression are we parsing based on the repeat */ |
| 9452 | switch (next_etype) { |
| 9453 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9454 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9455 | break; |
| 9456 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9457 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9458 | break; |
| 9459 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9460 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9461 | break; |
| 9462 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9463 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9464 | break; |
| 9465 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9466 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9467 | break; |
| 9468 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9469 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9470 | break; |
| 9471 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9472 | rc = eval_unary_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_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9475 | rc = eval_union_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_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9478 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9479 | break; |
| 9480 | default: |
| 9481 | LOGINT_RET(set->ctx); |
| 9482 | } |
| 9483 | |
| 9484 | return rc; |
| 9485 | } |
| 9486 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9487 | /** |
| 9488 | * @brief Get root type. |
| 9489 | * |
| 9490 | * @param[in] ctx_node Context node. |
| 9491 | * @param[in] ctx_scnode Schema context node. |
| 9492 | * @param[in] options XPath options. |
| 9493 | * @return Root type. |
| 9494 | */ |
| 9495 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9496 | 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] | 9497 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9498 | const struct lysc_node *op; |
| 9499 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9500 | /* explicit */ |
| 9501 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9502 | return LYXP_NODE_ROOT; |
| 9503 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9504 | return LYXP_NODE_ROOT_CONFIG; |
| 9505 | } |
| 9506 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9507 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9508 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9509 | 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] | 9510 | |
| 9511 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9512 | /* general root that can access everything */ |
| 9513 | return LYXP_NODE_ROOT; |
| 9514 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9515 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9516 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9517 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9518 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9519 | } |
| 9520 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9521 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9522 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9523 | 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] | 9524 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9525 | if (op || !(options & LYXP_SCHEMA)) { |
| 9526 | /* general root that can access everything */ |
| 9527 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9528 | } 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] | 9529 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9530 | return LYXP_NODE_ROOT_CONFIG; |
| 9531 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9532 | return LYXP_NODE_ROOT; |
| 9533 | } |
| 9534 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9535 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9536 | 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] | 9537 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node, |
| 9538 | 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] | 9539 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9540 | uint32_t tok_idx = 0; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9541 | const struct lysc_node *snode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9542 | LY_ERR rc; |
| 9543 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9544 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9545 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9546 | 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] | 9547 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9548 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9549 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9550 | if (tree) { |
| 9551 | /* adjust the pointer to be the first top-level sibling */ |
| 9552 | while (tree->parent) { |
| 9553 | tree = lyd_parent(tree); |
| 9554 | } |
| 9555 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9556 | |
| 9557 | for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {} |
| 9558 | if (snode) { |
| 9559 | /* unable to evaluate absolute paths */ |
| 9560 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9561 | LYD_NAME(tree)); |
| 9562 | return LY_EINVAL; |
| 9563 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9564 | } |
| 9565 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9566 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9567 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9568 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9569 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9570 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9571 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9572 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9573 | set->cur_node = cur_node; |
| 9574 | for (set->context_op = cur_node ? cur_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9575 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9576 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9577 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9578 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9579 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9580 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9581 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9582 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9583 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9584 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9585 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9586 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9587 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9588 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9589 | } |
| 9590 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9591 | if (set->cur_node) { |
| 9592 | LOG_LOCBACK(0, 1, 0, 0); |
| 9593 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9594 | return rc; |
| 9595 | } |
| 9596 | |
| 9597 | #if 0 |
| 9598 | |
| 9599 | /* full xml printing of set elements, not used currently */ |
| 9600 | |
| 9601 | void |
| 9602 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9603 | { |
| 9604 | uint32_t i; |
| 9605 | char *str_num; |
| 9606 | struct lyout out; |
| 9607 | |
| 9608 | memset(&out, 0, sizeof out); |
| 9609 | |
| 9610 | out.type = LYOUT_STREAM; |
| 9611 | out.method.f = f; |
| 9612 | |
| 9613 | switch (set->type) { |
| 9614 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9615 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9616 | break; |
| 9617 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9618 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9619 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9620 | break; |
| 9621 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9622 | ly_print_(&out, "String XPath set:\n"); |
| 9623 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9624 | break; |
| 9625 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9626 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9627 | |
| 9628 | if (isnan(set->value.num)) { |
| 9629 | str_num = strdup("NaN"); |
| 9630 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9631 | str_num = strdup("0"); |
| 9632 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9633 | str_num = strdup("Infinity"); |
| 9634 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9635 | str_num = strdup("-Infinity"); |
| 9636 | } else if ((long long)set->value.num == set->value.num) { |
| 9637 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9638 | str_num = NULL; |
| 9639 | } |
| 9640 | } else { |
| 9641 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9642 | str_num = NULL; |
| 9643 | } |
| 9644 | } |
| 9645 | if (!str_num) { |
| 9646 | LOGMEM; |
| 9647 | return; |
| 9648 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9649 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9650 | free(str_num); |
| 9651 | break; |
| 9652 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9653 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9654 | |
| 9655 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9656 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9657 | switch (set->node_type[i]) { |
| 9658 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9659 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9660 | break; |
| 9661 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9662 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9663 | break; |
| 9664 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9665 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9666 | break; |
| 9667 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9668 | 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] | 9669 | break; |
| 9670 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9671 | 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] | 9672 | break; |
| 9673 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9674 | 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] | 9675 | break; |
| 9676 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9677 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9678 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9679 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9680 | break; |
| 9681 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9682 | 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] | 9683 | break; |
| 9684 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9685 | 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] | 9686 | break; |
| 9687 | } |
| 9688 | } |
| 9689 | break; |
| 9690 | } |
| 9691 | } |
| 9692 | |
| 9693 | #endif |
| 9694 | |
| 9695 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9696 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9697 | { |
| 9698 | long double num; |
| 9699 | char *str; |
| 9700 | LY_ERR rc; |
| 9701 | |
| 9702 | if (!set || (set->type == target)) { |
| 9703 | return LY_SUCCESS; |
| 9704 | } |
| 9705 | |
| 9706 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9707 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9708 | |
| 9709 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9710 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9711 | return LY_EINVAL; |
| 9712 | } |
| 9713 | |
| 9714 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9715 | 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] | 9716 | switch (set->type) { |
| 9717 | case LYXP_SET_NUMBER: |
| 9718 | if (isnan(set->val.num)) { |
| 9719 | set->val.str = strdup("NaN"); |
| 9720 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9721 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9722 | set->val.str = strdup("0"); |
| 9723 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9724 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9725 | set->val.str = strdup("Infinity"); |
| 9726 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9727 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9728 | set->val.str = strdup("-Infinity"); |
| 9729 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9730 | } else if ((long long)set->val.num == set->val.num) { |
| 9731 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9732 | LOGMEM_RET(set->ctx); |
| 9733 | } |
| 9734 | set->val.str = str; |
| 9735 | } else { |
| 9736 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9737 | LOGMEM_RET(set->ctx); |
| 9738 | } |
| 9739 | set->val.str = str; |
| 9740 | } |
| 9741 | break; |
| 9742 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9743 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9744 | set->val.str = strdup("true"); |
| 9745 | } else { |
| 9746 | set->val.str = strdup("false"); |
| 9747 | } |
| 9748 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9749 | break; |
| 9750 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9751 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9752 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9753 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9754 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9755 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9756 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9757 | set->val.str = str; |
| 9758 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9759 | default: |
| 9760 | LOGINT_RET(set->ctx); |
| 9761 | } |
| 9762 | set->type = LYXP_SET_STRING; |
| 9763 | } |
| 9764 | |
| 9765 | /* to NUMBER */ |
| 9766 | if (target == LYXP_SET_NUMBER) { |
| 9767 | switch (set->type) { |
| 9768 | case LYXP_SET_STRING: |
| 9769 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9770 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9771 | set->val.num = num; |
| 9772 | break; |
| 9773 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9774 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9775 | set->val.num = 1; |
| 9776 | } else { |
| 9777 | set->val.num = 0; |
| 9778 | } |
| 9779 | break; |
| 9780 | default: |
| 9781 | LOGINT_RET(set->ctx); |
| 9782 | } |
| 9783 | set->type = LYXP_SET_NUMBER; |
| 9784 | } |
| 9785 | |
| 9786 | /* to BOOLEAN */ |
| 9787 | if (target == LYXP_SET_BOOLEAN) { |
| 9788 | switch (set->type) { |
| 9789 | case LYXP_SET_NUMBER: |
| 9790 | 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] | 9791 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9792 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9793 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9794 | } |
| 9795 | break; |
| 9796 | case LYXP_SET_STRING: |
| 9797 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9798 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9799 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9800 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9801 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9802 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9803 | } |
| 9804 | break; |
| 9805 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9806 | if (set->used) { |
| 9807 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9808 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9809 | } else { |
| 9810 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9811 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9812 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9813 | break; |
| 9814 | default: |
| 9815 | LOGINT_RET(set->ctx); |
| 9816 | } |
| 9817 | set->type = LYXP_SET_BOOLEAN; |
| 9818 | } |
| 9819 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9820 | return LY_SUCCESS; |
| 9821 | } |
| 9822 | |
| 9823 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9824 | 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] | 9825 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode, |
| 9826 | 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] | 9827 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9828 | LY_ERR ret; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9829 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9830 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9831 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9832 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9833 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9834 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9835 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9836 | |
| 9837 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9838 | memset(set, 0, sizeof *set); |
| 9839 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9840 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 9841 | 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] | 9842 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9843 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9844 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9845 | set->cur_scnode = cur_scnode; |
| 9846 | for (set->context_op = cur_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9847 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9848 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9849 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9850 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9851 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9852 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9853 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9854 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9855 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9856 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9857 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9858 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9859 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9860 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9861 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9862 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9863 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9864 | { |
| 9865 | if (!path) { |
| 9866 | return NULL; |
| 9867 | } |
| 9868 | |
| 9869 | return path->expr; |
| 9870 | } |