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); |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 47 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 48 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, |
| 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 |
| 167 | str2axis(const char *str, uint16_t str_len) |
| 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 | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 384 | cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 385 | { |
| 386 | if (*size - *used < needed) { |
| 387 | do { |
| 388 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 389 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 390 | return LY_EINVAL; |
| 391 | } |
| 392 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 393 | } while (*size - *used < needed); |
| 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 | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 413 | cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint16_t indent, char **str, uint16_t *used, |
| 414 | uint16_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 | { |
| 565 | uint16_t used, size; |
| 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 |
| 936 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 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 | */ |
| 1536 | static uint16_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 | { |
| 1539 | uint16_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 | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1925 | lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_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 |
| 1946 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 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 | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1958 | exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_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 |
| 1980 | lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1, |
| 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 | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | { |
| 2001 | uint16_t i; |
| 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 |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2028 | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_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 |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2060 | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_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 |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2151 | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_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 |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2202 | reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_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; |
| 2205 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2206 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2207 | LY_ERR rc; |
| 2208 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2209 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2210 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | func_tok_idx = *tok_idx; |
| 2212 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2213 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2215 | min_arg_count = 1; |
| 2216 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | min_arg_count = 1; |
| 2219 | max_arg_count = 1; |
| 2220 | } |
| 2221 | break; |
| 2222 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | min_arg_count = 1; |
| 2225 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | min_arg_count = 0; |
| 2228 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | min_arg_count = 0; |
| 2231 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | min_arg_count = 0; |
| 2234 | max_arg_count = 0; |
| 2235 | } |
| 2236 | break; |
| 2237 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2239 | min_arg_count = 1; |
| 2240 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2242 | min_arg_count = 0; |
| 2243 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2244 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2245 | min_arg_count = 1; |
| 2246 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2248 | min_arg_count = 1; |
| 2249 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2250 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2251 | min_arg_count = 1; |
| 2252 | max_arg_count = 1; |
| 2253 | } |
| 2254 | break; |
| 2255 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2258 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | min_arg_count = 0; |
| 2261 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | min_arg_count = 0; |
| 2264 | max_arg_count = 1; |
| 2265 | } |
| 2266 | break; |
| 2267 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2268 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | min_arg_count = 1; |
| 2270 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | min_arg_count = 1; |
| 2273 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2274 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2275 | min_arg_count = 0; |
| 2276 | max_arg_count = 0; |
| 2277 | } |
| 2278 | break; |
| 2279 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2280 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2281 | min_arg_count = 2; |
| 2282 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2283 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2284 | min_arg_count = 0; |
| 2285 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2286 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2287 | min_arg_count = 2; |
| 2288 | max_arg_count = 2; |
| 2289 | } |
| 2290 | break; |
| 2291 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2292 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2293 | min_arg_count = 2; |
| 2294 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2295 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | min_arg_count = 3; |
| 2297 | max_arg_count = 3; |
| 2298 | } |
| 2299 | break; |
| 2300 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2301 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2302 | min_arg_count = 0; |
| 2303 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2304 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2305 | min_arg_count = 1; |
| 2306 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2307 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2308 | min_arg_count = 2; |
| 2309 | max_arg_count = 2; |
| 2310 | } |
| 2311 | break; |
| 2312 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2313 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2314 | min_arg_count = 2; |
| 2315 | max_arg_count = 2; |
| 2316 | } |
| 2317 | break; |
| 2318 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2320 | min_arg_count = 2; |
| 2321 | max_arg_count = 2; |
| 2322 | } |
| 2323 | break; |
| 2324 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2325 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2326 | min_arg_count = 0; |
| 2327 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | min_arg_count = 0; |
| 2330 | max_arg_count = 1; |
| 2331 | } |
| 2332 | break; |
| 2333 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2334 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2335 | min_arg_count = 0; |
| 2336 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2337 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2338 | min_arg_count = 2; |
| 2339 | max_arg_count = 2; |
| 2340 | } |
| 2341 | break; |
| 2342 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2343 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2344 | min_arg_count = 2; |
| 2345 | max_arg_count = 2; |
| 2346 | } |
| 2347 | break; |
| 2348 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2349 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2350 | min_arg_count = 2; |
| 2351 | max_arg_count = 2; |
| 2352 | } |
| 2353 | break; |
| 2354 | } |
| 2355 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2356 | LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2357 | return LY_EINVAL; |
| 2358 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2359 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2360 | |
| 2361 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2362 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2363 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2364 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2365 | |
| 2366 | /* ( Expr ( ',' Expr )* )? */ |
| 2367 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2368 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2369 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2370 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2371 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2372 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2373 | LY_CHECK_RET(rc); |
| 2374 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2375 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2376 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2377 | |
| 2378 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2379 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2380 | LY_CHECK_RET(rc); |
| 2381 | } |
| 2382 | |
| 2383 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2384 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2385 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2386 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2387 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2388 | if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2389 | LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2390 | return LY_EVALID; |
| 2391 | } |
| 2392 | |
| 2393 | return LY_SUCCESS; |
| 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * @brief Reparse PathExpr. Logs directly on error. |
| 2398 | * |
| 2399 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2400 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2401 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2402 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2403 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2404 | * |
| 2405 | * @param[in] ctx Context for logging. |
| 2406 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2407 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2408 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2409 | * @return LY_ERR |
| 2410 | */ |
| 2411 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2412 | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2413 | { |
| 2414 | LY_ERR rc; |
| 2415 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2417 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2418 | } |
| 2419 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2420 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2421 | case LYXP_TOKEN_PAR1: |
| 2422 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2423 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2424 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2425 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2426 | LY_CHECK_RET(rc); |
| 2427 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2428 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2429 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2430 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2431 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2432 | case LYXP_TOKEN_DOT: |
| 2433 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2434 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2435 | case LYXP_TOKEN_AT: |
| 2436 | case LYXP_TOKEN_NAMETEST: |
| 2437 | case LYXP_TOKEN_NODETYPE: |
| 2438 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2439 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2440 | LY_CHECK_RET(rc); |
| 2441 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2442 | case LYXP_TOKEN_VARREF: |
| 2443 | /* VariableReference */ |
| 2444 | ++(*tok_idx); |
| 2445 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2446 | case LYXP_TOKEN_FUNCNAME: |
| 2447 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2448 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2449 | LY_CHECK_RET(rc); |
| 2450 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2451 | case LYXP_TOKEN_OPER_PATH: |
| 2452 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2454 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2455 | LY_CHECK_RET(rc); |
| 2456 | break; |
| 2457 | case LYXP_TOKEN_LITERAL: |
| 2458 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2459 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2460 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2461 | case LYXP_TOKEN_NUMBER: |
| 2462 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2463 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2465 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2466 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2467 | return LY_EVALID; |
| 2468 | } |
| 2469 | |
| 2470 | return LY_SUCCESS; |
| 2471 | |
| 2472 | predicate: |
| 2473 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2474 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2475 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2476 | LY_CHECK_RET(rc); |
| 2477 | } |
| 2478 | |
| 2479 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2480 | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2481 | |
| 2482 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2483 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2484 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2485 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2486 | LY_CHECK_RET(rc); |
| 2487 | } |
| 2488 | |
| 2489 | return LY_SUCCESS; |
| 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2494 | * |
| 2495 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2496 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2497 | * |
| 2498 | * @param[in] ctx Context for logging. |
| 2499 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2501 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2502 | * @return LY_ERR |
| 2503 | */ |
| 2504 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2505 | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | { |
| 2507 | uint16_t prev_exp; |
| 2508 | LY_ERR rc; |
| 2509 | |
| 2510 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2512 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2513 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2514 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2519 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2520 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | LY_CHECK_RET(rc); |
| 2522 | |
| 2523 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2524 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2525 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2526 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2527 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2528 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2529 | LY_CHECK_RET(rc); |
| 2530 | } |
| 2531 | |
| 2532 | return LY_SUCCESS; |
| 2533 | } |
| 2534 | |
| 2535 | /** |
| 2536 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2537 | * |
| 2538 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2539 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2540 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2541 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2542 | * | MultiplicativeExpr '*' UnaryExpr |
| 2543 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2544 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2545 | * |
| 2546 | * @param[in] ctx Context for logging. |
| 2547 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2548 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2549 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2550 | * @return LY_ERR |
| 2551 | */ |
| 2552 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2553 | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2554 | { |
| 2555 | uint16_t prev_add_exp, prev_mul_exp; |
| 2556 | LY_ERR rc; |
| 2557 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2558 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2559 | goto reparse_multiplicative_expr; |
| 2560 | |
| 2561 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2562 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2563 | ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2564 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2565 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2566 | |
| 2567 | reparse_multiplicative_expr: |
| 2568 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2569 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2570 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2571 | LY_CHECK_RET(rc); |
| 2572 | |
| 2573 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2574 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2575 | ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2576 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2577 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2578 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2579 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2580 | LY_CHECK_RET(rc); |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | return LY_SUCCESS; |
| 2585 | } |
| 2586 | |
| 2587 | /** |
| 2588 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2589 | * |
| 2590 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2591 | * | EqualityExpr '!=' RelationalExpr |
| 2592 | * [14] RelationalExpr ::= AdditiveExpr |
| 2593 | * | RelationalExpr '<' AdditiveExpr |
| 2594 | * | RelationalExpr '>' AdditiveExpr |
| 2595 | * | RelationalExpr '<=' AdditiveExpr |
| 2596 | * | RelationalExpr '>=' AdditiveExpr |
| 2597 | * |
| 2598 | * @param[in] ctx Context for logging. |
| 2599 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2600 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2601 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2602 | * @return LY_ERR |
| 2603 | */ |
| 2604 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2605 | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2606 | { |
| 2607 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2608 | LY_ERR rc; |
| 2609 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2610 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2611 | goto reparse_additive_expr; |
| 2612 | |
| 2613 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2614 | while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2615 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2616 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | |
| 2618 | reparse_additive_expr: |
| 2619 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2620 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2621 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2622 | LY_CHECK_RET(rc); |
| 2623 | |
| 2624 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2625 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2626 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2627 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2628 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2629 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2630 | LY_CHECK_RET(rc); |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | return LY_SUCCESS; |
| 2635 | } |
| 2636 | |
| 2637 | /** |
| 2638 | * @brief Reparse OrExpr. Logs directly on error. |
| 2639 | * |
| 2640 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2641 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2642 | * |
| 2643 | * @param[in] ctx Context for logging. |
| 2644 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2645 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2646 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2647 | * @return LY_ERR |
| 2648 | */ |
| 2649 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2650 | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2651 | { |
| 2652 | uint16_t prev_or_exp, prev_and_exp; |
| 2653 | LY_ERR rc; |
| 2654 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2655 | ++depth; |
| 2656 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2657 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2658 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2659 | goto reparse_equality_expr; |
| 2660 | |
| 2661 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2662 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2663 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2664 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2665 | |
| 2666 | reparse_equality_expr: |
| 2667 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2668 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2669 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2670 | LY_CHECK_RET(rc); |
| 2671 | |
| 2672 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2673 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2674 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2675 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2676 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2677 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2678 | LY_CHECK_RET(rc); |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | return LY_SUCCESS; |
| 2683 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2684 | |
| 2685 | /** |
| 2686 | * @brief Parse NCName. |
| 2687 | * |
| 2688 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2689 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2690 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2691 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2692 | parse_ncname(const char *ncname) |
| 2693 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2694 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2695 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2696 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2697 | |
| 2698 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2699 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2700 | return len; |
| 2701 | } |
| 2702 | |
| 2703 | do { |
| 2704 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2705 | if (!*ncname) { |
| 2706 | break; |
| 2707 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2708 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2709 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2710 | |
| 2711 | return len; |
| 2712 | } |
| 2713 | |
| 2714 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2715 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2716 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2717 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2718 | * @param[in] exp Expression to use. |
| 2719 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2720 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2721 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2722 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2723 | */ |
| 2724 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2725 | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2726 | { |
| 2727 | uint32_t prev; |
| 2728 | |
| 2729 | if (exp->used == exp->size) { |
| 2730 | prev = exp->size; |
| 2731 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2732 | if (prev > exp->size) { |
| 2733 | LOGINT(ctx); |
| 2734 | return LY_EINT; |
| 2735 | } |
| 2736 | |
| 2737 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2738 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2739 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2740 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2741 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2742 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2743 | } |
| 2744 | |
| 2745 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2746 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2747 | exp->tok_len[exp->used] = tok_len; |
| 2748 | ++exp->used; |
| 2749 | return LY_SUCCESS; |
| 2750 | } |
| 2751 | |
| 2752 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2753 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2754 | { |
| 2755 | uint16_t i; |
| 2756 | |
| 2757 | if (!expr) { |
| 2758 | return; |
| 2759 | } |
| 2760 | |
| 2761 | lydict_remove(ctx, expr->expr); |
| 2762 | free(expr->tokens); |
| 2763 | free(expr->tok_pos); |
| 2764 | free(expr->tok_len); |
| 2765 | if (expr->repeat) { |
| 2766 | for (i = 0; i < expr->used; ++i) { |
| 2767 | free(expr->repeat[i]); |
| 2768 | } |
| 2769 | } |
| 2770 | free(expr->repeat); |
| 2771 | free(expr); |
| 2772 | } |
| 2773 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2774 | /** |
| 2775 | * @brief Parse Axis name. |
| 2776 | * |
| 2777 | * @param[in] str String to parse. |
| 2778 | * @param[in] str_len Length of @p str. |
| 2779 | * @return LY_SUCCESS if an axis. |
| 2780 | * @return LY_ENOT otherwise. |
| 2781 | */ |
| 2782 | static LY_ERR |
| 2783 | expr_parse_axis(const char *str, size_t str_len) |
| 2784 | { |
| 2785 | switch (str_len) { |
| 2786 | case 4: |
| 2787 | if (!strncmp("self", str, str_len)) { |
| 2788 | return LY_SUCCESS; |
| 2789 | } |
| 2790 | break; |
| 2791 | case 5: |
| 2792 | if (!strncmp("child", str, str_len)) { |
| 2793 | return LY_SUCCESS; |
| 2794 | } |
| 2795 | break; |
| 2796 | case 6: |
| 2797 | if (!strncmp("parent", str, str_len)) { |
| 2798 | return LY_SUCCESS; |
| 2799 | } |
| 2800 | break; |
| 2801 | case 8: |
| 2802 | if (!strncmp("ancestor", str, str_len)) { |
| 2803 | return LY_SUCCESS; |
| 2804 | } |
| 2805 | break; |
| 2806 | case 9: |
| 2807 | if (!strncmp("attribute", str, str_len)) { |
| 2808 | return LY_SUCCESS; |
| 2809 | } else if (!strncmp("following", str, str_len)) { |
| 2810 | return LY_SUCCESS; |
| 2811 | } else if (!strncmp("namespace", str, str_len)) { |
| 2812 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2813 | return LY_ENOT; |
| 2814 | } else if (!strncmp("preceding", str, str_len)) { |
| 2815 | return LY_SUCCESS; |
| 2816 | } |
| 2817 | break; |
| 2818 | case 10: |
| 2819 | if (!strncmp("descendant", str, str_len)) { |
| 2820 | return LY_SUCCESS; |
| 2821 | } |
| 2822 | break; |
| 2823 | case 16: |
| 2824 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2825 | return LY_SUCCESS; |
| 2826 | } |
| 2827 | break; |
| 2828 | case 17: |
| 2829 | if (!strncmp("following-sibling", str, str_len)) { |
| 2830 | return LY_SUCCESS; |
| 2831 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2832 | return LY_SUCCESS; |
| 2833 | } |
| 2834 | break; |
| 2835 | case 18: |
| 2836 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2837 | return LY_SUCCESS; |
| 2838 | } |
| 2839 | break; |
| 2840 | } |
| 2841 | |
| 2842 | return LY_ENOT; |
| 2843 | } |
| 2844 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2845 | LY_ERR |
| 2846 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2847 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2848 | LY_ERR ret = LY_SUCCESS; |
| 2849 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2850 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2851 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2852 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2853 | uint16_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2854 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2855 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2856 | assert(expr_p); |
| 2857 | |
| 2858 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2859 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2860 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2864 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2865 | } |
| 2866 | if (expr_len > UINT16_MAX) { |
Michal Vasko | 623ac7b | 2021-04-09 12:44:23 +0200 | [diff] [blame] | 2867 | LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2868 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2872 | expr = calloc(1, sizeof *expr); |
| 2873 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2874 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2875 | expr->used = 0; |
| 2876 | expr->size = LYXP_EXPR_SIZE_START; |
| 2877 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2878 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2879 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2880 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2881 | LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2882 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2883 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2884 | LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2885 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2886 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2887 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2888 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2889 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2890 | ++parsed; |
| 2891 | } |
| 2892 | |
| 2893 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2894 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2895 | |
| 2896 | /* '(' */ |
| 2897 | tok_len = 1; |
| 2898 | tok_type = LYXP_TOKEN_PAR1; |
| 2899 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2900 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2901 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2902 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2903 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2904 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2905 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2906 | /* it is NodeType after all */ |
| 2907 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2908 | |
| 2909 | prev_ntype_check = 0; |
| 2910 | prev_func_check = 0; |
| 2911 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2912 | /* it is FunctionName after all */ |
| 2913 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2914 | |
| 2915 | prev_ntype_check = 0; |
| 2916 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2917 | } |
| 2918 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2919 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2920 | |
| 2921 | /* ')' */ |
| 2922 | tok_len = 1; |
| 2923 | tok_type = LYXP_TOKEN_PAR2; |
| 2924 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2925 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2926 | |
| 2927 | /* '[' */ |
| 2928 | tok_len = 1; |
| 2929 | tok_type = LYXP_TOKEN_BRACK1; |
| 2930 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2931 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2932 | |
| 2933 | /* ']' */ |
| 2934 | tok_len = 1; |
| 2935 | tok_type = LYXP_TOKEN_BRACK2; |
| 2936 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2937 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2938 | |
| 2939 | /* '..' */ |
| 2940 | tok_len = 2; |
| 2941 | tok_type = LYXP_TOKEN_DDOT; |
| 2942 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2943 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2944 | |
| 2945 | /* '.' */ |
| 2946 | tok_len = 1; |
| 2947 | tok_type = LYXP_TOKEN_DOT; |
| 2948 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2949 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2950 | |
| 2951 | /* '@' */ |
| 2952 | tok_len = 1; |
| 2953 | tok_type = LYXP_TOKEN_AT; |
| 2954 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2955 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2956 | |
| 2957 | /* ',' */ |
| 2958 | tok_len = 1; |
| 2959 | tok_type = LYXP_TOKEN_COMMA; |
| 2960 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2961 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2962 | |
| 2963 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2964 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2965 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2966 | LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2967 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2968 | ++tok_len; |
| 2969 | tok_type = LYXP_TOKEN_LITERAL; |
| 2970 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2971 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2972 | |
| 2973 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2974 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2975 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2976 | LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2977 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2978 | ++tok_len; |
| 2979 | tok_type = LYXP_TOKEN_LITERAL; |
| 2980 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2981 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2982 | |
| 2983 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2984 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2985 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2986 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2987 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2988 | } |
| 2989 | tok_type = LYXP_TOKEN_NUMBER; |
| 2990 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2991 | } else if (expr_str[parsed] == '$') { |
| 2992 | |
| 2993 | /* VariableReference */ |
| 2994 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2995 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2996 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 2997 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 2998 | tok_len = ncname_len; |
| 2999 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 3000 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 3001 | error); |
| 3002 | tok_type = LYXP_TOKEN_VARREF; |
| 3003 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3004 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3005 | |
| 3006 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3007 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3008 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3009 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3010 | } else { |
| 3011 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3012 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3013 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3014 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3015 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3016 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3017 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3018 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3019 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 3020 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3021 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3022 | |
| 3023 | /* Operator '<=', '>=' */ |
| 3024 | tok_len = 2; |
| 3025 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3026 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3027 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3028 | |
| 3029 | /* Operator '|' */ |
| 3030 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3031 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3032 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3033 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3034 | |
| 3035 | /* Operator '+', '-' */ |
| 3036 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3037 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3038 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3039 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3040 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3041 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3042 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3043 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3044 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3045 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3046 | |
| 3047 | /* Operator '<', '>' */ |
| 3048 | tok_len = 1; |
| 3049 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3050 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3051 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3052 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3053 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3054 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3055 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3056 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3057 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3058 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3059 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3060 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3061 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3062 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3063 | |
| 3064 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3065 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3066 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3067 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3068 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3069 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3070 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3071 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3072 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3073 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3074 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3075 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3076 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3077 | } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3078 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3079 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3080 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3081 | } else if (prev_ntype_check || prev_func_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3082 | LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3083 | expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3084 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3085 | goto error; |
| 3086 | } else { |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3087 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3088 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3089 | goto error; |
| 3090 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3091 | } else { |
| 3092 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3093 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3094 | if (expr_str[parsed] == '*') { |
| 3095 | ncname_len = 1; |
| 3096 | } else { |
| 3097 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3098 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3099 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3100 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3101 | tok_len = ncname_len; |
| 3102 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3103 | has_axis = 0; |
| 3104 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3105 | /* axis */ |
| 3106 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
| 3107 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error); |
| 3108 | tok_type = LYXP_TOKEN_AXISNAME; |
| 3109 | |
| 3110 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3111 | parsed += tok_len; |
| 3112 | |
| 3113 | /* '::' */ |
| 3114 | tok_len = 2; |
| 3115 | tok_type = LYXP_TOKEN_DCOLON; |
| 3116 | |
| 3117 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3118 | parsed += tok_len; |
| 3119 | |
| 3120 | if (expr_str[parsed] == '*') { |
| 3121 | ncname_len = 1; |
| 3122 | } else { |
| 3123 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3124 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3125 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3126 | } |
| 3127 | tok_len = ncname_len; |
| 3128 | |
| 3129 | has_axis = 1; |
| 3130 | } |
| 3131 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3132 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3133 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3134 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3135 | ++tok_len; |
| 3136 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3137 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 3138 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3139 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3140 | tok_len += ncname_len; |
| 3141 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3142 | /* remove old flags to prevent ambiguities */ |
| 3143 | prev_ntype_check = 0; |
| 3144 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3145 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3146 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3147 | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 3148 | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
| 3149 | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3150 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3155 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3156 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3157 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3158 | ++parsed; |
| 3159 | } |
| 3160 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3161 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3162 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3163 | if (reparse) { |
| 3164 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3165 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 3166 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3167 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3168 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 3169 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3170 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3171 | LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3172 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3173 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3174 | goto error; |
| 3175 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3176 | } |
| 3177 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3178 | print_expr_struct_debug(expr); |
| 3179 | *expr_p = expr; |
| 3180 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3181 | |
| 3182 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3183 | lyxp_expr_free(ctx, expr); |
| 3184 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3185 | } |
| 3186 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3187 | LY_ERR |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3188 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t start_idx, uint16_t end_idx, |
| 3189 | struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3190 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3191 | LY_ERR ret = LY_SUCCESS; |
| 3192 | struct lyxp_expr *dup = NULL; |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3193 | uint16_t used = 0, i, j, k, expr_len; |
| 3194 | const char *expr_start; |
| 3195 | |
| 3196 | assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx))); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3197 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3198 | if (!exp) { |
| 3199 | goto cleanup; |
| 3200 | } |
| 3201 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3202 | if (!start_idx && !end_idx) { |
| 3203 | end_idx = exp->used - 1; |
| 3204 | } |
| 3205 | |
| 3206 | expr_start = exp->expr + exp->tok_pos[start_idx]; |
| 3207 | expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx]; |
| 3208 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3209 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3210 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3211 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3212 | if (exp->used) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3213 | used = (end_idx - start_idx) + 1; |
| 3214 | |
| 3215 | dup->tokens = malloc(used * sizeof *dup->tokens); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3216 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3217 | memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3218 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3219 | dup->tok_pos = malloc(used * sizeof *dup->tok_pos); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3220 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3221 | memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3222 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3223 | if (start_idx) { |
| 3224 | /* fix the indices in the expression */ |
| 3225 | for (i = 0; i < used; ++i) { |
| 3226 | dup->tok_pos[i] -= expr_start - exp->expr; |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | dup->tok_len = malloc(used * sizeof *dup->tok_len); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3231 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3232 | memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3233 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3234 | if (exp->repeat) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3235 | dup->repeat = malloc(used * sizeof *dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3236 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3237 | for (i = start_idx; i <= end_idx; ++i) { |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3238 | if (!exp->repeat[i]) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3239 | dup->repeat[i - start_idx] = NULL; |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3240 | } else { |
| 3241 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3242 | /* the ending 0 as well */ |
| 3243 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3244 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3245 | dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat); |
| 3246 | LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3247 | memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat); |
| 3248 | dup->repeat[i - start_idx][j - 1] = 0; |
| 3249 | |
| 3250 | if (start_idx) { |
| 3251 | /* fix the indices in the tokens */ |
| 3252 | for (k = 0; k < j; ++k) { |
| 3253 | dup->repeat[i - start_idx][k] -= start_idx; |
| 3254 | } |
| 3255 | } |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3256 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3257 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3258 | } |
| 3259 | } |
| 3260 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3261 | dup->used = used; |
| 3262 | dup->size = used; |
| 3263 | |
| 3264 | /* copy only subexpression */ |
| 3265 | 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] | 3266 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3267 | cleanup: |
| 3268 | if (ret) { |
| 3269 | lyxp_expr_free(ctx, dup); |
| 3270 | } else { |
| 3271 | *dup_p = dup; |
| 3272 | } |
| 3273 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3274 | } |
| 3275 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3276 | /** |
| 3277 | * @brief Get the last-added schema node that is currently in the context. |
| 3278 | * |
| 3279 | * @param[in] set Set to search in. |
| 3280 | * @return Last-added schema context node, NULL if no node is in context. |
| 3281 | */ |
| 3282 | static struct lysc_node * |
| 3283 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3284 | { |
| 3285 | uint32_t i; |
| 3286 | |
| 3287 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3288 | return NULL; |
| 3289 | } |
| 3290 | |
| 3291 | i = set->used; |
| 3292 | do { |
| 3293 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3294 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3295 | /* if there are more, simply return the first found (last added) */ |
| 3296 | return set->val.scnodes[i].scnode; |
| 3297 | } |
| 3298 | } while (i); |
| 3299 | |
| 3300 | return NULL; |
| 3301 | } |
| 3302 | |
| 3303 | /** |
| 3304 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3305 | * |
| 3306 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3307 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3308 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3309 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3310 | warn_is_numeric_type(struct lysc_type *type) |
| 3311 | { |
| 3312 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3313 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3314 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3315 | |
| 3316 | switch (type->basetype) { |
| 3317 | case LY_TYPE_DEC64: |
| 3318 | case LY_TYPE_INT8: |
| 3319 | case LY_TYPE_UINT8: |
| 3320 | case LY_TYPE_INT16: |
| 3321 | case LY_TYPE_UINT16: |
| 3322 | case LY_TYPE_INT32: |
| 3323 | case LY_TYPE_UINT32: |
| 3324 | case LY_TYPE_INT64: |
| 3325 | case LY_TYPE_UINT64: |
| 3326 | return 1; |
| 3327 | case LY_TYPE_UNION: |
| 3328 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3329 | LY_ARRAY_FOR(uni->types, u) { |
| 3330 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3331 | if (ret) { |
| 3332 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3333 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3334 | } |
| 3335 | } |
| 3336 | /* did not find any suitable type */ |
| 3337 | return 0; |
| 3338 | case LY_TYPE_LEAFREF: |
| 3339 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3340 | default: |
| 3341 | return 0; |
| 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | /** |
| 3346 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3347 | * |
| 3348 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3349 | * @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] | 3350 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3351 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3352 | warn_is_string_type(struct lysc_type *type) |
| 3353 | { |
| 3354 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3355 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3356 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3357 | |
| 3358 | switch (type->basetype) { |
| 3359 | case LY_TYPE_BITS: |
| 3360 | case LY_TYPE_ENUM: |
| 3361 | case LY_TYPE_IDENT: |
| 3362 | case LY_TYPE_INST: |
| 3363 | case LY_TYPE_STRING: |
| 3364 | return 1; |
| 3365 | case LY_TYPE_UNION: |
| 3366 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3367 | LY_ARRAY_FOR(uni->types, u) { |
| 3368 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3369 | if (ret) { |
| 3370 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3371 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3372 | } |
| 3373 | } |
| 3374 | /* did not find any suitable type */ |
| 3375 | return 0; |
| 3376 | case LY_TYPE_LEAFREF: |
| 3377 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3378 | default: |
| 3379 | return 0; |
| 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | /** |
| 3384 | * @brief Test whether a type is one specific type. |
| 3385 | * |
| 3386 | * @param[in] type Type to test. |
| 3387 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3388 | * @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] | 3389 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3390 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3391 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3392 | { |
| 3393 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3394 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3395 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3396 | |
| 3397 | if (type->basetype == base) { |
| 3398 | return 1; |
| 3399 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3400 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3401 | LY_ARRAY_FOR(uni->types, u) { |
| 3402 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3403 | if (ret) { |
| 3404 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3405 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3406 | } |
| 3407 | } |
| 3408 | /* did not find any suitable type */ |
| 3409 | return 0; |
| 3410 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3411 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3412 | } |
| 3413 | |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
| 3417 | /** |
| 3418 | * @brief Get next type of a (union) type. |
| 3419 | * |
| 3420 | * @param[in] type Base type. |
| 3421 | * @param[in] prev_type Previously returned type. |
| 3422 | * @return Next type or NULL. |
| 3423 | */ |
| 3424 | static struct lysc_type * |
| 3425 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3426 | { |
| 3427 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3428 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3429 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3430 | |
| 3431 | switch (type->basetype) { |
| 3432 | case LY_TYPE_UNION: |
| 3433 | uni = (struct lysc_type_union *)type; |
| 3434 | if (!prev_type) { |
| 3435 | return uni->types[0]; |
| 3436 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3437 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3438 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3439 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3440 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3441 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3442 | found = 1; |
| 3443 | } |
| 3444 | } |
| 3445 | return NULL; |
| 3446 | default: |
| 3447 | if (prev_type) { |
| 3448 | assert(type == prev_type); |
| 3449 | return NULL; |
| 3450 | } else { |
| 3451 | return type; |
| 3452 | } |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | /** |
| 3457 | * @brief Test whether 2 types have a common type. |
| 3458 | * |
| 3459 | * @param[in] type1 First type. |
| 3460 | * @param[in] type2 Second type. |
| 3461 | * @return 1 if they do, 0 otherwise. |
| 3462 | */ |
| 3463 | static int |
| 3464 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3465 | { |
| 3466 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3467 | |
| 3468 | t1 = NULL; |
| 3469 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3470 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3471 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3472 | } else { |
| 3473 | rt1 = t1; |
| 3474 | } |
| 3475 | |
| 3476 | t2 = NULL; |
| 3477 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3478 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3479 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3480 | } else { |
| 3481 | rt2 = t2; |
| 3482 | } |
| 3483 | |
| 3484 | if (rt2->basetype == rt1->basetype) { |
| 3485 | /* match found */ |
| 3486 | return 1; |
| 3487 | } |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | return 0; |
| 3492 | } |
| 3493 | |
| 3494 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3495 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3496 | * |
| 3497 | * @param[in] ctx Context for logging. |
| 3498 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3499 | * @param[in] subexpr Subexpression start. |
| 3500 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3501 | * @param[in] cur_scnode Expression context node. |
| 3502 | */ |
| 3503 | static void |
| 3504 | warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len, |
| 3505 | const struct lysc_node *cur_scnode) |
| 3506 | { |
| 3507 | char *path; |
| 3508 | |
| 3509 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 3510 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".", |
| 3511 | tok_pos, subexpr_len, subexpr, path); |
| 3512 | free(path); |
| 3513 | } |
| 3514 | |
| 3515 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3516 | * @brief Check both operands of comparison operators. |
| 3517 | * |
| 3518 | * @param[in] ctx Context for errors. |
| 3519 | * @param[in] set1 First operand set. |
| 3520 | * @param[in] set2 Second operand set. |
| 3521 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3522 | * @param[in] expr Start of the expression to print with the warning. |
| 3523 | * @param[in] tok_pos Token position. |
| 3524 | */ |
| 3525 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3526 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, |
| 3527 | uint16_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3528 | { |
| 3529 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3530 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3531 | |
| 3532 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3533 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3534 | |
| 3535 | if (!node1 && !node2) { |
| 3536 | /* no node-sets involved, nothing to do */ |
| 3537 | return; |
| 3538 | } |
| 3539 | |
| 3540 | if (node1) { |
| 3541 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3542 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3543 | warning = 1; |
| 3544 | leaves = 0; |
| 3545 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3546 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3547 | warning = 1; |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | if (node2) { |
| 3552 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3553 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3554 | warning = 1; |
| 3555 | leaves = 0; |
| 3556 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3557 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3558 | warning = 1; |
| 3559 | } |
| 3560 | } |
| 3561 | |
| 3562 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3563 | if ((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_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3566 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3567 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3568 | warning = 1; |
| 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3573 | 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] | 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | /** |
| 3578 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3579 | * |
| 3580 | * @param[in] exp Parsed XPath expression. |
| 3581 | * @param[in] set Set with the leaf/leaf-list. |
| 3582 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3583 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3584 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3585 | */ |
| 3586 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3587 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3588 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3589 | { |
| 3590 | struct lysc_node *scnode; |
| 3591 | struct lysc_type *type; |
| 3592 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3593 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3594 | LY_ERR rc; |
| 3595 | struct ly_err_item *err = NULL; |
| 3596 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3597 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3598 | ((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] | 3599 | /* check that the node can have the specified value */ |
| 3600 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3601 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3602 | } else { |
| 3603 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3604 | } |
| 3605 | if (!value) { |
| 3606 | LOGMEM(set->ctx); |
| 3607 | return; |
| 3608 | } |
| 3609 | |
| 3610 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3611 | 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] | 3612 | " 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] | 3613 | 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] | 3614 | (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] | 3615 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3616 | } |
| 3617 | |
| 3618 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3619 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3620 | 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] | 3621 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3622 | if (rc == LY_EINCOMPLETE) { |
| 3623 | rc = LY_SUCCESS; |
| 3624 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3625 | |
| 3626 | if (err) { |
| 3627 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3628 | ly_err_free(err); |
| 3629 | } else if (rc != LY_SUCCESS) { |
| 3630 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3631 | } |
| 3632 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3633 | 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] | 3634 | (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] | 3635 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3636 | } else { |
| 3637 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3638 | } |
| 3639 | } |
| 3640 | free(value); |
| 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | /* |
| 3645 | * XPath functions |
| 3646 | */ |
| 3647 | |
| 3648 | /** |
| 3649 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3650 | * depending on whether the first node bit value from the second argument is set. |
| 3651 | * |
| 3652 | * @param[in] args Array of arguments. |
| 3653 | * @param[in] arg_count Count of elements in @p args. |
| 3654 | * @param[in,out] set Context and result set at the same time. |
| 3655 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3656 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3657 | */ |
| 3658 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3659 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3660 | { |
| 3661 | struct lyd_node_term *leaf; |
| 3662 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3663 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3664 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3665 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3666 | |
| 3667 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3668 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3669 | 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] | 3670 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3671 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3672 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3673 | sleaf->name); |
| 3674 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3675 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3676 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3680 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3681 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3682 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3683 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3684 | 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] | 3685 | } |
| 3686 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3687 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3688 | return rc; |
| 3689 | } |
| 3690 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3691 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3692 | 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] | 3693 | return LY_EVALID; |
| 3694 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3695 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3696 | LY_CHECK_RET(rc); |
| 3697 | |
| 3698 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3699 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3700 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3701 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3702 | LYD_VALUE_GET(&leaf->value, bits); |
| 3703 | LY_ARRAY_FOR(bits->items, u) { |
| 3704 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3705 | set_fill_boolean(set, 1); |
| 3706 | break; |
| 3707 | } |
| 3708 | } |
| 3709 | } |
| 3710 | } |
| 3711 | |
| 3712 | return LY_SUCCESS; |
| 3713 | } |
| 3714 | |
| 3715 | /** |
| 3716 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3717 | * with the argument converted to boolean. |
| 3718 | * |
| 3719 | * @param[in] args Array of arguments. |
| 3720 | * @param[in] arg_count Count of elements in @p args. |
| 3721 | * @param[in,out] set Context and result set at the same time. |
| 3722 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3723 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3724 | */ |
| 3725 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3726 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3727 | { |
| 3728 | LY_ERR rc; |
| 3729 | |
| 3730 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3731 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3732 | return LY_SUCCESS; |
| 3733 | } |
| 3734 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3735 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3736 | LY_CHECK_RET(rc); |
| 3737 | set_fill_set(set, args[0]); |
| 3738 | |
| 3739 | return LY_SUCCESS; |
| 3740 | } |
| 3741 | |
| 3742 | /** |
| 3743 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3744 | * with the first argument rounded up to the nearest integer. |
| 3745 | * |
| 3746 | * @param[in] args Array of arguments. |
| 3747 | * @param[in] arg_count Count of elements in @p args. |
| 3748 | * @param[in,out] set Context and result set at the same time. |
| 3749 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3750 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3751 | */ |
| 3752 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3753 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3754 | { |
| 3755 | struct lysc_node_leaf *sleaf; |
| 3756 | LY_ERR rc = LY_SUCCESS; |
| 3757 | |
| 3758 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3759 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3760 | 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] | 3761 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3762 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3763 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3764 | sleaf->name); |
| 3765 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3766 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3767 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3768 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3769 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3770 | return rc; |
| 3771 | } |
| 3772 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3773 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3774 | LY_CHECK_RET(rc); |
| 3775 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3776 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3777 | } else { |
| 3778 | set_fill_number(set, args[0]->val.num); |
| 3779 | } |
| 3780 | |
| 3781 | return LY_SUCCESS; |
| 3782 | } |
| 3783 | |
| 3784 | /** |
| 3785 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3786 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3787 | * |
| 3788 | * @param[in] args Array of arguments. |
| 3789 | * @param[in] arg_count Count of elements in @p args. |
| 3790 | * @param[in,out] set Context and result set at the same time. |
| 3791 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3792 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3793 | */ |
| 3794 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3795 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3796 | { |
| 3797 | uint16_t i; |
| 3798 | char *str = NULL; |
| 3799 | size_t used = 1; |
| 3800 | LY_ERR rc = LY_SUCCESS; |
| 3801 | struct lysc_node_leaf *sleaf; |
| 3802 | |
| 3803 | if (options & LYXP_SCNODE_ALL) { |
| 3804 | for (i = 0; i < arg_count; ++i) { |
| 3805 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3806 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3807 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3808 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3809 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3810 | 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] | 3811 | } |
| 3812 | } |
| 3813 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3814 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3815 | return rc; |
| 3816 | } |
| 3817 | |
| 3818 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3819 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3820 | if (rc != LY_SUCCESS) { |
| 3821 | free(str); |
| 3822 | return rc; |
| 3823 | } |
| 3824 | |
| 3825 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3826 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3827 | strcpy(str + used - 1, args[i]->val.str); |
| 3828 | used += strlen(args[i]->val.str); |
| 3829 | } |
| 3830 | |
| 3831 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3832 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3833 | set->type = LYXP_SET_STRING; |
| 3834 | set->val.str = str; |
| 3835 | |
| 3836 | return LY_SUCCESS; |
| 3837 | } |
| 3838 | |
| 3839 | /** |
| 3840 | * @brief Execute the XPath contains(string, string) function. |
| 3841 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3842 | * be found in the first or not. |
| 3843 | * |
| 3844 | * @param[in] args Array of arguments. |
| 3845 | * @param[in] arg_count Count of elements in @p args. |
| 3846 | * @param[in,out] set Context and result set at the same time. |
| 3847 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3848 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3849 | */ |
| 3850 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3851 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3852 | { |
| 3853 | struct lysc_node_leaf *sleaf; |
| 3854 | LY_ERR rc = LY_SUCCESS; |
| 3855 | |
| 3856 | if (options & LYXP_SCNODE_ALL) { |
| 3857 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3858 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3859 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3860 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3861 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3862 | LOGWRN(set->ctx, "Argument #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] | 3863 | } |
| 3864 | } |
| 3865 | |
| 3866 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3867 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3868 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3869 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3870 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3871 | 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] | 3872 | } |
| 3873 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3874 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3875 | return rc; |
| 3876 | } |
| 3877 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3878 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3879 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3880 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3881 | LY_CHECK_RET(rc); |
| 3882 | |
| 3883 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3884 | set_fill_boolean(set, 1); |
| 3885 | } else { |
| 3886 | set_fill_boolean(set, 0); |
| 3887 | } |
| 3888 | |
| 3889 | return LY_SUCCESS; |
| 3890 | } |
| 3891 | |
| 3892 | /** |
| 3893 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3894 | * with the size of the node-set from the argument. |
| 3895 | * |
| 3896 | * @param[in] args Array of arguments. |
| 3897 | * @param[in] arg_count Count of elements in @p args. |
| 3898 | * @param[in,out] set Context and result set at the same time. |
| 3899 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3900 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3901 | */ |
| 3902 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3903 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3904 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3905 | LY_ERR rc = LY_SUCCESS; |
| 3906 | |
| 3907 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3908 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3909 | 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] | 3910 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3911 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3912 | return rc; |
| 3913 | } |
| 3914 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3915 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3916 | 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] | 3917 | return LY_EVALID; |
| 3918 | } |
| 3919 | |
| 3920 | set_fill_number(set, args[0]->used); |
| 3921 | return LY_SUCCESS; |
| 3922 | } |
| 3923 | |
| 3924 | /** |
| 3925 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3926 | * with the context with the intial node. |
| 3927 | * |
| 3928 | * @param[in] args Array of arguments. |
| 3929 | * @param[in] arg_count Count of elements in @p args. |
| 3930 | * @param[in,out] set Context and result set at the same time. |
| 3931 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3932 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3933 | */ |
| 3934 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3935 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3936 | { |
| 3937 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3938 | 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] | 3939 | return LY_EVALID; |
| 3940 | } |
| 3941 | |
| 3942 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3943 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3944 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3945 | if (set->cur_scnode) { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 3946 | 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] | 3947 | } else { |
| 3948 | /* root node */ |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 3949 | 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] | 3950 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3951 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3952 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3953 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3954 | if (set->cur_node) { |
| 3955 | /* position is filled later */ |
| 3956 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3957 | } else { |
| 3958 | /* root node */ |
| 3959 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3960 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | return LY_SUCCESS; |
| 3964 | } |
| 3965 | |
| 3966 | /** |
| 3967 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3968 | * leafref or instance-identifier target node(s). |
| 3969 | * |
| 3970 | * @param[in] args Array of arguments. |
| 3971 | * @param[in] arg_count Count of elements in @p args. |
| 3972 | * @param[in,out] set Context and result set at the same time. |
| 3973 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3974 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3975 | */ |
| 3976 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3977 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3978 | { |
| 3979 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3980 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3981 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3982 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3983 | struct ly_path *p; |
| 3984 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3985 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3986 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3987 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3988 | |
| 3989 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3990 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3991 | 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] | 3992 | } 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] | 3993 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3994 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3995 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3996 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3997 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3998 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3999 | __func__, sleaf->name); |
| 4000 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4001 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4002 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 4003 | 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] | 4004 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 4005 | 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] | 4006 | |
| 4007 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4008 | 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] | 4009 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4010 | if (!r) { |
| 4011 | /* get the target node */ |
| 4012 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 4013 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4014 | |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 4015 | 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] | 4016 | } /* 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] | 4017 | } |
| 4018 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4019 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4020 | } |
| 4021 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4022 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4023 | 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] | 4024 | return LY_EVALID; |
| 4025 | } |
| 4026 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4027 | lyxp_set_free_content(set); |
| 4028 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4029 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4030 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4031 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4032 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 4033 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4034 | 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] | 4035 | &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4036 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4037 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4038 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4039 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4040 | } else { |
| 4041 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4042 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4043 | 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] | 4044 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4045 | return LY_EVALID; |
| 4046 | } |
| 4047 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4048 | |
| 4049 | /* insert it */ |
| 4050 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4051 | } |
| 4052 | } |
| 4053 | |
| 4054 | return LY_SUCCESS; |
| 4055 | } |
| 4056 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4057 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4058 | 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] | 4059 | { |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 4060 | uint32_t i, id_len; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4061 | LY_ARRAY_COUNT_TYPE u; |
| 4062 | struct lyd_node_term *leaf; |
| 4063 | struct lysc_node_leaf *sleaf; |
| 4064 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4065 | struct lyd_value *val; |
| 4066 | const struct lys_module *mod; |
| 4067 | const char *id_name; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4068 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4069 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4070 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4071 | |
| 4072 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4073 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4074 | 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] | 4075 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4076 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4077 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4078 | sleaf->name); |
| 4079 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4080 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4081 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4085 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4086 | 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] | 4087 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4088 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4089 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4090 | } |
| 4091 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4092 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4093 | return rc; |
| 4094 | } |
| 4095 | |
| 4096 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4097 | 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] | 4098 | return LY_EVALID; |
| 4099 | } |
| 4100 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4101 | LY_CHECK_RET(rc); |
| 4102 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4103 | /* parse the identity */ |
| 4104 | id_name = args[1]->val.str; |
| 4105 | id_len = strlen(id_name); |
| 4106 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4107 | LY_CHECK_RET(rc); |
| 4108 | if (!mod) { |
| 4109 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4110 | return LY_EVALID; |
| 4111 | } |
| 4112 | |
| 4113 | /* find the identity */ |
| 4114 | found = 0; |
| 4115 | LY_ARRAY_FOR(mod->identities, u) { |
| 4116 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4117 | /* we have match */ |
| 4118 | found = 1; |
| 4119 | break; |
| 4120 | } |
| 4121 | } |
| 4122 | if (!found) { |
| 4123 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4124 | return LY_EVALID; |
| 4125 | } |
| 4126 | id = &mod->identities[u]; |
| 4127 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4128 | set_fill_boolean(set, 0); |
| 4129 | found = 0; |
| 4130 | for (i = 0; i < args[0]->used; ++i) { |
| 4131 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4132 | continue; |
| 4133 | } |
| 4134 | |
| 4135 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4136 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4137 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4138 | val = &leaf->value; |
| 4139 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 4140 | /* uninteresting */ |
| 4141 | continue; |
| 4142 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4143 | } else { |
| 4144 | meta = args[0]->val.meta[i].meta; |
| 4145 | val = &meta->value; |
| 4146 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4147 | /* uninteresting */ |
| 4148 | continue; |
| 4149 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4150 | } |
| 4151 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4152 | /* check the identity itself */ |
| 4153 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4154 | set_fill_boolean(set, 1); |
| 4155 | found = 1; |
| 4156 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4157 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4158 | set_fill_boolean(set, 1); |
| 4159 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4160 | } |
| 4161 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4162 | if (found) { |
| 4163 | break; |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | return LY_SUCCESS; |
| 4168 | } |
| 4169 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4170 | /** |
| 4171 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4172 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4173 | * argument identity. |
| 4174 | * |
| 4175 | * @param[in] args Array of arguments. |
| 4176 | * @param[in] arg_count Count of elements in @p args. |
| 4177 | * @param[in,out] set Context and result set at the same time. |
| 4178 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4179 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4180 | */ |
| 4181 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4182 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4183 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4184 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | /** |
| 4188 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4189 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4190 | * the second argument identity. |
| 4191 | * |
| 4192 | * @param[in] args Array of arguments. |
| 4193 | * @param[in] arg_count Count of elements in @p args. |
| 4194 | * @param[in,out] set Context and result set at the same time. |
| 4195 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4196 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4197 | */ |
| 4198 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4199 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4200 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4201 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | /** |
| 4205 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4206 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4207 | * |
| 4208 | * @param[in] args Array of arguments. |
| 4209 | * @param[in] arg_count Count of elements in @p args. |
| 4210 | * @param[in,out] set Context and result set at the same time. |
| 4211 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4212 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4213 | */ |
| 4214 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4215 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4216 | { |
| 4217 | struct lyd_node_term *leaf; |
| 4218 | struct lysc_node_leaf *sleaf; |
| 4219 | LY_ERR rc = LY_SUCCESS; |
| 4220 | |
| 4221 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4222 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4223 | 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] | 4224 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4225 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4226 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4227 | sleaf->name); |
| 4228 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4229 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4230 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4231 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4232 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4233 | return rc; |
| 4234 | } |
| 4235 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4236 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4237 | 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] | 4238 | return LY_EVALID; |
| 4239 | } |
| 4240 | |
| 4241 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4242 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4243 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4244 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4245 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4246 | set_fill_number(set, leaf->value.enum_item->value); |
| 4247 | } |
| 4248 | } |
| 4249 | |
| 4250 | return LY_SUCCESS; |
| 4251 | } |
| 4252 | |
| 4253 | /** |
| 4254 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4255 | * with false value. |
| 4256 | * |
| 4257 | * @param[in] args Array of arguments. |
| 4258 | * @param[in] arg_count Count of elements in @p args. |
| 4259 | * @param[in,out] set Context and result set at the same time. |
| 4260 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4261 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4262 | */ |
| 4263 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4264 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4265 | { |
| 4266 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4267 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4268 | return LY_SUCCESS; |
| 4269 | } |
| 4270 | |
| 4271 | set_fill_boolean(set, 0); |
| 4272 | return LY_SUCCESS; |
| 4273 | } |
| 4274 | |
| 4275 | /** |
| 4276 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4277 | * with the first argument floored (truncated). |
| 4278 | * |
| 4279 | * @param[in] args Array of arguments. |
| 4280 | * @param[in] arg_count Count of elements in @p args. |
| 4281 | * @param[in,out] set Context and result set at the same time. |
| 4282 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4283 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4284 | */ |
| 4285 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4286 | xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4287 | { |
| 4288 | LY_ERR rc; |
| 4289 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4290 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4291 | LY_CHECK_RET(rc); |
| 4292 | if (isfinite(args[0]->val.num)) { |
| 4293 | set_fill_number(set, (long long)args[0]->val.num); |
| 4294 | } |
| 4295 | |
| 4296 | return LY_SUCCESS; |
| 4297 | } |
| 4298 | |
| 4299 | /** |
| 4300 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4301 | * whether the language of the text matches the one from the argument. |
| 4302 | * |
| 4303 | * @param[in] args Array of arguments. |
| 4304 | * @param[in] arg_count Count of elements in @p args. |
| 4305 | * @param[in,out] set Context and result set at the same time. |
| 4306 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4307 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4308 | */ |
| 4309 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4310 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4311 | { |
| 4312 | const struct lyd_node *node; |
| 4313 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4314 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4315 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4316 | LY_ERR rc = LY_SUCCESS; |
| 4317 | |
| 4318 | if (options & LYXP_SCNODE_ALL) { |
| 4319 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4320 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4321 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4322 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4323 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4324 | 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] | 4325 | } |
| 4326 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4327 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4328 | return rc; |
| 4329 | } |
| 4330 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4331 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4332 | LY_CHECK_RET(rc); |
| 4333 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4334 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4335 | 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] | 4336 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4337 | } else if (!set->used) { |
| 4338 | set_fill_boolean(set, 0); |
| 4339 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | switch (set->val.nodes[0].type) { |
| 4343 | case LYXP_NODE_ELEM: |
| 4344 | case LYXP_NODE_TEXT: |
| 4345 | node = set->val.nodes[0].node; |
| 4346 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4347 | case LYXP_NODE_META: |
| 4348 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4349 | break; |
| 4350 | default: |
| 4351 | /* nothing to do with roots */ |
| 4352 | set_fill_boolean(set, 0); |
| 4353 | return LY_SUCCESS; |
| 4354 | } |
| 4355 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4356 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4357 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4358 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4359 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4360 | 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] | 4361 | break; |
| 4362 | } |
| 4363 | } |
| 4364 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4365 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4366 | break; |
| 4367 | } |
| 4368 | } |
| 4369 | |
| 4370 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4371 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4372 | set_fill_boolean(set, 0); |
| 4373 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4374 | uint64_t i; |
| 4375 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4376 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4377 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4378 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4379 | set_fill_boolean(set, 0); |
| 4380 | break; |
| 4381 | } |
| 4382 | } |
| 4383 | if (!args[0]->val.str[i]) { |
| 4384 | if (!val[i] || (val[i] == '-')) { |
| 4385 | set_fill_boolean(set, 1); |
| 4386 | } else { |
| 4387 | set_fill_boolean(set, 0); |
| 4388 | } |
| 4389 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | return LY_SUCCESS; |
| 4393 | } |
| 4394 | |
| 4395 | /** |
| 4396 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4397 | * with the context size. |
| 4398 | * |
| 4399 | * @param[in] args Array of arguments. |
| 4400 | * @param[in] arg_count Count of elements in @p args. |
| 4401 | * @param[in,out] set Context and result set at the same time. |
| 4402 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4403 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4404 | */ |
| 4405 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4406 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4407 | { |
| 4408 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4409 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4410 | return LY_SUCCESS; |
| 4411 | } |
| 4412 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4413 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4414 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4415 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4416 | } else if (!set->used) { |
| 4417 | set_fill_number(set, 0); |
| 4418 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | set_fill_number(set, set->ctx_size); |
| 4422 | return LY_SUCCESS; |
| 4423 | } |
| 4424 | |
| 4425 | /** |
| 4426 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4427 | * with the node name without namespace from the argument or the context. |
| 4428 | * |
| 4429 | * @param[in] args Array of arguments. |
| 4430 | * @param[in] arg_count Count of elements in @p args. |
| 4431 | * @param[in,out] set Context and result set at the same time. |
| 4432 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4433 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4434 | */ |
| 4435 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4436 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4437 | { |
| 4438 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4439 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4440 | /* suppress unused variable warning */ |
| 4441 | (void)options; |
| 4442 | |
| 4443 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4444 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4445 | return LY_SUCCESS; |
| 4446 | } |
| 4447 | |
| 4448 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4449 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4450 | 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] | 4451 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4452 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4453 | } else if (!args[0]->used) { |
| 4454 | set_fill_string(set, "", 0); |
| 4455 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4459 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4460 | |
| 4461 | item = &args[0]->val.nodes[0]; |
| 4462 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4463 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4464 | 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] | 4465 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4466 | } else if (!set->used) { |
| 4467 | set_fill_string(set, "", 0); |
| 4468 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4472 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4473 | |
| 4474 | item = &set->val.nodes[0]; |
| 4475 | } |
| 4476 | |
| 4477 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4478 | case LYXP_NODE_NONE: |
| 4479 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4480 | case LYXP_NODE_ROOT: |
| 4481 | case LYXP_NODE_ROOT_CONFIG: |
| 4482 | case LYXP_NODE_TEXT: |
| 4483 | set_fill_string(set, "", 0); |
| 4484 | break; |
| 4485 | case LYXP_NODE_ELEM: |
| 4486 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4487 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4488 | case LYXP_NODE_META: |
| 4489 | 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] | 4490 | break; |
| 4491 | } |
| 4492 | |
| 4493 | return LY_SUCCESS; |
| 4494 | } |
| 4495 | |
| 4496 | /** |
| 4497 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4498 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4499 | * |
| 4500 | * @param[in] args Array of arguments. |
| 4501 | * @param[in] arg_count Count of elements in @p args. |
| 4502 | * @param[in,out] set Context and result set at the same time. |
| 4503 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4504 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4505 | */ |
| 4506 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4507 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4508 | { |
| 4509 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4510 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4511 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4512 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4513 | |
| 4514 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4515 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4516 | return LY_SUCCESS; |
| 4517 | } |
| 4518 | |
| 4519 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4520 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4521 | 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] | 4522 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4523 | } else if (!args[0]->used) { |
| 4524 | set_fill_string(set, "", 0); |
| 4525 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4529 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4530 | |
| 4531 | item = &args[0]->val.nodes[0]; |
| 4532 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4533 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4534 | 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] | 4535 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4536 | } else if (!set->used) { |
| 4537 | set_fill_string(set, "", 0); |
| 4538 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4539 | } |
| 4540 | |
| 4541 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4542 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4543 | |
| 4544 | item = &set->val.nodes[0]; |
| 4545 | } |
| 4546 | |
| 4547 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4548 | case LYXP_NODE_NONE: |
| 4549 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4550 | case LYXP_NODE_ROOT: |
| 4551 | case LYXP_NODE_ROOT_CONFIG: |
| 4552 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4553 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4554 | break; |
| 4555 | case LYXP_NODE_ELEM: |
| 4556 | mod = item->node->schema->module; |
| 4557 | name = item->node->schema->name; |
| 4558 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4559 | case LYXP_NODE_META: |
| 4560 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4561 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4562 | break; |
| 4563 | } |
| 4564 | |
| 4565 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4566 | 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] | 4567 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4568 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4569 | set_fill_string(set, str, strlen(str)); |
| 4570 | free(str); |
| 4571 | } else { |
| 4572 | set_fill_string(set, "", 0); |
| 4573 | } |
| 4574 | |
| 4575 | return LY_SUCCESS; |
| 4576 | } |
| 4577 | |
| 4578 | /** |
| 4579 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4580 | * with the namespace of the node from the argument or the context. |
| 4581 | * |
| 4582 | * @param[in] args Array of arguments. |
| 4583 | * @param[in] arg_count Count of elements in @p args. |
| 4584 | * @param[in,out] set Context and result set at the same time. |
| 4585 | * @param[in] options XPath options. |
| 4586 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4587 | */ |
| 4588 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4589 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4590 | { |
| 4591 | struct lyxp_set_node *item; |
| 4592 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4593 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4594 | /* suppress unused variable warning */ |
| 4595 | (void)options; |
| 4596 | |
| 4597 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4598 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4599 | return LY_SUCCESS; |
| 4600 | } |
| 4601 | |
| 4602 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4603 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4604 | 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] | 4605 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4606 | return LY_EVALID; |
| 4607 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4608 | set_fill_string(set, "", 0); |
| 4609 | return LY_SUCCESS; |
| 4610 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4611 | |
| 4612 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4613 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4614 | |
| 4615 | item = &args[0]->val.nodes[0]; |
| 4616 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4617 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4618 | 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] | 4619 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4620 | } else if (!set->used) { |
| 4621 | set_fill_string(set, "", 0); |
| 4622 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4626 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4627 | |
| 4628 | item = &set->val.nodes[0]; |
| 4629 | } |
| 4630 | |
| 4631 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4632 | case LYXP_NODE_NONE: |
| 4633 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4634 | case LYXP_NODE_ROOT: |
| 4635 | case LYXP_NODE_ROOT_CONFIG: |
| 4636 | case LYXP_NODE_TEXT: |
| 4637 | set_fill_string(set, "", 0); |
| 4638 | break; |
| 4639 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4640 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4641 | if (item->type == LYXP_NODE_ELEM) { |
| 4642 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4643 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4644 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4645 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4649 | break; |
| 4650 | } |
| 4651 | |
| 4652 | return LY_SUCCESS; |
| 4653 | } |
| 4654 | |
| 4655 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4656 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4657 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4658 | * from the argument or the context. |
| 4659 | * |
| 4660 | * @param[in] args Array of arguments. |
| 4661 | * @param[in] arg_count Count of elements in @p args. |
| 4662 | * @param[in,out] set Context and result set at the same time. |
| 4663 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4664 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4665 | */ |
| 4666 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4667 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4668 | { |
| 4669 | uint16_t i, new_used; |
| 4670 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4671 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4672 | struct lysc_node_leaf *sleaf; |
| 4673 | LY_ERR rc = LY_SUCCESS; |
| 4674 | |
| 4675 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4676 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4677 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4678 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4679 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4680 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4681 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4682 | 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] | 4683 | } |
| 4684 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4685 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4686 | return rc; |
| 4687 | } |
| 4688 | |
| 4689 | if (arg_count) { |
| 4690 | set_fill_set(set, args[0]); |
| 4691 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4692 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4693 | LY_CHECK_RET(rc); |
| 4694 | |
| 4695 | /* is there any normalization necessary? */ |
| 4696 | for (i = 0; set->val.str[i]; ++i) { |
| 4697 | if (is_xmlws(set->val.str[i])) { |
| 4698 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4699 | have_spaces = 1; |
| 4700 | break; |
| 4701 | } |
| 4702 | space_before = 1; |
| 4703 | } else { |
| 4704 | space_before = 0; |
| 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | /* yep, there is */ |
| 4709 | if (have_spaces) { |
| 4710 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4711 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4712 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4713 | new_used = 0; |
| 4714 | |
| 4715 | space_before = 0; |
| 4716 | for (i = 0; set->val.str[i]; ++i) { |
| 4717 | if (is_xmlws(set->val.str[i])) { |
| 4718 | if ((i == 0) || space_before) { |
| 4719 | space_before = 1; |
| 4720 | continue; |
| 4721 | } else { |
| 4722 | space_before = 1; |
| 4723 | } |
| 4724 | } else { |
| 4725 | space_before = 0; |
| 4726 | } |
| 4727 | |
| 4728 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4729 | ++new_used; |
| 4730 | } |
| 4731 | |
| 4732 | /* at worst there is one trailing space now */ |
| 4733 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4734 | --new_used; |
| 4735 | } |
| 4736 | |
| 4737 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4738 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4739 | new[new_used] = '\0'; |
| 4740 | |
| 4741 | free(set->val.str); |
| 4742 | set->val.str = new; |
| 4743 | } |
| 4744 | |
| 4745 | return LY_SUCCESS; |
| 4746 | } |
| 4747 | |
| 4748 | /** |
| 4749 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4750 | * with the argument converted to boolean and logically inverted. |
| 4751 | * |
| 4752 | * @param[in] args Array of arguments. |
| 4753 | * @param[in] arg_count Count of elements in @p args. |
| 4754 | * @param[in,out] set Context and result set at the same time. |
| 4755 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4756 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4757 | */ |
| 4758 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4759 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4760 | { |
| 4761 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4762 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4763 | return LY_SUCCESS; |
| 4764 | } |
| 4765 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4766 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4767 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4768 | set_fill_boolean(set, 0); |
| 4769 | } else { |
| 4770 | set_fill_boolean(set, 1); |
| 4771 | } |
| 4772 | |
| 4773 | return LY_SUCCESS; |
| 4774 | } |
| 4775 | |
| 4776 | /** |
| 4777 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4778 | * with the number representation of either the argument or the context. |
| 4779 | * |
| 4780 | * @param[in] args Array of arguments. |
| 4781 | * @param[in] arg_count Count of elements in @p args. |
| 4782 | * @param[in,out] set Context and result set at the same time. |
| 4783 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4784 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4785 | */ |
| 4786 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4787 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4788 | { |
| 4789 | LY_ERR rc; |
| 4790 | |
| 4791 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4792 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4793 | return LY_SUCCESS; |
| 4794 | } |
| 4795 | |
| 4796 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4797 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4798 | LY_CHECK_RET(rc); |
| 4799 | set_fill_set(set, args[0]); |
| 4800 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4801 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4802 | LY_CHECK_RET(rc); |
| 4803 | } |
| 4804 | |
| 4805 | return LY_SUCCESS; |
| 4806 | } |
| 4807 | |
| 4808 | /** |
| 4809 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4810 | * with the context position. |
| 4811 | * |
| 4812 | * @param[in] args Array of arguments. |
| 4813 | * @param[in] arg_count Count of elements in @p args. |
| 4814 | * @param[in,out] set Context and result set at the same time. |
| 4815 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4816 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4817 | */ |
| 4818 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4819 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4820 | { |
| 4821 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4822 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4823 | return LY_SUCCESS; |
| 4824 | } |
| 4825 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4826 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4827 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4828 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4829 | } else if (!set->used) { |
| 4830 | set_fill_number(set, 0); |
| 4831 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4832 | } |
| 4833 | |
| 4834 | set_fill_number(set, set->ctx_pos); |
| 4835 | |
| 4836 | /* UNUSED in 'Release' build type */ |
| 4837 | (void)options; |
| 4838 | return LY_SUCCESS; |
| 4839 | } |
| 4840 | |
| 4841 | /** |
| 4842 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4843 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4844 | * YANG 1.1 RFC section 10.2.1. |
| 4845 | * |
| 4846 | * @param[in] args Array of arguments. |
| 4847 | * @param[in] arg_count Count of elements in @p args. |
| 4848 | * @param[in,out] set Context and result set at the same time. |
| 4849 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4850 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4851 | */ |
| 4852 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4853 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4854 | { |
| 4855 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4856 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4857 | LY_ERR rc = LY_SUCCESS; |
| 4858 | struct ly_err_item *err; |
| 4859 | |
| 4860 | if (options & LYXP_SCNODE_ALL) { |
| 4861 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4862 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4863 | 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] | 4864 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4865 | 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] | 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4870 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4871 | 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] | 4872 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4873 | 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] | 4874 | } |
| 4875 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4876 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4877 | return rc; |
| 4878 | } |
| 4879 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4880 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4881 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4882 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4883 | LY_CHECK_RET(rc); |
| 4884 | |
| 4885 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4886 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4887 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4888 | 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] | 4889 | if (set->cur_node) { |
| 4890 | LOG_LOCBACK(0, 1, 0, 0); |
| 4891 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4892 | if (rc != LY_SUCCESS) { |
| 4893 | LY_ARRAY_FREE(patterns); |
| 4894 | return rc; |
| 4895 | } |
| 4896 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4897 | 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] | 4898 | pcre2_code_free((*pattern)->code); |
| 4899 | free(*pattern); |
| 4900 | LY_ARRAY_FREE(patterns); |
| 4901 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4902 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4903 | ly_err_free(err); |
| 4904 | return rc; |
| 4905 | } |
| 4906 | |
| 4907 | if (rc == LY_EVALID) { |
| 4908 | ly_err_free(err); |
| 4909 | set_fill_boolean(set, 0); |
| 4910 | } else { |
| 4911 | set_fill_boolean(set, 1); |
| 4912 | } |
| 4913 | |
| 4914 | return LY_SUCCESS; |
| 4915 | } |
| 4916 | |
| 4917 | /** |
| 4918 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4919 | * with the rounded first argument. For details refer to |
| 4920 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4921 | * |
| 4922 | * @param[in] args Array of arguments. |
| 4923 | * @param[in] arg_count Count of elements in @p args. |
| 4924 | * @param[in,out] set Context and result set at the same time. |
| 4925 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4926 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4927 | */ |
| 4928 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4929 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4930 | { |
| 4931 | struct lysc_node_leaf *sleaf; |
| 4932 | LY_ERR rc = LY_SUCCESS; |
| 4933 | |
| 4934 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4935 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4936 | 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] | 4937 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4938 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4939 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4940 | sleaf->name); |
| 4941 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4942 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4943 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4944 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4945 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4946 | return rc; |
| 4947 | } |
| 4948 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4949 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4950 | LY_CHECK_RET(rc); |
| 4951 | |
| 4952 | /* cover only the cases where floor can't be used */ |
| 4953 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4954 | set_fill_number(set, -0.0f); |
| 4955 | } else { |
| 4956 | args[0]->val.num += 0.5; |
| 4957 | rc = xpath_floor(args, 1, args[0], options); |
| 4958 | LY_CHECK_RET(rc); |
| 4959 | set_fill_number(set, args[0]->val.num); |
| 4960 | } |
| 4961 | |
| 4962 | return LY_SUCCESS; |
| 4963 | } |
| 4964 | |
| 4965 | /** |
| 4966 | * @brief Execute the XPath starts-with(string, string) function. |
| 4967 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4968 | * the prefix of the first or not. |
| 4969 | * |
| 4970 | * @param[in] args Array of arguments. |
| 4971 | * @param[in] arg_count Count of elements in @p args. |
| 4972 | * @param[in,out] set Context and result set at the same time. |
| 4973 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4974 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4975 | */ |
| 4976 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4977 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4978 | { |
| 4979 | struct lysc_node_leaf *sleaf; |
| 4980 | LY_ERR rc = LY_SUCCESS; |
| 4981 | |
| 4982 | if (options & LYXP_SCNODE_ALL) { |
| 4983 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4984 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4985 | 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] | 4986 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4987 | 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] | 4988 | } |
| 4989 | } |
| 4990 | |
| 4991 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4992 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4993 | 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] | 4994 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4995 | 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] | 4996 | } |
| 4997 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4998 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4999 | return rc; |
| 5000 | } |
| 5001 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5002 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5003 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5004 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5005 | LY_CHECK_RET(rc); |
| 5006 | |
| 5007 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 5008 | set_fill_boolean(set, 0); |
| 5009 | } else { |
| 5010 | set_fill_boolean(set, 1); |
| 5011 | } |
| 5012 | |
| 5013 | return LY_SUCCESS; |
| 5014 | } |
| 5015 | |
| 5016 | /** |
| 5017 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 5018 | * with the string representation of either the argument or the context. |
| 5019 | * |
| 5020 | * @param[in] args Array of arguments. |
| 5021 | * @param[in] arg_count Count of elements in @p args. |
| 5022 | * @param[in,out] set Context and result set at the same time. |
| 5023 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5024 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5025 | */ |
| 5026 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5027 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5028 | { |
| 5029 | LY_ERR rc; |
| 5030 | |
| 5031 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5032 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5033 | return LY_SUCCESS; |
| 5034 | } |
| 5035 | |
| 5036 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5037 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5038 | LY_CHECK_RET(rc); |
| 5039 | set_fill_set(set, args[0]); |
| 5040 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5041 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5042 | LY_CHECK_RET(rc); |
| 5043 | } |
| 5044 | |
| 5045 | return LY_SUCCESS; |
| 5046 | } |
| 5047 | |
| 5048 | /** |
| 5049 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5050 | * with the length of the string in either the argument or the context. |
| 5051 | * |
| 5052 | * @param[in] args Array of arguments. |
| 5053 | * @param[in] arg_count Count of elements in @p args. |
| 5054 | * @param[in,out] set Context and result set at the same time. |
| 5055 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5056 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5057 | */ |
| 5058 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5059 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5060 | { |
| 5061 | struct lysc_node_leaf *sleaf; |
| 5062 | LY_ERR rc = LY_SUCCESS; |
| 5063 | |
| 5064 | if (options & LYXP_SCNODE_ALL) { |
| 5065 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5066 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5067 | 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] | 5068 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5069 | 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] | 5070 | } |
| 5071 | } |
| 5072 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5073 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5074 | 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] | 5075 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5076 | 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] | 5077 | } |
| 5078 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5079 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5080 | return rc; |
| 5081 | } |
| 5082 | |
| 5083 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5084 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5085 | LY_CHECK_RET(rc); |
| 5086 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5087 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5088 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5089 | LY_CHECK_RET(rc); |
| 5090 | set_fill_number(set, strlen(set->val.str)); |
| 5091 | } |
| 5092 | |
| 5093 | return LY_SUCCESS; |
| 5094 | } |
| 5095 | |
| 5096 | /** |
| 5097 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5098 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5099 | * on the second argument index ending on the third argument index, |
| 5100 | * indexed from 1. For exact definition refer to |
| 5101 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5102 | * |
| 5103 | * @param[in] args Array of arguments. |
| 5104 | * @param[in] arg_count Count of elements in @p args. |
| 5105 | * @param[in,out] set Context and result set at the same time. |
| 5106 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5107 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5108 | */ |
| 5109 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5110 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5111 | { |
Michal Vasko | 6db996e | 2022-07-28 10:28:04 +0200 | [diff] [blame] | 5112 | int64_t start; |
| 5113 | int32_t len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5114 | uint16_t str_start, str_len, pos; |
| 5115 | struct lysc_node_leaf *sleaf; |
| 5116 | LY_ERR rc = LY_SUCCESS; |
| 5117 | |
| 5118 | if (options & LYXP_SCNODE_ALL) { |
| 5119 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5120 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5121 | 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] | 5122 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5123 | 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] | 5124 | } |
| 5125 | } |
| 5126 | |
| 5127 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5128 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5129 | 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] | 5130 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5131 | 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] | 5132 | } |
| 5133 | } |
| 5134 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5135 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5136 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5137 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5138 | 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] | 5139 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5140 | 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] | 5141 | } |
| 5142 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5143 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5144 | return rc; |
| 5145 | } |
| 5146 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5147 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5148 | LY_CHECK_RET(rc); |
| 5149 | |
| 5150 | /* start */ |
| 5151 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5152 | return -1; |
| 5153 | } |
| 5154 | if (isfinite(args[1]->val.num)) { |
| 5155 | start = args[1]->val.num - 1; |
| 5156 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5157 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5158 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5159 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | /* len */ |
| 5163 | if (arg_count == 3) { |
| 5164 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5165 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5166 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5167 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5168 | } else if (isfinite(args[2]->val.num)) { |
| 5169 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5170 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5171 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5172 | } |
| 5173 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5174 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | /* find matching character positions */ |
| 5178 | str_start = 0; |
| 5179 | str_len = 0; |
| 5180 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5181 | if (pos < start) { |
| 5182 | ++str_start; |
| 5183 | } else if (pos < start + len) { |
| 5184 | ++str_len; |
| 5185 | } else { |
| 5186 | break; |
| 5187 | } |
| 5188 | } |
| 5189 | |
| 5190 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5191 | return LY_SUCCESS; |
| 5192 | } |
| 5193 | |
| 5194 | /** |
| 5195 | * @brief Execute the XPath substring-after(string, string) function. |
| 5196 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5197 | * of the second argument in the first or an empty string. |
| 5198 | * |
| 5199 | * @param[in] args Array of arguments. |
| 5200 | * @param[in] arg_count Count of elements in @p args. |
| 5201 | * @param[in,out] set Context and result set at the same time. |
| 5202 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5203 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5204 | */ |
| 5205 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5206 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5207 | { |
| 5208 | char *ptr; |
| 5209 | struct lysc_node_leaf *sleaf; |
| 5210 | LY_ERR rc = LY_SUCCESS; |
| 5211 | |
| 5212 | if (options & LYXP_SCNODE_ALL) { |
| 5213 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5214 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5215 | 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] | 5216 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5217 | 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] | 5218 | } |
| 5219 | } |
| 5220 | |
| 5221 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5222 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5223 | 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] | 5224 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5225 | 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] | 5226 | } |
| 5227 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5228 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5229 | return rc; |
| 5230 | } |
| 5231 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5232 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5233 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5234 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5235 | LY_CHECK_RET(rc); |
| 5236 | |
| 5237 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5238 | if (ptr) { |
| 5239 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5240 | } else { |
| 5241 | set_fill_string(set, "", 0); |
| 5242 | } |
| 5243 | |
| 5244 | return LY_SUCCESS; |
| 5245 | } |
| 5246 | |
| 5247 | /** |
| 5248 | * @brief Execute the XPath substring-before(string, string) function. |
| 5249 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5250 | * of the second argument in the first or an empty string. |
| 5251 | * |
| 5252 | * @param[in] args Array of arguments. |
| 5253 | * @param[in] arg_count Count of elements in @p args. |
| 5254 | * @param[in,out] set Context and result set at the same time. |
| 5255 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5256 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5257 | */ |
| 5258 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5259 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5260 | { |
| 5261 | char *ptr; |
| 5262 | struct lysc_node_leaf *sleaf; |
| 5263 | LY_ERR rc = LY_SUCCESS; |
| 5264 | |
| 5265 | if (options & LYXP_SCNODE_ALL) { |
| 5266 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5267 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5268 | 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] | 5269 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5270 | 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] | 5271 | } |
| 5272 | } |
| 5273 | |
| 5274 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5275 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5276 | 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] | 5277 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5278 | 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] | 5279 | } |
| 5280 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5281 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5282 | return rc; |
| 5283 | } |
| 5284 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5285 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5286 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5287 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5288 | LY_CHECK_RET(rc); |
| 5289 | |
| 5290 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5291 | if (ptr) { |
| 5292 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5293 | } else { |
| 5294 | set_fill_string(set, "", 0); |
| 5295 | } |
| 5296 | |
| 5297 | return LY_SUCCESS; |
| 5298 | } |
| 5299 | |
| 5300 | /** |
| 5301 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5302 | * with the sum of all the nodes in the context. |
| 5303 | * |
| 5304 | * @param[in] args Array of arguments. |
| 5305 | * @param[in] arg_count Count of elements in @p args. |
| 5306 | * @param[in,out] set Context and result set at the same time. |
| 5307 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5308 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5309 | */ |
| 5310 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5311 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5312 | { |
| 5313 | long double num; |
| 5314 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5315 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5316 | struct lyxp_set set_item; |
| 5317 | struct lysc_node_leaf *sleaf; |
| 5318 | LY_ERR rc = LY_SUCCESS; |
| 5319 | |
| 5320 | if (options & LYXP_SCNODE_ALL) { |
| 5321 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5322 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5323 | 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] | 5324 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5325 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5326 | 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] | 5327 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5328 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5329 | 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] | 5330 | } |
| 5331 | } |
| 5332 | } |
| 5333 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5334 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5335 | return rc; |
| 5336 | } |
| 5337 | |
| 5338 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5339 | |
| 5340 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5341 | 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] | 5342 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5343 | } else if (!args[0]->used) { |
| 5344 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5345 | } |
| 5346 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5347 | set_init(&set_item, set); |
| 5348 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5349 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5350 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5351 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5352 | |
| 5353 | set_item.used = 1; |
| 5354 | set_item.size = 1; |
| 5355 | |
| 5356 | for (i = 0; i < args[0]->used; ++i) { |
| 5357 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5358 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5359 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5360 | LY_CHECK_RET(rc); |
| 5361 | num = cast_string_to_number(str); |
| 5362 | free(str); |
| 5363 | set->val.num += num; |
| 5364 | } |
| 5365 | |
| 5366 | free(set_item.val.nodes); |
| 5367 | |
| 5368 | return LY_SUCCESS; |
| 5369 | } |
| 5370 | |
| 5371 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5372 | * @brief Execute the XPath translate(string, string, string) function. |
| 5373 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5374 | * from the second argument replaced by those on the corresponding |
| 5375 | * positions in the third argument. |
| 5376 | * |
| 5377 | * @param[in] args Array of arguments. |
| 5378 | * @param[in] arg_count Count of elements in @p args. |
| 5379 | * @param[in,out] set Context and result set at the same time. |
| 5380 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5381 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5382 | */ |
| 5383 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5384 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5385 | { |
| 5386 | uint16_t i, j, new_used; |
| 5387 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5388 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5389 | struct lysc_node_leaf *sleaf; |
| 5390 | LY_ERR rc = LY_SUCCESS; |
| 5391 | |
| 5392 | if (options & LYXP_SCNODE_ALL) { |
| 5393 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5394 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5395 | 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] | 5396 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5397 | 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] | 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5402 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5403 | 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] | 5404 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5405 | 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] | 5406 | } |
| 5407 | } |
| 5408 | |
| 5409 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5410 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5411 | 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] | 5412 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5413 | 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] | 5414 | } |
| 5415 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5416 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5417 | return rc; |
| 5418 | } |
| 5419 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5420 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5421 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5422 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5423 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5424 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5425 | LY_CHECK_RET(rc); |
| 5426 | |
| 5427 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5428 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5429 | new_used = 0; |
| 5430 | |
| 5431 | have_removed = 0; |
| 5432 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5433 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5434 | |
| 5435 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5436 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5437 | /* removing this char */ |
| 5438 | if (j >= strlen(args[2]->val.str)) { |
| 5439 | have_removed = 1; |
| 5440 | found = 1; |
| 5441 | break; |
| 5442 | } |
| 5443 | /* replacing this char */ |
| 5444 | new[new_used] = args[2]->val.str[j]; |
| 5445 | ++new_used; |
| 5446 | found = 1; |
| 5447 | break; |
| 5448 | } |
| 5449 | } |
| 5450 | |
| 5451 | /* copying this char */ |
| 5452 | if (!found) { |
| 5453 | new[new_used] = args[0]->val.str[i]; |
| 5454 | ++new_used; |
| 5455 | } |
| 5456 | } |
| 5457 | |
| 5458 | if (have_removed) { |
| 5459 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5460 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5461 | } |
| 5462 | new[new_used] = '\0'; |
| 5463 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5464 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5465 | set->type = LYXP_SET_STRING; |
| 5466 | set->val.str = new; |
| 5467 | |
| 5468 | return LY_SUCCESS; |
| 5469 | } |
| 5470 | |
| 5471 | /** |
| 5472 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5473 | * with true value. |
| 5474 | * |
| 5475 | * @param[in] args Array of arguments. |
| 5476 | * @param[in] arg_count Count of elements in @p args. |
| 5477 | * @param[in,out] set Context and result set at the same time. |
| 5478 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5479 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5480 | */ |
| 5481 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5482 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5483 | { |
| 5484 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5485 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5486 | return LY_SUCCESS; |
| 5487 | } |
| 5488 | |
| 5489 | set_fill_boolean(set, 1); |
| 5490 | return LY_SUCCESS; |
| 5491 | } |
| 5492 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5493 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5494 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5495 | * with only nodes from the context. |
| 5496 | * |
| 5497 | * @param[in,out] set Context and result set at the same time. |
| 5498 | * @param[in] axis Axis to search on. |
| 5499 | * @param[in] options XPath options. |
| 5500 | * @return LY_ERR |
| 5501 | */ |
| 5502 | static LY_ERR |
| 5503 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5504 | { |
| 5505 | if (options & LYXP_SCNODE_ALL) { |
| 5506 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5507 | } |
| 5508 | |
| 5509 | if (set->type != LYXP_SET_NODE_SET) { |
| 5510 | lyxp_set_free_content(set); |
| 5511 | return LY_SUCCESS; |
| 5512 | } |
| 5513 | |
| 5514 | /* just like moving to a node with no restrictions */ |
| 5515 | return moveto_node(set, NULL, NULL, axis, options); |
| 5516 | } |
| 5517 | |
| 5518 | /** |
| 5519 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5520 | * with the text content of the nodes in the context. |
| 5521 | * |
| 5522 | * @param[in,out] set Context and result set at the same time. |
| 5523 | * @param[in] axis Axis to search on. |
| 5524 | * @param[in] options XPath options. |
| 5525 | * @return LY_ERR |
| 5526 | */ |
| 5527 | static LY_ERR |
| 5528 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5529 | { |
| 5530 | uint32_t i; |
| 5531 | |
| 5532 | if (options & LYXP_SCNODE_ALL) { |
| 5533 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5534 | return LY_SUCCESS; |
| 5535 | } |
| 5536 | |
| 5537 | if (set->type != LYXP_SET_NODE_SET) { |
| 5538 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5539 | return LY_EVALID; |
| 5540 | } |
| 5541 | |
| 5542 | if (axis != LYXP_AXIS_CHILD) { |
| 5543 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5544 | lyxp_set_free_content(set); |
| 5545 | return LY_SUCCESS; |
| 5546 | } |
| 5547 | |
| 5548 | for (i = 0; i < set->used; ++i) { |
| 5549 | switch (set->val.nodes[i].type) { |
| 5550 | case LYXP_NODE_NONE: |
| 5551 | LOGINT_RET(set->ctx); |
| 5552 | case LYXP_NODE_ELEM: |
| 5553 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5554 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5555 | break; |
| 5556 | } |
| 5557 | /* fall through */ |
| 5558 | case LYXP_NODE_ROOT: |
| 5559 | case LYXP_NODE_ROOT_CONFIG: |
| 5560 | case LYXP_NODE_TEXT: |
| 5561 | case LYXP_NODE_META: |
| 5562 | set_remove_node_none(set, i); |
| 5563 | break; |
| 5564 | } |
| 5565 | } |
| 5566 | set_remove_nodes_none(set); |
| 5567 | |
| 5568 | return LY_SUCCESS; |
| 5569 | } |
| 5570 | |
| 5571 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5572 | * @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] | 5573 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5574 | * XPath @p set is expected to be a (sc)node set! |
| 5575 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5576 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5577 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5578 | * @param[in] set Set with general XPath context. |
| 5579 | * @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] | 5580 | * @param[out] moveto_mod Expected module of a matching node. |
| 5581 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5582 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5583 | static LY_ERR |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 5584 | 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] | 5585 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5586 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5587 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5588 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5589 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5590 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5591 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5592 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5593 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5594 | /* specific module */ |
| 5595 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5596 | 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] | 5597 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5598 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5599 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5600 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5601 | return LY_EVALID; |
| 5602 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5603 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5604 | *qname += pref_len + 1; |
| 5605 | *qname_len -= pref_len + 1; |
| 5606 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5607 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5608 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5609 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5610 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5611 | case LY_VALUE_SCHEMA: |
| 5612 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5613 | /* current module */ |
| 5614 | mod = set->cur_mod; |
| 5615 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5616 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5617 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5618 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5619 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5620 | /* inherit parent (context node) module */ |
| 5621 | if (ctx_scnode) { |
| 5622 | mod = ctx_scnode->module; |
| 5623 | } else { |
| 5624 | mod = NULL; |
| 5625 | } |
| 5626 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5627 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5628 | /* all nodes need to be prefixed */ |
| 5629 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5630 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5631 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5632 | } |
| 5633 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5634 | *moveto_mod = mod; |
| 5635 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5636 | } |
| 5637 | |
| 5638 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5639 | * @brief Move context @p set to the root. Handles absolute path. |
| 5640 | * Result is LYXP_SET_NODE_SET. |
| 5641 | * |
| 5642 | * @param[in,out] set Set to use. |
| 5643 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5644 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5645 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5646 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5647 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5648 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5649 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5650 | |
| 5651 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5652 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 5653 | 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] | 5654 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5655 | set->type = LYXP_SET_NODE_SET; |
| 5656 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5657 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 5658 | set->non_child_axis = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5659 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5660 | |
| 5661 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | /** |
| 5665 | * @brief Check @p node as a part of NameTest processing. |
| 5666 | * |
| 5667 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5668 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5669 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5670 | * @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] | 5671 | * @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] | 5672 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5673 | * @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] | 5674 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5675 | */ |
| 5676 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5677 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5678 | 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] | 5679 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5680 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5681 | assert(node_type == set->root_type); |
| 5682 | |
| 5683 | if (node_name || moveto_mod) { |
| 5684 | /* root will not match a specific node */ |
| 5685 | return LY_ENOT; |
| 5686 | } |
| 5687 | return LY_SUCCESS; |
| 5688 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5689 | /* other types will not match */ |
| 5690 | return LY_ENOT; |
| 5691 | } |
| 5692 | |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5693 | if (!node->schema) { |
| 5694 | /* opaque node never matches */ |
| 5695 | return LY_ENOT; |
| 5696 | } |
| 5697 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5698 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5699 | if (moveto_mod) { |
| 5700 | if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) { |
| 5701 | return LY_ENOT; |
| 5702 | } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) { |
| 5703 | return LY_ENOT; |
| 5704 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5705 | } |
| 5706 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5707 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5708 | 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] | 5709 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5710 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5711 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5712 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5713 | } |
| 5714 | |
| 5715 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5716 | if (node_name) { |
| 5717 | if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) { |
| 5718 | return LY_ENOT; |
| 5719 | } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) { |
| 5720 | return LY_ENOT; |
| 5721 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5722 | } |
| 5723 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5724 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5725 | 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] | 5726 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5727 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5728 | |
| 5729 | /* match */ |
| 5730 | return LY_SUCCESS; |
| 5731 | } |
| 5732 | |
| 5733 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5734 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5735 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5736 | * @param[in] iter Last returned node. |
| 5737 | * @param[in] stop Node to stop the search on and not return. |
| 5738 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5739 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5740 | static const struct lyd_node * |
| 5741 | 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] | 5742 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5743 | const struct lyd_node *next = NULL; |
| 5744 | |
| 5745 | /* 1) child */ |
| 5746 | next = lyd_child(iter); |
| 5747 | if (!next) { |
| 5748 | if (iter == stop) { |
| 5749 | /* reached stop, no more descendants */ |
| 5750 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5751 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5752 | /* 2) child next sibling */ |
| 5753 | next = iter->next; |
| 5754 | } |
| 5755 | while (!next) { |
| 5756 | iter = lyd_parent(iter); |
| 5757 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5758 | return NULL; |
| 5759 | } |
| 5760 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5761 | } |
| 5762 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5763 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5764 | } |
| 5765 | |
| 5766 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5767 | * @brief Get the next node in a backward DFS. |
| 5768 | * |
| 5769 | * @param[in] iter Last returned node. |
| 5770 | * @param[in] stop Node to stop the search on and not return. |
| 5771 | * @return Next node, NULL if there are no more. |
| 5772 | */ |
| 5773 | static const struct lyd_node * |
| 5774 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5775 | { |
| 5776 | const struct lyd_node *next = NULL; |
| 5777 | |
| 5778 | /* 1) previous sibling innermost last child */ |
| 5779 | next = iter->prev->next ? iter->prev : NULL; |
| 5780 | while (next && lyd_child(next)) { |
| 5781 | next = lyd_child(next); |
| 5782 | next = next->prev; |
| 5783 | } |
| 5784 | |
| 5785 | if (!next) { |
| 5786 | /* 2) parent */ |
| 5787 | iter = lyd_parent(iter); |
| 5788 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5789 | return NULL; |
| 5790 | } |
| 5791 | next = iter; |
| 5792 | } |
| 5793 | |
| 5794 | return next; |
| 5795 | } |
| 5796 | |
| 5797 | /** |
| 5798 | * @brief Get the first node on an axis for a context node. |
| 5799 | * |
| 5800 | * @param[in,out] iter NULL, updated to the next node. |
| 5801 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5802 | * @param[in] node Context node. |
| 5803 | * @param[in] node_type Type of @p node. |
| 5804 | * @param[in] axis Axis to use. |
| 5805 | * @param[in] set XPath set with the general context. |
| 5806 | * @return LY_SUCCESS on success. |
| 5807 | * @return LY_ENOTFOUND if no next node found. |
| 5808 | */ |
| 5809 | static LY_ERR |
| 5810 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5811 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5812 | { |
| 5813 | const struct lyd_node *next = NULL; |
| 5814 | enum lyxp_node_type next_type = 0; |
| 5815 | |
| 5816 | assert(!*iter); |
| 5817 | assert(!*iter_type); |
| 5818 | |
| 5819 | switch (axis) { |
| 5820 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5821 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5822 | case LYXP_AXIS_SELF: |
| 5823 | /* return the context node */ |
| 5824 | next = node; |
| 5825 | next_type = node_type; |
| 5826 | break; |
| 5827 | |
| 5828 | case LYXP_AXIS_ANCESTOR: |
| 5829 | case LYXP_AXIS_PARENT: |
| 5830 | if (node_type == LYXP_NODE_ELEM) { |
| 5831 | next = lyd_parent(node); |
| 5832 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5833 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5834 | next = node; |
| 5835 | next_type = LYXP_NODE_ELEM; |
| 5836 | } else if (node_type == LYXP_NODE_META) { |
| 5837 | next = ((struct lyd_meta *)node)->parent; |
| 5838 | next_type = LYXP_NODE_ELEM; |
| 5839 | } /* else root does not have a parent */ |
| 5840 | break; |
| 5841 | |
| 5842 | case LYXP_AXIS_CHILD: |
| 5843 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5844 | assert(!node); |
| 5845 | |
| 5846 | /* search in all the trees */ |
| 5847 | next = set->tree; |
| 5848 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5849 | } else { |
| 5850 | /* search in children */ |
| 5851 | next = lyd_child(node); |
| 5852 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5853 | } |
| 5854 | break; |
| 5855 | |
| 5856 | case LYXP_AXIS_DESCENDANT: |
| 5857 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5858 | /* top-level nodes */ |
| 5859 | next = set->tree; |
| 5860 | next_type = LYXP_NODE_ELEM; |
| 5861 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5862 | /* start from the context node */ |
| 5863 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5864 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5865 | } /* else no children */ |
| 5866 | break; |
| 5867 | |
| 5868 | case LYXP_AXIS_FOLLOWING: |
| 5869 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5870 | if (node_type == LYXP_NODE_ELEM) { |
| 5871 | /* first next sibling */ |
| 5872 | next = node->next; |
| 5873 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5874 | } /* else no sibling */ |
| 5875 | break; |
| 5876 | |
| 5877 | case LYXP_AXIS_PRECEDING: |
| 5878 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5879 | /* skip ancestors */ |
| 5880 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5881 | assert(next); |
| 5882 | next_type = LYXP_NODE_ELEM; |
| 5883 | } /* else no sibling */ |
| 5884 | break; |
| 5885 | |
| 5886 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5887 | if (node_type == LYXP_NODE_ELEM) { |
| 5888 | /* first previous sibling */ |
| 5889 | next = node->prev->next ? node->prev : NULL; |
| 5890 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5891 | } /* else no sibling */ |
| 5892 | break; |
| 5893 | |
| 5894 | case LYXP_AXIS_ATTRIBUTE: |
| 5895 | /* handled specially */ |
| 5896 | assert(0); |
| 5897 | LOGINT(set->ctx); |
| 5898 | break; |
| 5899 | } |
| 5900 | |
| 5901 | *iter = next; |
| 5902 | *iter_type = next_type; |
| 5903 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5904 | } |
| 5905 | |
| 5906 | /** |
| 5907 | * @brief Iterate over all nodes on an axis for a context node. |
| 5908 | * |
| 5909 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5910 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5911 | * @param[in] node Context node. |
| 5912 | * @param[in] node_type Type of @p node. |
| 5913 | * @param[in] axis Axis to use. |
| 5914 | * @param[in] set XPath set with the general context. |
| 5915 | * @return LY_SUCCESS on success. |
| 5916 | * @return LY_ENOTFOUND if no next node found. |
| 5917 | */ |
| 5918 | static LY_ERR |
| 5919 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5920 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5921 | { |
| 5922 | const struct lyd_node *next = NULL; |
| 5923 | enum lyxp_node_type next_type = 0; |
| 5924 | |
| 5925 | if (!*iter_type) { |
| 5926 | /* first returned node */ |
| 5927 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5928 | } |
| 5929 | |
| 5930 | switch (axis) { |
| 5931 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5932 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5933 | /* fake first ancestor, we returned self before */ |
| 5934 | *iter = NULL; |
| 5935 | *iter_type = 0; |
| 5936 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 5937 | } /* else continue ancestor */ |
| 5938 | |
| 5939 | /* fallthrough */ |
| 5940 | case LYXP_AXIS_ANCESTOR: |
| 5941 | if (*iter_type == LYXP_NODE_ELEM) { |
| 5942 | /* iter parent */ |
| 5943 | next = lyd_parent(*iter); |
| 5944 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5945 | } /* else root, no ancestors */ |
| 5946 | break; |
| 5947 | |
| 5948 | case LYXP_AXIS_CHILD: |
| 5949 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5950 | |
| 5951 | /* next sibling (child) */ |
| 5952 | next = (*iter)->next; |
| 5953 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5954 | break; |
| 5955 | |
| 5956 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5957 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5958 | /* fake first descendant, we returned self before */ |
| 5959 | *iter = NULL; |
| 5960 | *iter_type = 0; |
| 5961 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 5962 | } /* else continue descendant */ |
| 5963 | |
| 5964 | /* fallthrough */ |
| 5965 | case LYXP_AXIS_DESCENDANT: |
| 5966 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5967 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 5968 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5969 | break; |
| 5970 | |
| 5971 | case LYXP_AXIS_FOLLOWING: |
| 5972 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5973 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 5974 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5975 | break; |
| 5976 | |
| 5977 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5978 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5979 | |
| 5980 | /* next sibling */ |
| 5981 | next = (*iter)->next; |
| 5982 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5983 | break; |
| 5984 | |
| 5985 | case LYXP_AXIS_PARENT: |
| 5986 | case LYXP_AXIS_SELF: |
| 5987 | /* parent/self was returned before */ |
| 5988 | break; |
| 5989 | |
| 5990 | case LYXP_AXIS_PRECEDING: |
| 5991 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5992 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 5993 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5994 | break; |
| 5995 | |
| 5996 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5997 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5998 | |
| 5999 | /* previous sibling */ |
| 6000 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 6001 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6002 | break; |
| 6003 | |
| 6004 | case LYXP_AXIS_ATTRIBUTE: |
| 6005 | /* handled specially */ |
| 6006 | assert(0); |
| 6007 | LOGINT(set->ctx); |
| 6008 | break; |
| 6009 | } |
| 6010 | |
| 6011 | *iter = next; |
| 6012 | *iter_type = next_type; |
| 6013 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6014 | } |
| 6015 | |
| 6016 | /** |
| 6017 | * @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] | 6018 | * |
| 6019 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6020 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6021 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6022 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6023 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6024 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6025 | */ |
| 6026 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6027 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6028 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6029 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6030 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6031 | const struct lyd_node *iter; |
| 6032 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6033 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6034 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6035 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6036 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6037 | return LY_SUCCESS; |
| 6038 | } |
| 6039 | |
| 6040 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6041 | 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] | 6042 | return LY_EVALID; |
| 6043 | } |
| 6044 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6045 | /* init result set */ |
| 6046 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6047 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6048 | for (i = 0; i < set->used; ++i) { |
| 6049 | /* iterate over all the nodes on the axis of the node */ |
| 6050 | iter = NULL; |
| 6051 | iter_type = 0; |
| 6052 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6053 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6054 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6055 | rc = r; |
| 6056 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6057 | } else if (r) { |
| 6058 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6059 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6060 | |
| 6061 | /* check for duplicates if they are possible */ |
| 6062 | switch (axis) { |
| 6063 | case LYXP_AXIS_ANCESTOR: |
| 6064 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6065 | case LYXP_AXIS_DESCENDANT: |
| 6066 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6067 | case LYXP_AXIS_FOLLOWING: |
| 6068 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6069 | case LYXP_AXIS_PARENT: |
| 6070 | case LYXP_AXIS_PRECEDING: |
| 6071 | case LYXP_AXIS_PRECEDING_SIBLING: |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6072 | result.non_child_axis = 1; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6073 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6074 | continue; |
| 6075 | } |
| 6076 | break; |
| 6077 | case LYXP_AXIS_CHILD: |
| 6078 | case LYXP_AXIS_SELF: |
| 6079 | break; |
| 6080 | case LYXP_AXIS_ATTRIBUTE: |
| 6081 | /* handled specially */ |
| 6082 | assert(0); |
| 6083 | LOGINT(set->ctx); |
| 6084 | break; |
| 6085 | } |
| 6086 | |
| 6087 | /* matching node */ |
| 6088 | set_insert_node(&result, iter, 0, iter_type, result.used); |
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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6091 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6092 | /* move result to the set */ |
| 6093 | lyxp_set_free_content(set); |
| 6094 | *set = result; |
| 6095 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6096 | |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6097 | /* sort the final set if the document order could have been broken */ |
| 6098 | if (set->non_child_axis) { |
| 6099 | set_sort(set); |
| 6100 | } else { |
| 6101 | assert(!set_sort(set)); |
| 6102 | } |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6103 | |
| 6104 | cleanup: |
| 6105 | lyxp_set_free_content(&result); |
| 6106 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6107 | } |
| 6108 | |
| 6109 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6110 | * @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] | 6111 | * |
| 6112 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6113 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6114 | * @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] | 6115 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6116 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6117 | */ |
| 6118 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6119 | 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] | 6120 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6121 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6122 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6123 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6124 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6125 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6126 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6127 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6128 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6129 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6130 | /* init result set */ |
| 6131 | set_init(&result, set); |
| 6132 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6133 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6134 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6135 | } |
| 6136 | |
| 6137 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6138 | 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] | 6139 | ret = LY_EVALID; |
| 6140 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | /* context check for all the nodes since we have the schema node */ |
| 6144 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6145 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6146 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6147 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6148 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6149 | lyxp_set_free_content(set); |
| 6150 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6151 | } |
| 6152 | |
| 6153 | /* create specific data instance if needed */ |
| 6154 | if (scnode->nodetype == LYS_LIST) { |
| 6155 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 6156 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6157 | 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] | 6158 | } |
| 6159 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6160 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6161 | siblings = NULL; |
| 6162 | |
| 6163 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6164 | assert(!set->val.nodes[i].node); |
| 6165 | |
| 6166 | /* search in all the trees */ |
| 6167 | siblings = set->tree; |
| 6168 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6169 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6170 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6171 | } |
| 6172 | |
| 6173 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6174 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6175 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6176 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6177 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6178 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6179 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6180 | |
| 6181 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6182 | 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] | 6183 | ret = LY_EINCOMPLETE; |
| 6184 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6185 | } |
| 6186 | |
| 6187 | if (sub) { |
| 6188 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6189 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6190 | } |
| 6191 | } |
| 6192 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6193 | /* move result to the set */ |
| 6194 | lyxp_set_free_content(set); |
| 6195 | *set = result; |
| 6196 | result.type = LYXP_SET_NUMBER; |
| 6197 | assert(!set_sort(set)); |
| 6198 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6199 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6200 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6201 | lyd_free_tree(inst); |
| 6202 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6206 | * @brief Check @p node as a part of schema NameTest processing. |
| 6207 | * |
| 6208 | * @param[in] node Schema node to check. |
| 6209 | * @param[in] ctx_scnode Context node. |
| 6210 | * @param[in] set Set to read general context from. |
| 6211 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6212 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6213 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6214 | */ |
| 6215 | static LY_ERR |
| 6216 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6217 | const char *node_name, const struct lys_module *moveto_mod) |
| 6218 | { |
| 6219 | if (!moveto_mod && node_name) { |
| 6220 | switch (set->format) { |
| 6221 | case LY_VALUE_SCHEMA: |
| 6222 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6223 | /* use current module */ |
| 6224 | moveto_mod = set->cur_mod; |
| 6225 | break; |
| 6226 | case LY_VALUE_JSON: |
| 6227 | case LY_VALUE_LYB: |
| 6228 | case LY_VALUE_STR_NS: |
| 6229 | /* inherit module of the context node, if any */ |
| 6230 | if (ctx_scnode) { |
| 6231 | moveto_mod = ctx_scnode->module; |
| 6232 | } |
| 6233 | break; |
| 6234 | case LY_VALUE_CANON: |
| 6235 | case LY_VALUE_XML: |
| 6236 | /* not defined */ |
| 6237 | LOGINT(set->ctx); |
| 6238 | return LY_EINVAL; |
| 6239 | } |
| 6240 | } |
| 6241 | |
| 6242 | if (!node) { |
| 6243 | /* root will not match a specific node */ |
| 6244 | if (node_name || moveto_mod) { |
| 6245 | return LY_ENOT; |
| 6246 | } |
| 6247 | return LY_SUCCESS; |
| 6248 | } |
| 6249 | |
| 6250 | /* module check */ |
| 6251 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6252 | return LY_ENOT; |
| 6253 | } |
| 6254 | |
| 6255 | /* context check */ |
| 6256 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6257 | return LY_EINVAL; |
| 6258 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6259 | return LY_EINVAL; |
| 6260 | } |
| 6261 | |
| 6262 | /* name check */ |
| 6263 | if (node_name && (node->name != node_name)) { |
| 6264 | return LY_ENOT; |
| 6265 | } |
| 6266 | |
| 6267 | /* match */ |
| 6268 | return LY_SUCCESS; |
| 6269 | } |
| 6270 | |
| 6271 | /** |
| 6272 | * @brief Get the next node in a forward schema node DFS. |
| 6273 | * |
| 6274 | * @param[in] iter Last returned node. |
| 6275 | * @param[in] stop Node to stop the search on and not return. |
| 6276 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6277 | * @return Next node, NULL if there are no more. |
| 6278 | */ |
| 6279 | static const struct lysc_node * |
| 6280 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6281 | { |
| 6282 | const struct lysc_node *next = NULL; |
| 6283 | |
| 6284 | next = lysc_node_child(iter); |
| 6285 | if (!next) { |
| 6286 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6287 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6288 | /* we are done, no next element to process */ |
| 6289 | return NULL; |
| 6290 | } |
| 6291 | |
| 6292 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6293 | } |
| 6294 | while (!next && iter) { |
| 6295 | /* parent is already processed, go to its sibling */ |
| 6296 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6297 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6298 | /* we are done, no next element to process */ |
| 6299 | return NULL; |
| 6300 | } |
| 6301 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6302 | } |
| 6303 | |
| 6304 | return next; |
| 6305 | } |
| 6306 | |
| 6307 | /** |
| 6308 | * @brief Consider schema node based on its in_ctx enum value. |
| 6309 | * |
| 6310 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6311 | * @param[in] axis Axis to use. |
| 6312 | * @return LY_SUCCESS on success. |
| 6313 | * @return LY_ENOT if the node should not be returned. |
| 6314 | */ |
| 6315 | static LY_ERR |
| 6316 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6317 | { |
| 6318 | switch (axis) { |
| 6319 | case LYXP_AXIS_SELF: |
| 6320 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6321 | /* additionally put the start node into context */ |
| 6322 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6323 | return LY_SUCCESS; |
| 6324 | } |
| 6325 | break; |
| 6326 | case LYXP_AXIS_PARENT: |
| 6327 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6328 | case LYXP_AXIS_ANCESTOR: |
| 6329 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6330 | case LYXP_AXIS_DESCENDANT: |
| 6331 | case LYXP_AXIS_FOLLOWING: |
| 6332 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6333 | case LYXP_AXIS_PRECEDING: |
| 6334 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6335 | case LYXP_AXIS_CHILD: |
| 6336 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6337 | /* remember that context node was used */ |
| 6338 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6339 | return LY_SUCCESS; |
| 6340 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6341 | /* traversed */ |
| 6342 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6343 | return LY_SUCCESS; |
| 6344 | } |
| 6345 | break; |
| 6346 | case LYXP_AXIS_ATTRIBUTE: |
| 6347 | /* unreachable */ |
| 6348 | assert(0); |
| 6349 | LOGINT(NULL); |
| 6350 | break; |
| 6351 | } |
| 6352 | |
| 6353 | return LY_ENOT; |
| 6354 | } |
| 6355 | |
| 6356 | /** |
| 6357 | * @brief Get previous sibling for a schema node. |
| 6358 | * |
| 6359 | * @param[in] scnode Schema node. |
| 6360 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6361 | * @return Previous sibling, NULL if none. |
| 6362 | */ |
| 6363 | static const struct lysc_node * |
| 6364 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6365 | { |
| 6366 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6367 | |
| 6368 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6369 | if (next == scnode) { |
| 6370 | break; |
| 6371 | } |
| 6372 | |
| 6373 | prev = next; |
| 6374 | } |
| 6375 | |
| 6376 | return prev; |
| 6377 | } |
| 6378 | |
| 6379 | /** |
| 6380 | * @brief Get the first schema node on an axis for a context node. |
| 6381 | * |
| 6382 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6383 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6384 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6385 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6386 | * @param[in] scnode Context node. |
| 6387 | * @param[in] node_type Type of @p scnode. |
| 6388 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6389 | * @param[in] axis Axis to use. |
| 6390 | * @param[in] set XPath set with the general context. |
| 6391 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6392 | * @return LY_SUCCESS on success. |
| 6393 | * @return LY_ENOTFOUND if no next node found. |
| 6394 | */ |
| 6395 | static LY_ERR |
| 6396 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6397 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6398 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6399 | { |
| 6400 | const struct lysc_node *next = NULL; |
| 6401 | enum lyxp_node_type next_type = 0; |
| 6402 | |
| 6403 | assert(!*iter); |
| 6404 | assert(!*iter_type); |
| 6405 | |
| 6406 | *iter_mod = NULL; |
| 6407 | *iter_mod_idx = 0; |
| 6408 | |
| 6409 | switch (axis) { |
| 6410 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6411 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6412 | case LYXP_AXIS_SELF: |
| 6413 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6414 | /* just return the node */ |
| 6415 | next = scnode; |
| 6416 | next_type = node_type; |
| 6417 | } |
| 6418 | break; |
| 6419 | |
| 6420 | case LYXP_AXIS_ANCESTOR: |
| 6421 | case LYXP_AXIS_PARENT: |
| 6422 | if (node_type == LYXP_NODE_ELEM) { |
| 6423 | next = lysc_data_parent(scnode); |
| 6424 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6425 | } /* else no parent */ |
| 6426 | break; |
| 6427 | |
| 6428 | case LYXP_AXIS_DESCENDANT: |
| 6429 | case LYXP_AXIS_CHILD: |
| 6430 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6431 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6432 | * it can be in a top-level augment */ |
| 6433 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6434 | /* module may not be implemented or not compiled yet */ |
| 6435 | if (!(*iter_mod)->compiled) { |
| 6436 | continue; |
| 6437 | } |
| 6438 | |
| 6439 | /* get next node */ |
| 6440 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6441 | next_type = LYXP_NODE_ELEM; |
| 6442 | break; |
| 6443 | } |
| 6444 | } |
| 6445 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6446 | /* get next node */ |
| 6447 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6448 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6449 | } |
| 6450 | break; |
| 6451 | |
| 6452 | case LYXP_AXIS_FOLLOWING: |
| 6453 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6454 | if (node_type == LYXP_NODE_ELEM) { |
| 6455 | /* first next sibling */ |
| 6456 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6457 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6458 | } /* else no sibling */ |
| 6459 | break; |
| 6460 | |
| 6461 | case LYXP_AXIS_PRECEDING: |
| 6462 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6463 | if (node_type == LYXP_NODE_ELEM) { |
| 6464 | /* first parent sibling */ |
| 6465 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6466 | if (next == scnode) { |
| 6467 | /* no preceding sibling */ |
| 6468 | next = NULL; |
| 6469 | } |
| 6470 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6471 | } /* else no sibling */ |
| 6472 | break; |
| 6473 | |
| 6474 | case LYXP_AXIS_ATTRIBUTE: |
| 6475 | /* unreachable */ |
| 6476 | assert(0); |
| 6477 | LOGINT(set->ctx); |
| 6478 | break; |
| 6479 | } |
| 6480 | |
| 6481 | *iter = next; |
| 6482 | *iter_type = next_type; |
| 6483 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6484 | } |
| 6485 | |
| 6486 | /** |
| 6487 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6488 | * |
| 6489 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6490 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6491 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6492 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6493 | * @param[in] scnode Context node. |
| 6494 | * @param[in] node_type Type of @p scnode. |
| 6495 | * @param[in] axis Axis to use. |
| 6496 | * @param[in] set XPath set with the general context. |
| 6497 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6498 | * @return LY_SUCCESS on success. |
| 6499 | * @return LY_ENOTFOUND if no next node found. |
| 6500 | */ |
| 6501 | static LY_ERR |
| 6502 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6503 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6504 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6505 | { |
| 6506 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6507 | enum lyxp_node_type next_type = 0; |
| 6508 | |
| 6509 | if (!*iter_type) { |
| 6510 | /* first returned node */ |
| 6511 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6512 | getnext_opts); |
| 6513 | } |
| 6514 | |
| 6515 | switch (axis) { |
| 6516 | case LYXP_AXIS_PARENT: |
| 6517 | case LYXP_AXIS_SELF: |
| 6518 | /* parent/self was returned before */ |
| 6519 | break; |
| 6520 | |
| 6521 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6522 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6523 | /* fake first ancestor, we returned self before */ |
| 6524 | *iter = NULL; |
| 6525 | *iter_type = 0; |
| 6526 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6527 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
| 6528 | } /* else continue ancestor */ |
| 6529 | |
| 6530 | /* fallthrough */ |
| 6531 | case LYXP_AXIS_ANCESTOR: |
| 6532 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6533 | next = lysc_data_parent(*iter); |
| 6534 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6535 | } /* else no ancestor */ |
| 6536 | break; |
| 6537 | |
| 6538 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6539 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6540 | /* fake first descendant, we returned self before */ |
| 6541 | *iter = NULL; |
| 6542 | *iter_type = 0; |
| 6543 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6544 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6545 | } /* else DFS until context node */ |
| 6546 | dfs_stop = scnode; |
| 6547 | |
| 6548 | /* fallthrough */ |
| 6549 | case LYXP_AXIS_DESCENDANT: |
| 6550 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6551 | /* DFS until the context node */ |
| 6552 | dfs_stop = scnode; |
| 6553 | } |
| 6554 | |
| 6555 | /* fallthrough */ |
| 6556 | case LYXP_AXIS_PRECEDING: |
| 6557 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6558 | /* DFS until the previous sibling */ |
| 6559 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6560 | assert(dfs_stop); |
| 6561 | |
| 6562 | if (*iter == dfs_stop) { |
| 6563 | /* we are done */ |
| 6564 | break; |
| 6565 | } |
| 6566 | } |
| 6567 | |
| 6568 | /* fallthrough */ |
| 6569 | case LYXP_AXIS_FOLLOWING: |
| 6570 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6571 | /* DFS through the whole module */ |
| 6572 | dfs_stop = NULL; |
| 6573 | } |
| 6574 | |
| 6575 | /* nested nodes */ |
| 6576 | assert(*iter); |
| 6577 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6578 | if (next) { |
| 6579 | next_type = LYXP_NODE_ELEM; |
| 6580 | break; |
| 6581 | } /* else get next top-level node just like a child */ |
| 6582 | |
| 6583 | /* fallthrough */ |
| 6584 | case LYXP_AXIS_CHILD: |
| 6585 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6586 | if (!*iter_mod) { |
| 6587 | /* nodes from a single module */ |
| 6588 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6589 | next_type = LYXP_NODE_ELEM; |
| 6590 | break; |
| 6591 | } |
| 6592 | |
| 6593 | assert(scnode); |
| 6594 | if (!lysc_data_parent(scnode)) { |
| 6595 | /* iterating over top-level nodes, find next */ |
| 6596 | while (lysc_data_parent(*iter)) { |
| 6597 | *iter = lysc_data_parent(*iter); |
| 6598 | } |
| 6599 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6600 | next_type = LYXP_NODE_ELEM; |
| 6601 | break; |
| 6602 | } |
| 6603 | } |
| 6604 | } |
| 6605 | |
| 6606 | while (*iter_mod) { |
| 6607 | /* module top-level nodes */ |
| 6608 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6609 | next_type = LYXP_NODE_ELEM; |
| 6610 | break; |
| 6611 | } |
| 6612 | |
| 6613 | /* get next module */ |
| 6614 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6615 | /* module may not be implemented or not compiled yet */ |
| 6616 | if ((*iter_mod)->compiled) { |
| 6617 | break; |
| 6618 | } |
| 6619 | } |
| 6620 | |
| 6621 | /* new module, start over */ |
| 6622 | *iter = NULL; |
| 6623 | } |
| 6624 | break; |
| 6625 | |
| 6626 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6627 | assert(*iter); |
| 6628 | |
| 6629 | /* next parent sibling until scnode */ |
| 6630 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6631 | if (next == scnode) { |
| 6632 | /* no previous sibling */ |
| 6633 | next = NULL; |
| 6634 | } |
| 6635 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6636 | break; |
| 6637 | |
| 6638 | case LYXP_AXIS_ATTRIBUTE: |
| 6639 | /* unreachable */ |
| 6640 | assert(0); |
| 6641 | LOGINT(set->ctx); |
| 6642 | break; |
| 6643 | } |
| 6644 | |
| 6645 | *iter = next; |
| 6646 | *iter_type = next_type; |
| 6647 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6648 | } |
| 6649 | |
| 6650 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6651 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6652 | * |
| 6653 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6654 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6655 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6656 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6657 | * @param[in] options XPath options. |
| 6658 | * @return LY_ERR |
| 6659 | */ |
| 6660 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6661 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6662 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6663 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6664 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6665 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6666 | const struct lys_module *mod; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6667 | const struct lysc_node *iter; |
| 6668 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6669 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6670 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6671 | return LY_SUCCESS; |
| 6672 | } |
| 6673 | |
| 6674 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6675 | 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] | 6676 | return LY_EVALID; |
| 6677 | } |
| 6678 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6679 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6680 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6681 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6682 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6683 | } |
| 6684 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6685 | orig_used = set->used; |
| 6686 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6687 | /* update in_ctx first */ |
| 6688 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6689 | /* not usable, skip */ |
| 6690 | continue; |
| 6691 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6692 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6693 | iter = NULL; |
| 6694 | iter_type = 0; |
| 6695 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6696 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6697 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6698 | continue; |
| 6699 | } |
| 6700 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6701 | /* insert */ |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 6702 | 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] | 6703 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6704 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6705 | if ((idx < orig_used) && (idx > i)) { |
| 6706 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6707 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6708 | } |
| 6709 | } |
| 6710 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6711 | |
| 6712 | /* correct temporary in_ctx values */ |
| 6713 | if (temp_ctx) { |
| 6714 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6715 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6716 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6717 | } |
| 6718 | } |
| 6719 | } |
| 6720 | |
| 6721 | return LY_SUCCESS; |
| 6722 | } |
| 6723 | |
| 6724 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6725 | * @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] | 6726 | * Context position aware. |
| 6727 | * |
| 6728 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6729 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6730 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6731 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6732 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6733 | */ |
| 6734 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6735 | 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] | 6736 | { |
| 6737 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6738 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6739 | struct lyxp_set ret_set; |
| 6740 | LY_ERR rc; |
| 6741 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6742 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6743 | return LY_SUCCESS; |
| 6744 | } |
| 6745 | |
| 6746 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6747 | 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] | 6748 | return LY_EVALID; |
| 6749 | } |
| 6750 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6751 | /* 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] | 6752 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6753 | LY_CHECK_RET(rc); |
| 6754 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6755 | /* 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] | 6756 | set_init(&ret_set, set); |
| 6757 | for (i = 0; i < set->used; ++i) { |
| 6758 | |
| 6759 | /* TREE DFS */ |
| 6760 | start = set->val.nodes[i].node; |
| 6761 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6762 | 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] | 6763 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6764 | /* add matching node into result set */ |
| 6765 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6766 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6767 | /* the node is a duplicate, we'll process it later in the set */ |
| 6768 | goto skip_children; |
| 6769 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6770 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6771 | return rc; |
| 6772 | } else if (rc == LY_EINVAL) { |
| 6773 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | /* TREE DFS NEXT ELEM */ |
| 6777 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6778 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6779 | if (!next) { |
| 6780 | skip_children: |
| 6781 | /* no children, so try siblings, but only if it's not the start, |
| 6782 | * that is considered to be the root and it's siblings are not traversed */ |
| 6783 | if (elem != start) { |
| 6784 | next = elem->next; |
| 6785 | } else { |
| 6786 | break; |
| 6787 | } |
| 6788 | } |
| 6789 | while (!next) { |
| 6790 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6791 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6792 | /* we are done, no next element to process */ |
| 6793 | break; |
| 6794 | } |
| 6795 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6796 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6797 | next = elem->next; |
| 6798 | } |
| 6799 | } |
| 6800 | } |
| 6801 | |
| 6802 | /* make the temporary set the current one */ |
| 6803 | ret_set.ctx_pos = set->ctx_pos; |
| 6804 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6805 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6806 | memcpy(set, &ret_set, sizeof *set); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6807 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6808 | |
| 6809 | return LY_SUCCESS; |
| 6810 | } |
| 6811 | |
| 6812 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6813 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6814 | * Result is LYXP_SET_NODE_SET. |
| 6815 | * |
| 6816 | * @param[in] set Set to use. |
| 6817 | * @param[in] start Start node whose subtree to add. |
| 6818 | * @param[in] start_idx Index of @p start in @p set. |
| 6819 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6820 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6821 | * @param[in] options XPath options. |
| 6822 | * @return LY_ERR value. |
| 6823 | */ |
| 6824 | static LY_ERR |
| 6825 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6826 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6827 | { |
| 6828 | const struct lysc_node *next, *elem; |
| 6829 | uint32_t idx; |
| 6830 | LY_ERR rc; |
| 6831 | |
| 6832 | /* TREE DFS */ |
| 6833 | for (elem = next = start; elem; elem = next) { |
| 6834 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6835 | /* schema-only nodes, skip root */ |
| 6836 | goto next_iter; |
| 6837 | } |
| 6838 | |
| 6839 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6840 | if (!rc) { |
| 6841 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6842 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6843 | if (idx > start_idx) { |
| 6844 | /* we will process it later in the set */ |
| 6845 | goto skip_children; |
| 6846 | } |
| 6847 | } else { |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 6848 | 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] | 6849 | } |
| 6850 | } else if (rc == LY_EINVAL) { |
| 6851 | goto skip_children; |
| 6852 | } |
| 6853 | |
| 6854 | next_iter: |
| 6855 | /* TREE DFS NEXT ELEM */ |
| 6856 | /* select element for the next run - children first */ |
| 6857 | next = lysc_node_child(elem); |
| 6858 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6859 | next = next->next; |
| 6860 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6861 | next = next->next; |
| 6862 | } |
| 6863 | if (!next) { |
| 6864 | skip_children: |
| 6865 | /* no children, so try siblings, but only if it's not the start, |
| 6866 | * that is considered to be the root and it's siblings are not traversed */ |
| 6867 | if (elem != start) { |
| 6868 | next = elem->next; |
| 6869 | } else { |
| 6870 | break; |
| 6871 | } |
| 6872 | } |
| 6873 | while (!next) { |
| 6874 | /* no siblings, go back through the parents */ |
| 6875 | if (elem->parent == start) { |
| 6876 | /* we are done, no next element to process */ |
| 6877 | break; |
| 6878 | } |
| 6879 | /* parent is already processed, go to its sibling */ |
| 6880 | elem = elem->parent; |
| 6881 | next = elem->next; |
| 6882 | } |
| 6883 | } |
| 6884 | |
| 6885 | return LY_SUCCESS; |
| 6886 | } |
| 6887 | |
| 6888 | /** |
| 6889 | * @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] | 6890 | * |
| 6891 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6892 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6893 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6894 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6895 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6896 | */ |
| 6897 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6898 | 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] | 6899 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6900 | uint32_t i, orig_used, mod_idx; |
| 6901 | const struct lys_module *mod; |
| 6902 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6903 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6904 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6905 | return LY_SUCCESS; |
| 6906 | } |
| 6907 | |
| 6908 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6909 | 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] | 6910 | return LY_EVALID; |
| 6911 | } |
| 6912 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6913 | orig_used = set->used; |
| 6914 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6915 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6916 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6917 | continue; |
| 6918 | } |
| 6919 | |
| 6920 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6921 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6922 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6923 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6924 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6925 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6926 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6927 | /* traverse all top-level nodes in all the modules */ |
| 6928 | mod_idx = 0; |
| 6929 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6930 | /* module may not be implemented or not compiled yet */ |
| 6931 | if (!mod->compiled) { |
| 6932 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6933 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6934 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6935 | root = NULL; |
| 6936 | /* no getnext opts needed */ |
| 6937 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 6938 | 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] | 6939 | } |
| 6940 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6941 | |
| 6942 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6943 | /* add all the descendants recursively */ |
| 6944 | 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] | 6945 | } |
| 6946 | } |
| 6947 | |
| 6948 | return LY_SUCCESS; |
| 6949 | } |
| 6950 | |
| 6951 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6952 | * @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] | 6953 | * Indirectly context position aware. |
| 6954 | * |
| 6955 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6956 | * @param[in] mod Matching metadata module, NULL for any. |
| 6957 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6958 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6959 | * @return LY_ERR |
| 6960 | */ |
| 6961 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6962 | 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] | 6963 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6964 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6965 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6966 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6967 | return LY_SUCCESS; |
| 6968 | } |
| 6969 | |
| 6970 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6971 | 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] | 6972 | return LY_EVALID; |
| 6973 | } |
| 6974 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6975 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6976 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6977 | |
| 6978 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 6979 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6980 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6981 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6982 | |
| 6983 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6984 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6985 | continue; |
| 6986 | } |
| 6987 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6988 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6989 | /* match */ |
| 6990 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6991 | set->val.meta[i].meta = sub; |
| 6992 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6993 | /* pos does not change */ |
| 6994 | replaced = 1; |
| 6995 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6996 | 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] | 6997 | } |
| 6998 | ++i; |
| 6999 | } |
| 7000 | } |
| 7001 | } |
| 7002 | |
| 7003 | if (!replaced) { |
| 7004 | /* no match */ |
| 7005 | set_remove_node(set, i); |
| 7006 | } |
| 7007 | } |
| 7008 | |
| 7009 | return LY_SUCCESS; |
| 7010 | } |
| 7011 | |
| 7012 | /** |
| 7013 | * @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] | 7014 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7015 | * |
| 7016 | * @param[in,out] set1 Set to use for the result. |
| 7017 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7018 | * @return LY_ERR |
| 7019 | */ |
| 7020 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7021 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7022 | { |
| 7023 | LY_ERR rc; |
| 7024 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7025 | 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] | 7026 | 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] | 7027 | return LY_EVALID; |
| 7028 | } |
| 7029 | |
| 7030 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7031 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7032 | return LY_SUCCESS; |
| 7033 | } |
| 7034 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7035 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 7036 | /* release hidden allocated data (lyxp_set.size) */ |
| 7037 | lyxp_set_free_content(set1); |
| 7038 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7039 | memcpy(set1, set2, sizeof *set1); |
| 7040 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7041 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7042 | return LY_SUCCESS; |
| 7043 | } |
| 7044 | |
| 7045 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7046 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7047 | |
| 7048 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7049 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7050 | LY_CHECK_RET(rc); |
| 7051 | |
| 7052 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7053 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7054 | |
| 7055 | return LY_SUCCESS; |
| 7056 | } |
| 7057 | |
| 7058 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7059 | * @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] | 7060 | * Context position aware. |
| 7061 | * |
| 7062 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7063 | * @param[in] mod Matching metadata module, NULL for any. |
| 7064 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7065 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7066 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7067 | */ |
| 7068 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7069 | 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] | 7070 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7071 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7072 | struct lyxp_set *set_all_desc = NULL; |
| 7073 | LY_ERR rc; |
| 7074 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7075 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7076 | return LY_SUCCESS; |
| 7077 | } |
| 7078 | |
| 7079 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7080 | 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] | 7081 | return LY_EVALID; |
| 7082 | } |
| 7083 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7084 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7085 | * but it likely won't be used much, so it's a waste of time */ |
| 7086 | /* copy the context */ |
| 7087 | set_all_desc = set_copy(set); |
| 7088 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7089 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7090 | if (rc != LY_SUCCESS) { |
| 7091 | lyxp_set_free(set_all_desc); |
| 7092 | return rc; |
| 7093 | } |
| 7094 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7095 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7096 | if (rc != LY_SUCCESS) { |
| 7097 | lyxp_set_free(set_all_desc); |
| 7098 | return rc; |
| 7099 | } |
| 7100 | lyxp_set_free(set_all_desc); |
| 7101 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7102 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7103 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7104 | |
| 7105 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7106 | * we have all attributes qualified in lyd tree */ |
| 7107 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7108 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7109 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7110 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7111 | continue; |
| 7112 | } |
| 7113 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7114 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7115 | /* match */ |
| 7116 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7117 | set->val.meta[i].meta = sub; |
| 7118 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7119 | /* pos does not change */ |
| 7120 | replaced = 1; |
| 7121 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7122 | 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] | 7123 | } |
| 7124 | ++i; |
| 7125 | } |
| 7126 | } |
| 7127 | } |
| 7128 | |
| 7129 | if (!replaced) { |
| 7130 | /* no match */ |
| 7131 | set_remove_node(set, i); |
| 7132 | } |
| 7133 | } |
| 7134 | |
| 7135 | return LY_SUCCESS; |
| 7136 | } |
| 7137 | |
| 7138 | /** |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7139 | * @brief Move context @p set1 single item to the result of a comparison. |
| 7140 | * |
| 7141 | * @param[in] set1 First set with the item to compare. |
| 7142 | * @param[in] idx1 Index of the item in @p set1. |
| 7143 | * @param[in] set2 Second set. |
| 7144 | * @param[in] op Comparison operator to process. |
| 7145 | * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`. |
| 7146 | * @param[out] result Result of the comparison. |
| 7147 | * @return LY_ERR value. |
| 7148 | */ |
| 7149 | static LY_ERR |
| 7150 | moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op, |
| 7151 | ly_bool switch_operands, ly_bool *result) |
| 7152 | { |
| 7153 | struct lyxp_set tmp1 = {0}; |
| 7154 | LY_ERR rc = LY_SUCCESS; |
| 7155 | |
| 7156 | assert(set1->type == LYXP_SET_NODE_SET); |
| 7157 | |
| 7158 | /* cast set1 */ |
| 7159 | switch (set2->type) { |
| 7160 | case LYXP_SET_NUMBER: |
| 7161 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); |
| 7162 | break; |
| 7163 | case LYXP_SET_BOOLEAN: |
| 7164 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); |
| 7165 | break; |
| 7166 | default: |
| 7167 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); |
| 7168 | break; |
| 7169 | } |
| 7170 | LY_CHECK_GOTO(rc, cleanup); |
| 7171 | |
| 7172 | /* canonize set2 */ |
| 7173 | LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup); |
| 7174 | |
| 7175 | /* compare recursively and store the result */ |
| 7176 | if (switch_operands) { |
| 7177 | LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op), cleanup); |
| 7178 | *result = set2->val.bln; |
| 7179 | } else { |
| 7180 | LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op), cleanup); |
| 7181 | *result = tmp1.val.bln; |
| 7182 | } |
| 7183 | |
| 7184 | cleanup: |
| 7185 | lyxp_set_free_content(&tmp1); |
| 7186 | return rc; |
| 7187 | } |
| 7188 | |
| 7189 | /** |
| 7190 | * @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] | 7191 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7192 | * |
| 7193 | * @param[in,out] set1 Set to use for the result. |
| 7194 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7195 | * @param[in] op Comparison operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7196 | * @return LY_ERR |
| 7197 | */ |
| 7198 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7199 | 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] | 7200 | { |
| 7201 | /* |
| 7202 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7203 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7204 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7205 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7206 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7207 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7208 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7209 | * |
| 7210 | * '=' or '!=' |
| 7211 | * BOOLEAN + BOOLEAN |
| 7212 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7213 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7214 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7215 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7216 | * NUMBER + NUMBER |
| 7217 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7218 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7219 | * STRING + STRING |
| 7220 | * |
| 7221 | * '<=', '<', '>=', '>' |
| 7222 | * NUMBER + NUMBER |
| 7223 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7224 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7225 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7226 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7227 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7228 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7229 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7230 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7231 | */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7232 | ly_bool result; |
| 7233 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7234 | LY_ERR rc; |
| 7235 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7236 | /* iterative evaluation with node-sets */ |
| 7237 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7238 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7239 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7240 | /* evaluate for the single item */ |
| 7241 | 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] | 7242 | |
| 7243 | /* lazy evaluation until true */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7244 | if (result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7245 | set_fill_boolean(set1, 1); |
| 7246 | return LY_SUCCESS; |
| 7247 | } |
| 7248 | } |
| 7249 | } else { |
| 7250 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7251 | /* evaluate for the single item */ |
| 7252 | 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] | 7253 | |
| 7254 | /* lazy evaluation until true */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7255 | if (result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7256 | set_fill_boolean(set1, 1); |
| 7257 | return LY_SUCCESS; |
| 7258 | } |
| 7259 | } |
| 7260 | } |
| 7261 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7262 | /* false for all the nodes */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7263 | set_fill_boolean(set1, 0); |
| 7264 | return LY_SUCCESS; |
| 7265 | } |
| 7266 | |
| 7267 | /* first convert properly */ |
| 7268 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7269 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7270 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7271 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7272 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7273 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7274 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7275 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7276 | LY_CHECK_RET(rc); |
| 7277 | } /* else we have 2 strings */ |
| 7278 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7279 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7280 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7281 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7282 | LY_CHECK_RET(rc); |
| 7283 | } |
| 7284 | |
| 7285 | assert(set1->type == set2->type); |
| 7286 | |
| 7287 | /* compute result */ |
| 7288 | if (op[0] == '=') { |
| 7289 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7290 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7291 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 7292 | result = (set1->val.num == set2->val.num); |
| 7293 | } else { |
| 7294 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | f60d178 | 2022-08-02 11:00:32 +0200 | [diff] [blame] | 7295 | result = strcmp(set1->val.str, set2->val.str) ? 0 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7296 | } |
| 7297 | } else if (op[0] == '!') { |
| 7298 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7299 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7300 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 7301 | result = (set1->val.num != set2->val.num); |
| 7302 | } else { |
| 7303 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | f60d178 | 2022-08-02 11:00:32 +0200 | [diff] [blame] | 7304 | result = strcmp(set1->val.str, set2->val.str) ? 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7305 | } |
| 7306 | } else { |
| 7307 | assert(set1->type == LYXP_SET_NUMBER); |
| 7308 | if (op[0] == '<') { |
| 7309 | if (op[1] == '=') { |
| 7310 | result = (set1->val.num <= set2->val.num); |
| 7311 | } else { |
| 7312 | result = (set1->val.num < set2->val.num); |
| 7313 | } |
| 7314 | } else { |
| 7315 | if (op[1] == '=') { |
| 7316 | result = (set1->val.num >= set2->val.num); |
| 7317 | } else { |
| 7318 | result = (set1->val.num > set2->val.num); |
| 7319 | } |
| 7320 | } |
| 7321 | } |
| 7322 | |
| 7323 | /* assign result */ |
| 7324 | if (result) { |
| 7325 | set_fill_boolean(set1, 1); |
| 7326 | } else { |
| 7327 | set_fill_boolean(set1, 0); |
| 7328 | } |
| 7329 | |
| 7330 | return LY_SUCCESS; |
| 7331 | } |
| 7332 | |
| 7333 | /** |
| 7334 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7335 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7336 | * |
| 7337 | * @param[in,out] set1 Set to use for the result. |
| 7338 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7339 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7340 | * @return LY_ERR |
| 7341 | */ |
| 7342 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7343 | 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] | 7344 | { |
| 7345 | LY_ERR rc; |
| 7346 | |
| 7347 | /* unary '-' */ |
| 7348 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7349 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | LY_CHECK_RET(rc); |
| 7351 | set1->val.num *= -1; |
| 7352 | lyxp_set_free(set2); |
| 7353 | return LY_SUCCESS; |
| 7354 | } |
| 7355 | |
| 7356 | assert(set1 && set2); |
| 7357 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7358 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7360 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | LY_CHECK_RET(rc); |
| 7362 | |
| 7363 | switch (op[0]) { |
| 7364 | /* '+' */ |
| 7365 | case '+': |
| 7366 | set1->val.num += set2->val.num; |
| 7367 | break; |
| 7368 | |
| 7369 | /* '-' */ |
| 7370 | case '-': |
| 7371 | set1->val.num -= set2->val.num; |
| 7372 | break; |
| 7373 | |
| 7374 | /* '*' */ |
| 7375 | case '*': |
| 7376 | set1->val.num *= set2->val.num; |
| 7377 | break; |
| 7378 | |
| 7379 | /* 'div' */ |
| 7380 | case 'd': |
| 7381 | set1->val.num /= set2->val.num; |
| 7382 | break; |
| 7383 | |
| 7384 | /* 'mod' */ |
| 7385 | case 'm': |
| 7386 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7387 | break; |
| 7388 | |
| 7389 | default: |
| 7390 | LOGINT_RET(set1->ctx); |
| 7391 | } |
| 7392 | |
| 7393 | return LY_SUCCESS; |
| 7394 | } |
| 7395 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7396 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7397 | * @brief Evaluate Predicate. Logs directly on error. |
| 7398 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7399 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7400 | * |
| 7401 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7402 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7403 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7404 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7405 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7406 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7407 | */ |
| 7408 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7409 | eval_predicate(const struct lyxp_expr *exp, uint16_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] | 7410 | { |
| 7411 | LY_ERR rc; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 7412 | uint16_t orig_exp; |
| 7413 | uint32_t i, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7414 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7415 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7416 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7417 | |
| 7418 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7419 | 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] | 7420 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7421 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7422 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7423 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7424 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7425 | 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] | 7426 | LY_CHECK_RET(rc); |
| 7427 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7428 | /* 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] | 7429 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7430 | |
| 7431 | /* empty set, nothing to evaluate */ |
| 7432 | if (!set->used) { |
| 7433 | goto only_parse; |
| 7434 | } |
| 7435 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7436 | /* decide forward or reverse axis */ |
| 7437 | switch (axis) { |
| 7438 | case LYXP_AXIS_ANCESTOR: |
| 7439 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7440 | case LYXP_AXIS_PRECEDING: |
| 7441 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7442 | reverse_axis = 1; |
| 7443 | break; |
| 7444 | case LYXP_AXIS_DESCENDANT: |
| 7445 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7446 | case LYXP_AXIS_FOLLOWING: |
| 7447 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7448 | case LYXP_AXIS_PARENT: |
| 7449 | case LYXP_AXIS_CHILD: |
| 7450 | case LYXP_AXIS_SELF: |
| 7451 | case LYXP_AXIS_ATTRIBUTE: |
| 7452 | reverse_axis = 0; |
| 7453 | break; |
| 7454 | } |
| 7455 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7456 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7457 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7458 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7459 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7460 | set_init(&set2, set); |
| 7461 | 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] | 7462 | |
| 7463 | /* remember the node context position for position() and context size for last() */ |
| 7464 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7465 | |
| 7466 | set2.ctx_pos = orig_pos; |
| 7467 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7468 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7469 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7470 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7472 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | return rc; |
| 7474 | } |
| 7475 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7476 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7477 | if (set2.type == LYXP_SET_NUMBER) { |
| 7478 | if ((long long)set2.val.num == orig_pos) { |
| 7479 | set2.val.num = 1; |
| 7480 | } else { |
| 7481 | set2.val.num = 0; |
| 7482 | } |
| 7483 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7484 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7485 | |
| 7486 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7487 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7488 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7489 | } |
| 7490 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7491 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7492 | |
| 7493 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7494 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7495 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7496 | /* there is a currently-valid node */ |
| 7497 | break; |
| 7498 | } |
| 7499 | } |
| 7500 | /* empty set, nothing to evaluate */ |
| 7501 | if (i == set->used) { |
| 7502 | goto only_parse; |
| 7503 | } |
| 7504 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7505 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7506 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7507 | /* set special in_ctx to all the valid snodes */ |
| 7508 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7509 | |
| 7510 | /* use the valid snodes one-by-one */ |
| 7511 | for (i = 0; i < set->used; ++i) { |
| 7512 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7513 | continue; |
| 7514 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7515 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
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 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7518 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7519 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7520 | LY_CHECK_RET(rc); |
| 7521 | |
| 7522 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7523 | } |
| 7524 | |
| 7525 | /* restore the state as it was before the predicate */ |
| 7526 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7527 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7528 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7529 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7530 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7531 | } |
| 7532 | } |
| 7533 | |
| 7534 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7535 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7536 | set_fill_set(&set2, set); |
| 7537 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7538 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7539 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7540 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7541 | return rc; |
| 7542 | } |
| 7543 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7544 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7545 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7546 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7547 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7548 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7549 | } |
| 7550 | |
| 7551 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7552 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7553 | 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] | 7554 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7555 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7556 | |
| 7557 | return LY_SUCCESS; |
| 7558 | } |
| 7559 | |
| 7560 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7561 | * @brief Evaluate Literal. Logs directly on error. |
| 7562 | * |
| 7563 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7564 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7565 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7566 | */ |
| 7567 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7568 | eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7569 | { |
| 7570 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7571 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7572 | set_fill_string(set, "", 0); |
| 7573 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7574 | 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] | 7575 | } |
| 7576 | } |
| 7577 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7578 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7579 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7580 | } |
| 7581 | |
| 7582 | /** |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7583 | * @brief Check that a nametest in a predicate matches a key node. |
| 7584 | * |
| 7585 | * @param[in] nametest Nametest to check. |
| 7586 | * @param[in] len Length of @p nametest. |
| 7587 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7588 | * @param[in] set Context set. |
| 7589 | * @param[in] key Expected key node. |
| 7590 | * @return LY_SUCCESS on success, |
| 7591 | * @return LY_ENOT if a predicate could not be compiled. |
| 7592 | * @return LY_ERR on any error. |
| 7593 | */ |
| 7594 | static LY_ERR |
| 7595 | eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode, |
| 7596 | const struct lyxp_set *set, const struct lysc_node *key) |
| 7597 | { |
| 7598 | const struct lys_module *mod; |
| 7599 | |
| 7600 | /* prefix (module) */ |
| 7601 | LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod)); |
| 7602 | if (mod != key->module) { |
| 7603 | return LY_ENOT; |
| 7604 | } |
| 7605 | |
| 7606 | /* node name */ |
| 7607 | if (ly_strncmp(key->name, nametest, len)) { |
| 7608 | return LY_ENOT; |
| 7609 | } |
| 7610 | |
| 7611 | return LY_SUCCESS; |
| 7612 | } |
| 7613 | |
| 7614 | /** |
| 7615 | * @brief Append a simple predicate for the node. |
| 7616 | * |
| 7617 | * @param[in] exp Full parsed XPath expression. |
| 7618 | * @param[in] tok_idx Predicate start index in @p exp. |
| 7619 | * @param[in] end_tok_idx Predicate end index in @p exp. |
| 7620 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7621 | * @param[in] set Context set. |
| 7622 | * @param[in] pred_node Node with the value referenced in the predicate. |
| 7623 | * @param[in,out] pred Predicate to append to. |
| 7624 | * @param[in,out] pred_len Length of @p pred, is updated. |
| 7625 | * @return LY_SUCCESS on success, |
| 7626 | * @return LY_ENOT if a predicate could not be compiled. |
| 7627 | * @return LY_ERR on any error. |
| 7628 | */ |
| 7629 | static LY_ERR |
| 7630 | eval_name_test_try_compile_predicate_append(const struct lyxp_expr *exp, uint16_t tok_idx, uint16_t end_tok_idx, |
| 7631 | const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred, |
| 7632 | uint32_t *pred_len) |
| 7633 | { |
| 7634 | LY_ERR rc = LY_SUCCESS; |
| 7635 | uint32_t i; |
| 7636 | const struct lyd_node *siblings; |
| 7637 | struct lyd_node *ctx_node; |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7638 | const struct lysc_node *sparent, *cur_scnode; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7639 | struct lyxp_expr *val_exp = NULL; |
| 7640 | struct lyxp_set set2 = {0}; |
| 7641 | char quot; |
| 7642 | |
| 7643 | /* duplicate the value expression */ |
| 7644 | LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup); |
| 7645 | |
| 7646 | /* get its atoms */ |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7647 | cur_scnode = set->cur_node ? set->cur_node->schema : NULL; |
| 7648 | LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode, |
| 7649 | ctx_scnode, &set2, LYXP_SCNODE), cleanup); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7650 | |
| 7651 | /* check whether we can compile a single predicate (evaluation result value is always the same) */ |
| 7652 | for (i = 0; i < set2.used; ++i) { |
| 7653 | if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) { |
| 7654 | /* skip root and context node */ |
| 7655 | continue; |
| 7656 | } |
| 7657 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7658 | /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the |
| 7659 | * context node instance */ |
| 7660 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) { |
| 7661 | /* 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] | 7662 | rc = LY_ENOT; |
| 7663 | goto cleanup; |
| 7664 | } |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7665 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) { |
| 7666 | for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {} |
| 7667 | if (sparent) { |
| 7668 | /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */ |
| 7669 | rc = LY_ENOT; |
| 7670 | goto cleanup; |
| 7671 | } |
| 7672 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7673 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7674 | /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered, |
| 7675 | * but the current node can be safely ignored, it is always the same data instance */ |
| 7676 | 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] | 7677 | rc = LY_ENOT; |
| 7678 | goto cleanup; |
| 7679 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7680 | } |
| 7681 | |
| 7682 | /* get any data instance of the context node, we checked it makes no difference */ |
| 7683 | siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree; |
| 7684 | LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup); |
| 7685 | |
| 7686 | /* evaluate the value subexpression with the root context node */ |
| 7687 | lyxp_set_free_content(&set2); |
| 7688 | LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node, |
| 7689 | ctx_node, set->tree, NULL, &set2, 0), cleanup); |
| 7690 | |
| 7691 | /* cast it into a string */ |
| 7692 | LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup); |
| 7693 | |
| 7694 | /* append the JSON predicate */ |
| 7695 | *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3); |
| 7696 | LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7697 | quot = strchr(set2.val.str, '\'') ? '\"' : '\''; |
| 7698 | *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot); |
| 7699 | |
| 7700 | cleanup: |
| 7701 | lyxp_expr_free(set->ctx, val_exp); |
| 7702 | lyxp_set_free_content(&set2); |
| 7703 | return rc; |
| 7704 | } |
| 7705 | |
| 7706 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7707 | * @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] | 7708 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7709 | * @param[in] exp Full parsed XPath expression. |
| 7710 | * @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] | 7711 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7712 | * @param[in] set Context set. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7713 | * @param[out] predicates Parsed predicates. |
| 7714 | * @param[out] pred_type Type of @p predicates. |
| 7715 | * @return LY_SUCCESS on success, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7716 | * @return LY_ENOT if a predicate could not be compiled. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7717 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7718 | */ |
| 7719 | static LY_ERR |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7720 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_scnode, |
| 7721 | 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] | 7722 | { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7723 | LY_ERR rc = LY_SUCCESS; |
| 7724 | uint16_t e_idx, val_start_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7725 | const struct lysc_node *key; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7726 | uint32_t prev_lo, pred_len = 0; |
| 7727 | char *pred = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7728 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7729 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7730 | assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7731 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7732 | /* turn logging off */ |
| 7733 | prev_lo = ly_log_options(0); |
| 7734 | |
| 7735 | if (ctx_scnode->nodetype == LYS_LIST) { |
| 7736 | /* check for predicates "[key1=...][key2=...]..." */ |
| 7737 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7738 | /* get key count */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7739 | if (ctx_scnode->flags & LYS_KEYLESS) { |
| 7740 | rc = LY_ENOT; |
| 7741 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7742 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7743 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7744 | /* learn where the predicates end */ |
| 7745 | e_idx = *tok_idx; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7746 | 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] | 7747 | /* '[' */ |
| 7748 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7749 | rc = LY_ENOT; |
| 7750 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7751 | } |
| 7752 | ++e_idx; |
| 7753 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7754 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7755 | /* not a key */ |
| 7756 | rc = LY_ENOT; |
| 7757 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7758 | } |
| 7759 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7760 | /* check key */ |
| 7761 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx], |
| 7762 | exp->tok_len[e_idx], ctx_scnode, set, key), cleanup); |
| 7763 | |
| 7764 | ++e_idx; |
| 7765 | |
| 7766 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7767 | /* not '=' */ |
| 7768 | rc = LY_ENOT; |
| 7769 | goto cleanup; |
| 7770 | } |
| 7771 | ++e_idx; |
| 7772 | |
| 7773 | /* value start */ |
| 7774 | val_start_idx = e_idx; |
| 7775 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7776 | /* ']' */ |
| 7777 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7778 | if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
| 7779 | /* higher priority than '=' */ |
| 7780 | rc = LY_ENOT; |
| 7781 | goto cleanup; |
| 7782 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7783 | ++e_idx; |
| 7784 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7785 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7786 | /* try to evaluate the value */ |
| 7787 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, |
| 7788 | set, key, &pred, &pred_len), cleanup); |
| 7789 | |
| 7790 | ++e_idx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7791 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7792 | } else { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7793 | /* check for predicate "[.=...]" */ |
| 7794 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7795 | /* learn just where this single predicate ends */ |
| 7796 | e_idx = *tok_idx; |
| 7797 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7798 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7799 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7800 | rc = LY_ENOT; |
| 7801 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7802 | } |
| 7803 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7804 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7805 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7806 | /* not the node value */ |
| 7807 | rc = LY_ENOT; |
| 7808 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7809 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7810 | ++e_idx; |
| 7811 | |
| 7812 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7813 | /* not '=' */ |
| 7814 | rc = LY_ENOT; |
| 7815 | goto cleanup; |
| 7816 | } |
| 7817 | ++e_idx; |
| 7818 | |
| 7819 | /* value start */ |
| 7820 | val_start_idx = e_idx; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7821 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7822 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7823 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7824 | if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
| 7825 | /* higher priority than '=' */ |
| 7826 | rc = LY_ENOT; |
| 7827 | goto cleanup; |
| 7828 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7829 | ++e_idx; |
| 7830 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7831 | |
| 7832 | /* try to evaluate the value */ |
| 7833 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set, |
| 7834 | ctx_scnode, &pred, &pred_len), cleanup); |
| 7835 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7836 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7837 | } |
| 7838 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7839 | /* parse the predicate(s) */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7840 | LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL, |
| 7841 | LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7842 | |
| 7843 | /* compile */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7844 | rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2, |
| 7845 | &pred_idx, LY_VALUE_JSON, NULL, predicates, pred_type); |
| 7846 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7847 | |
| 7848 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7849 | *tok_idx = e_idx; |
| 7850 | |
| 7851 | cleanup: |
| 7852 | ly_log_options(prev_lo); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7853 | lyxp_expr_free(set->ctx, exp2); |
| 7854 | free(pred); |
| 7855 | return rc; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7856 | } |
| 7857 | |
| 7858 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7859 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7860 | * data node(s) could be found using a single hash-based search. |
| 7861 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7862 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7863 | * @param[in] node Next context node to check. |
| 7864 | * @param[in] name Expected node name. |
| 7865 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7866 | * @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] | 7867 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7868 | * @param[in] format Prefix format. |
| 7869 | * @param[in,out] found Previously found node, is updated. |
| 7870 | * @return LY_SUCCESS on success, |
| 7871 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7872 | */ |
| 7873 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7874 | eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7875 | uint16_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] | 7876 | const struct lysc_node **found) |
| 7877 | { |
| 7878 | const struct lysc_node *scnode; |
| 7879 | const struct lys_module *mod; |
| 7880 | uint32_t idx = 0; |
| 7881 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7882 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7883 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7884 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7885 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7886 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7887 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7888 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7889 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7890 | if (!mod->implemented) { |
| 7891 | continue; |
| 7892 | } |
| 7893 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7894 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7895 | if (scnode) { |
| 7896 | /* we have found a match */ |
| 7897 | break; |
| 7898 | } |
| 7899 | } |
| 7900 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7901 | if (!scnode) { |
| 7902 | /* all modules searched */ |
| 7903 | idx = 0; |
| 7904 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7905 | } else { |
| 7906 | /* search in top-level */ |
| 7907 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7908 | } |
| 7909 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7910 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7911 | /* we must adjust the module to inherit the one from the context node */ |
| 7912 | moveto_mod = node->schema->module; |
| 7913 | } |
| 7914 | |
| 7915 | /* search in children, do not repeat the same search */ |
| 7916 | 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] | 7917 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7918 | |
| 7919 | /* additional context check */ |
| 7920 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7921 | scnode = NULL; |
| 7922 | } |
| 7923 | |
| 7924 | if (scnode) { |
| 7925 | if (*found) { |
| 7926 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7927 | * (more hash-based searches would be required, not supported) */ |
| 7928 | return LY_ENOT; |
| 7929 | } else { |
| 7930 | /* remember the found schema node and continue to make sure it can be used */ |
| 7931 | *found = scnode; |
| 7932 | } |
| 7933 | } |
| 7934 | |
| 7935 | if (idx) { |
| 7936 | /* continue searching all the following models */ |
| 7937 | goto continue_search; |
| 7938 | } |
| 7939 | |
| 7940 | return LY_SUCCESS; |
| 7941 | } |
| 7942 | |
| 7943 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7944 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7945 | * |
| 7946 | * @param[in] set XPath set. |
| 7947 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7948 | * @param[in] ncname XPath NCName being evaluated. |
| 7949 | * @param[in] ncname_len Length of @p ncname. |
| 7950 | * @param[in] expr Whole XPath expression. |
| 7951 | * @param[in] options XPath options. |
| 7952 | */ |
| 7953 | static void |
| 7954 | eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname, |
| 7955 | uint16_t ncname_len, const char *expr, uint32_t options) |
| 7956 | { |
| 7957 | const char *format; |
| 7958 | char *path = NULL, *ppath = NULL; |
| 7959 | |
| 7960 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7961 | if (scparent) { |
| 7962 | /* generate path for the parent */ |
| 7963 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7964 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7965 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7966 | ppath = strdup("<root>"); |
| 7967 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7968 | ppath = strdup("<config-root>"); |
| 7969 | } |
| 7970 | } |
| 7971 | if (ppath) { |
| 7972 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7973 | if (options & LYXP_SCNODE_ERROR) { |
| 7974 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7975 | } else { |
| 7976 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7977 | } |
| 7978 | } else { |
| 7979 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7980 | if (options & LYXP_SCNODE_ERROR) { |
| 7981 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7982 | } else { |
| 7983 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7984 | } |
| 7985 | } |
| 7986 | free(path); |
| 7987 | free(ppath); |
| 7988 | } |
| 7989 | |
| 7990 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7991 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7992 | * |
| 7993 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7994 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7995 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7996 | * |
| 7997 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7998 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7999 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8000 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8001 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8002 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8003 | * @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] | 8004 | */ |
| 8005 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8006 | eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8007 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8008 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8009 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8010 | const char *ncname, *ncname_dict = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8011 | uint32_t i, ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8012 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8013 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8014 | struct ly_path_predicate *predicates = NULL; |
| 8015 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8016 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8017 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8018 | 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] | 8019 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8020 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8021 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8022 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8023 | goto moveto; |
| 8024 | } |
| 8025 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8026 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 8027 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8028 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8029 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8030 | /* all nodes will match */ |
| 8031 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8032 | } |
| 8033 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8034 | /* parse (and skip) module name */ |
| 8035 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8036 | LY_CHECK_GOTO(rc, cleanup); |
| 8037 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8038 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8039 | /* all nodes from the module will match */ |
| 8040 | goto moveto; |
| 8041 | } |
| 8042 | |
| 8043 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 8044 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8045 | /* find the matching schema node in some parent in the context */ |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8046 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8047 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 8048 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8049 | /* check failed */ |
| 8050 | scnode = NULL; |
| 8051 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8052 | } |
| 8053 | } |
| 8054 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8055 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 8056 | /* try to create the predicates */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 8057 | 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] | 8058 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8059 | scnode = NULL; |
| 8060 | } |
| 8061 | } |
| 8062 | } |
| 8063 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8064 | if (!scnode) { |
| 8065 | /* 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] | 8066 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 8067 | 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] | 8068 | } |
| 8069 | |
| 8070 | moveto: |
| 8071 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8072 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8073 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8074 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8075 | } else { |
| 8076 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 8077 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8078 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8079 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8080 | } |
| 8081 | LY_CHECK_GOTO(rc, cleanup); |
| 8082 | } |
| 8083 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8084 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8085 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8086 | ly_bool found = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8087 | |
| 8088 | /* remember parent if there is only one, to print in the warning */ |
| 8089 | for (i = 0; i < set->used; ++i) { |
| 8090 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 8091 | if (!scparent) { |
| 8092 | /* remember the context node */ |
| 8093 | scparent = &set->val.scnodes[i]; |
| 8094 | } else { |
| 8095 | /* several context nodes, no reasonable error possible */ |
| 8096 | scparent = NULL; |
| 8097 | break; |
| 8098 | } |
| 8099 | } |
| 8100 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8101 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8102 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8103 | /* efficient evaluation that does not add all the descendants into the set */ |
| 8104 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8105 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8106 | if (all_desc) { |
| 8107 | /* "//" == "/descendant-or-self::node()/" */ |
| 8108 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8109 | LY_CHECK_GOTO(rc, cleanup); |
| 8110 | } |
| 8111 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8112 | } |
| 8113 | LY_CHECK_GOTO(rc, cleanup); |
| 8114 | |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8115 | i = set->used; |
| 8116 | do { |
| 8117 | --i; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8118 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8119 | found = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8120 | break; |
| 8121 | } |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8122 | } while (i); |
| 8123 | if (!found) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8124 | /* generate message */ |
| 8125 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 8126 | |
| 8127 | if (options & LYXP_SCNODE_ERROR) { |
| 8128 | /* error */ |
| 8129 | rc = LY_EVALID; |
| 8130 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8131 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8132 | |
| 8133 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 8134 | rc = LY_ENOT; |
| 8135 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8136 | } |
| 8137 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8138 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8139 | /* efficient evaluation */ |
| 8140 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 8141 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 8142 | /* we can find the child nodes using hashes */ |
| 8143 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8144 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8145 | if (all_desc) { |
| 8146 | /* "//" == "/descendant-or-self::node()/" */ |
| 8147 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8148 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8149 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8150 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8151 | } |
| 8152 | LY_CHECK_GOTO(rc, cleanup); |
| 8153 | } |
| 8154 | } |
| 8155 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8156 | if (scnode_skip_pred) { |
| 8157 | /* skip predicates */ |
| 8158 | options |= LYXP_SKIP_EXPR; |
| 8159 | } |
| 8160 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8161 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8162 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8163 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8164 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8165 | } |
| 8166 | |
| 8167 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8168 | if (scnode_skip_pred) { |
| 8169 | /* restore options */ |
| 8170 | options &= ~LYXP_SKIP_EXPR; |
| 8171 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8172 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8173 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 8174 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 8175 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8176 | return rc; |
| 8177 | } |
| 8178 | |
| 8179 | /** |
| 8180 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 8181 | * |
| 8182 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8183 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8184 | * [8] NodeType ::= 'text' | 'node' |
| 8185 | * |
| 8186 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8187 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8188 | * @param[in] axis Axis to search on. |
| 8189 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8190 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8191 | * @param[in] options XPath options. |
| 8192 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8193 | */ |
| 8194 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8195 | eval_node_type_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8196 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8197 | { |
| 8198 | LY_ERR rc; |
| 8199 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8200 | (void)all_desc; |
| 8201 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8202 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8203 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8204 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 8205 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8206 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8207 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 8208 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8209 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8210 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8211 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8212 | 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] | 8213 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8214 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8215 | |
| 8216 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8217 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8218 | 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] | 8219 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8220 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8221 | |
| 8222 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8223 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8224 | 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] | 8225 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8226 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8227 | |
| 8228 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8229 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8230 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8231 | LY_CHECK_RET(rc); |
| 8232 | } |
| 8233 | |
| 8234 | return LY_SUCCESS; |
| 8235 | } |
| 8236 | |
| 8237 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8238 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8239 | * |
| 8240 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8241 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8242 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8243 | * |
| 8244 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8245 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8246 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8247 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | * @param[in] options XPath options. |
| 8249 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8250 | */ |
| 8251 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8252 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 8253 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8255 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8256 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8257 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8258 | |
| 8259 | goto step; |
| 8260 | do { |
| 8261 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8262 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | all_desc = 0; |
| 8264 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8265 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8266 | all_desc = 1; |
| 8267 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8268 | 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] | 8269 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8270 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8271 | |
| 8272 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8273 | /* AxisSpecifier */ |
| 8274 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8275 | 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] | 8276 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8277 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8278 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8279 | ++(*tok_idx); |
| 8280 | |
| 8281 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8282 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8283 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8284 | ++(*tok_idx); |
| 8285 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8286 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8287 | |
| 8288 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8289 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8290 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8291 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8292 | /* default */ |
| 8293 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8294 | } |
| 8295 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8296 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8297 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8298 | case LYXP_TOKEN_DOT: |
| 8299 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8300 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8301 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8302 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8303 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8304 | rc = LY_EVALID; |
| 8305 | goto cleanup; |
| 8306 | } |
| 8307 | |
| 8308 | if (all_desc) { |
| 8309 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8310 | LY_CHECK_GOTO(rc, cleanup); |
| 8311 | } |
| 8312 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8313 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8314 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8315 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8316 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8317 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8318 | break; |
| 8319 | |
| 8320 | case LYXP_TOKEN_DDOT: |
| 8321 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8322 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8323 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8324 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8325 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8326 | rc = LY_EVALID; |
| 8327 | goto cleanup; |
| 8328 | } |
| 8329 | |
| 8330 | if (all_desc) { |
| 8331 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8332 | LY_CHECK_GOTO(rc, cleanup); |
| 8333 | } |
| 8334 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8335 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8336 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8337 | 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] | 8338 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8339 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8340 | break; |
| 8341 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8342 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8343 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8344 | 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] | 8345 | if (rc == LY_ENOT) { |
| 8346 | assert(options & LYXP_SCNODE_ALL); |
| 8347 | /* skip the rest of this path */ |
| 8348 | rc = LY_SUCCESS; |
| 8349 | scnode_skip_path = 1; |
| 8350 | options |= LYXP_SKIP_EXPR; |
| 8351 | } |
| 8352 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8353 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8354 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8355 | case LYXP_TOKEN_NODETYPE: |
| 8356 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8357 | 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] | 8358 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8359 | break; |
| 8360 | |
| 8361 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8362 | LOGINT(set->ctx); |
| 8363 | rc = LY_EINT; |
| 8364 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8365 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8366 | } 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] | 8367 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8368 | cleanup: |
| 8369 | if (scnode_skip_path) { |
| 8370 | options &= ~LYXP_SKIP_EXPR; |
| 8371 | } |
| 8372 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8373 | } |
| 8374 | |
| 8375 | /** |
| 8376 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8377 | * |
| 8378 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8379 | * |
| 8380 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8381 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8382 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8383 | * @param[in] options XPath options. |
| 8384 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8385 | */ |
| 8386 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8387 | eval_absolute_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8388 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8389 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8390 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8391 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8392 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8393 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8394 | } |
| 8395 | |
| 8396 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8397 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8398 | /* evaluate '/' - deferred */ |
| 8399 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8400 | 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] | 8401 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8402 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8403 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8404 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8405 | return LY_SUCCESS; |
| 8406 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8407 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8408 | case LYXP_TOKEN_DOT: |
| 8409 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8410 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8411 | case LYXP_TOKEN_AT: |
| 8412 | case LYXP_TOKEN_NAMETEST: |
| 8413 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8414 | 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] | 8415 | break; |
| 8416 | default: |
| 8417 | break; |
| 8418 | } |
| 8419 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8420 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8421 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8422 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8423 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8424 | 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] | 8425 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8426 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8427 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8428 | LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8429 | } |
| 8430 | |
| 8431 | return LY_SUCCESS; |
| 8432 | } |
| 8433 | |
| 8434 | /** |
| 8435 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8436 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8437 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8438 | * |
| 8439 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8440 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8441 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8442 | * @param[in] options XPath options. |
| 8443 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8444 | */ |
| 8445 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8446 | eval_function_call(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8447 | { |
| 8448 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8449 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8450 | LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL; |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 8451 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8452 | struct lyxp_set **args = NULL, **args_aux; |
| 8453 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8454 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8455 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8456 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8457 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8458 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8459 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8460 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8461 | xpath_func = &xpath_sum; |
| 8462 | } |
| 8463 | break; |
| 8464 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8465 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8466 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8467 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8468 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8469 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8470 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8471 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8472 | xpath_func = &xpath_true; |
| 8473 | } |
| 8474 | break; |
| 8475 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8476 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8477 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8478 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8479 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8480 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8481 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8482 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8483 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8484 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8485 | xpath_func = &xpath_deref; |
| 8486 | } |
| 8487 | break; |
| 8488 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8489 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8490 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8491 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8492 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8493 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8494 | xpath_func = &xpath_string; |
| 8495 | } |
| 8496 | break; |
| 8497 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8498 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8499 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8500 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8501 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8502 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8503 | xpath_func = &xpath_current; |
| 8504 | } |
| 8505 | break; |
| 8506 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8507 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8508 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8509 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8510 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8511 | } 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] | 8512 | xpath_func = &xpath_re_match; |
| 8513 | } |
| 8514 | break; |
| 8515 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8516 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8517 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8518 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8519 | xpath_func = &xpath_translate; |
| 8520 | } |
| 8521 | break; |
| 8522 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8523 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8524 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8525 | } 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] | 8526 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8527 | } 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] | 8528 | xpath_func = &xpath_bit_is_set; |
| 8529 | } |
| 8530 | break; |
| 8531 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8532 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | xpath_func = &xpath_starts_with; |
| 8534 | } |
| 8535 | break; |
| 8536 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8537 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8538 | xpath_func = &xpath_derived_from; |
| 8539 | } |
| 8540 | break; |
| 8541 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8542 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8543 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8544 | } 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] | 8545 | xpath_func = &xpath_string_length; |
| 8546 | } |
| 8547 | break; |
| 8548 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8549 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8550 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8551 | } 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] | 8552 | xpath_func = &xpath_substring_after; |
| 8553 | } |
| 8554 | break; |
| 8555 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8556 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8557 | xpath_func = &xpath_substring_before; |
| 8558 | } |
| 8559 | break; |
| 8560 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8561 | 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] | 8562 | xpath_func = &xpath_derived_from_or_self; |
| 8563 | } |
| 8564 | break; |
| 8565 | } |
| 8566 | |
| 8567 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8568 | 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] | 8569 | return LY_EVALID; |
| 8570 | } |
| 8571 | } |
| 8572 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8573 | 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] | 8574 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8575 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8576 | |
| 8577 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8578 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8579 | 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] | 8580 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8581 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8582 | |
| 8583 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8584 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8585 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8586 | args = malloc(sizeof *args); |
| 8587 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8588 | arg_count = 1; |
| 8589 | args[0] = set_copy(set); |
| 8590 | if (!args[0]) { |
| 8591 | rc = LY_EMEM; |
| 8592 | goto cleanup; |
| 8593 | } |
| 8594 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8595 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8596 | LY_CHECK_GOTO(rc, cleanup); |
| 8597 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8598 | 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] | 8599 | LY_CHECK_GOTO(rc, cleanup); |
| 8600 | } |
| 8601 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8602 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8603 | 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] | 8604 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8605 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8606 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8607 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8608 | ++arg_count; |
| 8609 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8610 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8611 | args = args_aux; |
| 8612 | args[arg_count - 1] = set_copy(set); |
| 8613 | if (!args[arg_count - 1]) { |
| 8614 | rc = LY_EMEM; |
| 8615 | goto cleanup; |
| 8616 | } |
| 8617 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8618 | 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] | 8619 | LY_CHECK_GOTO(rc, cleanup); |
| 8620 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8621 | 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] | 8622 | LY_CHECK_GOTO(rc, cleanup); |
| 8623 | } |
| 8624 | } |
| 8625 | |
| 8626 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8627 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8628 | 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] | 8629 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8630 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8631 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8632 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8633 | /* evaluate function */ |
| 8634 | rc = xpath_func(args, arg_count, set, options); |
| 8635 | |
| 8636 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8637 | /* merge all nodes from arg evaluations */ |
| 8638 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8639 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8640 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8641 | } |
| 8642 | } |
| 8643 | } else { |
| 8644 | rc = LY_SUCCESS; |
| 8645 | } |
| 8646 | |
| 8647 | cleanup: |
| 8648 | for (i = 0; i < arg_count; ++i) { |
| 8649 | lyxp_set_free(args[i]); |
| 8650 | } |
| 8651 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8652 | return rc; |
| 8653 | } |
| 8654 | |
| 8655 | /** |
| 8656 | * @brief Evaluate Number. Logs directly on error. |
| 8657 | * |
| 8658 | * @param[in] ctx Context for errors. |
| 8659 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8660 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8661 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8662 | * @return LY_ERR |
| 8663 | */ |
| 8664 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8665 | eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8666 | { |
| 8667 | long double num; |
| 8668 | char *endptr; |
| 8669 | |
| 8670 | if (set) { |
| 8671 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8672 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8673 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8674 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8675 | 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] | 8676 | 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] | 8677 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8678 | } 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] | 8679 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8680 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8681 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8682 | return LY_EVALID; |
| 8683 | } |
| 8684 | |
| 8685 | set_fill_number(set, num); |
| 8686 | } |
| 8687 | |
| 8688 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8689 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8690 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8691 | return LY_SUCCESS; |
| 8692 | } |
| 8693 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8694 | LY_ERR |
| 8695 | lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var) |
| 8696 | { |
| 8697 | LY_ERR ret = LY_ENOTFOUND; |
| 8698 | LY_ARRAY_COUNT_TYPE u; |
| 8699 | |
| 8700 | assert(vars && name); |
| 8701 | |
| 8702 | name_len = name_len ? name_len : strlen(name); |
| 8703 | |
| 8704 | LY_ARRAY_FOR(vars, u) { |
| 8705 | if (!strncmp(vars[u].name, name, name_len)) { |
| 8706 | ret = LY_SUCCESS; |
| 8707 | break; |
| 8708 | } |
| 8709 | } |
| 8710 | |
| 8711 | if (var && !ret) { |
| 8712 | *var = &vars[u]; |
| 8713 | } |
| 8714 | |
| 8715 | return ret; |
| 8716 | } |
| 8717 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8718 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8719 | * @brief Evaluate VariableReference. |
| 8720 | * |
| 8721 | * @param[in] exp Parsed XPath expression. |
| 8722 | * @param[in] tok_idx Position in the expression @p exp. |
| 8723 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8724 | * @param[in,out] set Context and result set. |
| 8725 | * @param[in] options XPath options. |
| 8726 | * @return LY_ERR value. |
| 8727 | */ |
| 8728 | static LY_ERR |
| 8729 | eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
| 8730 | { |
| 8731 | LY_ERR ret; |
| 8732 | const char *name; |
| 8733 | struct lyxp_var *var; |
| 8734 | const struct lyxp_var *vars; |
| 8735 | struct lyxp_expr *tokens = NULL; |
Michal Vasko | e68b540 | 2022-07-29 14:58:15 +0200 | [diff] [blame] | 8736 | uint16_t token_index, name_len; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8737 | |
| 8738 | vars = set->vars; |
| 8739 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8740 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8741 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
Michal Vasko | e68b540 | 2022-07-29 14:58:15 +0200 | [diff] [blame] | 8742 | name_len = exp->tok_len[*tok_idx]; |
| 8743 | ret = lyxp_vars_find((struct lyxp_var *)vars, name, name_len, &var); |
| 8744 | 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] | 8745 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8746 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8747 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8748 | LY_CHECK_GOTO(ret, cleanup); |
| 8749 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8750 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8751 | token_index = 0; |
| 8752 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8753 | LY_CHECK_GOTO(ret, cleanup); |
| 8754 | |
| 8755 | cleanup: |
| 8756 | lyxp_expr_free(set->ctx, tokens); |
| 8757 | |
| 8758 | return ret; |
| 8759 | } |
| 8760 | |
| 8761 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8762 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8763 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8764 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8765 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8766 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8767 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8768 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8769 | * |
| 8770 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8771 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8772 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8773 | * @param[in] options XPath options. |
| 8774 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8775 | */ |
| 8776 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8777 | eval_path_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8778 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8779 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8780 | LY_ERR rc; |
| 8781 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8782 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8783 | case LYXP_TOKEN_PAR1: |
| 8784 | /* '(' Expr ')' */ |
| 8785 | |
| 8786 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8787 | 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] | 8788 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8789 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8790 | |
| 8791 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8792 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8793 | LY_CHECK_RET(rc); |
| 8794 | |
| 8795 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8796 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8797 | 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] | 8798 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8799 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8800 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8801 | goto predicate; |
| 8802 | |
| 8803 | case LYXP_TOKEN_DOT: |
| 8804 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8805 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8806 | case LYXP_TOKEN_AT: |
| 8807 | case LYXP_TOKEN_NAMETEST: |
| 8808 | case LYXP_TOKEN_NODETYPE: |
| 8809 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8810 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8811 | LY_CHECK_RET(rc); |
| 8812 | break; |
| 8813 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8814 | case LYXP_TOKEN_VARREF: |
| 8815 | /* VariableReference */ |
| 8816 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8817 | LY_CHECK_RET(rc); |
| 8818 | ++(*tok_idx); |
| 8819 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8820 | goto predicate; |
| 8821 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8822 | case LYXP_TOKEN_FUNCNAME: |
| 8823 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8824 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8825 | LY_CHECK_RET(rc); |
| 8826 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8827 | goto predicate; |
| 8828 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8829 | case LYXP_TOKEN_OPER_PATH: |
| 8830 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8831 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8832 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8833 | LY_CHECK_RET(rc); |
| 8834 | break; |
| 8835 | |
| 8836 | case LYXP_TOKEN_LITERAL: |
| 8837 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8838 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8839 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8840 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8841 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8842 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8843 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8844 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8845 | } |
| 8846 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8847 | goto predicate; |
| 8848 | |
| 8849 | case LYXP_TOKEN_NUMBER: |
| 8850 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8851 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8852 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8853 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8854 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8855 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8856 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8857 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8858 | } |
| 8859 | LY_CHECK_RET(rc); |
| 8860 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8861 | goto predicate; |
| 8862 | |
| 8863 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8864 | 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] | 8865 | return LY_EVALID; |
| 8866 | } |
| 8867 | |
| 8868 | return LY_SUCCESS; |
| 8869 | |
| 8870 | predicate: |
| 8871 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8872 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8873 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8874 | LY_CHECK_RET(rc); |
| 8875 | } |
| 8876 | |
| 8877 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8878 | 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] | 8879 | |
| 8880 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8881 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8882 | all_desc = 0; |
| 8883 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8884 | all_desc = 1; |
| 8885 | } |
| 8886 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8887 | 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] | 8888 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8889 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8890 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8891 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8892 | LY_CHECK_RET(rc); |
| 8893 | } |
| 8894 | |
| 8895 | return LY_SUCCESS; |
| 8896 | } |
| 8897 | |
| 8898 | /** |
| 8899 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8900 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8901 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8902 | * |
| 8903 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8904 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8905 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8906 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8907 | * @param[in] options XPath options. |
| 8908 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8909 | */ |
| 8910 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8911 | eval_union_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8912 | { |
| 8913 | LY_ERR rc = LY_SUCCESS; |
| 8914 | struct lyxp_set orig_set, set2; |
| 8915 | uint16_t i; |
| 8916 | |
| 8917 | assert(repeat); |
| 8918 | |
| 8919 | set_init(&orig_set, set); |
| 8920 | set_init(&set2, set); |
| 8921 | |
| 8922 | set_fill_set(&orig_set, set); |
| 8923 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8924 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8925 | LY_CHECK_GOTO(rc, cleanup); |
| 8926 | |
| 8927 | /* ('|' PathExpr)* */ |
| 8928 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8929 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8930 | 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] | 8931 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8932 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8933 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8934 | if (options & LYXP_SKIP_EXPR) { |
| 8935 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8936 | LY_CHECK_GOTO(rc, cleanup); |
| 8937 | continue; |
| 8938 | } |
| 8939 | |
| 8940 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8941 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8942 | LY_CHECK_GOTO(rc, cleanup); |
| 8943 | |
| 8944 | /* eval */ |
| 8945 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8946 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8947 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8948 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8949 | LY_CHECK_GOTO(rc, cleanup); |
| 8950 | } |
| 8951 | } |
| 8952 | |
| 8953 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8954 | lyxp_set_free_content(&orig_set); |
| 8955 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8956 | return rc; |
| 8957 | } |
| 8958 | |
| 8959 | /** |
| 8960 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8961 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8962 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8963 | * |
| 8964 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8965 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8966 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8967 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8968 | * @param[in] options XPath options. |
| 8969 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8970 | */ |
| 8971 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8972 | eval_unary_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8973 | { |
| 8974 | LY_ERR rc; |
| 8975 | uint16_t this_op, i; |
| 8976 | |
| 8977 | assert(repeat); |
| 8978 | |
| 8979 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8980 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8981 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8982 | 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] | 8983 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8984 | 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] | 8985 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8986 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8987 | } |
| 8988 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8989 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8990 | LY_CHECK_RET(rc); |
| 8991 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8992 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8993 | if (options & LYXP_SCNODE_ALL) { |
| 8994 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 8995 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8996 | 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] | 8997 | LY_CHECK_RET(rc); |
| 8998 | } |
| 8999 | } |
| 9000 | |
| 9001 | return LY_SUCCESS; |
| 9002 | } |
| 9003 | |
| 9004 | /** |
| 9005 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 9006 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9007 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9008 | * | MultiplicativeExpr '*' UnaryExpr |
| 9009 | * | MultiplicativeExpr 'div' UnaryExpr |
| 9010 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 9011 | * |
| 9012 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9013 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9014 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9015 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9016 | * @param[in] options XPath options. |
| 9017 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9018 | */ |
| 9019 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9020 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 9021 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9022 | { |
| 9023 | LY_ERR rc; |
| 9024 | uint16_t this_op; |
| 9025 | struct lyxp_set orig_set, set2; |
| 9026 | uint16_t i; |
| 9027 | |
| 9028 | assert(repeat); |
| 9029 | |
| 9030 | set_init(&orig_set, set); |
| 9031 | set_init(&set2, set); |
| 9032 | |
| 9033 | set_fill_set(&orig_set, set); |
| 9034 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9035 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9036 | LY_CHECK_GOTO(rc, cleanup); |
| 9037 | |
| 9038 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 9039 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9040 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9041 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9042 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9043 | 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] | 9044 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9045 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9046 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9047 | if (options & LYXP_SKIP_EXPR) { |
| 9048 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9049 | LY_CHECK_GOTO(rc, cleanup); |
| 9050 | continue; |
| 9051 | } |
| 9052 | |
| 9053 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9054 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9055 | LY_CHECK_GOTO(rc, cleanup); |
| 9056 | |
| 9057 | /* eval */ |
| 9058 | if (options & LYXP_SCNODE_ALL) { |
| 9059 | 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] | 9060 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9061 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9062 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9063 | 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] | 9064 | LY_CHECK_GOTO(rc, cleanup); |
| 9065 | } |
| 9066 | } |
| 9067 | |
| 9068 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9069 | lyxp_set_free_content(&orig_set); |
| 9070 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9071 | return rc; |
| 9072 | } |
| 9073 | |
| 9074 | /** |
| 9075 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 9076 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9077 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9078 | * | AdditiveExpr '+' MultiplicativeExpr |
| 9079 | * | AdditiveExpr '-' MultiplicativeExpr |
| 9080 | * |
| 9081 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9082 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9083 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9084 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9085 | * @param[in] options XPath options. |
| 9086 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9087 | */ |
| 9088 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9089 | eval_additive_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9090 | { |
| 9091 | LY_ERR rc; |
| 9092 | uint16_t this_op; |
| 9093 | struct lyxp_set orig_set, set2; |
| 9094 | uint16_t i; |
| 9095 | |
| 9096 | assert(repeat); |
| 9097 | |
| 9098 | set_init(&orig_set, set); |
| 9099 | set_init(&set2, set); |
| 9100 | |
| 9101 | set_fill_set(&orig_set, set); |
| 9102 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9103 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9104 | LY_CHECK_GOTO(rc, cleanup); |
| 9105 | |
| 9106 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 9107 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9108 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9109 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9110 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9111 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9112 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9113 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9114 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9115 | if (options & LYXP_SKIP_EXPR) { |
| 9116 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9117 | LY_CHECK_GOTO(rc, cleanup); |
| 9118 | continue; |
| 9119 | } |
| 9120 | |
| 9121 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9122 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9123 | LY_CHECK_GOTO(rc, cleanup); |
| 9124 | |
| 9125 | /* eval */ |
| 9126 | if (options & LYXP_SCNODE_ALL) { |
| 9127 | 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] | 9128 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9129 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9130 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9131 | 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] | 9132 | LY_CHECK_GOTO(rc, cleanup); |
| 9133 | } |
| 9134 | } |
| 9135 | |
| 9136 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9137 | lyxp_set_free_content(&orig_set); |
| 9138 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9139 | return rc; |
| 9140 | } |
| 9141 | |
| 9142 | /** |
| 9143 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 9144 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9145 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9146 | * | RelationalExpr '<' AdditiveExpr |
| 9147 | * | RelationalExpr '>' AdditiveExpr |
| 9148 | * | RelationalExpr '<=' AdditiveExpr |
| 9149 | * | RelationalExpr '>=' AdditiveExpr |
| 9150 | * |
| 9151 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9152 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9153 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9154 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9155 | * @param[in] options XPath options. |
| 9156 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9157 | */ |
| 9158 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9159 | eval_relational_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9160 | { |
| 9161 | LY_ERR rc; |
| 9162 | uint16_t this_op; |
| 9163 | struct lyxp_set orig_set, set2; |
| 9164 | uint16_t i; |
| 9165 | |
| 9166 | assert(repeat); |
| 9167 | |
| 9168 | set_init(&orig_set, set); |
| 9169 | set_init(&set2, set); |
| 9170 | |
| 9171 | set_fill_set(&orig_set, set); |
| 9172 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9173 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9174 | LY_CHECK_GOTO(rc, cleanup); |
| 9175 | |
| 9176 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 9177 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9178 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9179 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9180 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9181 | 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] | 9182 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9183 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9184 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9185 | if (options & LYXP_SKIP_EXPR) { |
| 9186 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9187 | LY_CHECK_GOTO(rc, cleanup); |
| 9188 | continue; |
| 9189 | } |
| 9190 | |
| 9191 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9192 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9193 | LY_CHECK_GOTO(rc, cleanup); |
| 9194 | |
| 9195 | /* eval */ |
| 9196 | if (options & LYXP_SCNODE_ALL) { |
| 9197 | 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] | 9198 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9199 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9200 | } else { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 9201 | 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] | 9202 | LY_CHECK_GOTO(rc, cleanup); |
| 9203 | } |
| 9204 | } |
| 9205 | |
| 9206 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9207 | lyxp_set_free_content(&orig_set); |
| 9208 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9209 | return rc; |
| 9210 | } |
| 9211 | |
| 9212 | /** |
| 9213 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 9214 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9215 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9216 | * | EqualityExpr '!=' RelationalExpr |
| 9217 | * |
| 9218 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9219 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9220 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9221 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9222 | * @param[in] options XPath options. |
| 9223 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9224 | */ |
| 9225 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9226 | eval_equality_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9227 | { |
| 9228 | LY_ERR rc; |
| 9229 | uint16_t this_op; |
| 9230 | struct lyxp_set orig_set, set2; |
| 9231 | uint16_t i; |
| 9232 | |
| 9233 | assert(repeat); |
| 9234 | |
| 9235 | set_init(&orig_set, set); |
| 9236 | set_init(&set2, set); |
| 9237 | |
| 9238 | set_fill_set(&orig_set, set); |
| 9239 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9240 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9241 | LY_CHECK_GOTO(rc, cleanup); |
| 9242 | |
| 9243 | /* ('=' / '!=' RelationalExpr)* */ |
| 9244 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9245 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9246 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9247 | 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] | 9248 | 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] | 9249 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9250 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9251 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9252 | if (options & LYXP_SKIP_EXPR) { |
| 9253 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9254 | LY_CHECK_GOTO(rc, cleanup); |
| 9255 | continue; |
| 9256 | } |
| 9257 | |
| 9258 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9259 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9260 | LY_CHECK_GOTO(rc, cleanup); |
| 9261 | |
| 9262 | /* eval */ |
| 9263 | if (options & LYXP_SCNODE_ALL) { |
| 9264 | 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] | 9265 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9266 | 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] | 9267 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9268 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9269 | } else { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 9270 | 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] | 9271 | LY_CHECK_GOTO(rc, cleanup); |
| 9272 | } |
| 9273 | } |
| 9274 | |
| 9275 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9276 | lyxp_set_free_content(&orig_set); |
| 9277 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9278 | return rc; |
| 9279 | } |
| 9280 | |
| 9281 | /** |
| 9282 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9283 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9284 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9285 | * |
| 9286 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9287 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9288 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9289 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9290 | * @param[in] options XPath options. |
| 9291 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9292 | */ |
| 9293 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9294 | eval_and_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9295 | { |
| 9296 | LY_ERR rc; |
| 9297 | struct lyxp_set orig_set, set2; |
| 9298 | uint16_t i; |
| 9299 | |
| 9300 | assert(repeat); |
| 9301 | |
| 9302 | set_init(&orig_set, set); |
| 9303 | set_init(&set2, set); |
| 9304 | |
| 9305 | set_fill_set(&orig_set, set); |
| 9306 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9307 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9308 | LY_CHECK_GOTO(rc, cleanup); |
| 9309 | |
| 9310 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9311 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9312 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9313 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9314 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9315 | } |
| 9316 | |
| 9317 | /* ('and' EqualityExpr)* */ |
| 9318 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9319 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9320 | 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] | 9321 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9322 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9323 | |
| 9324 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9325 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9326 | 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] | 9327 | LY_CHECK_GOTO(rc, cleanup); |
| 9328 | continue; |
| 9329 | } |
| 9330 | |
| 9331 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9332 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9333 | LY_CHECK_GOTO(rc, cleanup); |
| 9334 | |
| 9335 | /* eval - just get boolean value actually */ |
| 9336 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9337 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9338 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9339 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9340 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9341 | set_fill_set(set, &set2); |
| 9342 | } |
| 9343 | } |
| 9344 | |
| 9345 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9346 | lyxp_set_free_content(&orig_set); |
| 9347 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9348 | return rc; |
| 9349 | } |
| 9350 | |
| 9351 | /** |
| 9352 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9353 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9354 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9355 | * |
| 9356 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9357 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9358 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9359 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9360 | * @param[in] options XPath options. |
| 9361 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9362 | */ |
| 9363 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9364 | eval_or_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9365 | { |
| 9366 | LY_ERR rc; |
| 9367 | struct lyxp_set orig_set, set2; |
| 9368 | uint16_t i; |
| 9369 | |
| 9370 | assert(repeat); |
| 9371 | |
| 9372 | set_init(&orig_set, set); |
| 9373 | set_init(&set2, set); |
| 9374 | |
| 9375 | set_fill_set(&orig_set, set); |
| 9376 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9377 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9378 | LY_CHECK_GOTO(rc, cleanup); |
| 9379 | |
| 9380 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9381 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9382 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9383 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9384 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9385 | } |
| 9386 | |
| 9387 | /* ('or' AndExpr)* */ |
| 9388 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9389 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9390 | 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] | 9391 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9392 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9393 | |
| 9394 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9395 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9396 | 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] | 9397 | LY_CHECK_GOTO(rc, cleanup); |
| 9398 | continue; |
| 9399 | } |
| 9400 | |
| 9401 | set_fill_set(&set2, &orig_set); |
| 9402 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9403 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9404 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9405 | LY_CHECK_GOTO(rc, cleanup); |
| 9406 | |
| 9407 | /* eval - just get boolean value actually */ |
| 9408 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9409 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9410 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9411 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9412 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9413 | set_fill_set(set, &set2); |
| 9414 | } |
| 9415 | } |
| 9416 | |
| 9417 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9418 | lyxp_set_free_content(&orig_set); |
| 9419 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9420 | return rc; |
| 9421 | } |
| 9422 | |
| 9423 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9424 | * @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] | 9425 | * |
| 9426 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9427 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9428 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9429 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9430 | * @param[in] options XPath options. |
| 9431 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9432 | */ |
| 9433 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9434 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 9435 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9436 | { |
| 9437 | uint16_t i, count; |
| 9438 | enum lyxp_expr_type next_etype; |
| 9439 | LY_ERR rc; |
| 9440 | |
| 9441 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9442 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9443 | next_etype = LYXP_EXPR_NONE; |
| 9444 | } else { |
| 9445 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9446 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9447 | |
| 9448 | /* select one-priority lower because etype expression called us */ |
| 9449 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9450 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9451 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9452 | 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] | 9453 | } else { |
| 9454 | next_etype = LYXP_EXPR_NONE; |
| 9455 | } |
| 9456 | } |
| 9457 | |
| 9458 | /* decide what expression are we parsing based on the repeat */ |
| 9459 | switch (next_etype) { |
| 9460 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9461 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9462 | break; |
| 9463 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9464 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9465 | break; |
| 9466 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9467 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9468 | break; |
| 9469 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9470 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9471 | break; |
| 9472 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9473 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9474 | break; |
| 9475 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9476 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9477 | break; |
| 9478 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9479 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9480 | break; |
| 9481 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9482 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9483 | break; |
| 9484 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9485 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9486 | break; |
| 9487 | default: |
| 9488 | LOGINT_RET(set->ctx); |
| 9489 | } |
| 9490 | |
| 9491 | return rc; |
| 9492 | } |
| 9493 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9494 | /** |
| 9495 | * @brief Get root type. |
| 9496 | * |
| 9497 | * @param[in] ctx_node Context node. |
| 9498 | * @param[in] ctx_scnode Schema context node. |
| 9499 | * @param[in] options XPath options. |
| 9500 | * @return Root type. |
| 9501 | */ |
| 9502 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9503 | 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] | 9504 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9505 | const struct lysc_node *op; |
| 9506 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9507 | /* explicit */ |
| 9508 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9509 | return LYXP_NODE_ROOT; |
| 9510 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9511 | return LYXP_NODE_ROOT_CONFIG; |
| 9512 | } |
| 9513 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9514 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9515 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9516 | 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] | 9517 | |
| 9518 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9519 | /* general root that can access everything */ |
| 9520 | return LYXP_NODE_ROOT; |
| 9521 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9522 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9523 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9524 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9525 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9526 | } |
| 9527 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9528 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9529 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9530 | 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] | 9531 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9532 | if (op || !(options & LYXP_SCHEMA)) { |
| 9533 | /* general root that can access everything */ |
| 9534 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9535 | } 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] | 9536 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9537 | return LYXP_NODE_ROOT_CONFIG; |
| 9538 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9539 | return LYXP_NODE_ROOT; |
| 9540 | } |
| 9541 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9542 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9543 | 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] | 9544 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node, |
| 9545 | 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] | 9546 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9547 | uint16_t tok_idx = 0; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9548 | const struct lysc_node *snode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9549 | LY_ERR rc; |
| 9550 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9551 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9552 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9553 | 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] | 9554 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9555 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9556 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9557 | if (tree) { |
| 9558 | /* adjust the pointer to be the first top-level sibling */ |
| 9559 | while (tree->parent) { |
| 9560 | tree = lyd_parent(tree); |
| 9561 | } |
| 9562 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9563 | |
| 9564 | for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {} |
| 9565 | if (snode) { |
| 9566 | /* unable to evaluate absolute paths */ |
| 9567 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9568 | LYD_NAME(tree)); |
| 9569 | return LY_EINVAL; |
| 9570 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9571 | } |
| 9572 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9573 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9574 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9575 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9576 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9577 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9578 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9579 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9580 | set->cur_node = cur_node; |
| 9581 | for (set->context_op = cur_node ? cur_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9582 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9583 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9584 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9585 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9586 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9587 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9588 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9589 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9590 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9591 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9592 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9593 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9594 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9595 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9596 | } |
| 9597 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9598 | if (set->cur_node) { |
| 9599 | LOG_LOCBACK(0, 1, 0, 0); |
| 9600 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9601 | return rc; |
| 9602 | } |
| 9603 | |
| 9604 | #if 0 |
| 9605 | |
| 9606 | /* full xml printing of set elements, not used currently */ |
| 9607 | |
| 9608 | void |
| 9609 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9610 | { |
| 9611 | uint32_t i; |
| 9612 | char *str_num; |
| 9613 | struct lyout out; |
| 9614 | |
| 9615 | memset(&out, 0, sizeof out); |
| 9616 | |
| 9617 | out.type = LYOUT_STREAM; |
| 9618 | out.method.f = f; |
| 9619 | |
| 9620 | switch (set->type) { |
| 9621 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9622 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9623 | break; |
| 9624 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9625 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9626 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9627 | break; |
| 9628 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9629 | ly_print_(&out, "String XPath set:\n"); |
| 9630 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9631 | break; |
| 9632 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9633 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9634 | |
| 9635 | if (isnan(set->value.num)) { |
| 9636 | str_num = strdup("NaN"); |
| 9637 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9638 | str_num = strdup("0"); |
| 9639 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9640 | str_num = strdup("Infinity"); |
| 9641 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9642 | str_num = strdup("-Infinity"); |
| 9643 | } else if ((long long)set->value.num == set->value.num) { |
| 9644 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9645 | str_num = NULL; |
| 9646 | } |
| 9647 | } else { |
| 9648 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9649 | str_num = NULL; |
| 9650 | } |
| 9651 | } |
| 9652 | if (!str_num) { |
| 9653 | LOGMEM; |
| 9654 | return; |
| 9655 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9656 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9657 | free(str_num); |
| 9658 | break; |
| 9659 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9660 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9661 | |
| 9662 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9663 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9664 | switch (set->node_type[i]) { |
| 9665 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9666 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9667 | break; |
| 9668 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9669 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9670 | break; |
| 9671 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9672 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9673 | break; |
| 9674 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9675 | 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] | 9676 | break; |
| 9677 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9678 | 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] | 9679 | break; |
| 9680 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9681 | 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] | 9682 | break; |
| 9683 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9684 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9685 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9686 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9687 | break; |
| 9688 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9689 | 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] | 9690 | break; |
| 9691 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9692 | 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] | 9693 | break; |
| 9694 | } |
| 9695 | } |
| 9696 | break; |
| 9697 | } |
| 9698 | } |
| 9699 | |
| 9700 | #endif |
| 9701 | |
| 9702 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9703 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9704 | { |
| 9705 | long double num; |
| 9706 | char *str; |
| 9707 | LY_ERR rc; |
| 9708 | |
| 9709 | if (!set || (set->type == target)) { |
| 9710 | return LY_SUCCESS; |
| 9711 | } |
| 9712 | |
| 9713 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9714 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9715 | |
| 9716 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9717 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9718 | return LY_EINVAL; |
| 9719 | } |
| 9720 | |
| 9721 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9722 | 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] | 9723 | switch (set->type) { |
| 9724 | case LYXP_SET_NUMBER: |
| 9725 | if (isnan(set->val.num)) { |
| 9726 | set->val.str = strdup("NaN"); |
| 9727 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9728 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9729 | set->val.str = strdup("0"); |
| 9730 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9731 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9732 | set->val.str = strdup("Infinity"); |
| 9733 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9734 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9735 | set->val.str = strdup("-Infinity"); |
| 9736 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9737 | } else if ((long long)set->val.num == set->val.num) { |
| 9738 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9739 | LOGMEM_RET(set->ctx); |
| 9740 | } |
| 9741 | set->val.str = str; |
| 9742 | } else { |
| 9743 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9744 | LOGMEM_RET(set->ctx); |
| 9745 | } |
| 9746 | set->val.str = str; |
| 9747 | } |
| 9748 | break; |
| 9749 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9750 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9751 | set->val.str = strdup("true"); |
| 9752 | } else { |
| 9753 | set->val.str = strdup("false"); |
| 9754 | } |
| 9755 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9756 | break; |
| 9757 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9758 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9759 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9760 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9761 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9762 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9763 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9764 | set->val.str = str; |
| 9765 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9766 | default: |
| 9767 | LOGINT_RET(set->ctx); |
| 9768 | } |
| 9769 | set->type = LYXP_SET_STRING; |
| 9770 | } |
| 9771 | |
| 9772 | /* to NUMBER */ |
| 9773 | if (target == LYXP_SET_NUMBER) { |
| 9774 | switch (set->type) { |
| 9775 | case LYXP_SET_STRING: |
| 9776 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9777 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9778 | set->val.num = num; |
| 9779 | break; |
| 9780 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9781 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9782 | set->val.num = 1; |
| 9783 | } else { |
| 9784 | set->val.num = 0; |
| 9785 | } |
| 9786 | break; |
| 9787 | default: |
| 9788 | LOGINT_RET(set->ctx); |
| 9789 | } |
| 9790 | set->type = LYXP_SET_NUMBER; |
| 9791 | } |
| 9792 | |
| 9793 | /* to BOOLEAN */ |
| 9794 | if (target == LYXP_SET_BOOLEAN) { |
| 9795 | switch (set->type) { |
| 9796 | case LYXP_SET_NUMBER: |
| 9797 | 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] | 9798 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9799 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9800 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9801 | } |
| 9802 | break; |
| 9803 | case LYXP_SET_STRING: |
| 9804 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9805 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9806 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9807 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9808 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9809 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9810 | } |
| 9811 | break; |
| 9812 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9813 | if (set->used) { |
| 9814 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9815 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9816 | } else { |
| 9817 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9818 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9819 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9820 | break; |
| 9821 | default: |
| 9822 | LOGINT_RET(set->ctx); |
| 9823 | } |
| 9824 | set->type = LYXP_SET_BOOLEAN; |
| 9825 | } |
| 9826 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9827 | return LY_SUCCESS; |
| 9828 | } |
| 9829 | |
| 9830 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9831 | 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] | 9832 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode, |
| 9833 | 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] | 9834 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9835 | LY_ERR ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9836 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9837 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9838 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9839 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9840 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9841 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9842 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9843 | |
| 9844 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9845 | memset(set, 0, sizeof *set); |
| 9846 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9847 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 9848 | 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] | 9849 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9850 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9851 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9852 | set->cur_scnode = cur_scnode; |
| 9853 | for (set->context_op = cur_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9854 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9855 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9856 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9857 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9858 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9859 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9860 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9861 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9862 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9863 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9864 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9865 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9866 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9867 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9868 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9869 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9870 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9871 | { |
| 9872 | if (!path) { |
| 9873 | return NULL; |
| 9874 | } |
| 9875 | |
| 9876 | return path->expr; |
| 9877 | } |