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 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 45 | 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] | 46 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, |
| 47 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 48 | static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 49 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 50 | static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, |
| 51 | 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] | 52 | static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 53 | enum lyxp_axis axis, uint32_t options); |
| 54 | static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 55 | enum lyxp_axis axis, uint32_t options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 56 | |
aPiecek | 96dc1e3 | 2021-10-08 15:45:59 +0200 | [diff] [blame] | 57 | /* Functions are divided into the following basic classes: |
| 58 | * |
| 59 | * (re)parse functions: |
| 60 | * Parse functions parse the expression into |
| 61 | * tokens (syntactic analysis). |
| 62 | * Reparse functions perform semantic analysis |
| 63 | * (do not save the result, just a check) of |
| 64 | * the expression and fill repeat indices. |
| 65 | * |
| 66 | * warn functions: |
| 67 | * Warn functions check specific reasonable conditions for schema XPath |
| 68 | * and print a warning if they are not satisfied. |
| 69 | * |
| 70 | * moveto functions: |
| 71 | * They and only they actually change the context (set). |
| 72 | * |
| 73 | * eval functions: |
| 74 | * They execute a parsed XPath expression on some data subtree. |
| 75 | */ |
| 76 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 77 | /** |
| 78 | * @brief Print the type of an XPath \p set. |
| 79 | * |
| 80 | * @param[in] set Set to use. |
| 81 | * @return Set type string. |
| 82 | */ |
| 83 | static const char * |
| 84 | print_set_type(struct lyxp_set *set) |
| 85 | { |
| 86 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 87 | case LYXP_SET_NODE_SET: |
| 88 | return "node set"; |
| 89 | case LYXP_SET_SCNODE_SET: |
| 90 | return "schema node set"; |
| 91 | case LYXP_SET_BOOLEAN: |
| 92 | return "boolean"; |
| 93 | case LYXP_SET_NUMBER: |
| 94 | return "number"; |
| 95 | case LYXP_SET_STRING: |
| 96 | return "string"; |
| 97 | } |
| 98 | |
| 99 | return NULL; |
| 100 | } |
| 101 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 102 | const char * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 103 | lyxp_token2str(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 104 | { |
| 105 | switch (tok) { |
| 106 | case LYXP_TOKEN_PAR1: |
| 107 | return "("; |
| 108 | case LYXP_TOKEN_PAR2: |
| 109 | return ")"; |
| 110 | case LYXP_TOKEN_BRACK1: |
| 111 | return "["; |
| 112 | case LYXP_TOKEN_BRACK2: |
| 113 | return "]"; |
| 114 | case LYXP_TOKEN_DOT: |
| 115 | return "."; |
| 116 | case LYXP_TOKEN_DDOT: |
| 117 | return ".."; |
| 118 | case LYXP_TOKEN_AT: |
| 119 | return "@"; |
| 120 | case LYXP_TOKEN_COMMA: |
| 121 | return ","; |
| 122 | case LYXP_TOKEN_NAMETEST: |
| 123 | return "NameTest"; |
| 124 | case LYXP_TOKEN_NODETYPE: |
| 125 | return "NodeType"; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 126 | case LYXP_TOKEN_VARREF: |
| 127 | return "VariableReference"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 128 | case LYXP_TOKEN_FUNCNAME: |
| 129 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 130 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 131 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 132 | case LYXP_TOKEN_OPER_EQUAL: |
| 133 | return "Operator(Equal)"; |
| 134 | case LYXP_TOKEN_OPER_NEQUAL: |
| 135 | return "Operator(Non-equal)"; |
| 136 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 137 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 138 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 139 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 140 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 141 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 142 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 143 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 144 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 145 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 146 | case LYXP_TOKEN_LITERAL: |
| 147 | return "Literal"; |
| 148 | case LYXP_TOKEN_NUMBER: |
| 149 | return "Number"; |
| 150 | default: |
| 151 | LOGINT(NULL); |
| 152 | return ""; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 157 | * @brief Transform string into an axis. |
| 158 | * |
| 159 | * @param[in] str String to transform. |
| 160 | * @param[in] str_len Length of @p str. |
| 161 | * @return Transformed axis. |
| 162 | */ |
| 163 | static enum lyxp_axis |
| 164 | str2axis(const char *str, uint16_t str_len) |
| 165 | { |
| 166 | switch (str_len) { |
| 167 | case 4: |
| 168 | assert(!strncmp("self", str, str_len)); |
| 169 | return LYXP_AXIS_SELF; |
| 170 | case 5: |
| 171 | assert(!strncmp("child", str, str_len)); |
| 172 | return LYXP_AXIS_CHILD; |
| 173 | case 6: |
| 174 | assert(!strncmp("parent", str, str_len)); |
| 175 | return LYXP_AXIS_PARENT; |
| 176 | case 8: |
| 177 | assert(!strncmp("ancestor", str, str_len)); |
| 178 | return LYXP_AXIS_ANCESTOR; |
| 179 | case 9: |
| 180 | if (str[0] == 'a') { |
| 181 | assert(!strncmp("attribute", str, str_len)); |
| 182 | return LYXP_AXIS_ATTRIBUTE; |
| 183 | } else if (str[0] == 'f') { |
| 184 | assert(!strncmp("following", str, str_len)); |
| 185 | return LYXP_AXIS_FOLLOWING; |
| 186 | } else { |
| 187 | assert(!strncmp("preceding", str, str_len)); |
| 188 | return LYXP_AXIS_PRECEDING; |
| 189 | } |
| 190 | break; |
| 191 | case 10: |
| 192 | assert(!strncmp("descendant", str, str_len)); |
| 193 | return LYXP_AXIS_DESCENDANT; |
| 194 | case 16: |
| 195 | assert(!strncmp("ancestor-or-self", str, str_len)); |
| 196 | return LYXP_AXIS_ANCESTOR_OR_SELF; |
| 197 | case 17: |
| 198 | if (str[0] == 'f') { |
| 199 | assert(!strncmp("following-sibling", str, str_len)); |
| 200 | return LYXP_AXIS_FOLLOWING_SIBLING; |
| 201 | } else { |
| 202 | assert(!strncmp("preceding-sibling", str, str_len)); |
| 203 | return LYXP_AXIS_PRECEDING_SIBLING; |
| 204 | } |
| 205 | break; |
| 206 | case 18: |
| 207 | assert(!strncmp("descendant-or-self", str, str_len)); |
| 208 | return LYXP_AXIS_DESCENDANT_OR_SELF; |
| 209 | } |
| 210 | |
| 211 | LOGINT(NULL); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 216 | * @brief Print the whole expression \p exp to debug output. |
| 217 | * |
| 218 | * @param[in] exp Expression to use. |
| 219 | */ |
| 220 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 221 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 222 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 223 | #define MSG_BUFFER_SIZE 128 |
| 224 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 225 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 226 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 227 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 232 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 233 | 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] | 234 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 235 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 236 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 237 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 238 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 239 | } |
| 240 | strcat(tmp, ")"); |
| 241 | } |
| 242 | LOGDBG(LY_LDGXPATH, tmp); |
| 243 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 244 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | #ifndef NDEBUG |
| 248 | |
| 249 | /** |
| 250 | * @brief Print XPath set content to debug output. |
| 251 | * |
| 252 | * @param[in] set Set to print. |
| 253 | */ |
| 254 | static void |
| 255 | print_set_debug(struct lyxp_set *set) |
| 256 | { |
| 257 | uint32_t i; |
| 258 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 259 | struct lyxp_set_node *item; |
| 260 | struct lyxp_set_scnode *sitem; |
| 261 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 262 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
| 266 | switch (set->type) { |
| 267 | case LYXP_SET_NODE_SET: |
| 268 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 269 | for (i = 0; i < set->used; ++i) { |
| 270 | item = &set->val.nodes[i]; |
| 271 | |
| 272 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 273 | case LYXP_NODE_NONE: |
| 274 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 275 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 276 | case LYXP_NODE_ROOT: |
| 277 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 278 | break; |
| 279 | case LYXP_NODE_ROOT_CONFIG: |
| 280 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 281 | break; |
| 282 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 283 | 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] | 284 | 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] | 285 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 286 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 287 | 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] | 288 | item->node->schema->name, lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 289 | } else { |
| 290 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 291 | } |
| 292 | break; |
| 293 | case LYXP_NODE_TEXT: |
| 294 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 295 | 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] | 296 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 297 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 298 | 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] | 299 | } |
| 300 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 301 | case LYXP_NODE_META: |
| 302 | 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] | 303 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | } |
| 307 | break; |
| 308 | |
| 309 | case LYXP_SET_SCNODE_SET: |
| 310 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 311 | for (i = 0; i < set->used; ++i) { |
| 312 | sitem = &set->val.scnodes[i]; |
| 313 | |
| 314 | switch (sitem->type) { |
| 315 | case LYXP_NODE_ROOT: |
| 316 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 317 | break; |
| 318 | case LYXP_NODE_ROOT_CONFIG: |
| 319 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 320 | break; |
| 321 | case LYXP_NODE_ELEM: |
| 322 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 323 | break; |
| 324 | default: |
| 325 | LOGINT(NULL); |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | break; |
| 330 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 331 | case LYXP_SET_BOOLEAN: |
| 332 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 333 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 334 | break; |
| 335 | |
| 336 | case LYXP_SET_STRING: |
| 337 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 338 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 339 | break; |
| 340 | |
| 341 | case LYXP_SET_NUMBER: |
| 342 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 343 | |
| 344 | if (isnan(set->val.num)) { |
| 345 | str = strdup("NaN"); |
| 346 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 347 | str = strdup("0"); |
| 348 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 349 | str = strdup("Infinity"); |
| 350 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 351 | str = strdup("-Infinity"); |
| 352 | } else if ((long long)set->val.num == set->val.num) { |
| 353 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 354 | str = NULL; |
| 355 | } |
| 356 | } else { |
| 357 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 358 | str = NULL; |
| 359 | } |
| 360 | } |
| 361 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 362 | |
| 363 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 364 | free(str); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | #endif |
| 369 | |
| 370 | /** |
| 371 | * @brief Realloc the string \p str. |
| 372 | * |
| 373 | * @param[in] ctx libyang context for logging. |
| 374 | * @param[in] needed How much free space is required. |
| 375 | * @param[in,out] str Pointer to the string to use. |
| 376 | * @param[in,out] used Used bytes in \p str. |
| 377 | * @param[in,out] size Allocated bytes in \p str. |
| 378 | * @return LY_ERR |
| 379 | */ |
| 380 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 381 | 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] | 382 | { |
| 383 | if (*size - *used < needed) { |
| 384 | do { |
| 385 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 386 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 387 | return LY_EINVAL; |
| 388 | } |
| 389 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 390 | } while (*size - *used < needed); |
| 391 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 392 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 393 | } |
| 394 | |
| 395 | return LY_SUCCESS; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * @brief Cast nodes recursively to one string @p str. |
| 400 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 401 | * @param[in] node Node to cast, NULL if root. |
| 402 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 403 | * @param[in] indent Current indent. |
| 404 | * @param[in,out] str Resulting string. |
| 405 | * @param[in,out] used Used bytes in @p str. |
| 406 | * @param[in,out] size Allocated bytes in @p str. |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 407 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 408 | */ |
| 409 | static LY_ERR |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 410 | cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint16_t indent, char **str, uint16_t *used, |
| 411 | uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 412 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 413 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 414 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 415 | const struct lyd_node *child; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 416 | enum lyxp_node_type child_type; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 417 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | struct lyd_node_any *any; |
| 419 | LY_ERR rc; |
| 420 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 421 | 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] | 422 | return LY_SUCCESS; |
| 423 | } |
| 424 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 425 | if (!node) { |
| 426 | /* fake container */ |
| 427 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 428 | strcpy(*str + (*used - 1), "\n"); |
| 429 | ++(*used); |
| 430 | |
| 431 | ++indent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 432 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 433 | /* print all the top-level nodes */ |
| 434 | child = NULL; |
| 435 | child_type = 0; |
| 436 | while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) { |
| 437 | LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 438 | } |
| 439 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 440 | /* end fake container */ |
| 441 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 442 | strcpy(*str + (*used - 1), "\n"); |
| 443 | ++(*used); |
| 444 | |
| 445 | --indent; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 446 | } else { |
| 447 | switch (node->schema->nodetype) { |
| 448 | case LYS_CONTAINER: |
| 449 | case LYS_LIST: |
| 450 | case LYS_RPC: |
| 451 | case LYS_NOTIF: |
| 452 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
| 453 | strcpy(*str + (*used - 1), "\n"); |
| 454 | ++(*used); |
| 455 | |
| 456 | for (child = lyd_child(node); child; child = child->next) { |
| 457 | LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size)); |
| 458 | } |
| 459 | |
| 460 | break; |
| 461 | |
| 462 | case LYS_LEAF: |
| 463 | case LYS_LEAFLIST: |
| 464 | value_str = lyd_get_value(node); |
| 465 | |
| 466 | /* print indent */ |
| 467 | LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size)); |
| 468 | memset(*str + (*used - 1), ' ', indent * 2); |
| 469 | *used += indent * 2; |
| 470 | |
| 471 | /* print value */ |
| 472 | if (*used == 1) { |
| 473 | sprintf(*str + (*used - 1), "%s", value_str); |
| 474 | *used += strlen(value_str); |
| 475 | } else { |
| 476 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 477 | *used += strlen(value_str) + 1; |
| 478 | } |
| 479 | |
| 480 | break; |
| 481 | |
| 482 | case LYS_ANYXML: |
| 483 | case LYS_ANYDATA: |
| 484 | any = (struct lyd_node_any *)node; |
| 485 | if (!(void *)any->value.tree) { |
| 486 | /* no content */ |
| 487 | buf = strdup(""); |
| 488 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 489 | } else { |
| 490 | struct ly_out *out; |
| 491 | |
| 492 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 493 | /* try to parse it into a data tree */ |
| 494 | if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB, |
| 495 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
| 496 | /* successfully parsed */ |
| 497 | free(any->value.mem); |
| 498 | any->value.tree = tree; |
| 499 | any->value_type = LYD_ANYDATA_DATATREE; |
| 500 | } |
| 501 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
| 502 | } |
| 503 | |
| 504 | switch (any->value_type) { |
| 505 | case LYD_ANYDATA_STRING: |
| 506 | case LYD_ANYDATA_XML: |
| 507 | case LYD_ANYDATA_JSON: |
| 508 | buf = strdup(any->value.json); |
| 509 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 510 | break; |
| 511 | case LYD_ANYDATA_DATATREE: |
| 512 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
| 513 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
| 514 | ly_out_free(out, NULL, 0); |
| 515 | LY_CHECK_RET(rc < 0, -rc); |
| 516 | break; |
| 517 | case LYD_ANYDATA_LYB: |
| 518 | LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 519 | return LY_EINVAL; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | line = strtok_r(buf, "\n", &ptr); |
| 524 | do { |
| 525 | rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size); |
| 526 | if (rc != LY_SUCCESS) { |
| 527 | free(buf); |
| 528 | return rc; |
| 529 | } |
| 530 | memset(*str + (*used - 1), ' ', indent * 2); |
| 531 | *used += indent * 2; |
| 532 | |
| 533 | strcpy(*str + (*used - 1), line); |
| 534 | *used += strlen(line); |
| 535 | |
| 536 | strcpy(*str + (*used - 1), "\n"); |
| 537 | *used += 1; |
| 538 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 539 | |
| 540 | free(buf); |
| 541 | break; |
| 542 | |
| 543 | default: |
| 544 | LOGINT_RET(set->ctx); |
| 545 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | return LY_SUCCESS; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * @brief Cast an element into a string. |
| 553 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 554 | * @param[in] node Node to cast, NULL if root. |
| 555 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 556 | * @param[out] str Element cast to dynamically-allocated string. |
| 557 | * @return LY_ERR |
| 558 | */ |
| 559 | static LY_ERR |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 560 | 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] | 561 | { |
| 562 | uint16_t used, size; |
| 563 | LY_ERR rc; |
| 564 | |
| 565 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 566 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 567 | (*str)[0] = '\0'; |
| 568 | used = 1; |
| 569 | size = LYXP_STRING_CAST_SIZE_START; |
| 570 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 571 | rc = cast_string_recursive(node, set, 0, str, &used, &size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 572 | if (rc != LY_SUCCESS) { |
| 573 | free(*str); |
| 574 | return rc; |
| 575 | } |
| 576 | |
| 577 | if (size > used) { |
| 578 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 579 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 580 | } |
| 581 | return LY_SUCCESS; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 586 | * Context position aware. |
| 587 | * |
| 588 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 589 | * @param[out] str Cast dynamically-allocated string. |
| 590 | * @return LY_ERR |
| 591 | */ |
| 592 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 593 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 594 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 595 | if (!set->used) { |
| 596 | *str = strdup(""); |
| 597 | if (!*str) { |
| 598 | LOGMEM_RET(set->ctx); |
| 599 | } |
| 600 | return LY_SUCCESS; |
| 601 | } |
| 602 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 603 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 604 | case LYXP_NODE_NONE: |
| 605 | /* invalid */ |
| 606 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 607 | case LYXP_NODE_ROOT: |
| 608 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 609 | case LYXP_NODE_ELEM: |
| 610 | case LYXP_NODE_TEXT: |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame^] | 611 | return cast_string_elem(set->val.nodes[0].node, set, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 612 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 613 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 614 | if (!*str) { |
| 615 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 616 | } |
| 617 | return LY_SUCCESS; |
| 618 | } |
| 619 | |
| 620 | LOGINT_RET(set->ctx); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * @brief Cast a string into an XPath number. |
| 625 | * |
| 626 | * @param[in] str String to use. |
| 627 | * @return Cast number. |
| 628 | */ |
| 629 | static long double |
| 630 | cast_string_to_number(const char *str) |
| 631 | { |
| 632 | long double num; |
| 633 | char *ptr; |
| 634 | |
| 635 | errno = 0; |
| 636 | num = strtold(str, &ptr); |
| 637 | if (errno || *ptr) { |
| 638 | num = NAN; |
| 639 | } |
| 640 | return num; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * @brief Callback for checking value equality. |
| 645 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 646 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 647 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 648 | * @param[in] val1_p First value. |
| 649 | * @param[in] val2_p Second value. |
| 650 | * @param[in] mod Whether hash table is being modified. |
| 651 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 652 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 653 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 654 | static ly_bool |
| 655 | 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] | 656 | { |
| 657 | struct lyxp_set_hash_node *val1, *val2; |
| 658 | |
| 659 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 660 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 661 | |
| 662 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 663 | return 1; |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * @brief Insert node and its hash into set. |
| 671 | * |
| 672 | * @param[in] set et to insert to. |
| 673 | * @param[in] node Node with hash. |
| 674 | * @param[in] type Node type. |
| 675 | */ |
| 676 | static void |
| 677 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 678 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 679 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 680 | uint32_t i, hash; |
| 681 | struct lyxp_set_hash_node hnode; |
| 682 | |
| 683 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 684 | /* create hash table and add all the nodes */ |
| 685 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 686 | for (i = 0; i < set->used; ++i) { |
| 687 | hnode.node = set->val.nodes[i].node; |
| 688 | hnode.type = set->val.nodes[i].type; |
| 689 | |
| 690 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 691 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 692 | hash = dict_hash_multi(hash, NULL, 0); |
| 693 | |
| 694 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 695 | assert(!r); |
| 696 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 697 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 698 | if (hnode.node == node) { |
| 699 | /* it was just added, do not add it twice */ |
| 700 | node = NULL; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 706 | /* add the new node into hash table */ |
| 707 | hnode.node = node; |
| 708 | hnode.type = type; |
| 709 | |
| 710 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 711 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 712 | hash = dict_hash_multi(hash, NULL, 0); |
| 713 | |
| 714 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 715 | assert(!r); |
| 716 | (void)r; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * @brief Remove node and its hash from set. |
| 722 | * |
| 723 | * @param[in] set Set to remove from. |
| 724 | * @param[in] node Node to remove. |
| 725 | * @param[in] type Node type. |
| 726 | */ |
| 727 | static void |
| 728 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 729 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 730 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 731 | struct lyxp_set_hash_node hnode; |
| 732 | uint32_t hash; |
| 733 | |
| 734 | if (set->ht) { |
| 735 | hnode.node = node; |
| 736 | hnode.type = type; |
| 737 | |
| 738 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 739 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 740 | hash = dict_hash_multi(hash, NULL, 0); |
| 741 | |
| 742 | r = lyht_remove(set->ht, &hnode, hash); |
| 743 | assert(!r); |
| 744 | (void)r; |
| 745 | |
| 746 | if (!set->ht->used) { |
| 747 | lyht_free(set->ht); |
| 748 | set->ht = NULL; |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * @brief Check whether node is in set based on its hash. |
| 755 | * |
| 756 | * @param[in] set Set to search in. |
| 757 | * @param[in] node Node to search for. |
| 758 | * @param[in] type Node type. |
| 759 | * @param[in] skip_idx Index in @p set to skip. |
| 760 | * @return LY_ERR |
| 761 | */ |
| 762 | static LY_ERR |
| 763 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 764 | { |
| 765 | struct lyxp_set_hash_node hnode, *match_p; |
| 766 | uint32_t hash; |
| 767 | |
| 768 | hnode.node = node; |
| 769 | hnode.type = type; |
| 770 | |
| 771 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 772 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 773 | hash = dict_hash_multi(hash, NULL, 0); |
| 774 | |
| 775 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 776 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 777 | /* we found it on the index that should be skipped, find another */ |
| 778 | hnode = *match_p; |
| 779 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 780 | /* none other found */ |
| 781 | return LY_SUCCESS; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | return LY_EEXIST; |
| 786 | } |
| 787 | |
| 788 | /* not found */ |
| 789 | return LY_SUCCESS; |
| 790 | } |
| 791 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 792 | void |
| 793 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 794 | { |
| 795 | if (!set) { |
| 796 | return; |
| 797 | } |
| 798 | |
| 799 | if (set->type == LYXP_SET_NODE_SET) { |
| 800 | free(set->val.nodes); |
| 801 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 802 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 803 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 804 | lyht_free(set->ht); |
| 805 | } else { |
| 806 | if (set->type == LYXP_SET_STRING) { |
| 807 | free(set->val.str); |
| 808 | } |
| 809 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 810 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 811 | |
| 812 | set->val.nodes = NULL; |
| 813 | set->used = 0; |
| 814 | set->size = 0; |
| 815 | set->ht = NULL; |
| 816 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 817 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 818 | } |
| 819 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 820 | /** |
| 821 | * @brief Free dynamically-allocated set. |
| 822 | * |
| 823 | * @param[in] set Set to free. |
| 824 | */ |
| 825 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 826 | lyxp_set_free(struct lyxp_set *set) |
| 827 | { |
| 828 | if (!set) { |
| 829 | return; |
| 830 | } |
| 831 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 832 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 833 | free(set); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * @brief Initialize set context. |
| 838 | * |
| 839 | * @param[in] new Set to initialize. |
| 840 | * @param[in] set Arbitrary initialized set. |
| 841 | */ |
| 842 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 843 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 844 | { |
| 845 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 846 | if (set) { |
| 847 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 848 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 849 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 850 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 851 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 852 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 853 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 854 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 855 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 856 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /** |
| 860 | * @brief Create a deep copy of a set. |
| 861 | * |
| 862 | * @param[in] set Set to copy. |
| 863 | * @return Copy of @p set. |
| 864 | */ |
| 865 | static struct lyxp_set * |
| 866 | set_copy(struct lyxp_set *set) |
| 867 | { |
| 868 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 869 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 870 | |
| 871 | if (!set) { |
| 872 | return NULL; |
| 873 | } |
| 874 | |
| 875 | ret = malloc(sizeof *ret); |
| 876 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 877 | set_init(ret, set); |
| 878 | |
| 879 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 880 | ret->type = set->type; |
| 881 | |
| 882 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 883 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 884 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 885 | uint32_t idx; |
| 886 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 887 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 888 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 889 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 890 | lyxp_set_free(ret); |
| 891 | return NULL; |
| 892 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 893 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 897 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 898 | if (set->used) { |
| 899 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 900 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 901 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 902 | } else { |
| 903 | ret->val.nodes = NULL; |
| 904 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 905 | |
| 906 | ret->used = ret->size = set->used; |
| 907 | ret->ctx_pos = set->ctx_pos; |
| 908 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 909 | if (set->ht) { |
| 910 | ret->ht = lyht_dup(set->ht); |
| 911 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 912 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 913 | memcpy(ret, set, sizeof *ret); |
| 914 | if (set->type == LYXP_SET_STRING) { |
| 915 | ret->val.str = strdup(set->val.str); |
| 916 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 917 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 925 | * |
| 926 | * @param[in] set Set to fill. |
| 927 | * @param[in] string String to fill into \p set. |
| 928 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 929 | */ |
| 930 | static void |
| 931 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 932 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 933 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 934 | |
| 935 | set->type = LYXP_SET_STRING; |
| 936 | if ((str_len == 0) && (string[0] != '\0')) { |
| 937 | string = ""; |
| 938 | } |
| 939 | set->val.str = strndup(string, str_len); |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 944 | * |
| 945 | * @param[in] set Set to fill. |
| 946 | * @param[in] number Number to fill into \p set. |
| 947 | */ |
| 948 | static void |
| 949 | set_fill_number(struct lyxp_set *set, long double number) |
| 950 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 951 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 952 | |
| 953 | set->type = LYXP_SET_NUMBER; |
| 954 | set->val.num = number; |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 959 | * |
| 960 | * @param[in] set Set to fill. |
| 961 | * @param[in] boolean Boolean to fill into \p set. |
| 962 | */ |
| 963 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 964 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 965 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 966 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 967 | |
| 968 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 969 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /** |
| 973 | * @brief Fill XPath set with the value from another set (deep assign). |
| 974 | * Any current data are disposed of. |
| 975 | * |
| 976 | * @param[in] trg Set to fill. |
| 977 | * @param[in] src Source set to copy into \p trg. |
| 978 | */ |
| 979 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 980 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 981 | { |
| 982 | if (!trg || !src) { |
| 983 | return; |
| 984 | } |
| 985 | |
| 986 | if (trg->type == LYXP_SET_NODE_SET) { |
| 987 | free(trg->val.nodes); |
| 988 | } else if (trg->type == LYXP_SET_STRING) { |
| 989 | free(trg->val.str); |
| 990 | } |
| 991 | set_init(trg, src); |
| 992 | |
| 993 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 994 | trg->type = LYXP_SET_SCNODE_SET; |
| 995 | trg->used = src->used; |
| 996 | trg->size = src->used; |
| 997 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 998 | if (trg->size) { |
| 999 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 1000 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1001 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 1002 | } else { |
| 1003 | trg->val.scnodes = NULL; |
| 1004 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1005 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1006 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1007 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1008 | set_fill_number(trg, src->val.num); |
| 1009 | } else if (src->type == LYXP_SET_STRING) { |
| 1010 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 1011 | } else { |
| 1012 | if (trg->type == LYXP_SET_NODE_SET) { |
| 1013 | free(trg->val.nodes); |
| 1014 | } else if (trg->type == LYXP_SET_STRING) { |
| 1015 | free(trg->val.str); |
| 1016 | } |
| 1017 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1018 | assert(src->type == LYXP_SET_NODE_SET); |
| 1019 | |
| 1020 | trg->type = LYXP_SET_NODE_SET; |
| 1021 | trg->used = src->used; |
| 1022 | trg->size = src->used; |
| 1023 | trg->ctx_pos = src->ctx_pos; |
| 1024 | trg->ctx_size = src->ctx_size; |
| 1025 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1026 | if (trg->size) { |
| 1027 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 1028 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1029 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 1030 | } else { |
| 1031 | trg->val.nodes = NULL; |
| 1032 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1033 | if (src->ht) { |
| 1034 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1035 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1036 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * @brief Clear context of all schema nodes. |
| 1043 | * |
| 1044 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1045 | * @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] | 1046 | */ |
| 1047 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1048 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1049 | { |
| 1050 | uint32_t i; |
| 1051 | |
| 1052 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1053 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1054 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1055 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 1056 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * @brief Remove a node from a set. Removing last node changes |
| 1063 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1064 | * |
| 1065 | * @param[in] set Set to use. |
| 1066 | * @param[in] idx Index from @p set of the node to be removed. |
| 1067 | */ |
| 1068 | static void |
| 1069 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1070 | { |
| 1071 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1072 | assert(idx < set->used); |
| 1073 | |
| 1074 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1075 | |
| 1076 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1077 | if (idx < set->used) { |
| 1078 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1079 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1080 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1085 | * @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] | 1086 | * |
| 1087 | * @param[in] set Set to use. |
| 1088 | * @param[in] idx Index from @p set of the node to be removed. |
| 1089 | */ |
| 1090 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1091 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1092 | { |
| 1093 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1094 | assert(idx < set->used); |
| 1095 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1096 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1097 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1098 | } |
| 1099 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1103 | * @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] | 1104 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1105 | * |
| 1106 | * @param[in] set Set to consolidate. |
| 1107 | */ |
| 1108 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1109 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1110 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1111 | uint32_t i, orig_used, end = 0; |
| 1112 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1113 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1114 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1115 | |
| 1116 | orig_used = set->used; |
| 1117 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1118 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1119 | start = -1; |
| 1120 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1121 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1122 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1123 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1124 | end = i; |
| 1125 | ++i; |
| 1126 | break; |
| 1127 | } |
| 1128 | |
| 1129 | ++i; |
| 1130 | if (i == orig_used) { |
| 1131 | end = i; |
| 1132 | } |
| 1133 | } while (i < orig_used); |
| 1134 | |
| 1135 | if (start > -1) { |
| 1136 | /* move the whole chunk of valid nodes together */ |
| 1137 | if (set->used != (unsigned)start) { |
| 1138 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1139 | } |
| 1140 | set->used += end - start; |
| 1141 | } |
| 1142 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1146 | * @brief Check for duplicates in a node set. |
| 1147 | * |
| 1148 | * @param[in] set Set to check. |
| 1149 | * @param[in] node Node to look for in @p set. |
| 1150 | * @param[in] node_type Type of @p node. |
| 1151 | * @param[in] skip_idx Index from @p set to skip. |
| 1152 | * @return LY_ERR |
| 1153 | */ |
| 1154 | static LY_ERR |
| 1155 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1156 | { |
| 1157 | uint32_t i; |
| 1158 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1159 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1160 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1161 | } |
| 1162 | |
| 1163 | for (i = 0; i < set->used; ++i) { |
| 1164 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1165 | continue; |
| 1166 | } |
| 1167 | |
| 1168 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1169 | return LY_EEXIST; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | return LY_SUCCESS; |
| 1174 | } |
| 1175 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1176 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1177 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1178 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1179 | { |
| 1180 | uint32_t i; |
| 1181 | |
| 1182 | for (i = 0; i < set->used; ++i) { |
| 1183 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1184 | continue; |
| 1185 | } |
| 1186 | |
| 1187 | 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] | 1188 | if (index_p) { |
| 1189 | *index_p = i; |
| 1190 | } |
| 1191 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1195 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1198 | void |
| 1199 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1200 | { |
| 1201 | uint32_t orig_used, i, j; |
| 1202 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1203 | 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] | 1204 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1205 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1206 | return; |
| 1207 | } |
| 1208 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1209 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1210 | /* release hidden allocated data (lyxp_set.size) */ |
| 1211 | lyxp_set_free_content(set1); |
| 1212 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1213 | memcpy(set1, set2, sizeof *set1); |
| 1214 | return; |
| 1215 | } |
| 1216 | |
| 1217 | if (set1->used + set2->used > set1->size) { |
| 1218 | set1->size = set1->used + set2->used; |
| 1219 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1220 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1221 | } |
| 1222 | |
| 1223 | orig_used = set1->used; |
| 1224 | |
| 1225 | for (i = 0; i < set2->used; ++i) { |
| 1226 | for (j = 0; j < orig_used; ++j) { |
| 1227 | /* detect duplicities */ |
| 1228 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1233 | if (j < orig_used) { |
| 1234 | /* node is there, but update its status if needed */ |
| 1235 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1236 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1237 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1238 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1239 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1240 | } |
| 1241 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1242 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1243 | ++set1->used; |
| 1244 | } |
| 1245 | } |
| 1246 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1247 | lyxp_set_free_content(set2); |
| 1248 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * @brief Insert a node into a set. Context position aware. |
| 1253 | * |
| 1254 | * @param[in] set Set to use. |
| 1255 | * @param[in] node Node to insert to @p set. |
| 1256 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1257 | * @param[in] node_type Node type of @p node. |
| 1258 | * @param[in] idx Index in @p set to insert into. |
| 1259 | */ |
| 1260 | static void |
| 1261 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1262 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1263 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1264 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1265 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1266 | /* first item */ |
| 1267 | if (idx) { |
| 1268 | /* no real harm done, but it is a bug */ |
| 1269 | LOGINT(set->ctx); |
| 1270 | idx = 0; |
| 1271 | } |
| 1272 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1273 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1274 | set->type = LYXP_SET_NODE_SET; |
| 1275 | set->used = 0; |
| 1276 | set->size = LYXP_SET_SIZE_START; |
| 1277 | set->ctx_pos = 1; |
| 1278 | set->ctx_size = 1; |
| 1279 | set->ht = NULL; |
| 1280 | } else { |
| 1281 | /* not an empty set */ |
| 1282 | if (set->used == set->size) { |
| 1283 | |
| 1284 | /* set is full */ |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1285 | 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] | 1286 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1287 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | if (idx > set->used) { |
| 1291 | LOGINT(set->ctx); |
| 1292 | idx = set->used; |
| 1293 | } |
| 1294 | |
| 1295 | /* make space for the new node */ |
| 1296 | if (idx < set->used) { |
| 1297 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | /* finally assign the value */ |
| 1302 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1303 | set->val.nodes[idx].type = node_type; |
| 1304 | set->val.nodes[idx].pos = pos; |
| 1305 | ++set->used; |
| 1306 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1307 | /* add into hash table */ |
| 1308 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1311 | LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1312 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
| 1313 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1314 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1315 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1316 | |
| 1317 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1318 | |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1319 | if (!set->size) { |
| 1320 | /* first item */ |
| 1321 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
| 1322 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
| 1323 | set->type = LYXP_SET_SCNODE_SET; |
| 1324 | set->used = 0; |
| 1325 | set->size = LYXP_SET_SIZE_START; |
| 1326 | set->ctx_pos = 1; |
| 1327 | set->ctx_size = 1; |
| 1328 | set->ht = NULL; |
| 1329 | } |
| 1330 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1331 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1332 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1333 | } else { |
| 1334 | if (set->used == set->size) { |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1335 | 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] | 1336 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1337 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1340 | index = set->used; |
| 1341 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1342 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1343 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1344 | ++set->used; |
| 1345 | } |
| 1346 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1347 | if (index_p) { |
| 1348 | *index_p = index; |
| 1349 | } |
| 1350 | |
| 1351 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1355 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1356 | * |
| 1357 | * @param[in] set Set to modify. |
| 1358 | * @return New context value. |
| 1359 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1360 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1361 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1362 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1363 | uint32_t i; |
| 1364 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1365 | |
| 1366 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1367 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1368 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1369 | retry: |
| 1370 | for (i = 0; i < set->used; ++i) { |
| 1371 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1372 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1373 | goto retry; |
| 1374 | } |
| 1375 | } |
| 1376 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1377 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1378 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | return ret_ctx; |
| 1383 | } |
| 1384 | |
| 1385 | /** |
| 1386 | * @brief Get unique @p node position in the data. |
| 1387 | * |
| 1388 | * @param[in] node Node to find. |
| 1389 | * @param[in] node_type Node type of @p node. |
| 1390 | * @param[in] root Root node. |
| 1391 | * @param[in] root_type Type of the XPath @p root node. |
| 1392 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1393 | * be used to increase efficiency and start the DFS from this node. |
| 1394 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1395 | * @return Node position. |
| 1396 | */ |
| 1397 | static uint32_t |
| 1398 | 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] | 1399 | 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] | 1400 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1401 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1402 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1403 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1404 | |
| 1405 | assert(prev && prev_pos && !root->prev->next); |
| 1406 | |
| 1407 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1408 | return 0; |
| 1409 | } |
| 1410 | |
| 1411 | if (*prev) { |
| 1412 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1413 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1414 | 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] | 1415 | goto dfs_search; |
| 1416 | } |
| 1417 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1418 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1419 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1420 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1421 | LYD_TREE_DFS_continue = 0; |
| 1422 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1423 | if (*prev && !elem) { |
| 1424 | /* resume previous DFS */ |
| 1425 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1426 | LYD_TREE_DFS_continue = 0; |
| 1427 | } |
| 1428 | |
Michal Vasko | 482a682 | 2022-05-06 08:56:12 +0200 | [diff] [blame] | 1429 | 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] | 1430 | /* skip */ |
| 1431 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1432 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1433 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1434 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1435 | break; |
| 1436 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1437 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1438 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1439 | |
| 1440 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1444 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1445 | break; |
| 1446 | } |
| 1447 | } |
| 1448 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1449 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1450 | if (!(*prev)) { |
| 1451 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1452 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1453 | return 0; |
| 1454 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1455 | /* start the search again from the beginning */ |
| 1456 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1457 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1458 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1459 | pos = 1; |
| 1460 | goto dfs_search; |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | /* remember the last found node for next time */ |
| 1465 | *prev = node; |
| 1466 | *prev_pos = pos; |
| 1467 | |
| 1468 | return pos; |
| 1469 | } |
| 1470 | |
| 1471 | /** |
| 1472 | * @brief Assign (fill) missing node positions. |
| 1473 | * |
| 1474 | * @param[in] set Set to fill positions in. |
| 1475 | * @param[in] root Context root node. |
| 1476 | * @param[in] root_type Context root type. |
| 1477 | * @return LY_ERR |
| 1478 | */ |
| 1479 | static LY_ERR |
| 1480 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1481 | { |
| 1482 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1483 | uint32_t i, tmp_pos = 0; |
| 1484 | |
| 1485 | for (i = 0; i < set->used; ++i) { |
| 1486 | if (!set->val.nodes[i].pos) { |
| 1487 | tmp_node = NULL; |
| 1488 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1489 | case LYXP_NODE_META: |
| 1490 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1491 | if (!tmp_node) { |
| 1492 | LOGINT_RET(root->schema->module->ctx); |
| 1493 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1494 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1495 | case LYXP_NODE_ELEM: |
| 1496 | case LYXP_NODE_TEXT: |
| 1497 | if (!tmp_node) { |
| 1498 | tmp_node = set->val.nodes[i].node; |
| 1499 | } |
| 1500 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1501 | break; |
| 1502 | default: |
| 1503 | /* all roots have position 0 */ |
| 1504 | break; |
| 1505 | } |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | return LY_SUCCESS; |
| 1510 | } |
| 1511 | |
| 1512 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1513 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1514 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1515 | * @param[in] meta Metadata to use. |
| 1516 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1517 | */ |
| 1518 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1519 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1520 | { |
| 1521 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1522 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1523 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1524 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1525 | ++pos; |
| 1526 | } |
| 1527 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1528 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1529 | return pos; |
| 1530 | } |
| 1531 | |
| 1532 | /** |
| 1533 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1534 | * |
| 1535 | * @param[in] item1 1st node. |
| 1536 | * @param[in] item2 2nd node. |
| 1537 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1538 | */ |
| 1539 | static int |
| 1540 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1541 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1542 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1543 | |
| 1544 | if (item1->pos < item2->pos) { |
| 1545 | return -1; |
| 1546 | } |
| 1547 | |
| 1548 | if (item1->pos > item2->pos) { |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
| 1552 | /* node positions are equal, the fun case */ |
| 1553 | |
| 1554 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1555 | /* special case since text nodes are actually saved as their parents */ |
| 1556 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1557 | if (item1->type == LYXP_NODE_ELEM) { |
| 1558 | assert(item2->type == LYXP_NODE_TEXT); |
| 1559 | return -1; |
| 1560 | } else { |
| 1561 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1562 | return 1; |
| 1563 | } |
| 1564 | } |
| 1565 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1566 | /* we need meta positions now */ |
| 1567 | if (item1->type == LYXP_NODE_META) { |
| 1568 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1569 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1570 | if (item2->type == LYXP_NODE_META) { |
| 1571 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1572 | } |
| 1573 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1574 | /* 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] | 1575 | /* check for duplicates */ |
| 1576 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1577 | 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] | 1578 | return 0; |
| 1579 | } |
| 1580 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1581 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1582 | /* elem is always first, 2nd node is after it */ |
| 1583 | if (item1->type == LYXP_NODE_ELEM) { |
| 1584 | assert(item2->type != LYXP_NODE_ELEM); |
| 1585 | return -1; |
| 1586 | } |
| 1587 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1588 | /* 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] | 1589 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1590 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1591 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1592 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1593 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1594 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1595 | return 1; |
| 1596 | } |
| 1597 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1598 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1599 | /* 2nd is after 1st */ |
| 1600 | return -1; |
| 1601 | } |
| 1602 | |
| 1603 | /** |
| 1604 | * @brief Set cast for comparisons. |
| 1605 | * |
| 1606 | * @param[in] trg Target set to cast source into. |
| 1607 | * @param[in] src Source set. |
| 1608 | * @param[in] type Target set type. |
| 1609 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1610 | * @return LY_ERR |
| 1611 | */ |
| 1612 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1613 | set_comp_cast(struct lyxp_set *trg, 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] | 1614 | { |
| 1615 | assert(src->type == LYXP_SET_NODE_SET); |
| 1616 | |
| 1617 | set_init(trg, src); |
| 1618 | |
| 1619 | /* insert node into target set */ |
| 1620 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1621 | |
| 1622 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1623 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1626 | /** |
| 1627 | * @brief Set content canonization for comparisons. |
| 1628 | * |
| 1629 | * @param[in] trg Target set to put the canononized source into. |
| 1630 | * @param[in] src Source set. |
| 1631 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1632 | * @return LY_ERR |
| 1633 | */ |
| 1634 | static LY_ERR |
| 1635 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1636 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1637 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1638 | struct lyd_value val; |
| 1639 | struct ly_err_item *err = NULL; |
| 1640 | char *str, *ptr; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1641 | LY_ERR rc; |
| 1642 | |
| 1643 | /* is there anything to canonize even? */ |
| 1644 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1645 | /* do we have a type to use for canonization? */ |
| 1646 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1647 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1648 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1649 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1650 | } |
| 1651 | } |
| 1652 | if (!type) { |
| 1653 | goto fill; |
| 1654 | } |
| 1655 | |
| 1656 | if (src->type == LYXP_SET_NUMBER) { |
| 1657 | /* canonize number */ |
| 1658 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1659 | LOGMEM(src->ctx); |
| 1660 | return LY_EMEM; |
| 1661 | } |
| 1662 | } else { |
| 1663 | /* canonize string */ |
| 1664 | str = strdup(src->val.str); |
| 1665 | } |
| 1666 | |
| 1667 | /* ignore errors, the value may not satisfy schema constraints */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 1668 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1669 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1670 | ly_err_free(err); |
| 1671 | if (rc) { |
| 1672 | /* invalid value */ |
Radek Iša | 4846022 | 2021-03-02 15:18:32 +0100 | [diff] [blame] | 1673 | /* function store automaticaly dealloc value when fail */ |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1674 | goto fill; |
| 1675 | } |
| 1676 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1677 | /* use the canonized value */ |
| 1678 | set_init(trg, src); |
| 1679 | trg->type = src->type; |
| 1680 | if (src->type == LYXP_SET_NUMBER) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1681 | trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1682 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1683 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1684 | trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL)); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1685 | } |
| 1686 | type->plugin->free(src->ctx, &val); |
| 1687 | return LY_SUCCESS; |
| 1688 | |
| 1689 | fill: |
| 1690 | /* no canonization needed/possible */ |
| 1691 | set_fill_set(trg, src); |
| 1692 | return LY_SUCCESS; |
| 1693 | } |
| 1694 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1695 | /** |
| 1696 | * @brief Bubble sort @p set into XPath document order. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1697 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1698 | * |
| 1699 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1700 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1701 | */ |
| 1702 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1703 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1704 | { |
| 1705 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1706 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1707 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1708 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1709 | struct lyxp_set_node item; |
| 1710 | struct lyxp_set_hash_node hnode; |
| 1711 | uint64_t hash; |
| 1712 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1713 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1714 | return 0; |
| 1715 | } |
| 1716 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1717 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1718 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1719 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1720 | |
| 1721 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1722 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1723 | return -1; |
| 1724 | } |
| 1725 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1726 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1727 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1728 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1729 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1730 | |
| 1731 | for (i = 0; i < set->used; ++i) { |
| 1732 | inverted = 0; |
| 1733 | change = 0; |
| 1734 | |
| 1735 | for (j = 1; j < set->used - i; ++j) { |
| 1736 | /* compare node positions */ |
| 1737 | if (inverted) { |
| 1738 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1739 | } else { |
| 1740 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1741 | } |
| 1742 | |
| 1743 | /* swap if needed */ |
| 1744 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1745 | change = 1; |
| 1746 | |
| 1747 | item = set->val.nodes[j - 1]; |
| 1748 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1749 | set->val.nodes[j] = item; |
| 1750 | } else { |
| 1751 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1752 | inverted = !inverted; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | ++ret; |
| 1757 | |
| 1758 | if (!change) { |
| 1759 | break; |
| 1760 | } |
| 1761 | } |
| 1762 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1763 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1764 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1765 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1766 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1767 | |
| 1768 | /* check node hashes */ |
| 1769 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1770 | assert(set->ht); |
| 1771 | for (i = 0; i < set->used; ++i) { |
| 1772 | hnode.node = set->val.nodes[i].node; |
| 1773 | hnode.type = set->val.nodes[i].type; |
| 1774 | |
| 1775 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1776 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1777 | hash = dict_hash_multi(hash, NULL, 0); |
| 1778 | |
| 1779 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | return ret - 1; |
| 1784 | } |
| 1785 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1786 | /** |
| 1787 | * @brief Merge 2 sorted sets into one. |
| 1788 | * |
| 1789 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1790 | * @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] | 1791 | * @return LY_ERR |
| 1792 | */ |
| 1793 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1794 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1795 | { |
| 1796 | uint32_t i, j, k, count, dup_count; |
| 1797 | int cmp; |
| 1798 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1799 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1800 | 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] | 1801 | return LY_EINVAL; |
| 1802 | } |
| 1803 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1804 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1805 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1806 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1807 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1808 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1809 | return LY_SUCCESS; |
| 1810 | } |
| 1811 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1812 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0143b68 | 2021-12-13 17:13:09 +0100 | [diff] [blame] | 1813 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1814 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1815 | |
| 1816 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1817 | 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] | 1818 | return LY_EINT; |
| 1819 | } |
| 1820 | |
| 1821 | #ifndef NDEBUG |
| 1822 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1823 | print_set_debug(trg); |
| 1824 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1825 | print_set_debug(src); |
| 1826 | #endif |
| 1827 | |
| 1828 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1829 | * will likely be wasted on them, too bad) */ |
| 1830 | if (trg->size - trg->used < src->used) { |
| 1831 | trg->size = trg->used + src->used; |
| 1832 | |
| 1833 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1834 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1835 | } |
| 1836 | |
| 1837 | i = 0; |
| 1838 | j = 0; |
| 1839 | count = 0; |
| 1840 | dup_count = 0; |
| 1841 | do { |
| 1842 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1843 | if (!cmp) { |
| 1844 | if (!count) { |
| 1845 | /* duplicate, just skip it */ |
| 1846 | ++i; |
| 1847 | ++j; |
| 1848 | } else { |
| 1849 | /* we are copying something already, so let's copy the duplicate too, |
| 1850 | * we are hoping that afterwards there are some more nodes to |
| 1851 | * copy and this way we can copy them all together */ |
| 1852 | ++count; |
| 1853 | ++dup_count; |
| 1854 | ++i; |
| 1855 | ++j; |
| 1856 | } |
| 1857 | } else if (cmp < 0) { |
| 1858 | /* inserting src node into trg, just remember it for now */ |
| 1859 | ++count; |
| 1860 | ++i; |
| 1861 | |
| 1862 | /* insert the hash now */ |
| 1863 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1864 | } else if (count) { |
| 1865 | copy_nodes: |
| 1866 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1867 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1868 | &trg->val.nodes[j], |
| 1869 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1870 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1871 | |
| 1872 | trg->used += count - dup_count; |
| 1873 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1874 | j += count - dup_count; |
| 1875 | |
| 1876 | count = 0; |
| 1877 | dup_count = 0; |
| 1878 | } else { |
| 1879 | ++j; |
| 1880 | } |
| 1881 | } while ((i < src->used) && (j < trg->used)); |
| 1882 | |
| 1883 | if ((i < src->used) || count) { |
| 1884 | /* insert all the hashes first */ |
| 1885 | for (k = i; k < src->used; ++k) { |
| 1886 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1887 | } |
| 1888 | |
| 1889 | /* loop ended, but we need to copy something at trg end */ |
| 1890 | count += src->used - i; |
| 1891 | i = src->used; |
| 1892 | goto copy_nodes; |
| 1893 | } |
| 1894 | |
| 1895 | /* we are inserting hashes before the actual node insert, which causes |
| 1896 | * situations when there were initially not enough items for a hash table, |
| 1897 | * but even after some were inserted, hash table was not created (during |
| 1898 | * insertion the number of items is not updated yet) */ |
| 1899 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1900 | set_insert_node_hash(trg, NULL, 0); |
| 1901 | } |
| 1902 | |
| 1903 | #ifndef NDEBUG |
| 1904 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1905 | print_set_debug(trg); |
| 1906 | #endif |
| 1907 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1908 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1909 | return LY_SUCCESS; |
| 1910 | } |
| 1911 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1912 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1913 | 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] | 1914 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1915 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1916 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1917 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1918 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1919 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1920 | } |
| 1921 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1922 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1923 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1924 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]), |
| 1925 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1926 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1927 | return LY_ENOT; |
| 1928 | } |
| 1929 | |
| 1930 | return LY_SUCCESS; |
| 1931 | } |
| 1932 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1933 | LY_ERR |
| 1934 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1935 | { |
| 1936 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1937 | |
| 1938 | /* skip the token */ |
| 1939 | ++(*tok_idx); |
| 1940 | |
| 1941 | return LY_SUCCESS; |
| 1942 | } |
| 1943 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1944 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1945 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1946 | 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] | 1947 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1948 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1949 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1950 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1951 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1952 | } |
| 1953 | return LY_EINCOMPLETE; |
| 1954 | } |
| 1955 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1956 | 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] | 1957 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1958 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1959 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1960 | } |
| 1961 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | return LY_SUCCESS; |
| 1965 | } |
| 1966 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 1967 | LY_ERR |
| 1968 | lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1, |
| 1969 | enum lyxp_token want_tok2) |
| 1970 | { |
| 1971 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 1972 | |
| 1973 | /* skip the token */ |
| 1974 | ++(*tok_idx); |
| 1975 | |
| 1976 | return LY_SUCCESS; |
| 1977 | } |
| 1978 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1979 | /** |
| 1980 | * @brief Stack operation push on the repeat array. |
| 1981 | * |
| 1982 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1983 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1984 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1985 | */ |
| 1986 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1987 | 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] | 1988 | { |
| 1989 | uint16_t i; |
| 1990 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1991 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1992 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1993 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1994 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1995 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1996 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1997 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1998 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1999 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 2000 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | /** |
| 2005 | * @brief Reparse Predicate. Logs directly on error. |
| 2006 | * |
| 2007 | * [7] Predicate ::= '[' Expr ']' |
| 2008 | * |
| 2009 | * @param[in] ctx Context for logging. |
| 2010 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2011 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2012 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2013 | * @return LY_ERR |
| 2014 | */ |
| 2015 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2016 | 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] | 2017 | { |
| 2018 | LY_ERR rc; |
| 2019 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2020 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2021 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2022 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2023 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2024 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2025 | LY_CHECK_RET(rc); |
| 2026 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2027 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2028 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2029 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2030 | |
| 2031 | return LY_SUCCESS; |
| 2032 | } |
| 2033 | |
| 2034 | /** |
| 2035 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 2036 | * |
| 2037 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 2038 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 2039 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 2040 | * |
| 2041 | * @param[in] ctx Context for logging. |
| 2042 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2043 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2044 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2045 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 2046 | */ |
| 2047 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2048 | 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] | 2049 | { |
| 2050 | LY_ERR rc; |
| 2051 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2052 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2053 | LY_CHECK_RET(rc); |
| 2054 | |
| 2055 | goto step; |
| 2056 | do { |
| 2057 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2058 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2059 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2060 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2061 | LY_CHECK_RET(rc); |
| 2062 | step: |
| 2063 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2064 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2065 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2066 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2067 | break; |
| 2068 | |
| 2069 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2070 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | break; |
| 2072 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2073 | case LYXP_TOKEN_AXISNAME: |
| 2074 | ++(*tok_idx); |
| 2075 | |
| 2076 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON); |
| 2077 | LY_CHECK_RET(rc); |
| 2078 | |
| 2079 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2080 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2081 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2082 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2083 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2084 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2085 | 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] | 2086 | 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] | 2087 | return LY_EVALID; |
| 2088 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2089 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) { |
| 2090 | goto reparse_nodetype; |
| 2091 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2092 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2093 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2094 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2095 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | |
| 2097 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2098 | reparse_nodetype: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2099 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2100 | |
| 2101 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2102 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2103 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2104 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | |
| 2106 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2107 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2109 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2110 | |
| 2111 | reparse_predicate: |
| 2112 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2114 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2115 | LY_CHECK_RET(rc); |
| 2116 | } |
| 2117 | break; |
| 2118 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2119 | 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] | 2120 | return LY_EVALID; |
| 2121 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2122 | } 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] | 2123 | |
| 2124 | return LY_SUCCESS; |
| 2125 | } |
| 2126 | |
| 2127 | /** |
| 2128 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2129 | * |
| 2130 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2131 | * |
| 2132 | * @param[in] ctx Context for logging. |
| 2133 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2135 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2136 | * @return LY_ERR |
| 2137 | */ |
| 2138 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2139 | 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] | 2140 | { |
| 2141 | LY_ERR rc; |
| 2142 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2143 | 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] | 2144 | |
| 2145 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2147 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2148 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2150 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2151 | return LY_SUCCESS; |
| 2152 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2153 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2154 | case LYXP_TOKEN_DOT: |
| 2155 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2156 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2157 | case LYXP_TOKEN_AT: |
| 2158 | case LYXP_TOKEN_NAMETEST: |
| 2159 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2160 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2162 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2163 | default: |
| 2164 | break; |
| 2165 | } |
| 2166 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2168 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2169 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2170 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2171 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2172 | LY_CHECK_RET(rc); |
| 2173 | } |
| 2174 | |
| 2175 | return LY_SUCCESS; |
| 2176 | } |
| 2177 | |
| 2178 | /** |
| 2179 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2180 | * |
| 2181 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2182 | * |
| 2183 | * @param[in] ctx Context for logging. |
| 2184 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2185 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2186 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2187 | * @return LY_ERR |
| 2188 | */ |
| 2189 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2190 | 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] | 2191 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2192 | int8_t min_arg_count = -1; |
| 2193 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2194 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2195 | LY_ERR rc; |
| 2196 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2197 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2198 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | func_tok_idx = *tok_idx; |
| 2200 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2201 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2203 | min_arg_count = 1; |
| 2204 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2205 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2206 | min_arg_count = 1; |
| 2207 | max_arg_count = 1; |
| 2208 | } |
| 2209 | break; |
| 2210 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | min_arg_count = 1; |
| 2213 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2215 | min_arg_count = 0; |
| 2216 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | min_arg_count = 0; |
| 2219 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2220 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2221 | min_arg_count = 0; |
| 2222 | max_arg_count = 0; |
| 2223 | } |
| 2224 | break; |
| 2225 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | min_arg_count = 1; |
| 2228 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | min_arg_count = 0; |
| 2231 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | min_arg_count = 1; |
| 2234 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2235 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2236 | min_arg_count = 1; |
| 2237 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2239 | min_arg_count = 1; |
| 2240 | max_arg_count = 1; |
| 2241 | } |
| 2242 | break; |
| 2243 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2244 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2245 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2246 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2248 | min_arg_count = 0; |
| 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]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2251 | min_arg_count = 0; |
| 2252 | max_arg_count = 1; |
| 2253 | } |
| 2254 | break; |
| 2255 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | min_arg_count = 1; |
| 2258 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | min_arg_count = 1; |
| 2261 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | min_arg_count = 0; |
| 2264 | max_arg_count = 0; |
| 2265 | } |
| 2266 | break; |
| 2267 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2268 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | min_arg_count = 2; |
| 2270 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | min_arg_count = 0; |
| 2273 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2274 | } 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] | 2275 | min_arg_count = 2; |
| 2276 | max_arg_count = 2; |
| 2277 | } |
| 2278 | break; |
| 2279 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2280 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2281 | min_arg_count = 2; |
| 2282 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2283 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2284 | min_arg_count = 3; |
| 2285 | max_arg_count = 3; |
| 2286 | } |
| 2287 | break; |
| 2288 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2289 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2290 | min_arg_count = 0; |
| 2291 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2292 | } 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] | 2293 | min_arg_count = 1; |
| 2294 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2295 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | min_arg_count = 2; |
| 2297 | max_arg_count = 2; |
| 2298 | } |
| 2299 | break; |
| 2300 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2301 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2302 | min_arg_count = 2; |
| 2303 | max_arg_count = 2; |
| 2304 | } |
| 2305 | break; |
| 2306 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2307 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
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 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2313 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2314 | min_arg_count = 0; |
| 2315 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2316 | } 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] | 2317 | min_arg_count = 0; |
| 2318 | max_arg_count = 1; |
| 2319 | } |
| 2320 | break; |
| 2321 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2322 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2323 | min_arg_count = 0; |
| 2324 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2325 | } 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] | 2326 | min_arg_count = 2; |
| 2327 | max_arg_count = 2; |
| 2328 | } |
| 2329 | break; |
| 2330 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2331 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2332 | min_arg_count = 2; |
| 2333 | max_arg_count = 2; |
| 2334 | } |
| 2335 | break; |
| 2336 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2337 | 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] | 2338 | min_arg_count = 2; |
| 2339 | max_arg_count = 2; |
| 2340 | } |
| 2341 | break; |
| 2342 | } |
| 2343 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2344 | 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] | 2345 | return LY_EINVAL; |
| 2346 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2347 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2348 | |
| 2349 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2350 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2351 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2352 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2353 | |
| 2354 | /* ( Expr ( ',' Expr )* )? */ |
| 2355 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2356 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2357 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2358 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2359 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2360 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2361 | LY_CHECK_RET(rc); |
| 2362 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2363 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2364 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2365 | |
| 2366 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2367 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2368 | LY_CHECK_RET(rc); |
| 2369 | } |
| 2370 | |
| 2371 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2372 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2373 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2374 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2375 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2376 | 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] | 2377 | 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] | 2378 | return LY_EVALID; |
| 2379 | } |
| 2380 | |
| 2381 | return LY_SUCCESS; |
| 2382 | } |
| 2383 | |
| 2384 | /** |
| 2385 | * @brief Reparse PathExpr. Logs directly on error. |
| 2386 | * |
| 2387 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2388 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2389 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2390 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2391 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2392 | * |
| 2393 | * @param[in] ctx Context for logging. |
| 2394 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2395 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2396 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2397 | * @return LY_ERR |
| 2398 | */ |
| 2399 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2400 | 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] | 2401 | { |
| 2402 | LY_ERR rc; |
| 2403 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2404 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2405 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2406 | } |
| 2407 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2408 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2409 | case LYXP_TOKEN_PAR1: |
| 2410 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2411 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2412 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2413 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2414 | LY_CHECK_RET(rc); |
| 2415 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2418 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2419 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2420 | case LYXP_TOKEN_DOT: |
| 2421 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2422 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2423 | case LYXP_TOKEN_AT: |
| 2424 | case LYXP_TOKEN_NAMETEST: |
| 2425 | case LYXP_TOKEN_NODETYPE: |
| 2426 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2427 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2428 | LY_CHECK_RET(rc); |
| 2429 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2430 | case LYXP_TOKEN_VARREF: |
| 2431 | /* VariableReference */ |
| 2432 | ++(*tok_idx); |
| 2433 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2434 | case LYXP_TOKEN_FUNCNAME: |
| 2435 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2436 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2437 | LY_CHECK_RET(rc); |
| 2438 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2439 | case LYXP_TOKEN_OPER_PATH: |
| 2440 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2441 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2442 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2443 | LY_CHECK_RET(rc); |
| 2444 | break; |
| 2445 | case LYXP_TOKEN_LITERAL: |
| 2446 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2447 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2448 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2449 | case LYXP_TOKEN_NUMBER: |
| 2450 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2451 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2452 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2454 | 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] | 2455 | return LY_EVALID; |
| 2456 | } |
| 2457 | |
| 2458 | return LY_SUCCESS; |
| 2459 | |
| 2460 | predicate: |
| 2461 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2462 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2463 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | LY_CHECK_RET(rc); |
| 2465 | } |
| 2466 | |
| 2467 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2468 | 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] | 2469 | |
| 2470 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2471 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2472 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2473 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2474 | LY_CHECK_RET(rc); |
| 2475 | } |
| 2476 | |
| 2477 | return LY_SUCCESS; |
| 2478 | } |
| 2479 | |
| 2480 | /** |
| 2481 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2482 | * |
| 2483 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2484 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2485 | * |
| 2486 | * @param[in] ctx Context for logging. |
| 2487 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2488 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2489 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2490 | * @return LY_ERR |
| 2491 | */ |
| 2492 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2493 | 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] | 2494 | { |
| 2495 | uint16_t prev_exp; |
| 2496 | LY_ERR rc; |
| 2497 | |
| 2498 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2499 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2500 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2501 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2502 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2503 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2507 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2508 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2509 | LY_CHECK_RET(rc); |
| 2510 | |
| 2511 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2512 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2513 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2514 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2515 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2516 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2517 | LY_CHECK_RET(rc); |
| 2518 | } |
| 2519 | |
| 2520 | return LY_SUCCESS; |
| 2521 | } |
| 2522 | |
| 2523 | /** |
| 2524 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2525 | * |
| 2526 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2527 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2528 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2529 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2530 | * | MultiplicativeExpr '*' UnaryExpr |
| 2531 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2532 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2533 | * |
| 2534 | * @param[in] ctx Context for logging. |
| 2535 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2536 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2537 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2538 | * @return LY_ERR |
| 2539 | */ |
| 2540 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2541 | 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] | 2542 | { |
| 2543 | uint16_t prev_add_exp, prev_mul_exp; |
| 2544 | LY_ERR rc; |
| 2545 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2546 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2547 | goto reparse_multiplicative_expr; |
| 2548 | |
| 2549 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2550 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2551 | ((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] | 2552 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2553 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2554 | |
| 2555 | reparse_multiplicative_expr: |
| 2556 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2557 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2558 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2559 | LY_CHECK_RET(rc); |
| 2560 | |
| 2561 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
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->tok_len[*tok_idx] == 3))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2564 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
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 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2567 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2568 | LY_CHECK_RET(rc); |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | return LY_SUCCESS; |
| 2573 | } |
| 2574 | |
| 2575 | /** |
| 2576 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2577 | * |
| 2578 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2579 | * | EqualityExpr '!=' RelationalExpr |
| 2580 | * [14] RelationalExpr ::= AdditiveExpr |
| 2581 | * | RelationalExpr '<' AdditiveExpr |
| 2582 | * | RelationalExpr '>' AdditiveExpr |
| 2583 | * | RelationalExpr '<=' AdditiveExpr |
| 2584 | * | RelationalExpr '>=' AdditiveExpr |
| 2585 | * |
| 2586 | * @param[in] ctx Context for logging. |
| 2587 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2588 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2589 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2590 | * @return LY_ERR |
| 2591 | */ |
| 2592 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2593 | 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] | 2594 | { |
| 2595 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2596 | LY_ERR rc; |
| 2597 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2598 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2599 | goto reparse_additive_expr; |
| 2600 | |
| 2601 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2602 | 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] | 2603 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2604 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2605 | |
| 2606 | reparse_additive_expr: |
| 2607 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2608 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2609 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2610 | LY_CHECK_RET(rc); |
| 2611 | |
| 2612 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2613 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2614 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2615 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2616 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2617 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2618 | LY_CHECK_RET(rc); |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | return LY_SUCCESS; |
| 2623 | } |
| 2624 | |
| 2625 | /** |
| 2626 | * @brief Reparse OrExpr. Logs directly on error. |
| 2627 | * |
| 2628 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2629 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2630 | * |
| 2631 | * @param[in] ctx Context for logging. |
| 2632 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2633 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2634 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2635 | * @return LY_ERR |
| 2636 | */ |
| 2637 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2638 | 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] | 2639 | { |
| 2640 | uint16_t prev_or_exp, prev_and_exp; |
| 2641 | LY_ERR rc; |
| 2642 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2643 | ++depth; |
| 2644 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2645 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2646 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2647 | goto reparse_equality_expr; |
| 2648 | |
| 2649 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2650 | 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] | 2651 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2652 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2653 | |
| 2654 | reparse_equality_expr: |
| 2655 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2656 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2657 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2658 | LY_CHECK_RET(rc); |
| 2659 | |
| 2660 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2661 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2662 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2663 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2664 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2665 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2666 | LY_CHECK_RET(rc); |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | return LY_SUCCESS; |
| 2671 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2672 | |
| 2673 | /** |
| 2674 | * @brief Parse NCName. |
| 2675 | * |
| 2676 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2677 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2678 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2679 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2680 | parse_ncname(const char *ncname) |
| 2681 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2682 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2683 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2684 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2685 | |
| 2686 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2687 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2688 | return len; |
| 2689 | } |
| 2690 | |
| 2691 | do { |
| 2692 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2693 | if (!*ncname) { |
| 2694 | break; |
| 2695 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2696 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2697 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2698 | |
| 2699 | return len; |
| 2700 | } |
| 2701 | |
| 2702 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2703 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2704 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2705 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2706 | * @param[in] exp Expression to use. |
| 2707 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2708 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2709 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2710 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2711 | */ |
| 2712 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2713 | 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] | 2714 | { |
| 2715 | uint32_t prev; |
| 2716 | |
| 2717 | if (exp->used == exp->size) { |
| 2718 | prev = exp->size; |
| 2719 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2720 | if (prev > exp->size) { |
| 2721 | LOGINT(ctx); |
| 2722 | return LY_EINT; |
| 2723 | } |
| 2724 | |
| 2725 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2726 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2727 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2728 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2729 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2730 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2731 | } |
| 2732 | |
| 2733 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2734 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2735 | exp->tok_len[exp->used] = tok_len; |
| 2736 | ++exp->used; |
| 2737 | return LY_SUCCESS; |
| 2738 | } |
| 2739 | |
| 2740 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2741 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2742 | { |
| 2743 | uint16_t i; |
| 2744 | |
| 2745 | if (!expr) { |
| 2746 | return; |
| 2747 | } |
| 2748 | |
| 2749 | lydict_remove(ctx, expr->expr); |
| 2750 | free(expr->tokens); |
| 2751 | free(expr->tok_pos); |
| 2752 | free(expr->tok_len); |
| 2753 | if (expr->repeat) { |
| 2754 | for (i = 0; i < expr->used; ++i) { |
| 2755 | free(expr->repeat[i]); |
| 2756 | } |
| 2757 | } |
| 2758 | free(expr->repeat); |
| 2759 | free(expr); |
| 2760 | } |
| 2761 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2762 | /** |
| 2763 | * @brief Parse Axis name. |
| 2764 | * |
| 2765 | * @param[in] str String to parse. |
| 2766 | * @param[in] str_len Length of @p str. |
| 2767 | * @return LY_SUCCESS if an axis. |
| 2768 | * @return LY_ENOT otherwise. |
| 2769 | */ |
| 2770 | static LY_ERR |
| 2771 | expr_parse_axis(const char *str, size_t str_len) |
| 2772 | { |
| 2773 | switch (str_len) { |
| 2774 | case 4: |
| 2775 | if (!strncmp("self", str, str_len)) { |
| 2776 | return LY_SUCCESS; |
| 2777 | } |
| 2778 | break; |
| 2779 | case 5: |
| 2780 | if (!strncmp("child", str, str_len)) { |
| 2781 | return LY_SUCCESS; |
| 2782 | } |
| 2783 | break; |
| 2784 | case 6: |
| 2785 | if (!strncmp("parent", str, str_len)) { |
| 2786 | return LY_SUCCESS; |
| 2787 | } |
| 2788 | break; |
| 2789 | case 8: |
| 2790 | if (!strncmp("ancestor", str, str_len)) { |
| 2791 | return LY_SUCCESS; |
| 2792 | } |
| 2793 | break; |
| 2794 | case 9: |
| 2795 | if (!strncmp("attribute", str, str_len)) { |
| 2796 | return LY_SUCCESS; |
| 2797 | } else if (!strncmp("following", str, str_len)) { |
| 2798 | return LY_SUCCESS; |
| 2799 | } else if (!strncmp("namespace", str, str_len)) { |
| 2800 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2801 | return LY_ENOT; |
| 2802 | } else if (!strncmp("preceding", str, str_len)) { |
| 2803 | return LY_SUCCESS; |
| 2804 | } |
| 2805 | break; |
| 2806 | case 10: |
| 2807 | if (!strncmp("descendant", str, str_len)) { |
| 2808 | return LY_SUCCESS; |
| 2809 | } |
| 2810 | break; |
| 2811 | case 16: |
| 2812 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2813 | return LY_SUCCESS; |
| 2814 | } |
| 2815 | break; |
| 2816 | case 17: |
| 2817 | if (!strncmp("following-sibling", str, str_len)) { |
| 2818 | return LY_SUCCESS; |
| 2819 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2820 | return LY_SUCCESS; |
| 2821 | } |
| 2822 | break; |
| 2823 | case 18: |
| 2824 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2825 | return LY_SUCCESS; |
| 2826 | } |
| 2827 | break; |
| 2828 | } |
| 2829 | |
| 2830 | return LY_ENOT; |
| 2831 | } |
| 2832 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2833 | LY_ERR |
| 2834 | 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] | 2835 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2836 | LY_ERR ret = LY_SUCCESS; |
| 2837 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2838 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2839 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2840 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2841 | uint16_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2842 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2843 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2844 | assert(expr_p); |
| 2845 | |
| 2846 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2847 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2848 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2849 | } |
| 2850 | |
| 2851 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2852 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2853 | } |
| 2854 | if (expr_len > UINT16_MAX) { |
Michal Vasko | 623ac7b | 2021-04-09 12:44:23 +0200 | [diff] [blame] | 2855 | 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] | 2856 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2860 | expr = calloc(1, sizeof *expr); |
| 2861 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2862 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2863 | expr->used = 0; |
| 2864 | expr->size = LYXP_EXPR_SIZE_START; |
| 2865 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2866 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2867 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2868 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2869 | 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] | 2870 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2871 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2872 | 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] | 2873 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2874 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2875 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2876 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2877 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2878 | ++parsed; |
| 2879 | } |
| 2880 | |
| 2881 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2882 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2883 | |
| 2884 | /* '(' */ |
| 2885 | tok_len = 1; |
| 2886 | tok_type = LYXP_TOKEN_PAR1; |
| 2887 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2888 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2889 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2890 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2891 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2892 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2893 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2894 | /* it is NodeType after all */ |
| 2895 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2896 | |
| 2897 | prev_ntype_check = 0; |
| 2898 | prev_func_check = 0; |
| 2899 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2900 | /* it is FunctionName after all */ |
| 2901 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2902 | |
| 2903 | prev_ntype_check = 0; |
| 2904 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2905 | } |
| 2906 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2907 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2908 | |
| 2909 | /* ')' */ |
| 2910 | tok_len = 1; |
| 2911 | tok_type = LYXP_TOKEN_PAR2; |
| 2912 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2913 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2914 | |
| 2915 | /* '[' */ |
| 2916 | tok_len = 1; |
| 2917 | tok_type = LYXP_TOKEN_BRACK1; |
| 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_BRACK2; |
| 2924 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2925 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2926 | |
| 2927 | /* '..' */ |
| 2928 | tok_len = 2; |
| 2929 | tok_type = LYXP_TOKEN_DDOT; |
| 2930 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2931 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2932 | |
| 2933 | /* '.' */ |
| 2934 | tok_len = 1; |
| 2935 | tok_type = LYXP_TOKEN_DOT; |
| 2936 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2937 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2938 | |
| 2939 | /* '@' */ |
| 2940 | tok_len = 1; |
| 2941 | tok_type = LYXP_TOKEN_AT; |
| 2942 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2943 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2944 | |
| 2945 | /* ',' */ |
| 2946 | tok_len = 1; |
| 2947 | tok_type = LYXP_TOKEN_COMMA; |
| 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 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2952 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2953 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2954 | 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] | 2955 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2956 | ++tok_len; |
| 2957 | tok_type = LYXP_TOKEN_LITERAL; |
| 2958 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2959 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2960 | |
| 2961 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2962 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2963 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2964 | 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] | 2965 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2966 | ++tok_len; |
| 2967 | tok_type = LYXP_TOKEN_LITERAL; |
| 2968 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2969 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2970 | |
| 2971 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2972 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2973 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2974 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2975 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2976 | } |
| 2977 | tok_type = LYXP_TOKEN_NUMBER; |
| 2978 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2979 | } else if (expr_str[parsed] == '$') { |
| 2980 | |
| 2981 | /* VariableReference */ |
| 2982 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2983 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2984 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 2985 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 2986 | tok_len = ncname_len; |
| 2987 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 2988 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 2989 | error); |
| 2990 | tok_type = LYXP_TOKEN_VARREF; |
| 2991 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2992 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2993 | |
| 2994 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2995 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2996 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2997 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2998 | } else { |
| 2999 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3000 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3001 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3002 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3003 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3004 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3005 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3006 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3007 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 3008 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3009 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3010 | |
| 3011 | /* Operator '<=', '>=' */ |
| 3012 | tok_len = 2; |
| 3013 | tok_type = LYXP_TOKEN_OPER_COMP; |
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 (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3016 | |
| 3017 | /* Operator '|' */ |
| 3018 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3019 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3020 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3021 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3022 | |
| 3023 | /* Operator '+', '-' */ |
| 3024 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3025 | tok_type = LYXP_TOKEN_OPER_MATH; |
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 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3029 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3030 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3031 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3032 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3033 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3034 | |
| 3035 | /* Operator '<', '>' */ |
| 3036 | tok_len = 1; |
| 3037 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3038 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3039 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3040 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3041 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3042 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3043 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3044 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3045 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3046 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3047 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3048 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3049 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3050 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3051 | |
| 3052 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3053 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3054 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3055 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3056 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3057 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3058 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3059 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3060 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3061 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3062 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3063 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3064 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3065 | } 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] | 3066 | tok_len = 3; |
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 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3069 | } else if (prev_ntype_check || prev_func_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3070 | 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] | 3071 | 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] | 3072 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3073 | goto error; |
| 3074 | } else { |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3075 | 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] | 3076 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3077 | goto error; |
| 3078 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3079 | } else { |
| 3080 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3081 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3082 | if (expr_str[parsed] == '*') { |
| 3083 | ncname_len = 1; |
| 3084 | } else { |
| 3085 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3086 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3087 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3088 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3089 | tok_len = ncname_len; |
| 3090 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3091 | has_axis = 0; |
| 3092 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3093 | /* axis */ |
| 3094 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
| 3095 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error); |
| 3096 | tok_type = LYXP_TOKEN_AXISNAME; |
| 3097 | |
| 3098 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3099 | parsed += tok_len; |
| 3100 | |
| 3101 | /* '::' */ |
| 3102 | tok_len = 2; |
| 3103 | tok_type = LYXP_TOKEN_DCOLON; |
| 3104 | |
| 3105 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3106 | parsed += tok_len; |
| 3107 | |
| 3108 | if (expr_str[parsed] == '*') { |
| 3109 | ncname_len = 1; |
| 3110 | } else { |
| 3111 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3112 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3113 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3114 | } |
| 3115 | tok_len = ncname_len; |
| 3116 | |
| 3117 | has_axis = 1; |
| 3118 | } |
| 3119 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3120 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3121 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3122 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3123 | ++tok_len; |
| 3124 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3125 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 3126 | 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] | 3127 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3128 | tok_len += ncname_len; |
| 3129 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3130 | /* remove old flags to prevent ambiguities */ |
| 3131 | prev_ntype_check = 0; |
| 3132 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3133 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3134 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3135 | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 3136 | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
| 3137 | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3138 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3143 | 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] | 3144 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3145 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3146 | ++parsed; |
| 3147 | } |
| 3148 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3149 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3150 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3151 | if (reparse) { |
| 3152 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3153 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 3154 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3155 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3156 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 3157 | 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] | 3158 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3159 | 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] | 3160 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3161 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3162 | goto error; |
| 3163 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3166 | print_expr_struct_debug(expr); |
| 3167 | *expr_p = expr; |
| 3168 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3169 | |
| 3170 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3171 | lyxp_expr_free(ctx, expr); |
| 3172 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3173 | } |
| 3174 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3175 | LY_ERR |
| 3176 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3177 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3178 | LY_ERR ret = LY_SUCCESS; |
| 3179 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3180 | uint32_t i, j; |
| 3181 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3182 | if (!exp) { |
| 3183 | goto cleanup; |
| 3184 | } |
| 3185 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3186 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3187 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3188 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3189 | if (exp->used) { |
| 3190 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 3191 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3192 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3193 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3194 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 3195 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3196 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3197 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3198 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 3199 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3200 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3201 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3202 | if (exp->repeat) { |
| 3203 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 3204 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3205 | for (i = 0; i < exp->used; ++i) { |
| 3206 | if (!exp->repeat[i]) { |
| 3207 | dup->repeat[i] = NULL; |
| 3208 | } else { |
| 3209 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3210 | /* the ending 0 as well */ |
| 3211 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3212 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3213 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
| 3214 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3215 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 3216 | dup->repeat[i][j - 1] = 0; |
| 3217 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3218 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | dup->used = exp->used; |
| 3223 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3224 | LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3225 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3226 | cleanup: |
| 3227 | if (ret) { |
| 3228 | lyxp_expr_free(ctx, dup); |
| 3229 | } else { |
| 3230 | *dup_p = dup; |
| 3231 | } |
| 3232 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3233 | } |
| 3234 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3235 | /** |
| 3236 | * @brief Get the last-added schema node that is currently in the context. |
| 3237 | * |
| 3238 | * @param[in] set Set to search in. |
| 3239 | * @return Last-added schema context node, NULL if no node is in context. |
| 3240 | */ |
| 3241 | static struct lysc_node * |
| 3242 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3243 | { |
| 3244 | uint32_t i; |
| 3245 | |
| 3246 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3247 | return NULL; |
| 3248 | } |
| 3249 | |
| 3250 | i = set->used; |
| 3251 | do { |
| 3252 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3253 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3254 | /* if there are more, simply return the first found (last added) */ |
| 3255 | return set->val.scnodes[i].scnode; |
| 3256 | } |
| 3257 | } while (i); |
| 3258 | |
| 3259 | return NULL; |
| 3260 | } |
| 3261 | |
| 3262 | /** |
| 3263 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3264 | * |
| 3265 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3266 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3267 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3268 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3269 | warn_is_numeric_type(struct lysc_type *type) |
| 3270 | { |
| 3271 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3272 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3273 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3274 | |
| 3275 | switch (type->basetype) { |
| 3276 | case LY_TYPE_DEC64: |
| 3277 | case LY_TYPE_INT8: |
| 3278 | case LY_TYPE_UINT8: |
| 3279 | case LY_TYPE_INT16: |
| 3280 | case LY_TYPE_UINT16: |
| 3281 | case LY_TYPE_INT32: |
| 3282 | case LY_TYPE_UINT32: |
| 3283 | case LY_TYPE_INT64: |
| 3284 | case LY_TYPE_UINT64: |
| 3285 | return 1; |
| 3286 | case LY_TYPE_UNION: |
| 3287 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3288 | LY_ARRAY_FOR(uni->types, u) { |
| 3289 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3290 | if (ret) { |
| 3291 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3292 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3293 | } |
| 3294 | } |
| 3295 | /* did not find any suitable type */ |
| 3296 | return 0; |
| 3297 | case LY_TYPE_LEAFREF: |
| 3298 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3299 | default: |
| 3300 | return 0; |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | /** |
| 3305 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3306 | * |
| 3307 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3308 | * @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] | 3309 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3310 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3311 | warn_is_string_type(struct lysc_type *type) |
| 3312 | { |
| 3313 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3314 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3315 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3316 | |
| 3317 | switch (type->basetype) { |
| 3318 | case LY_TYPE_BITS: |
| 3319 | case LY_TYPE_ENUM: |
| 3320 | case LY_TYPE_IDENT: |
| 3321 | case LY_TYPE_INST: |
| 3322 | case LY_TYPE_STRING: |
| 3323 | return 1; |
| 3324 | case LY_TYPE_UNION: |
| 3325 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3326 | LY_ARRAY_FOR(uni->types, u) { |
| 3327 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3328 | if (ret) { |
| 3329 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3330 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3331 | } |
| 3332 | } |
| 3333 | /* did not find any suitable type */ |
| 3334 | return 0; |
| 3335 | case LY_TYPE_LEAFREF: |
| 3336 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3337 | default: |
| 3338 | return 0; |
| 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | /** |
| 3343 | * @brief Test whether a type is one specific type. |
| 3344 | * |
| 3345 | * @param[in] type Type to test. |
| 3346 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3347 | * @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] | 3348 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3349 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3350 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3351 | { |
| 3352 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3353 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3354 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3355 | |
| 3356 | if (type->basetype == base) { |
| 3357 | return 1; |
| 3358 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3359 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3360 | LY_ARRAY_FOR(uni->types, u) { |
| 3361 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3362 | if (ret) { |
| 3363 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3364 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3365 | } |
| 3366 | } |
| 3367 | /* did not find any suitable type */ |
| 3368 | return 0; |
| 3369 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3370 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3371 | } |
| 3372 | |
| 3373 | return 0; |
| 3374 | } |
| 3375 | |
| 3376 | /** |
| 3377 | * @brief Get next type of a (union) type. |
| 3378 | * |
| 3379 | * @param[in] type Base type. |
| 3380 | * @param[in] prev_type Previously returned type. |
| 3381 | * @return Next type or NULL. |
| 3382 | */ |
| 3383 | static struct lysc_type * |
| 3384 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3385 | { |
| 3386 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3387 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3388 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3389 | |
| 3390 | switch (type->basetype) { |
| 3391 | case LY_TYPE_UNION: |
| 3392 | uni = (struct lysc_type_union *)type; |
| 3393 | if (!prev_type) { |
| 3394 | return uni->types[0]; |
| 3395 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3396 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3397 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3398 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3399 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3400 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3401 | found = 1; |
| 3402 | } |
| 3403 | } |
| 3404 | return NULL; |
| 3405 | default: |
| 3406 | if (prev_type) { |
| 3407 | assert(type == prev_type); |
| 3408 | return NULL; |
| 3409 | } else { |
| 3410 | return type; |
| 3411 | } |
| 3412 | } |
| 3413 | } |
| 3414 | |
| 3415 | /** |
| 3416 | * @brief Test whether 2 types have a common type. |
| 3417 | * |
| 3418 | * @param[in] type1 First type. |
| 3419 | * @param[in] type2 Second type. |
| 3420 | * @return 1 if they do, 0 otherwise. |
| 3421 | */ |
| 3422 | static int |
| 3423 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3424 | { |
| 3425 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3426 | |
| 3427 | t1 = NULL; |
| 3428 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3429 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3430 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3431 | } else { |
| 3432 | rt1 = t1; |
| 3433 | } |
| 3434 | |
| 3435 | t2 = NULL; |
| 3436 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3437 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3438 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3439 | } else { |
| 3440 | rt2 = t2; |
| 3441 | } |
| 3442 | |
| 3443 | if (rt2->basetype == rt1->basetype) { |
| 3444 | /* match found */ |
| 3445 | return 1; |
| 3446 | } |
| 3447 | } |
| 3448 | } |
| 3449 | |
| 3450 | return 0; |
| 3451 | } |
| 3452 | |
| 3453 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3454 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3455 | * |
| 3456 | * @param[in] ctx Context for logging. |
| 3457 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3458 | * @param[in] subexpr Subexpression start. |
| 3459 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3460 | * @param[in] cur_scnode Expression context node. |
| 3461 | */ |
| 3462 | static void |
| 3463 | warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len, |
| 3464 | const struct lysc_node *cur_scnode) |
| 3465 | { |
| 3466 | char *path; |
| 3467 | |
| 3468 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 3469 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".", |
| 3470 | tok_pos, subexpr_len, subexpr, path); |
| 3471 | free(path); |
| 3472 | } |
| 3473 | |
| 3474 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3475 | * @brief Check both operands of comparison operators. |
| 3476 | * |
| 3477 | * @param[in] ctx Context for errors. |
| 3478 | * @param[in] set1 First operand set. |
| 3479 | * @param[in] set2 Second operand set. |
| 3480 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3481 | * @param[in] expr Start of the expression to print with the warning. |
| 3482 | * @param[in] tok_pos Token position. |
| 3483 | */ |
| 3484 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3485 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, |
| 3486 | uint16_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3487 | { |
| 3488 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3489 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3490 | |
| 3491 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3492 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3493 | |
| 3494 | if (!node1 && !node2) { |
| 3495 | /* no node-sets involved, nothing to do */ |
| 3496 | return; |
| 3497 | } |
| 3498 | |
| 3499 | if (node1) { |
| 3500 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3501 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3502 | warning = 1; |
| 3503 | leaves = 0; |
| 3504 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3505 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3506 | warning = 1; |
| 3507 | } |
| 3508 | } |
| 3509 | |
| 3510 | if (node2) { |
| 3511 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3512 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3513 | warning = 1; |
| 3514 | leaves = 0; |
| 3515 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3516 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3517 | warning = 1; |
| 3518 | } |
| 3519 | } |
| 3520 | |
| 3521 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3522 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3523 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3524 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3525 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3526 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3527 | warning = 1; |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3532 | 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] | 3533 | } |
| 3534 | } |
| 3535 | |
| 3536 | /** |
| 3537 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3538 | * |
| 3539 | * @param[in] exp Parsed XPath expression. |
| 3540 | * @param[in] set Set with the leaf/leaf-list. |
| 3541 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3542 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3543 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3544 | */ |
| 3545 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3546 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3547 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3548 | { |
| 3549 | struct lysc_node *scnode; |
| 3550 | struct lysc_type *type; |
| 3551 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3552 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3553 | LY_ERR rc; |
| 3554 | struct ly_err_item *err = NULL; |
| 3555 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3556 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3557 | ((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] | 3558 | /* check that the node can have the specified value */ |
| 3559 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3560 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3561 | } else { |
| 3562 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3563 | } |
| 3564 | if (!value) { |
| 3565 | LOGMEM(set->ctx); |
| 3566 | return; |
| 3567 | } |
| 3568 | |
| 3569 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3570 | 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] | 3571 | " 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] | 3572 | 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] | 3573 | (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] | 3574 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3578 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3579 | 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] | 3580 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3581 | if (rc == LY_EINCOMPLETE) { |
| 3582 | rc = LY_SUCCESS; |
| 3583 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3584 | |
| 3585 | if (err) { |
| 3586 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3587 | ly_err_free(err); |
| 3588 | } else if (rc != LY_SUCCESS) { |
| 3589 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3590 | } |
| 3591 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3592 | 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] | 3593 | (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] | 3594 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3595 | } else { |
| 3596 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3597 | } |
| 3598 | } |
| 3599 | free(value); |
| 3600 | } |
| 3601 | } |
| 3602 | |
| 3603 | /* |
| 3604 | * XPath functions |
| 3605 | */ |
| 3606 | |
| 3607 | /** |
| 3608 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3609 | * depending on whether the first node bit value from the second argument is set. |
| 3610 | * |
| 3611 | * @param[in] args Array of arguments. |
| 3612 | * @param[in] arg_count Count of elements in @p args. |
| 3613 | * @param[in,out] set Context and result set at the same time. |
| 3614 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3615 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3616 | */ |
| 3617 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3618 | 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] | 3619 | { |
| 3620 | struct lyd_node_term *leaf; |
| 3621 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3622 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3623 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3624 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3625 | |
| 3626 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3627 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3628 | 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] | 3629 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3630 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3631 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3632 | sleaf->name); |
| 3633 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3634 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3635 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3639 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3640 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3641 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3642 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3643 | 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] | 3644 | } |
| 3645 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3646 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3647 | return rc; |
| 3648 | } |
| 3649 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3650 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3651 | 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] | 3652 | return LY_EVALID; |
| 3653 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3654 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3655 | LY_CHECK_RET(rc); |
| 3656 | |
| 3657 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3658 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3659 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3660 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3661 | LYD_VALUE_GET(&leaf->value, bits); |
| 3662 | LY_ARRAY_FOR(bits->items, u) { |
| 3663 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3664 | set_fill_boolean(set, 1); |
| 3665 | break; |
| 3666 | } |
| 3667 | } |
| 3668 | } |
| 3669 | } |
| 3670 | |
| 3671 | return LY_SUCCESS; |
| 3672 | } |
| 3673 | |
| 3674 | /** |
| 3675 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3676 | * with the argument converted to boolean. |
| 3677 | * |
| 3678 | * @param[in] args Array of arguments. |
| 3679 | * @param[in] arg_count Count of elements in @p args. |
| 3680 | * @param[in,out] set Context and result set at the same time. |
| 3681 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3682 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3683 | */ |
| 3684 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3685 | 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] | 3686 | { |
| 3687 | LY_ERR rc; |
| 3688 | |
| 3689 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3690 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3691 | return LY_SUCCESS; |
| 3692 | } |
| 3693 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3694 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3695 | LY_CHECK_RET(rc); |
| 3696 | set_fill_set(set, args[0]); |
| 3697 | |
| 3698 | return LY_SUCCESS; |
| 3699 | } |
| 3700 | |
| 3701 | /** |
| 3702 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3703 | * with the first argument rounded up to the nearest integer. |
| 3704 | * |
| 3705 | * @param[in] args Array of arguments. |
| 3706 | * @param[in] arg_count Count of elements in @p args. |
| 3707 | * @param[in,out] set Context and result set at the same time. |
| 3708 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3709 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3710 | */ |
| 3711 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3712 | 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] | 3713 | { |
| 3714 | struct lysc_node_leaf *sleaf; |
| 3715 | LY_ERR rc = LY_SUCCESS; |
| 3716 | |
| 3717 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3718 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3719 | 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] | 3720 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3721 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3722 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3723 | sleaf->name); |
| 3724 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3725 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3726 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3727 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3728 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3729 | return rc; |
| 3730 | } |
| 3731 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3732 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3733 | LY_CHECK_RET(rc); |
| 3734 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3735 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3736 | } else { |
| 3737 | set_fill_number(set, args[0]->val.num); |
| 3738 | } |
| 3739 | |
| 3740 | return LY_SUCCESS; |
| 3741 | } |
| 3742 | |
| 3743 | /** |
| 3744 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3745 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3746 | * |
| 3747 | * @param[in] args Array of arguments. |
| 3748 | * @param[in] arg_count Count of elements in @p args. |
| 3749 | * @param[in,out] set Context and result set at the same time. |
| 3750 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3751 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3752 | */ |
| 3753 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3754 | 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] | 3755 | { |
| 3756 | uint16_t i; |
| 3757 | char *str = NULL; |
| 3758 | size_t used = 1; |
| 3759 | LY_ERR rc = LY_SUCCESS; |
| 3760 | struct lysc_node_leaf *sleaf; |
| 3761 | |
| 3762 | if (options & LYXP_SCNODE_ALL) { |
| 3763 | for (i = 0; i < arg_count; ++i) { |
| 3764 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3765 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3766 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3767 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3768 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3769 | 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] | 3770 | } |
| 3771 | } |
| 3772 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3773 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3774 | return rc; |
| 3775 | } |
| 3776 | |
| 3777 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3778 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3779 | if (rc != LY_SUCCESS) { |
| 3780 | free(str); |
| 3781 | return rc; |
| 3782 | } |
| 3783 | |
| 3784 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3785 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3786 | strcpy(str + used - 1, args[i]->val.str); |
| 3787 | used += strlen(args[i]->val.str); |
| 3788 | } |
| 3789 | |
| 3790 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3791 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3792 | set->type = LYXP_SET_STRING; |
| 3793 | set->val.str = str; |
| 3794 | |
| 3795 | return LY_SUCCESS; |
| 3796 | } |
| 3797 | |
| 3798 | /** |
| 3799 | * @brief Execute the XPath contains(string, string) function. |
| 3800 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3801 | * be found in the first or not. |
| 3802 | * |
| 3803 | * @param[in] args Array of arguments. |
| 3804 | * @param[in] arg_count Count of elements in @p args. |
| 3805 | * @param[in,out] set Context and result set at the same time. |
| 3806 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3807 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3808 | */ |
| 3809 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3810 | 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] | 3811 | { |
| 3812 | struct lysc_node_leaf *sleaf; |
| 3813 | LY_ERR rc = LY_SUCCESS; |
| 3814 | |
| 3815 | if (options & LYXP_SCNODE_ALL) { |
| 3816 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3817 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3818 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3819 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3820 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3821 | 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] | 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3826 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3827 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3828 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3829 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3830 | 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] | 3831 | } |
| 3832 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3833 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3834 | return rc; |
| 3835 | } |
| 3836 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3837 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3838 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3839 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3840 | LY_CHECK_RET(rc); |
| 3841 | |
| 3842 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3843 | set_fill_boolean(set, 1); |
| 3844 | } else { |
| 3845 | set_fill_boolean(set, 0); |
| 3846 | } |
| 3847 | |
| 3848 | return LY_SUCCESS; |
| 3849 | } |
| 3850 | |
| 3851 | /** |
| 3852 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3853 | * with the size of the node-set from the argument. |
| 3854 | * |
| 3855 | * @param[in] args Array of arguments. |
| 3856 | * @param[in] arg_count Count of elements in @p args. |
| 3857 | * @param[in,out] set Context and result set at the same time. |
| 3858 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3859 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3860 | */ |
| 3861 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3862 | 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] | 3863 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3864 | LY_ERR rc = LY_SUCCESS; |
| 3865 | |
| 3866 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3867 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3868 | 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] | 3869 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3870 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3871 | return rc; |
| 3872 | } |
| 3873 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3874 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3875 | 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] | 3876 | return LY_EVALID; |
| 3877 | } |
| 3878 | |
| 3879 | set_fill_number(set, args[0]->used); |
| 3880 | return LY_SUCCESS; |
| 3881 | } |
| 3882 | |
| 3883 | /** |
| 3884 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3885 | * with the context with the intial node. |
| 3886 | * |
| 3887 | * @param[in] args Array of arguments. |
| 3888 | * @param[in] arg_count Count of elements in @p args. |
| 3889 | * @param[in,out] set Context and result set at the same time. |
| 3890 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3891 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3892 | */ |
| 3893 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3894 | 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] | 3895 | { |
| 3896 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3897 | 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] | 3898 | return LY_EVALID; |
| 3899 | } |
| 3900 | |
| 3901 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3902 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3903 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3904 | if (set->cur_scnode) { |
| 3905 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL)); |
| 3906 | } else { |
| 3907 | /* root node */ |
| 3908 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL)); |
| 3909 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3910 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3911 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3912 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3913 | if (set->cur_node) { |
| 3914 | /* position is filled later */ |
| 3915 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3916 | } else { |
| 3917 | /* root node */ |
| 3918 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3919 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | return LY_SUCCESS; |
| 3923 | } |
| 3924 | |
| 3925 | /** |
| 3926 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3927 | * leafref or instance-identifier target node(s). |
| 3928 | * |
| 3929 | * @param[in] args Array of arguments. |
| 3930 | * @param[in] arg_count Count of elements in @p args. |
| 3931 | * @param[in,out] set Context and result set at the same time. |
| 3932 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3933 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3934 | */ |
| 3935 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3936 | 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] | 3937 | { |
| 3938 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3939 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3940 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3941 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3942 | struct ly_path *p; |
| 3943 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3944 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3945 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3946 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3947 | |
| 3948 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3949 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3950 | 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] | 3951 | } 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] | 3952 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3953 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3954 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3955 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3956 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3957 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3958 | __func__, sleaf->name); |
| 3959 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3960 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3961 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 3962 | 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] | 3963 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 3964 | 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] | 3965 | |
| 3966 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3967 | 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] | 3968 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3969 | if (!r) { |
| 3970 | /* get the target node */ |
| 3971 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 3972 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3973 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3974 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL)); |
| 3975 | } /* 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] | 3976 | } |
| 3977 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3978 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3979 | } |
| 3980 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3981 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3982 | 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] | 3983 | return LY_EVALID; |
| 3984 | } |
| 3985 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3986 | lyxp_set_free_content(set); |
| 3987 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3988 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3989 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3990 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3991 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3992 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 3993 | 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] | 3994 | &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3995 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3996 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3997 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3998 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3999 | } else { |
| 4000 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4001 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4002 | 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] | 4003 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4004 | return LY_EVALID; |
| 4005 | } |
| 4006 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4007 | |
| 4008 | /* insert it */ |
| 4009 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | return LY_SUCCESS; |
| 4014 | } |
| 4015 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4016 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4017 | 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] | 4018 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 4019 | uint32_t i; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4020 | LY_ARRAY_COUNT_TYPE u; |
| 4021 | struct lyd_node_term *leaf; |
| 4022 | struct lysc_node_leaf *sleaf; |
| 4023 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4024 | struct lyd_value *val; |
| 4025 | const struct lys_module *mod; |
| 4026 | const char *id_name; |
| 4027 | uint16_t id_len; |
| 4028 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4029 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4030 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4031 | |
| 4032 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4033 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4034 | 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] | 4035 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4036 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4037 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4038 | sleaf->name); |
| 4039 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4040 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4041 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4045 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4046 | 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] | 4047 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4048 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4049 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4050 | } |
| 4051 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4052 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4053 | return rc; |
| 4054 | } |
| 4055 | |
| 4056 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4057 | 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] | 4058 | return LY_EVALID; |
| 4059 | } |
| 4060 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4061 | LY_CHECK_RET(rc); |
| 4062 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4063 | /* parse the identity */ |
| 4064 | id_name = args[1]->val.str; |
| 4065 | id_len = strlen(id_name); |
| 4066 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4067 | LY_CHECK_RET(rc); |
| 4068 | if (!mod) { |
| 4069 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4070 | return LY_EVALID; |
| 4071 | } |
| 4072 | |
| 4073 | /* find the identity */ |
| 4074 | found = 0; |
| 4075 | LY_ARRAY_FOR(mod->identities, u) { |
| 4076 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4077 | /* we have match */ |
| 4078 | found = 1; |
| 4079 | break; |
| 4080 | } |
| 4081 | } |
| 4082 | if (!found) { |
| 4083 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4084 | return LY_EVALID; |
| 4085 | } |
| 4086 | id = &mod->identities[u]; |
| 4087 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4088 | set_fill_boolean(set, 0); |
| 4089 | found = 0; |
| 4090 | for (i = 0; i < args[0]->used; ++i) { |
| 4091 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4092 | continue; |
| 4093 | } |
| 4094 | |
| 4095 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4096 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4097 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4098 | val = &leaf->value; |
| 4099 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 4100 | /* uninteresting */ |
| 4101 | continue; |
| 4102 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4103 | } else { |
| 4104 | meta = args[0]->val.meta[i].meta; |
| 4105 | val = &meta->value; |
| 4106 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4107 | /* uninteresting */ |
| 4108 | continue; |
| 4109 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4110 | } |
| 4111 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4112 | /* check the identity itself */ |
| 4113 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4114 | set_fill_boolean(set, 1); |
| 4115 | found = 1; |
| 4116 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4117 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4118 | set_fill_boolean(set, 1); |
| 4119 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4120 | } |
| 4121 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4122 | if (found) { |
| 4123 | break; |
| 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | return LY_SUCCESS; |
| 4128 | } |
| 4129 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4130 | /** |
| 4131 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4132 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4133 | * argument identity. |
| 4134 | * |
| 4135 | * @param[in] args Array of arguments. |
| 4136 | * @param[in] arg_count Count of elements in @p args. |
| 4137 | * @param[in,out] set Context and result set at the same time. |
| 4138 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4139 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4140 | */ |
| 4141 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4142 | 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] | 4143 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4144 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | /** |
| 4148 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4149 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4150 | * the second argument identity. |
| 4151 | * |
| 4152 | * @param[in] args Array of arguments. |
| 4153 | * @param[in] arg_count Count of elements in @p args. |
| 4154 | * @param[in,out] set Context and result set at the same time. |
| 4155 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4156 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4157 | */ |
| 4158 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4159 | 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] | 4160 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4161 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | /** |
| 4165 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4166 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4167 | * |
| 4168 | * @param[in] args Array of arguments. |
| 4169 | * @param[in] arg_count Count of elements in @p args. |
| 4170 | * @param[in,out] set Context and result set at the same time. |
| 4171 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4172 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4173 | */ |
| 4174 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4175 | 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] | 4176 | { |
| 4177 | struct lyd_node_term *leaf; |
| 4178 | struct lysc_node_leaf *sleaf; |
| 4179 | LY_ERR rc = LY_SUCCESS; |
| 4180 | |
| 4181 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4182 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4183 | 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] | 4184 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4185 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4186 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4187 | sleaf->name); |
| 4188 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4189 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4190 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4191 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4192 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4193 | return rc; |
| 4194 | } |
| 4195 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4196 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4197 | 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] | 4198 | return LY_EVALID; |
| 4199 | } |
| 4200 | |
| 4201 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4202 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4203 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4204 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4205 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4206 | set_fill_number(set, leaf->value.enum_item->value); |
| 4207 | } |
| 4208 | } |
| 4209 | |
| 4210 | return LY_SUCCESS; |
| 4211 | } |
| 4212 | |
| 4213 | /** |
| 4214 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4215 | * with false value. |
| 4216 | * |
| 4217 | * @param[in] args Array of arguments. |
| 4218 | * @param[in] arg_count Count of elements in @p args. |
| 4219 | * @param[in,out] set Context and result set at the same time. |
| 4220 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4221 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | */ |
| 4223 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4224 | 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] | 4225 | { |
| 4226 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4227 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4228 | return LY_SUCCESS; |
| 4229 | } |
| 4230 | |
| 4231 | set_fill_boolean(set, 0); |
| 4232 | return LY_SUCCESS; |
| 4233 | } |
| 4234 | |
| 4235 | /** |
| 4236 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4237 | * with the first argument floored (truncated). |
| 4238 | * |
| 4239 | * @param[in] args Array of arguments. |
| 4240 | * @param[in] arg_count Count of elements in @p args. |
| 4241 | * @param[in,out] set Context and result set at the same time. |
| 4242 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4243 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4244 | */ |
| 4245 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4246 | 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] | 4247 | { |
| 4248 | LY_ERR rc; |
| 4249 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4250 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4251 | LY_CHECK_RET(rc); |
| 4252 | if (isfinite(args[0]->val.num)) { |
| 4253 | set_fill_number(set, (long long)args[0]->val.num); |
| 4254 | } |
| 4255 | |
| 4256 | return LY_SUCCESS; |
| 4257 | } |
| 4258 | |
| 4259 | /** |
| 4260 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4261 | * whether the language of the text matches the one from the argument. |
| 4262 | * |
| 4263 | * @param[in] args Array of arguments. |
| 4264 | * @param[in] arg_count Count of elements in @p args. |
| 4265 | * @param[in,out] set Context and result set at the same time. |
| 4266 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4267 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4268 | */ |
| 4269 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4270 | 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] | 4271 | { |
| 4272 | const struct lyd_node *node; |
| 4273 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4274 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4275 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4276 | LY_ERR rc = LY_SUCCESS; |
| 4277 | |
| 4278 | if (options & LYXP_SCNODE_ALL) { |
| 4279 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4280 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4281 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4282 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4283 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4284 | 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] | 4285 | } |
| 4286 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4287 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4288 | return rc; |
| 4289 | } |
| 4290 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4291 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4292 | LY_CHECK_RET(rc); |
| 4293 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4294 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4295 | 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] | 4296 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4297 | } else if (!set->used) { |
| 4298 | set_fill_boolean(set, 0); |
| 4299 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4300 | } |
| 4301 | |
| 4302 | switch (set->val.nodes[0].type) { |
| 4303 | case LYXP_NODE_ELEM: |
| 4304 | case LYXP_NODE_TEXT: |
| 4305 | node = set->val.nodes[0].node; |
| 4306 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4307 | case LYXP_NODE_META: |
| 4308 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4309 | break; |
| 4310 | default: |
| 4311 | /* nothing to do with roots */ |
| 4312 | set_fill_boolean(set, 0); |
| 4313 | return LY_SUCCESS; |
| 4314 | } |
| 4315 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4316 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4317 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4318 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4319 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4320 | 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] | 4321 | break; |
| 4322 | } |
| 4323 | } |
| 4324 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4325 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4326 | break; |
| 4327 | } |
| 4328 | } |
| 4329 | |
| 4330 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4331 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4332 | set_fill_boolean(set, 0); |
| 4333 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4334 | uint64_t i; |
| 4335 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4336 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4337 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4338 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4339 | set_fill_boolean(set, 0); |
| 4340 | break; |
| 4341 | } |
| 4342 | } |
| 4343 | if (!args[0]->val.str[i]) { |
| 4344 | if (!val[i] || (val[i] == '-')) { |
| 4345 | set_fill_boolean(set, 1); |
| 4346 | } else { |
| 4347 | set_fill_boolean(set, 0); |
| 4348 | } |
| 4349 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | return LY_SUCCESS; |
| 4353 | } |
| 4354 | |
| 4355 | /** |
| 4356 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4357 | * with the context size. |
| 4358 | * |
| 4359 | * @param[in] args Array of arguments. |
| 4360 | * @param[in] arg_count Count of elements in @p args. |
| 4361 | * @param[in,out] set Context and result set at the same time. |
| 4362 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4363 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4364 | */ |
| 4365 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4366 | 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] | 4367 | { |
| 4368 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4369 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4370 | return LY_SUCCESS; |
| 4371 | } |
| 4372 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4373 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4374 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4375 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4376 | } else if (!set->used) { |
| 4377 | set_fill_number(set, 0); |
| 4378 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | set_fill_number(set, set->ctx_size); |
| 4382 | return LY_SUCCESS; |
| 4383 | } |
| 4384 | |
| 4385 | /** |
| 4386 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4387 | * with the node name without namespace from the argument or the context. |
| 4388 | * |
| 4389 | * @param[in] args Array of arguments. |
| 4390 | * @param[in] arg_count Count of elements in @p args. |
| 4391 | * @param[in,out] set Context and result set at the same time. |
| 4392 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4393 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4394 | */ |
| 4395 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4396 | 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] | 4397 | { |
| 4398 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4399 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4400 | /* suppress unused variable warning */ |
| 4401 | (void)options; |
| 4402 | |
| 4403 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4404 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4405 | return LY_SUCCESS; |
| 4406 | } |
| 4407 | |
| 4408 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4409 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4410 | 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] | 4411 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4412 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4413 | } else if (!args[0]->used) { |
| 4414 | set_fill_string(set, "", 0); |
| 4415 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4419 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4420 | |
| 4421 | item = &args[0]->val.nodes[0]; |
| 4422 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4423 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4424 | 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] | 4425 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4426 | } else if (!set->used) { |
| 4427 | set_fill_string(set, "", 0); |
| 4428 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4432 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4433 | |
| 4434 | item = &set->val.nodes[0]; |
| 4435 | } |
| 4436 | |
| 4437 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4438 | case LYXP_NODE_NONE: |
| 4439 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4440 | case LYXP_NODE_ROOT: |
| 4441 | case LYXP_NODE_ROOT_CONFIG: |
| 4442 | case LYXP_NODE_TEXT: |
| 4443 | set_fill_string(set, "", 0); |
| 4444 | break; |
| 4445 | case LYXP_NODE_ELEM: |
| 4446 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4447 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4448 | case LYXP_NODE_META: |
| 4449 | 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] | 4450 | break; |
| 4451 | } |
| 4452 | |
| 4453 | return LY_SUCCESS; |
| 4454 | } |
| 4455 | |
| 4456 | /** |
| 4457 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4458 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4459 | * |
| 4460 | * @param[in] args Array of arguments. |
| 4461 | * @param[in] arg_count Count of elements in @p args. |
| 4462 | * @param[in,out] set Context and result set at the same time. |
| 4463 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4464 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4465 | */ |
| 4466 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4467 | 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] | 4468 | { |
| 4469 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4470 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4471 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4472 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4473 | |
| 4474 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4475 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4476 | return LY_SUCCESS; |
| 4477 | } |
| 4478 | |
| 4479 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4480 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4481 | 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] | 4482 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4483 | } else if (!args[0]->used) { |
| 4484 | set_fill_string(set, "", 0); |
| 4485 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4489 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4490 | |
| 4491 | item = &args[0]->val.nodes[0]; |
| 4492 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4493 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4494 | 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] | 4495 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4496 | } else if (!set->used) { |
| 4497 | set_fill_string(set, "", 0); |
| 4498 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4502 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4503 | |
| 4504 | item = &set->val.nodes[0]; |
| 4505 | } |
| 4506 | |
| 4507 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4508 | case LYXP_NODE_NONE: |
| 4509 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4510 | case LYXP_NODE_ROOT: |
| 4511 | case LYXP_NODE_ROOT_CONFIG: |
| 4512 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4513 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4514 | break; |
| 4515 | case LYXP_NODE_ELEM: |
| 4516 | mod = item->node->schema->module; |
| 4517 | name = item->node->schema->name; |
| 4518 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4519 | case LYXP_NODE_META: |
| 4520 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4521 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4522 | break; |
| 4523 | } |
| 4524 | |
| 4525 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4526 | int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4527 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4528 | set_fill_string(set, str, strlen(str)); |
| 4529 | free(str); |
| 4530 | } else { |
| 4531 | set_fill_string(set, "", 0); |
| 4532 | } |
| 4533 | |
| 4534 | return LY_SUCCESS; |
| 4535 | } |
| 4536 | |
| 4537 | /** |
| 4538 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4539 | * with the namespace of the node from the argument or the context. |
| 4540 | * |
| 4541 | * @param[in] args Array of arguments. |
| 4542 | * @param[in] arg_count Count of elements in @p args. |
| 4543 | * @param[in,out] set Context and result set at the same time. |
| 4544 | * @param[in] options XPath options. |
| 4545 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4546 | */ |
| 4547 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4548 | 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] | 4549 | { |
| 4550 | struct lyxp_set_node *item; |
| 4551 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4552 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4553 | /* suppress unused variable warning */ |
| 4554 | (void)options; |
| 4555 | |
| 4556 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4557 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4558 | return LY_SUCCESS; |
| 4559 | } |
| 4560 | |
| 4561 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4562 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4563 | 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] | 4564 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4565 | return LY_EVALID; |
| 4566 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4567 | set_fill_string(set, "", 0); |
| 4568 | return LY_SUCCESS; |
| 4569 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4570 | |
| 4571 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4572 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4573 | |
| 4574 | item = &args[0]->val.nodes[0]; |
| 4575 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4576 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4577 | 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] | 4578 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4579 | } else if (!set->used) { |
| 4580 | set_fill_string(set, "", 0); |
| 4581 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4582 | } |
| 4583 | |
| 4584 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4585 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4586 | |
| 4587 | item = &set->val.nodes[0]; |
| 4588 | } |
| 4589 | |
| 4590 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4591 | case LYXP_NODE_NONE: |
| 4592 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4593 | case LYXP_NODE_ROOT: |
| 4594 | case LYXP_NODE_ROOT_CONFIG: |
| 4595 | case LYXP_NODE_TEXT: |
| 4596 | set_fill_string(set, "", 0); |
| 4597 | break; |
| 4598 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4599 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4600 | if (item->type == LYXP_NODE_ELEM) { |
| 4601 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4602 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4603 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4604 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4608 | break; |
| 4609 | } |
| 4610 | |
| 4611 | return LY_SUCCESS; |
| 4612 | } |
| 4613 | |
| 4614 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4615 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4616 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4617 | * from the argument or the context. |
| 4618 | * |
| 4619 | * @param[in] args Array of arguments. |
| 4620 | * @param[in] arg_count Count of elements in @p args. |
| 4621 | * @param[in,out] set Context and result set at the same time. |
| 4622 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4623 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4624 | */ |
| 4625 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4626 | 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] | 4627 | { |
| 4628 | uint16_t i, new_used; |
| 4629 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4630 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4631 | struct lysc_node_leaf *sleaf; |
| 4632 | LY_ERR rc = LY_SUCCESS; |
| 4633 | |
| 4634 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4635 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4636 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4637 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4638 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4639 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4640 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4641 | 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] | 4642 | } |
| 4643 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4644 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4645 | return rc; |
| 4646 | } |
| 4647 | |
| 4648 | if (arg_count) { |
| 4649 | set_fill_set(set, args[0]); |
| 4650 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4651 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4652 | LY_CHECK_RET(rc); |
| 4653 | |
| 4654 | /* is there any normalization necessary? */ |
| 4655 | for (i = 0; set->val.str[i]; ++i) { |
| 4656 | if (is_xmlws(set->val.str[i])) { |
| 4657 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4658 | have_spaces = 1; |
| 4659 | break; |
| 4660 | } |
| 4661 | space_before = 1; |
| 4662 | } else { |
| 4663 | space_before = 0; |
| 4664 | } |
| 4665 | } |
| 4666 | |
| 4667 | /* yep, there is */ |
| 4668 | if (have_spaces) { |
| 4669 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4670 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4671 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4672 | new_used = 0; |
| 4673 | |
| 4674 | space_before = 0; |
| 4675 | for (i = 0; set->val.str[i]; ++i) { |
| 4676 | if (is_xmlws(set->val.str[i])) { |
| 4677 | if ((i == 0) || space_before) { |
| 4678 | space_before = 1; |
| 4679 | continue; |
| 4680 | } else { |
| 4681 | space_before = 1; |
| 4682 | } |
| 4683 | } else { |
| 4684 | space_before = 0; |
| 4685 | } |
| 4686 | |
| 4687 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4688 | ++new_used; |
| 4689 | } |
| 4690 | |
| 4691 | /* at worst there is one trailing space now */ |
| 4692 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4693 | --new_used; |
| 4694 | } |
| 4695 | |
| 4696 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4697 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4698 | new[new_used] = '\0'; |
| 4699 | |
| 4700 | free(set->val.str); |
| 4701 | set->val.str = new; |
| 4702 | } |
| 4703 | |
| 4704 | return LY_SUCCESS; |
| 4705 | } |
| 4706 | |
| 4707 | /** |
| 4708 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4709 | * with the argument converted to boolean and logically inverted. |
| 4710 | * |
| 4711 | * @param[in] args Array of arguments. |
| 4712 | * @param[in] arg_count Count of elements in @p args. |
| 4713 | * @param[in,out] set Context and result set at the same time. |
| 4714 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4715 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4716 | */ |
| 4717 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4718 | 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] | 4719 | { |
| 4720 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4721 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4722 | return LY_SUCCESS; |
| 4723 | } |
| 4724 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4725 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4726 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4727 | set_fill_boolean(set, 0); |
| 4728 | } else { |
| 4729 | set_fill_boolean(set, 1); |
| 4730 | } |
| 4731 | |
| 4732 | return LY_SUCCESS; |
| 4733 | } |
| 4734 | |
| 4735 | /** |
| 4736 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4737 | * with the number representation of either the argument or the context. |
| 4738 | * |
| 4739 | * @param[in] args Array of arguments. |
| 4740 | * @param[in] arg_count Count of elements in @p args. |
| 4741 | * @param[in,out] set Context and result set at the same time. |
| 4742 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4743 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4744 | */ |
| 4745 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4746 | 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] | 4747 | { |
| 4748 | LY_ERR rc; |
| 4749 | |
| 4750 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4751 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4752 | return LY_SUCCESS; |
| 4753 | } |
| 4754 | |
| 4755 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4756 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4757 | LY_CHECK_RET(rc); |
| 4758 | set_fill_set(set, args[0]); |
| 4759 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4760 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4761 | LY_CHECK_RET(rc); |
| 4762 | } |
| 4763 | |
| 4764 | return LY_SUCCESS; |
| 4765 | } |
| 4766 | |
| 4767 | /** |
| 4768 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4769 | * with the context position. |
| 4770 | * |
| 4771 | * @param[in] args Array of arguments. |
| 4772 | * @param[in] arg_count Count of elements in @p args. |
| 4773 | * @param[in,out] set Context and result set at the same time. |
| 4774 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4775 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4776 | */ |
| 4777 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4778 | 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] | 4779 | { |
| 4780 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4781 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4782 | return LY_SUCCESS; |
| 4783 | } |
| 4784 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4785 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4786 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4787 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4788 | } else if (!set->used) { |
| 4789 | set_fill_number(set, 0); |
| 4790 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | set_fill_number(set, set->ctx_pos); |
| 4794 | |
| 4795 | /* UNUSED in 'Release' build type */ |
| 4796 | (void)options; |
| 4797 | return LY_SUCCESS; |
| 4798 | } |
| 4799 | |
| 4800 | /** |
| 4801 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4802 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4803 | * YANG 1.1 RFC section 10.2.1. |
| 4804 | * |
| 4805 | * @param[in] args Array of arguments. |
| 4806 | * @param[in] arg_count Count of elements in @p args. |
| 4807 | * @param[in,out] set Context and result set at the same time. |
| 4808 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4809 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4810 | */ |
| 4811 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4812 | 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] | 4813 | { |
| 4814 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4815 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4816 | LY_ERR rc = LY_SUCCESS; |
| 4817 | struct ly_err_item *err; |
| 4818 | |
| 4819 | if (options & LYXP_SCNODE_ALL) { |
| 4820 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4821 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4822 | 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] | 4823 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4824 | 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] | 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4829 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4830 | 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] | 4831 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4832 | 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] | 4833 | } |
| 4834 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4835 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4836 | return rc; |
| 4837 | } |
| 4838 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4839 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4840 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4841 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4842 | LY_CHECK_RET(rc); |
| 4843 | |
| 4844 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4845 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4846 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4847 | 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] | 4848 | if (set->cur_node) { |
| 4849 | LOG_LOCBACK(0, 1, 0, 0); |
| 4850 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4851 | if (rc != LY_SUCCESS) { |
| 4852 | LY_ARRAY_FREE(patterns); |
| 4853 | return rc; |
| 4854 | } |
| 4855 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4856 | 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] | 4857 | pcre2_code_free((*pattern)->code); |
| 4858 | free(*pattern); |
| 4859 | LY_ARRAY_FREE(patterns); |
| 4860 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4861 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4862 | ly_err_free(err); |
| 4863 | return rc; |
| 4864 | } |
| 4865 | |
| 4866 | if (rc == LY_EVALID) { |
| 4867 | ly_err_free(err); |
| 4868 | set_fill_boolean(set, 0); |
| 4869 | } else { |
| 4870 | set_fill_boolean(set, 1); |
| 4871 | } |
| 4872 | |
| 4873 | return LY_SUCCESS; |
| 4874 | } |
| 4875 | |
| 4876 | /** |
| 4877 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4878 | * with the rounded first argument. For details refer to |
| 4879 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4880 | * |
| 4881 | * @param[in] args Array of arguments. |
| 4882 | * @param[in] arg_count Count of elements in @p args. |
| 4883 | * @param[in,out] set Context and result set at the same time. |
| 4884 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4885 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4886 | */ |
| 4887 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4888 | 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] | 4889 | { |
| 4890 | struct lysc_node_leaf *sleaf; |
| 4891 | LY_ERR rc = LY_SUCCESS; |
| 4892 | |
| 4893 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4894 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4895 | 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] | 4896 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4897 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4898 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4899 | sleaf->name); |
| 4900 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4901 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4902 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4903 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4904 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4905 | return rc; |
| 4906 | } |
| 4907 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4908 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4909 | LY_CHECK_RET(rc); |
| 4910 | |
| 4911 | /* cover only the cases where floor can't be used */ |
| 4912 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4913 | set_fill_number(set, -0.0f); |
| 4914 | } else { |
| 4915 | args[0]->val.num += 0.5; |
| 4916 | rc = xpath_floor(args, 1, args[0], options); |
| 4917 | LY_CHECK_RET(rc); |
| 4918 | set_fill_number(set, args[0]->val.num); |
| 4919 | } |
| 4920 | |
| 4921 | return LY_SUCCESS; |
| 4922 | } |
| 4923 | |
| 4924 | /** |
| 4925 | * @brief Execute the XPath starts-with(string, string) function. |
| 4926 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4927 | * the prefix of the first or not. |
| 4928 | * |
| 4929 | * @param[in] args Array of arguments. |
| 4930 | * @param[in] arg_count Count of elements in @p args. |
| 4931 | * @param[in,out] set Context and result set at the same time. |
| 4932 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4933 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4934 | */ |
| 4935 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4936 | 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] | 4937 | { |
| 4938 | struct lysc_node_leaf *sleaf; |
| 4939 | LY_ERR rc = LY_SUCCESS; |
| 4940 | |
| 4941 | if (options & LYXP_SCNODE_ALL) { |
| 4942 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4943 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4944 | 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] | 4945 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4946 | 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] | 4947 | } |
| 4948 | } |
| 4949 | |
| 4950 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4951 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4952 | 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] | 4953 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4954 | 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] | 4955 | } |
| 4956 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4957 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4958 | return rc; |
| 4959 | } |
| 4960 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4961 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4962 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4963 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4964 | LY_CHECK_RET(rc); |
| 4965 | |
| 4966 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4967 | set_fill_boolean(set, 0); |
| 4968 | } else { |
| 4969 | set_fill_boolean(set, 1); |
| 4970 | } |
| 4971 | |
| 4972 | return LY_SUCCESS; |
| 4973 | } |
| 4974 | |
| 4975 | /** |
| 4976 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4977 | * with the string representation of either the argument or the context. |
| 4978 | * |
| 4979 | * @param[in] args Array of arguments. |
| 4980 | * @param[in] arg_count Count of elements in @p args. |
| 4981 | * @param[in,out] set Context and result set at the same time. |
| 4982 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4983 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4984 | */ |
| 4985 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4986 | 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] | 4987 | { |
| 4988 | LY_ERR rc; |
| 4989 | |
| 4990 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4991 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4992 | return LY_SUCCESS; |
| 4993 | } |
| 4994 | |
| 4995 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4996 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4997 | LY_CHECK_RET(rc); |
| 4998 | set_fill_set(set, args[0]); |
| 4999 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5000 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5001 | LY_CHECK_RET(rc); |
| 5002 | } |
| 5003 | |
| 5004 | return LY_SUCCESS; |
| 5005 | } |
| 5006 | |
| 5007 | /** |
| 5008 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5009 | * with the length of the string in either the argument or the context. |
| 5010 | * |
| 5011 | * @param[in] args Array of arguments. |
| 5012 | * @param[in] arg_count Count of elements in @p args. |
| 5013 | * @param[in,out] set Context and result set at the same time. |
| 5014 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5015 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5016 | */ |
| 5017 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5018 | 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] | 5019 | { |
| 5020 | struct lysc_node_leaf *sleaf; |
| 5021 | LY_ERR rc = LY_SUCCESS; |
| 5022 | |
| 5023 | if (options & LYXP_SCNODE_ALL) { |
| 5024 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5025 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5026 | 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] | 5027 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5028 | 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] | 5029 | } |
| 5030 | } |
| 5031 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5032 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5033 | 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] | 5034 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5035 | 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] | 5036 | } |
| 5037 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5038 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5039 | return rc; |
| 5040 | } |
| 5041 | |
| 5042 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5043 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5044 | LY_CHECK_RET(rc); |
| 5045 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5046 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5047 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5048 | LY_CHECK_RET(rc); |
| 5049 | set_fill_number(set, strlen(set->val.str)); |
| 5050 | } |
| 5051 | |
| 5052 | return LY_SUCCESS; |
| 5053 | } |
| 5054 | |
| 5055 | /** |
| 5056 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5057 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5058 | * on the second argument index ending on the third argument index, |
| 5059 | * indexed from 1. For exact definition refer to |
| 5060 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5061 | * |
| 5062 | * @param[in] args Array of arguments. |
| 5063 | * @param[in] arg_count Count of elements in @p args. |
| 5064 | * @param[in,out] set Context and result set at the same time. |
| 5065 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5066 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5067 | */ |
| 5068 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5069 | 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] | 5070 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5071 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5072 | uint16_t str_start, str_len, pos; |
| 5073 | struct lysc_node_leaf *sleaf; |
| 5074 | LY_ERR rc = LY_SUCCESS; |
| 5075 | |
| 5076 | if (options & LYXP_SCNODE_ALL) { |
| 5077 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5078 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5079 | 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] | 5080 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5081 | 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] | 5082 | } |
| 5083 | } |
| 5084 | |
| 5085 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5086 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5087 | 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] | 5088 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5089 | 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] | 5090 | } |
| 5091 | } |
| 5092 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5093 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5094 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5095 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5096 | 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] | 5097 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5098 | 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] | 5099 | } |
| 5100 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5101 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5102 | return rc; |
| 5103 | } |
| 5104 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5105 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5106 | LY_CHECK_RET(rc); |
| 5107 | |
| 5108 | /* start */ |
| 5109 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5110 | return -1; |
| 5111 | } |
| 5112 | if (isfinite(args[1]->val.num)) { |
| 5113 | start = args[1]->val.num - 1; |
| 5114 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5115 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5116 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5117 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5118 | } |
| 5119 | |
| 5120 | /* len */ |
| 5121 | if (arg_count == 3) { |
| 5122 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5123 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5124 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5125 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5126 | } else if (isfinite(args[2]->val.num)) { |
| 5127 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5128 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5129 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5130 | } |
| 5131 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5132 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | /* find matching character positions */ |
| 5136 | str_start = 0; |
| 5137 | str_len = 0; |
| 5138 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5139 | if (pos < start) { |
| 5140 | ++str_start; |
| 5141 | } else if (pos < start + len) { |
| 5142 | ++str_len; |
| 5143 | } else { |
| 5144 | break; |
| 5145 | } |
| 5146 | } |
| 5147 | |
| 5148 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5149 | return LY_SUCCESS; |
| 5150 | } |
| 5151 | |
| 5152 | /** |
| 5153 | * @brief Execute the XPath substring-after(string, string) function. |
| 5154 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5155 | * of the second argument in the first or an empty string. |
| 5156 | * |
| 5157 | * @param[in] args Array of arguments. |
| 5158 | * @param[in] arg_count Count of elements in @p args. |
| 5159 | * @param[in,out] set Context and result set at the same time. |
| 5160 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5161 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5162 | */ |
| 5163 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5164 | 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] | 5165 | { |
| 5166 | char *ptr; |
| 5167 | struct lysc_node_leaf *sleaf; |
| 5168 | LY_ERR rc = LY_SUCCESS; |
| 5169 | |
| 5170 | if (options & LYXP_SCNODE_ALL) { |
| 5171 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5172 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5173 | 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] | 5174 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5175 | 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] | 5176 | } |
| 5177 | } |
| 5178 | |
| 5179 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5180 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5181 | 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] | 5182 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5183 | 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] | 5184 | } |
| 5185 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5186 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5187 | return rc; |
| 5188 | } |
| 5189 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5190 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5191 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5192 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5193 | LY_CHECK_RET(rc); |
| 5194 | |
| 5195 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5196 | if (ptr) { |
| 5197 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5198 | } else { |
| 5199 | set_fill_string(set, "", 0); |
| 5200 | } |
| 5201 | |
| 5202 | return LY_SUCCESS; |
| 5203 | } |
| 5204 | |
| 5205 | /** |
| 5206 | * @brief Execute the XPath substring-before(string, string) function. |
| 5207 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5208 | * of the second argument in the first or an empty string. |
| 5209 | * |
| 5210 | * @param[in] args Array of arguments. |
| 5211 | * @param[in] arg_count Count of elements in @p args. |
| 5212 | * @param[in,out] set Context and result set at the same time. |
| 5213 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5214 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5215 | */ |
| 5216 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5217 | 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] | 5218 | { |
| 5219 | char *ptr; |
| 5220 | struct lysc_node_leaf *sleaf; |
| 5221 | LY_ERR rc = LY_SUCCESS; |
| 5222 | |
| 5223 | if (options & LYXP_SCNODE_ALL) { |
| 5224 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5225 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5226 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5227 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5228 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5229 | } |
| 5230 | } |
| 5231 | |
| 5232 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5233 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5234 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5235 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5236 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5237 | } |
| 5238 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5239 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5240 | return rc; |
| 5241 | } |
| 5242 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5243 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5244 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5245 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5246 | LY_CHECK_RET(rc); |
| 5247 | |
| 5248 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5249 | if (ptr) { |
| 5250 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5251 | } else { |
| 5252 | set_fill_string(set, "", 0); |
| 5253 | } |
| 5254 | |
| 5255 | return LY_SUCCESS; |
| 5256 | } |
| 5257 | |
| 5258 | /** |
| 5259 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5260 | * with the sum of all the nodes in the context. |
| 5261 | * |
| 5262 | * @param[in] args Array of arguments. |
| 5263 | * @param[in] arg_count Count of elements in @p args. |
| 5264 | * @param[in,out] set Context and result set at the same time. |
| 5265 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5266 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5267 | */ |
| 5268 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5269 | 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] | 5270 | { |
| 5271 | long double num; |
| 5272 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5273 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5274 | struct lyxp_set set_item; |
| 5275 | struct lysc_node_leaf *sleaf; |
| 5276 | LY_ERR rc = LY_SUCCESS; |
| 5277 | |
| 5278 | if (options & LYXP_SCNODE_ALL) { |
| 5279 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5280 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5281 | 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] | 5282 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5283 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5284 | 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] | 5285 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5286 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5287 | 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] | 5288 | } |
| 5289 | } |
| 5290 | } |
| 5291 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5292 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5293 | return rc; |
| 5294 | } |
| 5295 | |
| 5296 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5297 | |
| 5298 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5299 | 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] | 5300 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5301 | } else if (!args[0]->used) { |
| 5302 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5303 | } |
| 5304 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5305 | set_init(&set_item, set); |
| 5306 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5307 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5308 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5309 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5310 | |
| 5311 | set_item.used = 1; |
| 5312 | set_item.size = 1; |
| 5313 | |
| 5314 | for (i = 0; i < args[0]->used; ++i) { |
| 5315 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5316 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5317 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5318 | LY_CHECK_RET(rc); |
| 5319 | num = cast_string_to_number(str); |
| 5320 | free(str); |
| 5321 | set->val.num += num; |
| 5322 | } |
| 5323 | |
| 5324 | free(set_item.val.nodes); |
| 5325 | |
| 5326 | return LY_SUCCESS; |
| 5327 | } |
| 5328 | |
| 5329 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5330 | * @brief Execute the XPath translate(string, string, string) function. |
| 5331 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5332 | * from the second argument replaced by those on the corresponding |
| 5333 | * positions in the third argument. |
| 5334 | * |
| 5335 | * @param[in] args Array of arguments. |
| 5336 | * @param[in] arg_count Count of elements in @p args. |
| 5337 | * @param[in,out] set Context and result set at the same time. |
| 5338 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5339 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5340 | */ |
| 5341 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5342 | 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] | 5343 | { |
| 5344 | uint16_t i, j, new_used; |
| 5345 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5346 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5347 | struct lysc_node_leaf *sleaf; |
| 5348 | LY_ERR rc = LY_SUCCESS; |
| 5349 | |
| 5350 | if (options & LYXP_SCNODE_ALL) { |
| 5351 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5352 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5353 | 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] | 5354 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5355 | 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] | 5356 | } |
| 5357 | } |
| 5358 | |
| 5359 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5360 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5361 | 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] | 5362 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5363 | 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] | 5364 | } |
| 5365 | } |
| 5366 | |
| 5367 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5368 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5369 | 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] | 5370 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5371 | 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] | 5372 | } |
| 5373 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5374 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5375 | return rc; |
| 5376 | } |
| 5377 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5378 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5379 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5380 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5381 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5382 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5383 | LY_CHECK_RET(rc); |
| 5384 | |
| 5385 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5386 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5387 | new_used = 0; |
| 5388 | |
| 5389 | have_removed = 0; |
| 5390 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5391 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5392 | |
| 5393 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5394 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5395 | /* removing this char */ |
| 5396 | if (j >= strlen(args[2]->val.str)) { |
| 5397 | have_removed = 1; |
| 5398 | found = 1; |
| 5399 | break; |
| 5400 | } |
| 5401 | /* replacing this char */ |
| 5402 | new[new_used] = args[2]->val.str[j]; |
| 5403 | ++new_used; |
| 5404 | found = 1; |
| 5405 | break; |
| 5406 | } |
| 5407 | } |
| 5408 | |
| 5409 | /* copying this char */ |
| 5410 | if (!found) { |
| 5411 | new[new_used] = args[0]->val.str[i]; |
| 5412 | ++new_used; |
| 5413 | } |
| 5414 | } |
| 5415 | |
| 5416 | if (have_removed) { |
| 5417 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5418 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5419 | } |
| 5420 | new[new_used] = '\0'; |
| 5421 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5422 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5423 | set->type = LYXP_SET_STRING; |
| 5424 | set->val.str = new; |
| 5425 | |
| 5426 | return LY_SUCCESS; |
| 5427 | } |
| 5428 | |
| 5429 | /** |
| 5430 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5431 | * with true value. |
| 5432 | * |
| 5433 | * @param[in] args Array of arguments. |
| 5434 | * @param[in] arg_count Count of elements in @p args. |
| 5435 | * @param[in,out] set Context and result set at the same time. |
| 5436 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5437 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5438 | */ |
| 5439 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5440 | 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] | 5441 | { |
| 5442 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5443 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5444 | return LY_SUCCESS; |
| 5445 | } |
| 5446 | |
| 5447 | set_fill_boolean(set, 1); |
| 5448 | return LY_SUCCESS; |
| 5449 | } |
| 5450 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5451 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5452 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5453 | * with only nodes from the context. |
| 5454 | * |
| 5455 | * @param[in,out] set Context and result set at the same time. |
| 5456 | * @param[in] axis Axis to search on. |
| 5457 | * @param[in] options XPath options. |
| 5458 | * @return LY_ERR |
| 5459 | */ |
| 5460 | static LY_ERR |
| 5461 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5462 | { |
| 5463 | if (options & LYXP_SCNODE_ALL) { |
| 5464 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5465 | } |
| 5466 | |
| 5467 | if (set->type != LYXP_SET_NODE_SET) { |
| 5468 | lyxp_set_free_content(set); |
| 5469 | return LY_SUCCESS; |
| 5470 | } |
| 5471 | |
| 5472 | /* just like moving to a node with no restrictions */ |
| 5473 | return moveto_node(set, NULL, NULL, axis, options); |
| 5474 | } |
| 5475 | |
| 5476 | /** |
| 5477 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5478 | * with the text content of the nodes in the context. |
| 5479 | * |
| 5480 | * @param[in,out] set Context and result set at the same time. |
| 5481 | * @param[in] axis Axis to search on. |
| 5482 | * @param[in] options XPath options. |
| 5483 | * @return LY_ERR |
| 5484 | */ |
| 5485 | static LY_ERR |
| 5486 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5487 | { |
| 5488 | uint32_t i; |
| 5489 | |
| 5490 | if (options & LYXP_SCNODE_ALL) { |
| 5491 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5492 | return LY_SUCCESS; |
| 5493 | } |
| 5494 | |
| 5495 | if (set->type != LYXP_SET_NODE_SET) { |
| 5496 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5497 | return LY_EVALID; |
| 5498 | } |
| 5499 | |
| 5500 | if (axis != LYXP_AXIS_CHILD) { |
| 5501 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5502 | lyxp_set_free_content(set); |
| 5503 | return LY_SUCCESS; |
| 5504 | } |
| 5505 | |
| 5506 | for (i = 0; i < set->used; ++i) { |
| 5507 | switch (set->val.nodes[i].type) { |
| 5508 | case LYXP_NODE_NONE: |
| 5509 | LOGINT_RET(set->ctx); |
| 5510 | case LYXP_NODE_ELEM: |
| 5511 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5512 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5513 | break; |
| 5514 | } |
| 5515 | /* fall through */ |
| 5516 | case LYXP_NODE_ROOT: |
| 5517 | case LYXP_NODE_ROOT_CONFIG: |
| 5518 | case LYXP_NODE_TEXT: |
| 5519 | case LYXP_NODE_META: |
| 5520 | set_remove_node_none(set, i); |
| 5521 | break; |
| 5522 | } |
| 5523 | } |
| 5524 | set_remove_nodes_none(set); |
| 5525 | |
| 5526 | return LY_SUCCESS; |
| 5527 | } |
| 5528 | |
| 5529 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5530 | * @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] | 5531 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5532 | * XPath @p set is expected to be a (sc)node set! |
| 5533 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5534 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5535 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5536 | * @param[in] set Set with general XPath context. |
| 5537 | * @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] | 5538 | * @param[out] moveto_mod Expected module of a matching node. |
| 5539 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5540 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5541 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5542 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5543 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5544 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5545 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5546 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5547 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5548 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5549 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5550 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5551 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5552 | /* specific module */ |
| 5553 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5554 | 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] | 5555 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5556 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5557 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5558 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5559 | return LY_EVALID; |
| 5560 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5561 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5562 | *qname += pref_len + 1; |
| 5563 | *qname_len -= pref_len + 1; |
| 5564 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5565 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5566 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5567 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5568 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5569 | case LY_VALUE_SCHEMA: |
| 5570 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5571 | /* current module */ |
| 5572 | mod = set->cur_mod; |
| 5573 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5574 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5575 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5576 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5577 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5578 | /* inherit parent (context node) module */ |
| 5579 | if (ctx_scnode) { |
| 5580 | mod = ctx_scnode->module; |
| 5581 | } else { |
| 5582 | mod = NULL; |
| 5583 | } |
| 5584 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5585 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5586 | /* all nodes need to be prefixed */ |
| 5587 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5588 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5589 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5590 | } |
| 5591 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5592 | *moveto_mod = mod; |
| 5593 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5597 | * @brief Move context @p set to the root. Handles absolute path. |
| 5598 | * Result is LYXP_SET_NODE_SET. |
| 5599 | * |
| 5600 | * @param[in,out] set Set to use. |
| 5601 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5602 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5603 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5604 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5605 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5606 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5607 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5608 | |
| 5609 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5610 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5611 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5612 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5613 | set->type = LYXP_SET_NODE_SET; |
| 5614 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5615 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5616 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5617 | |
| 5618 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5619 | } |
| 5620 | |
| 5621 | /** |
| 5622 | * @brief Check @p node as a part of NameTest processing. |
| 5623 | * |
| 5624 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5625 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5626 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5627 | * @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] | 5628 | * @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] | 5629 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5630 | * @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] | 5631 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5632 | */ |
| 5633 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5634 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5635 | 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] | 5636 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5637 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5638 | assert(node_type == set->root_type); |
| 5639 | |
| 5640 | if (node_name || moveto_mod) { |
| 5641 | /* root will not match a specific node */ |
| 5642 | return LY_ENOT; |
| 5643 | } |
| 5644 | return LY_SUCCESS; |
| 5645 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5646 | /* other types will not match */ |
| 5647 | return LY_ENOT; |
| 5648 | } |
| 5649 | |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5650 | if (!node->schema) { |
| 5651 | /* opaque node never matches */ |
| 5652 | return LY_ENOT; |
| 5653 | } |
| 5654 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5655 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5656 | if (moveto_mod) { |
| 5657 | if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) { |
| 5658 | return LY_ENOT; |
| 5659 | } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) { |
| 5660 | return LY_ENOT; |
| 5661 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5662 | } |
| 5663 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5664 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5665 | 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] | 5666 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5667 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5668 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5669 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5670 | } |
| 5671 | |
| 5672 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5673 | if (node_name) { |
| 5674 | if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) { |
| 5675 | return LY_ENOT; |
| 5676 | } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) { |
| 5677 | return LY_ENOT; |
| 5678 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5679 | } |
| 5680 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5681 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5682 | 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] | 5683 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5684 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5685 | |
| 5686 | /* match */ |
| 5687 | return LY_SUCCESS; |
| 5688 | } |
| 5689 | |
| 5690 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5691 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5692 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5693 | * @param[in] iter Last returned node. |
| 5694 | * @param[in] stop Node to stop the search on and not return. |
| 5695 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5696 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5697 | static const struct lyd_node * |
| 5698 | 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] | 5699 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5700 | const struct lyd_node *next = NULL; |
| 5701 | |
| 5702 | /* 1) child */ |
| 5703 | next = lyd_child(iter); |
| 5704 | if (!next) { |
| 5705 | if (iter == stop) { |
| 5706 | /* reached stop, no more descendants */ |
| 5707 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5708 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5709 | /* 2) child next sibling */ |
| 5710 | next = iter->next; |
| 5711 | } |
| 5712 | while (!next) { |
| 5713 | iter = lyd_parent(iter); |
| 5714 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5715 | return NULL; |
| 5716 | } |
| 5717 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5718 | } |
| 5719 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5720 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5721 | } |
| 5722 | |
| 5723 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5724 | * @brief Get the next node in a backward DFS. |
| 5725 | * |
| 5726 | * @param[in] iter Last returned node. |
| 5727 | * @param[in] stop Node to stop the search on and not return. |
| 5728 | * @return Next node, NULL if there are no more. |
| 5729 | */ |
| 5730 | static const struct lyd_node * |
| 5731 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5732 | { |
| 5733 | const struct lyd_node *next = NULL; |
| 5734 | |
| 5735 | /* 1) previous sibling innermost last child */ |
| 5736 | next = iter->prev->next ? iter->prev : NULL; |
| 5737 | while (next && lyd_child(next)) { |
| 5738 | next = lyd_child(next); |
| 5739 | next = next->prev; |
| 5740 | } |
| 5741 | |
| 5742 | if (!next) { |
| 5743 | /* 2) parent */ |
| 5744 | iter = lyd_parent(iter); |
| 5745 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5746 | return NULL; |
| 5747 | } |
| 5748 | next = iter; |
| 5749 | } |
| 5750 | |
| 5751 | return next; |
| 5752 | } |
| 5753 | |
| 5754 | /** |
| 5755 | * @brief Get the first node on an axis for a context node. |
| 5756 | * |
| 5757 | * @param[in,out] iter NULL, updated to the next node. |
| 5758 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5759 | * @param[in] node Context node. |
| 5760 | * @param[in] node_type Type of @p node. |
| 5761 | * @param[in] axis Axis to use. |
| 5762 | * @param[in] set XPath set with the general context. |
| 5763 | * @return LY_SUCCESS on success. |
| 5764 | * @return LY_ENOTFOUND if no next node found. |
| 5765 | */ |
| 5766 | static LY_ERR |
| 5767 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5768 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5769 | { |
| 5770 | const struct lyd_node *next = NULL; |
| 5771 | enum lyxp_node_type next_type = 0; |
| 5772 | |
| 5773 | assert(!*iter); |
| 5774 | assert(!*iter_type); |
| 5775 | |
| 5776 | switch (axis) { |
| 5777 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5778 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5779 | case LYXP_AXIS_SELF: |
| 5780 | /* return the context node */ |
| 5781 | next = node; |
| 5782 | next_type = node_type; |
| 5783 | break; |
| 5784 | |
| 5785 | case LYXP_AXIS_ANCESTOR: |
| 5786 | case LYXP_AXIS_PARENT: |
| 5787 | if (node_type == LYXP_NODE_ELEM) { |
| 5788 | next = lyd_parent(node); |
| 5789 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5790 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5791 | next = node; |
| 5792 | next_type = LYXP_NODE_ELEM; |
| 5793 | } else if (node_type == LYXP_NODE_META) { |
| 5794 | next = ((struct lyd_meta *)node)->parent; |
| 5795 | next_type = LYXP_NODE_ELEM; |
| 5796 | } /* else root does not have a parent */ |
| 5797 | break; |
| 5798 | |
| 5799 | case LYXP_AXIS_CHILD: |
| 5800 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5801 | assert(!node); |
| 5802 | |
| 5803 | /* search in all the trees */ |
| 5804 | next = set->tree; |
| 5805 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5806 | } else { |
| 5807 | /* search in children */ |
| 5808 | next = lyd_child(node); |
| 5809 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5810 | } |
| 5811 | break; |
| 5812 | |
| 5813 | case LYXP_AXIS_DESCENDANT: |
| 5814 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5815 | /* top-level nodes */ |
| 5816 | next = set->tree; |
| 5817 | next_type = LYXP_NODE_ELEM; |
| 5818 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5819 | /* start from the context node */ |
| 5820 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5821 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5822 | } /* else no children */ |
| 5823 | break; |
| 5824 | |
| 5825 | case LYXP_AXIS_FOLLOWING: |
| 5826 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5827 | if (node_type == LYXP_NODE_ELEM) { |
| 5828 | /* first next sibling */ |
| 5829 | next = node->next; |
| 5830 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5831 | } /* else no sibling */ |
| 5832 | break; |
| 5833 | |
| 5834 | case LYXP_AXIS_PRECEDING: |
| 5835 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5836 | /* skip ancestors */ |
| 5837 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5838 | assert(next); |
| 5839 | next_type = LYXP_NODE_ELEM; |
| 5840 | } /* else no sibling */ |
| 5841 | break; |
| 5842 | |
| 5843 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5844 | if (node_type == LYXP_NODE_ELEM) { |
| 5845 | /* first previous sibling */ |
| 5846 | next = node->prev->next ? node->prev : NULL; |
| 5847 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5848 | } /* else no sibling */ |
| 5849 | break; |
| 5850 | |
| 5851 | case LYXP_AXIS_ATTRIBUTE: |
| 5852 | /* handled specially */ |
| 5853 | assert(0); |
| 5854 | LOGINT(set->ctx); |
| 5855 | break; |
| 5856 | } |
| 5857 | |
| 5858 | *iter = next; |
| 5859 | *iter_type = next_type; |
| 5860 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5861 | } |
| 5862 | |
| 5863 | /** |
| 5864 | * @brief Iterate over all nodes on an axis for a context node. |
| 5865 | * |
| 5866 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5867 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5868 | * @param[in] node Context node. |
| 5869 | * @param[in] node_type Type of @p node. |
| 5870 | * @param[in] axis Axis to use. |
| 5871 | * @param[in] set XPath set with the general context. |
| 5872 | * @return LY_SUCCESS on success. |
| 5873 | * @return LY_ENOTFOUND if no next node found. |
| 5874 | */ |
| 5875 | static LY_ERR |
| 5876 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5877 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5878 | { |
| 5879 | const struct lyd_node *next = NULL; |
| 5880 | enum lyxp_node_type next_type = 0; |
| 5881 | |
| 5882 | if (!*iter_type) { |
| 5883 | /* first returned node */ |
| 5884 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5885 | } |
| 5886 | |
| 5887 | switch (axis) { |
| 5888 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5889 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5890 | /* fake first ancestor, we returned self before */ |
| 5891 | *iter = NULL; |
| 5892 | *iter_type = 0; |
| 5893 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 5894 | } /* else continue ancestor */ |
| 5895 | |
| 5896 | /* fallthrough */ |
| 5897 | case LYXP_AXIS_ANCESTOR: |
| 5898 | if (*iter_type == LYXP_NODE_ELEM) { |
| 5899 | /* iter parent */ |
| 5900 | next = lyd_parent(*iter); |
| 5901 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5902 | } /* else root, no ancestors */ |
| 5903 | break; |
| 5904 | |
| 5905 | case LYXP_AXIS_CHILD: |
| 5906 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5907 | |
| 5908 | /* next sibling (child) */ |
| 5909 | next = (*iter)->next; |
| 5910 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5911 | break; |
| 5912 | |
| 5913 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5914 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5915 | /* fake first descendant, we returned self before */ |
| 5916 | *iter = NULL; |
| 5917 | *iter_type = 0; |
| 5918 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 5919 | } /* else continue descendant */ |
| 5920 | |
| 5921 | /* fallthrough */ |
| 5922 | case LYXP_AXIS_DESCENDANT: |
| 5923 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5924 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 5925 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5926 | break; |
| 5927 | |
| 5928 | case LYXP_AXIS_FOLLOWING: |
| 5929 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5930 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 5931 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5932 | break; |
| 5933 | |
| 5934 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5935 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5936 | |
| 5937 | /* next sibling */ |
| 5938 | next = (*iter)->next; |
| 5939 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5940 | break; |
| 5941 | |
| 5942 | case LYXP_AXIS_PARENT: |
| 5943 | case LYXP_AXIS_SELF: |
| 5944 | /* parent/self was returned before */ |
| 5945 | break; |
| 5946 | |
| 5947 | case LYXP_AXIS_PRECEDING: |
| 5948 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5949 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 5950 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5951 | break; |
| 5952 | |
| 5953 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5954 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5955 | |
| 5956 | /* previous sibling */ |
| 5957 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 5958 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5959 | break; |
| 5960 | |
| 5961 | case LYXP_AXIS_ATTRIBUTE: |
| 5962 | /* handled specially */ |
| 5963 | assert(0); |
| 5964 | LOGINT(set->ctx); |
| 5965 | break; |
| 5966 | } |
| 5967 | |
| 5968 | *iter = next; |
| 5969 | *iter_type = next_type; |
| 5970 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5971 | } |
| 5972 | |
| 5973 | /** |
| 5974 | * @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] | 5975 | * |
| 5976 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5977 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5978 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5979 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5980 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5981 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5982 | */ |
| 5983 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5984 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 5985 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5986 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5987 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5988 | const struct lyd_node *iter; |
| 5989 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5990 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5991 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5992 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5993 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5994 | return LY_SUCCESS; |
| 5995 | } |
| 5996 | |
| 5997 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5998 | 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] | 5999 | return LY_EVALID; |
| 6000 | } |
| 6001 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6002 | /* init result set */ |
| 6003 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6004 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6005 | for (i = 0; i < set->used; ++i) { |
| 6006 | /* iterate over all the nodes on the axis of the node */ |
| 6007 | iter = NULL; |
| 6008 | iter_type = 0; |
| 6009 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6010 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6011 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6012 | rc = r; |
| 6013 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6014 | } else if (r) { |
| 6015 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6016 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6017 | |
| 6018 | /* check for duplicates if they are possible */ |
| 6019 | switch (axis) { |
| 6020 | case LYXP_AXIS_ANCESTOR: |
| 6021 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6022 | case LYXP_AXIS_DESCENDANT: |
| 6023 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6024 | case LYXP_AXIS_FOLLOWING: |
| 6025 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6026 | case LYXP_AXIS_PARENT: |
| 6027 | case LYXP_AXIS_PRECEDING: |
| 6028 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6029 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6030 | continue; |
| 6031 | } |
| 6032 | break; |
| 6033 | case LYXP_AXIS_CHILD: |
| 6034 | case LYXP_AXIS_SELF: |
| 6035 | break; |
| 6036 | case LYXP_AXIS_ATTRIBUTE: |
| 6037 | /* handled specially */ |
| 6038 | assert(0); |
| 6039 | LOGINT(set->ctx); |
| 6040 | break; |
| 6041 | } |
| 6042 | |
| 6043 | /* matching node */ |
| 6044 | set_insert_node(&result, iter, 0, iter_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6045 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6047 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6048 | /* move result to the set */ |
| 6049 | lyxp_set_free_content(set); |
| 6050 | *set = result; |
| 6051 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6052 | |
| 6053 | /* sort the final set */ |
| 6054 | set_sort(set); |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6055 | |
| 6056 | cleanup: |
| 6057 | lyxp_set_free_content(&result); |
| 6058 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6062 | * @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] | 6063 | * |
| 6064 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6065 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6066 | * @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] | 6067 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6068 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6069 | */ |
| 6070 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6071 | 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] | 6072 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6073 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6074 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6075 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6076 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6077 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6078 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6079 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6080 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6081 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6082 | /* init result set */ |
| 6083 | set_init(&result, set); |
| 6084 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6085 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6086 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6087 | } |
| 6088 | |
| 6089 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6090 | 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] | 6091 | ret = LY_EVALID; |
| 6092 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6093 | } |
| 6094 | |
| 6095 | /* context check for all the nodes since we have the schema node */ |
| 6096 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6097 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6098 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6099 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6100 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6101 | lyxp_set_free_content(set); |
| 6102 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6103 | } |
| 6104 | |
| 6105 | /* create specific data instance if needed */ |
| 6106 | if (scnode->nodetype == LYS_LIST) { |
| 6107 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 6108 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6109 | 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] | 6110 | } |
| 6111 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6112 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6113 | siblings = NULL; |
| 6114 | |
| 6115 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6116 | assert(!set->val.nodes[i].node); |
| 6117 | |
| 6118 | /* search in all the trees */ |
| 6119 | siblings = set->tree; |
| 6120 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6121 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6122 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6123 | } |
| 6124 | |
| 6125 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6126 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6127 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6128 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6129 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6130 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6131 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6132 | |
| 6133 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6134 | 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] | 6135 | ret = LY_EINCOMPLETE; |
| 6136 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6137 | } |
| 6138 | |
| 6139 | if (sub) { |
| 6140 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6141 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6142 | } |
| 6143 | } |
| 6144 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6145 | /* move result to the set */ |
| 6146 | lyxp_set_free_content(set); |
| 6147 | *set = result; |
| 6148 | result.type = LYXP_SET_NUMBER; |
| 6149 | assert(!set_sort(set)); |
| 6150 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6151 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6152 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6153 | lyd_free_tree(inst); |
| 6154 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6155 | } |
| 6156 | |
| 6157 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6158 | * @brief Check @p node as a part of schema NameTest processing. |
| 6159 | * |
| 6160 | * @param[in] node Schema node to check. |
| 6161 | * @param[in] ctx_scnode Context node. |
| 6162 | * @param[in] set Set to read general context from. |
| 6163 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6164 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6165 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6166 | */ |
| 6167 | static LY_ERR |
| 6168 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6169 | const char *node_name, const struct lys_module *moveto_mod) |
| 6170 | { |
| 6171 | if (!moveto_mod && node_name) { |
| 6172 | switch (set->format) { |
| 6173 | case LY_VALUE_SCHEMA: |
| 6174 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6175 | /* use current module */ |
| 6176 | moveto_mod = set->cur_mod; |
| 6177 | break; |
| 6178 | case LY_VALUE_JSON: |
| 6179 | case LY_VALUE_LYB: |
| 6180 | case LY_VALUE_STR_NS: |
| 6181 | /* inherit module of the context node, if any */ |
| 6182 | if (ctx_scnode) { |
| 6183 | moveto_mod = ctx_scnode->module; |
| 6184 | } |
| 6185 | break; |
| 6186 | case LY_VALUE_CANON: |
| 6187 | case LY_VALUE_XML: |
| 6188 | /* not defined */ |
| 6189 | LOGINT(set->ctx); |
| 6190 | return LY_EINVAL; |
| 6191 | } |
| 6192 | } |
| 6193 | |
| 6194 | if (!node) { |
| 6195 | /* root will not match a specific node */ |
| 6196 | if (node_name || moveto_mod) { |
| 6197 | return LY_ENOT; |
| 6198 | } |
| 6199 | return LY_SUCCESS; |
| 6200 | } |
| 6201 | |
| 6202 | /* module check */ |
| 6203 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6204 | return LY_ENOT; |
| 6205 | } |
| 6206 | |
| 6207 | /* context check */ |
| 6208 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6209 | return LY_EINVAL; |
| 6210 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6211 | return LY_EINVAL; |
| 6212 | } |
| 6213 | |
| 6214 | /* name check */ |
| 6215 | if (node_name && (node->name != node_name)) { |
| 6216 | return LY_ENOT; |
| 6217 | } |
| 6218 | |
| 6219 | /* match */ |
| 6220 | return LY_SUCCESS; |
| 6221 | } |
| 6222 | |
| 6223 | /** |
| 6224 | * @brief Get the next node in a forward schema node DFS. |
| 6225 | * |
| 6226 | * @param[in] iter Last returned node. |
| 6227 | * @param[in] stop Node to stop the search on and not return. |
| 6228 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6229 | * @return Next node, NULL if there are no more. |
| 6230 | */ |
| 6231 | static const struct lysc_node * |
| 6232 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6233 | { |
| 6234 | const struct lysc_node *next = NULL; |
| 6235 | |
| 6236 | next = lysc_node_child(iter); |
| 6237 | if (!next) { |
| 6238 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6239 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6240 | /* we are done, no next element to process */ |
| 6241 | return NULL; |
| 6242 | } |
| 6243 | |
| 6244 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6245 | } |
| 6246 | while (!next && iter) { |
| 6247 | /* parent is already processed, go to its sibling */ |
| 6248 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6249 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6250 | /* we are done, no next element to process */ |
| 6251 | return NULL; |
| 6252 | } |
| 6253 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6254 | } |
| 6255 | |
| 6256 | return next; |
| 6257 | } |
| 6258 | |
| 6259 | /** |
| 6260 | * @brief Consider schema node based on its in_ctx enum value. |
| 6261 | * |
| 6262 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6263 | * @param[in] axis Axis to use. |
| 6264 | * @return LY_SUCCESS on success. |
| 6265 | * @return LY_ENOT if the node should not be returned. |
| 6266 | */ |
| 6267 | static LY_ERR |
| 6268 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6269 | { |
| 6270 | switch (axis) { |
| 6271 | case LYXP_AXIS_SELF: |
| 6272 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6273 | /* additionally put the start node into context */ |
| 6274 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6275 | return LY_SUCCESS; |
| 6276 | } |
| 6277 | break; |
| 6278 | case LYXP_AXIS_PARENT: |
| 6279 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6280 | case LYXP_AXIS_ANCESTOR: |
| 6281 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6282 | case LYXP_AXIS_DESCENDANT: |
| 6283 | case LYXP_AXIS_FOLLOWING: |
| 6284 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6285 | case LYXP_AXIS_PRECEDING: |
| 6286 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6287 | case LYXP_AXIS_CHILD: |
| 6288 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6289 | /* remember that context node was used */ |
| 6290 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6291 | return LY_SUCCESS; |
| 6292 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6293 | /* traversed */ |
| 6294 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6295 | return LY_SUCCESS; |
| 6296 | } |
| 6297 | break; |
| 6298 | case LYXP_AXIS_ATTRIBUTE: |
| 6299 | /* unreachable */ |
| 6300 | assert(0); |
| 6301 | LOGINT(NULL); |
| 6302 | break; |
| 6303 | } |
| 6304 | |
| 6305 | return LY_ENOT; |
| 6306 | } |
| 6307 | |
| 6308 | /** |
| 6309 | * @brief Get previous sibling for a schema node. |
| 6310 | * |
| 6311 | * @param[in] scnode Schema node. |
| 6312 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6313 | * @return Previous sibling, NULL if none. |
| 6314 | */ |
| 6315 | static const struct lysc_node * |
| 6316 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6317 | { |
| 6318 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6319 | |
| 6320 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6321 | if (next == scnode) { |
| 6322 | break; |
| 6323 | } |
| 6324 | |
| 6325 | prev = next; |
| 6326 | } |
| 6327 | |
| 6328 | return prev; |
| 6329 | } |
| 6330 | |
| 6331 | /** |
| 6332 | * @brief Get the first schema node on an axis for a context node. |
| 6333 | * |
| 6334 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6335 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6336 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6337 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6338 | * @param[in] scnode Context node. |
| 6339 | * @param[in] node_type Type of @p scnode. |
| 6340 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6341 | * @param[in] axis Axis to use. |
| 6342 | * @param[in] set XPath set with the general context. |
| 6343 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6344 | * @return LY_SUCCESS on success. |
| 6345 | * @return LY_ENOTFOUND if no next node found. |
| 6346 | */ |
| 6347 | static LY_ERR |
| 6348 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6349 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6350 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6351 | { |
| 6352 | const struct lysc_node *next = NULL; |
| 6353 | enum lyxp_node_type next_type = 0; |
| 6354 | |
| 6355 | assert(!*iter); |
| 6356 | assert(!*iter_type); |
| 6357 | |
| 6358 | *iter_mod = NULL; |
| 6359 | *iter_mod_idx = 0; |
| 6360 | |
| 6361 | switch (axis) { |
| 6362 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6363 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6364 | case LYXP_AXIS_SELF: |
| 6365 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6366 | /* just return the node */ |
| 6367 | next = scnode; |
| 6368 | next_type = node_type; |
| 6369 | } |
| 6370 | break; |
| 6371 | |
| 6372 | case LYXP_AXIS_ANCESTOR: |
| 6373 | case LYXP_AXIS_PARENT: |
| 6374 | if (node_type == LYXP_NODE_ELEM) { |
| 6375 | next = lysc_data_parent(scnode); |
| 6376 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6377 | } /* else no parent */ |
| 6378 | break; |
| 6379 | |
| 6380 | case LYXP_AXIS_DESCENDANT: |
| 6381 | case LYXP_AXIS_CHILD: |
| 6382 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6383 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6384 | * it can be in a top-level augment */ |
| 6385 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6386 | /* module may not be implemented or not compiled yet */ |
| 6387 | if (!(*iter_mod)->compiled) { |
| 6388 | continue; |
| 6389 | } |
| 6390 | |
| 6391 | /* get next node */ |
| 6392 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6393 | next_type = LYXP_NODE_ELEM; |
| 6394 | break; |
| 6395 | } |
| 6396 | } |
| 6397 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6398 | /* get next node */ |
| 6399 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6400 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6401 | } |
| 6402 | break; |
| 6403 | |
| 6404 | case LYXP_AXIS_FOLLOWING: |
| 6405 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6406 | if (node_type == LYXP_NODE_ELEM) { |
| 6407 | /* first next sibling */ |
| 6408 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6409 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6410 | } /* else no sibling */ |
| 6411 | break; |
| 6412 | |
| 6413 | case LYXP_AXIS_PRECEDING: |
| 6414 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6415 | if (node_type == LYXP_NODE_ELEM) { |
| 6416 | /* first parent sibling */ |
| 6417 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6418 | if (next == scnode) { |
| 6419 | /* no preceding sibling */ |
| 6420 | next = NULL; |
| 6421 | } |
| 6422 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6423 | } /* else no sibling */ |
| 6424 | break; |
| 6425 | |
| 6426 | case LYXP_AXIS_ATTRIBUTE: |
| 6427 | /* unreachable */ |
| 6428 | assert(0); |
| 6429 | LOGINT(set->ctx); |
| 6430 | break; |
| 6431 | } |
| 6432 | |
| 6433 | *iter = next; |
| 6434 | *iter_type = next_type; |
| 6435 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6436 | } |
| 6437 | |
| 6438 | /** |
| 6439 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6440 | * |
| 6441 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6442 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6443 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6444 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6445 | * @param[in] scnode Context node. |
| 6446 | * @param[in] node_type Type of @p scnode. |
| 6447 | * @param[in] axis Axis to use. |
| 6448 | * @param[in] set XPath set with the general context. |
| 6449 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6450 | * @return LY_SUCCESS on success. |
| 6451 | * @return LY_ENOTFOUND if no next node found. |
| 6452 | */ |
| 6453 | static LY_ERR |
| 6454 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6455 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6456 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6457 | { |
| 6458 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6459 | enum lyxp_node_type next_type = 0; |
| 6460 | |
| 6461 | if (!*iter_type) { |
| 6462 | /* first returned node */ |
| 6463 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6464 | getnext_opts); |
| 6465 | } |
| 6466 | |
| 6467 | switch (axis) { |
| 6468 | case LYXP_AXIS_PARENT: |
| 6469 | case LYXP_AXIS_SELF: |
| 6470 | /* parent/self was returned before */ |
| 6471 | break; |
| 6472 | |
| 6473 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6474 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6475 | /* fake first ancestor, we returned self before */ |
| 6476 | *iter = NULL; |
| 6477 | *iter_type = 0; |
| 6478 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6479 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
| 6480 | } /* else continue ancestor */ |
| 6481 | |
| 6482 | /* fallthrough */ |
| 6483 | case LYXP_AXIS_ANCESTOR: |
| 6484 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6485 | next = lysc_data_parent(*iter); |
| 6486 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6487 | } /* else no ancestor */ |
| 6488 | break; |
| 6489 | |
| 6490 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6491 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6492 | /* fake first descendant, we returned self before */ |
| 6493 | *iter = NULL; |
| 6494 | *iter_type = 0; |
| 6495 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6496 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6497 | } /* else DFS until context node */ |
| 6498 | dfs_stop = scnode; |
| 6499 | |
| 6500 | /* fallthrough */ |
| 6501 | case LYXP_AXIS_DESCENDANT: |
| 6502 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6503 | /* DFS until the context node */ |
| 6504 | dfs_stop = scnode; |
| 6505 | } |
| 6506 | |
| 6507 | /* fallthrough */ |
| 6508 | case LYXP_AXIS_PRECEDING: |
| 6509 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6510 | /* DFS until the previous sibling */ |
| 6511 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6512 | assert(dfs_stop); |
| 6513 | |
| 6514 | if (*iter == dfs_stop) { |
| 6515 | /* we are done */ |
| 6516 | break; |
| 6517 | } |
| 6518 | } |
| 6519 | |
| 6520 | /* fallthrough */ |
| 6521 | case LYXP_AXIS_FOLLOWING: |
| 6522 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6523 | /* DFS through the whole module */ |
| 6524 | dfs_stop = NULL; |
| 6525 | } |
| 6526 | |
| 6527 | /* nested nodes */ |
| 6528 | assert(*iter); |
| 6529 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6530 | if (next) { |
| 6531 | next_type = LYXP_NODE_ELEM; |
| 6532 | break; |
| 6533 | } /* else get next top-level node just like a child */ |
| 6534 | |
| 6535 | /* fallthrough */ |
| 6536 | case LYXP_AXIS_CHILD: |
| 6537 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6538 | if (!*iter_mod) { |
| 6539 | /* nodes from a single module */ |
| 6540 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6541 | next_type = LYXP_NODE_ELEM; |
| 6542 | break; |
| 6543 | } |
| 6544 | |
| 6545 | assert(scnode); |
| 6546 | if (!lysc_data_parent(scnode)) { |
| 6547 | /* iterating over top-level nodes, find next */ |
| 6548 | while (lysc_data_parent(*iter)) { |
| 6549 | *iter = lysc_data_parent(*iter); |
| 6550 | } |
| 6551 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6552 | next_type = LYXP_NODE_ELEM; |
| 6553 | break; |
| 6554 | } |
| 6555 | } |
| 6556 | } |
| 6557 | |
| 6558 | while (*iter_mod) { |
| 6559 | /* module top-level nodes */ |
| 6560 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6561 | next_type = LYXP_NODE_ELEM; |
| 6562 | break; |
| 6563 | } |
| 6564 | |
| 6565 | /* get next module */ |
| 6566 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6567 | /* module may not be implemented or not compiled yet */ |
| 6568 | if ((*iter_mod)->compiled) { |
| 6569 | break; |
| 6570 | } |
| 6571 | } |
| 6572 | |
| 6573 | /* new module, start over */ |
| 6574 | *iter = NULL; |
| 6575 | } |
| 6576 | break; |
| 6577 | |
| 6578 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6579 | assert(*iter); |
| 6580 | |
| 6581 | /* next parent sibling until scnode */ |
| 6582 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6583 | if (next == scnode) { |
| 6584 | /* no previous sibling */ |
| 6585 | next = NULL; |
| 6586 | } |
| 6587 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6588 | break; |
| 6589 | |
| 6590 | case LYXP_AXIS_ATTRIBUTE: |
| 6591 | /* unreachable */ |
| 6592 | assert(0); |
| 6593 | LOGINT(set->ctx); |
| 6594 | break; |
| 6595 | } |
| 6596 | |
| 6597 | *iter = next; |
| 6598 | *iter_type = next_type; |
| 6599 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6600 | } |
| 6601 | |
| 6602 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6603 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6604 | * |
| 6605 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6606 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6607 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6608 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6609 | * @param[in] options XPath options. |
| 6610 | * @return LY_ERR |
| 6611 | */ |
| 6612 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6613 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6614 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6615 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6616 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6617 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6618 | const struct lys_module *mod; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6619 | const struct lysc_node *iter; |
| 6620 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6621 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6622 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6623 | return LY_SUCCESS; |
| 6624 | } |
| 6625 | |
| 6626 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6627 | 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] | 6628 | return LY_EVALID; |
| 6629 | } |
| 6630 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6631 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6632 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6633 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6634 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6635 | } |
| 6636 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6637 | orig_used = set->used; |
| 6638 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6639 | /* update in_ctx first */ |
| 6640 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6641 | /* not usable, skip */ |
| 6642 | continue; |
| 6643 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6644 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6645 | iter = NULL; |
| 6646 | iter_type = 0; |
| 6647 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6648 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6649 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6650 | continue; |
| 6651 | } |
| 6652 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6653 | /* insert */ |
| 6654 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, &idx)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6655 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6656 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6657 | if ((idx < orig_used) && (idx > i)) { |
| 6658 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6659 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6660 | } |
| 6661 | } |
| 6662 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6663 | |
| 6664 | /* correct temporary in_ctx values */ |
| 6665 | if (temp_ctx) { |
| 6666 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6667 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6668 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6669 | } |
| 6670 | } |
| 6671 | } |
| 6672 | |
| 6673 | return LY_SUCCESS; |
| 6674 | } |
| 6675 | |
| 6676 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6677 | * @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] | 6678 | * Context position aware. |
| 6679 | * |
| 6680 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6681 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6682 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6683 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6684 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6685 | */ |
| 6686 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6687 | 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] | 6688 | { |
| 6689 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6690 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6691 | struct lyxp_set ret_set; |
| 6692 | LY_ERR rc; |
| 6693 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6694 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6695 | return LY_SUCCESS; |
| 6696 | } |
| 6697 | |
| 6698 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6699 | 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] | 6700 | return LY_EVALID; |
| 6701 | } |
| 6702 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6703 | /* 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] | 6704 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6705 | LY_CHECK_RET(rc); |
| 6706 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6707 | /* 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] | 6708 | set_init(&ret_set, set); |
| 6709 | for (i = 0; i < set->used; ++i) { |
| 6710 | |
| 6711 | /* TREE DFS */ |
| 6712 | start = set->val.nodes[i].node; |
| 6713 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6714 | 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] | 6715 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6716 | /* add matching node into result set */ |
| 6717 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6718 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6719 | /* the node is a duplicate, we'll process it later in the set */ |
| 6720 | goto skip_children; |
| 6721 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6722 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6723 | return rc; |
| 6724 | } else if (rc == LY_EINVAL) { |
| 6725 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6726 | } |
| 6727 | |
| 6728 | /* TREE DFS NEXT ELEM */ |
| 6729 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6730 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6731 | if (!next) { |
| 6732 | skip_children: |
| 6733 | /* no children, so try siblings, but only if it's not the start, |
| 6734 | * that is considered to be the root and it's siblings are not traversed */ |
| 6735 | if (elem != start) { |
| 6736 | next = elem->next; |
| 6737 | } else { |
| 6738 | break; |
| 6739 | } |
| 6740 | } |
| 6741 | while (!next) { |
| 6742 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6743 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6744 | /* we are done, no next element to process */ |
| 6745 | break; |
| 6746 | } |
| 6747 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6748 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6749 | next = elem->next; |
| 6750 | } |
| 6751 | } |
| 6752 | } |
| 6753 | |
| 6754 | /* make the temporary set the current one */ |
| 6755 | ret_set.ctx_pos = set->ctx_pos; |
| 6756 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6757 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6758 | memcpy(set, &ret_set, sizeof *set); |
| 6759 | |
| 6760 | return LY_SUCCESS; |
| 6761 | } |
| 6762 | |
| 6763 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6764 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6765 | * Result is LYXP_SET_NODE_SET. |
| 6766 | * |
| 6767 | * @param[in] set Set to use. |
| 6768 | * @param[in] start Start node whose subtree to add. |
| 6769 | * @param[in] start_idx Index of @p start in @p set. |
| 6770 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6771 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6772 | * @param[in] options XPath options. |
| 6773 | * @return LY_ERR value. |
| 6774 | */ |
| 6775 | static LY_ERR |
| 6776 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6777 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6778 | { |
| 6779 | const struct lysc_node *next, *elem; |
| 6780 | uint32_t idx; |
| 6781 | LY_ERR rc; |
| 6782 | |
| 6783 | /* TREE DFS */ |
| 6784 | for (elem = next = start; elem; elem = next) { |
| 6785 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6786 | /* schema-only nodes, skip root */ |
| 6787 | goto next_iter; |
| 6788 | } |
| 6789 | |
| 6790 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6791 | if (!rc) { |
| 6792 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6793 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6794 | if (idx > start_idx) { |
| 6795 | /* we will process it later in the set */ |
| 6796 | goto skip_children; |
| 6797 | } |
| 6798 | } else { |
| 6799 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL)); |
| 6800 | } |
| 6801 | } else if (rc == LY_EINVAL) { |
| 6802 | goto skip_children; |
| 6803 | } |
| 6804 | |
| 6805 | next_iter: |
| 6806 | /* TREE DFS NEXT ELEM */ |
| 6807 | /* select element for the next run - children first */ |
| 6808 | next = lysc_node_child(elem); |
| 6809 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6810 | next = next->next; |
| 6811 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6812 | next = next->next; |
| 6813 | } |
| 6814 | if (!next) { |
| 6815 | skip_children: |
| 6816 | /* no children, so try siblings, but only if it's not the start, |
| 6817 | * that is considered to be the root and it's siblings are not traversed */ |
| 6818 | if (elem != start) { |
| 6819 | next = elem->next; |
| 6820 | } else { |
| 6821 | break; |
| 6822 | } |
| 6823 | } |
| 6824 | while (!next) { |
| 6825 | /* no siblings, go back through the parents */ |
| 6826 | if (elem->parent == start) { |
| 6827 | /* we are done, no next element to process */ |
| 6828 | break; |
| 6829 | } |
| 6830 | /* parent is already processed, go to its sibling */ |
| 6831 | elem = elem->parent; |
| 6832 | next = elem->next; |
| 6833 | } |
| 6834 | } |
| 6835 | |
| 6836 | return LY_SUCCESS; |
| 6837 | } |
| 6838 | |
| 6839 | /** |
| 6840 | * @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] | 6841 | * |
| 6842 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6843 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6844 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6845 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6846 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6847 | */ |
| 6848 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6849 | 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] | 6850 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6851 | uint32_t i, orig_used, mod_idx; |
| 6852 | const struct lys_module *mod; |
| 6853 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6854 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6855 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6856 | return LY_SUCCESS; |
| 6857 | } |
| 6858 | |
| 6859 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6860 | 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] | 6861 | return LY_EVALID; |
| 6862 | } |
| 6863 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6864 | orig_used = set->used; |
| 6865 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6866 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6867 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6868 | continue; |
| 6869 | } |
| 6870 | |
| 6871 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6872 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6873 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6874 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6875 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6876 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6877 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6878 | /* traverse all top-level nodes in all the modules */ |
| 6879 | mod_idx = 0; |
| 6880 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6881 | /* module may not be implemented or not compiled yet */ |
| 6882 | if (!mod->compiled) { |
| 6883 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6884 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6885 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6886 | root = NULL; |
| 6887 | /* no getnext opts needed */ |
| 6888 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 6889 | 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] | 6890 | } |
| 6891 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6892 | |
| 6893 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6894 | /* add all the descendants recursively */ |
| 6895 | 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] | 6896 | } |
| 6897 | } |
| 6898 | |
| 6899 | return LY_SUCCESS; |
| 6900 | } |
| 6901 | |
| 6902 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6903 | * @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] | 6904 | * Indirectly context position aware. |
| 6905 | * |
| 6906 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6907 | * @param[in] mod Matching metadata module, NULL for any. |
| 6908 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6909 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6910 | * @return LY_ERR |
| 6911 | */ |
| 6912 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6913 | 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] | 6914 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6915 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6916 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6917 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6918 | return LY_SUCCESS; |
| 6919 | } |
| 6920 | |
| 6921 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6922 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6923 | return LY_EVALID; |
| 6924 | } |
| 6925 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6926 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6927 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6928 | |
| 6929 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 6930 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6931 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6932 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6933 | |
| 6934 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6935 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6936 | continue; |
| 6937 | } |
| 6938 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6939 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6940 | /* match */ |
| 6941 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6942 | set->val.meta[i].meta = sub; |
| 6943 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6944 | /* pos does not change */ |
| 6945 | replaced = 1; |
| 6946 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6947 | 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] | 6948 | } |
| 6949 | ++i; |
| 6950 | } |
| 6951 | } |
| 6952 | } |
| 6953 | |
| 6954 | if (!replaced) { |
| 6955 | /* no match */ |
| 6956 | set_remove_node(set, i); |
| 6957 | } |
| 6958 | } |
| 6959 | |
| 6960 | return LY_SUCCESS; |
| 6961 | } |
| 6962 | |
| 6963 | /** |
| 6964 | * @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] | 6965 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6966 | * |
| 6967 | * @param[in,out] set1 Set to use for the result. |
| 6968 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6969 | * @return LY_ERR |
| 6970 | */ |
| 6971 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6972 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6973 | { |
| 6974 | LY_ERR rc; |
| 6975 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6976 | 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] | 6977 | 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] | 6978 | return LY_EVALID; |
| 6979 | } |
| 6980 | |
| 6981 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6982 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6983 | return LY_SUCCESS; |
| 6984 | } |
| 6985 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6986 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 6987 | /* release hidden allocated data (lyxp_set.size) */ |
| 6988 | lyxp_set_free_content(set1); |
| 6989 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6990 | memcpy(set1, set2, sizeof *set1); |
| 6991 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6992 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6993 | return LY_SUCCESS; |
| 6994 | } |
| 6995 | |
| 6996 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6997 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6998 | |
| 6999 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7000 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7001 | LY_CHECK_RET(rc); |
| 7002 | |
| 7003 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7004 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7005 | |
| 7006 | return LY_SUCCESS; |
| 7007 | } |
| 7008 | |
| 7009 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7010 | * @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] | 7011 | * Context position aware. |
| 7012 | * |
| 7013 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7014 | * @param[in] mod Matching metadata module, NULL for any. |
| 7015 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7016 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7017 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7018 | */ |
| 7019 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7020 | 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] | 7021 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7022 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7023 | struct lyxp_set *set_all_desc = NULL; |
| 7024 | LY_ERR rc; |
| 7025 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7026 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7027 | return LY_SUCCESS; |
| 7028 | } |
| 7029 | |
| 7030 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7031 | 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] | 7032 | return LY_EVALID; |
| 7033 | } |
| 7034 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7035 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7036 | * but it likely won't be used much, so it's a waste of time */ |
| 7037 | /* copy the context */ |
| 7038 | set_all_desc = set_copy(set); |
| 7039 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7040 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7041 | if (rc != LY_SUCCESS) { |
| 7042 | lyxp_set_free(set_all_desc); |
| 7043 | return rc; |
| 7044 | } |
| 7045 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7046 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7047 | if (rc != LY_SUCCESS) { |
| 7048 | lyxp_set_free(set_all_desc); |
| 7049 | return rc; |
| 7050 | } |
| 7051 | lyxp_set_free(set_all_desc); |
| 7052 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7053 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7054 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7055 | |
| 7056 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7057 | * we have all attributes qualified in lyd tree */ |
| 7058 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7059 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7060 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7061 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7062 | continue; |
| 7063 | } |
| 7064 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7065 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7066 | /* match */ |
| 7067 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7068 | set->val.meta[i].meta = sub; |
| 7069 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7070 | /* pos does not change */ |
| 7071 | replaced = 1; |
| 7072 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7073 | 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] | 7074 | } |
| 7075 | ++i; |
| 7076 | } |
| 7077 | } |
| 7078 | } |
| 7079 | |
| 7080 | if (!replaced) { |
| 7081 | /* no match */ |
| 7082 | set_remove_node(set, i); |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | return LY_SUCCESS; |
| 7087 | } |
| 7088 | |
| 7089 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7090 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 7091 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7092 | * |
| 7093 | * @param[in,out] set1 Set to use for the result. |
| 7094 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7095 | * @param[in] op Comparison operator to process. |
| 7096 | * @param[in] options XPath options. |
| 7097 | * @return LY_ERR |
| 7098 | */ |
| 7099 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7100 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7101 | { |
| 7102 | /* |
| 7103 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7104 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7105 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7106 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7107 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7108 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7109 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7110 | * |
| 7111 | * '=' or '!=' |
| 7112 | * BOOLEAN + BOOLEAN |
| 7113 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7114 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7115 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7116 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7117 | * NUMBER + NUMBER |
| 7118 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7119 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7120 | * STRING + STRING |
| 7121 | * |
| 7122 | * '<=', '<', '>=', '>' |
| 7123 | * NUMBER + NUMBER |
| 7124 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7125 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7126 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7127 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7128 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7129 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7130 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7131 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7132 | */ |
| 7133 | struct lyxp_set iter1, iter2; |
| 7134 | int result; |
| 7135 | int64_t i; |
| 7136 | LY_ERR rc; |
| 7137 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7138 | memset(&iter1, 0, sizeof iter1); |
| 7139 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7140 | |
| 7141 | /* iterative evaluation with node-sets */ |
| 7142 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7143 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7144 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7145 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7146 | switch (set2->type) { |
| 7147 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7148 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7149 | break; |
| 7150 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7151 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7152 | break; |
| 7153 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7154 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7155 | break; |
| 7156 | } |
| 7157 | LY_CHECK_RET(rc); |
| 7158 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7159 | /* canonize set2 */ |
| 7160 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 7161 | |
| 7162 | /* compare recursively */ |
| 7163 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 7164 | lyxp_set_free_content(&iter2); |
| 7165 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7166 | |
| 7167 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7168 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7169 | set_fill_boolean(set1, 1); |
| 7170 | return LY_SUCCESS; |
| 7171 | } |
| 7172 | } |
| 7173 | } else { |
| 7174 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7175 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7176 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7177 | case LYXP_SET_NUMBER: |
| 7178 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 7179 | break; |
| 7180 | case LYXP_SET_BOOLEAN: |
| 7181 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 7182 | break; |
| 7183 | default: |
| 7184 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 7185 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7186 | } |
| 7187 | LY_CHECK_RET(rc); |
| 7188 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7189 | /* canonize set1 */ |
| 7190 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter1, set1, &set2->val.nodes[i]), lyxp_set_free_content(&iter2), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7191 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7192 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7193 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7194 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7195 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7196 | |
| 7197 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7198 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7199 | set_fill_boolean(set1, 1); |
| 7200 | return LY_SUCCESS; |
| 7201 | } |
| 7202 | } |
| 7203 | } |
| 7204 | |
| 7205 | /* false for all nodes */ |
| 7206 | set_fill_boolean(set1, 0); |
| 7207 | return LY_SUCCESS; |
| 7208 | } |
| 7209 | |
| 7210 | /* first convert properly */ |
| 7211 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7212 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7213 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7214 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7215 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7216 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7217 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7218 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7219 | LY_CHECK_RET(rc); |
| 7220 | } /* else we have 2 strings */ |
| 7221 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7222 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7223 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7224 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7225 | LY_CHECK_RET(rc); |
| 7226 | } |
| 7227 | |
| 7228 | assert(set1->type == set2->type); |
| 7229 | |
| 7230 | /* compute result */ |
| 7231 | if (op[0] == '=') { |
| 7232 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7233 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7234 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 7235 | result = (set1->val.num == set2->val.num); |
| 7236 | } else { |
| 7237 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 7238 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7239 | } |
| 7240 | } else if (op[0] == '!') { |
| 7241 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7242 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7243 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 7244 | result = (set1->val.num != set2->val.num); |
| 7245 | } else { |
| 7246 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | c205843 | 2020-11-06 17:26:21 +0100 | [diff] [blame] | 7247 | result = strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7248 | } |
| 7249 | } else { |
| 7250 | assert(set1->type == LYXP_SET_NUMBER); |
| 7251 | if (op[0] == '<') { |
| 7252 | if (op[1] == '=') { |
| 7253 | result = (set1->val.num <= set2->val.num); |
| 7254 | } else { |
| 7255 | result = (set1->val.num < set2->val.num); |
| 7256 | } |
| 7257 | } else { |
| 7258 | if (op[1] == '=') { |
| 7259 | result = (set1->val.num >= set2->val.num); |
| 7260 | } else { |
| 7261 | result = (set1->val.num > set2->val.num); |
| 7262 | } |
| 7263 | } |
| 7264 | } |
| 7265 | |
| 7266 | /* assign result */ |
| 7267 | if (result) { |
| 7268 | set_fill_boolean(set1, 1); |
| 7269 | } else { |
| 7270 | set_fill_boolean(set1, 0); |
| 7271 | } |
| 7272 | |
| 7273 | return LY_SUCCESS; |
| 7274 | } |
| 7275 | |
| 7276 | /** |
| 7277 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7278 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7279 | * |
| 7280 | * @param[in,out] set1 Set to use for the result. |
| 7281 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7282 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7283 | * @return LY_ERR |
| 7284 | */ |
| 7285 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7286 | 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] | 7287 | { |
| 7288 | LY_ERR rc; |
| 7289 | |
| 7290 | /* unary '-' */ |
| 7291 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7292 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7293 | LY_CHECK_RET(rc); |
| 7294 | set1->val.num *= -1; |
| 7295 | lyxp_set_free(set2); |
| 7296 | return LY_SUCCESS; |
| 7297 | } |
| 7298 | |
| 7299 | assert(set1 && set2); |
| 7300 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7301 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7302 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7303 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7304 | LY_CHECK_RET(rc); |
| 7305 | |
| 7306 | switch (op[0]) { |
| 7307 | /* '+' */ |
| 7308 | case '+': |
| 7309 | set1->val.num += set2->val.num; |
| 7310 | break; |
| 7311 | |
| 7312 | /* '-' */ |
| 7313 | case '-': |
| 7314 | set1->val.num -= set2->val.num; |
| 7315 | break; |
| 7316 | |
| 7317 | /* '*' */ |
| 7318 | case '*': |
| 7319 | set1->val.num *= set2->val.num; |
| 7320 | break; |
| 7321 | |
| 7322 | /* 'div' */ |
| 7323 | case 'd': |
| 7324 | set1->val.num /= set2->val.num; |
| 7325 | break; |
| 7326 | |
| 7327 | /* 'mod' */ |
| 7328 | case 'm': |
| 7329 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7330 | break; |
| 7331 | |
| 7332 | default: |
| 7333 | LOGINT_RET(set1->ctx); |
| 7334 | } |
| 7335 | |
| 7336 | return LY_SUCCESS; |
| 7337 | } |
| 7338 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7339 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7340 | * @brief Evaluate Predicate. Logs directly on error. |
| 7341 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7342 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | * |
| 7344 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7345 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7346 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7347 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7348 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7349 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7350 | */ |
| 7351 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7352 | 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] | 7353 | { |
| 7354 | LY_ERR rc; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 7355 | uint16_t orig_exp; |
| 7356 | uint32_t i, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7357 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7358 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7359 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7360 | |
| 7361 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7362 | 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] | 7363 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7364 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7365 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7366 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7367 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7368 | 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] | 7369 | LY_CHECK_RET(rc); |
| 7370 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7371 | /* 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] | 7372 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7373 | |
| 7374 | /* empty set, nothing to evaluate */ |
| 7375 | if (!set->used) { |
| 7376 | goto only_parse; |
| 7377 | } |
| 7378 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7379 | /* decide forward or reverse axis */ |
| 7380 | switch (axis) { |
| 7381 | case LYXP_AXIS_ANCESTOR: |
| 7382 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7383 | case LYXP_AXIS_PRECEDING: |
| 7384 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7385 | reverse_axis = 1; |
| 7386 | break; |
| 7387 | case LYXP_AXIS_DESCENDANT: |
| 7388 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7389 | case LYXP_AXIS_FOLLOWING: |
| 7390 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7391 | case LYXP_AXIS_PARENT: |
| 7392 | case LYXP_AXIS_CHILD: |
| 7393 | case LYXP_AXIS_SELF: |
| 7394 | case LYXP_AXIS_ATTRIBUTE: |
| 7395 | reverse_axis = 0; |
| 7396 | break; |
| 7397 | } |
| 7398 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7399 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7400 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7401 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7402 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7403 | set_init(&set2, set); |
| 7404 | 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] | 7405 | |
| 7406 | /* remember the node context position for position() and context size for last() */ |
| 7407 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7408 | |
| 7409 | set2.ctx_pos = orig_pos; |
| 7410 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7411 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7412 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7413 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7414 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7415 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7416 | return rc; |
| 7417 | } |
| 7418 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7419 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7420 | if (set2.type == LYXP_SET_NUMBER) { |
| 7421 | if ((long long)set2.val.num == orig_pos) { |
| 7422 | set2.val.num = 1; |
| 7423 | } else { |
| 7424 | set2.val.num = 0; |
| 7425 | } |
| 7426 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7427 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7428 | |
| 7429 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7430 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7431 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7432 | } |
| 7433 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7434 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7435 | |
| 7436 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7437 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7438 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7439 | /* there is a currently-valid node */ |
| 7440 | break; |
| 7441 | } |
| 7442 | } |
| 7443 | /* empty set, nothing to evaluate */ |
| 7444 | if (i == set->used) { |
| 7445 | goto only_parse; |
| 7446 | } |
| 7447 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7448 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7449 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7450 | /* set special in_ctx to all the valid snodes */ |
| 7451 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7452 | |
| 7453 | /* use the valid snodes one-by-one */ |
| 7454 | for (i = 0; i < set->used; ++i) { |
| 7455 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7456 | continue; |
| 7457 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7458 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7459 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7460 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7461 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7462 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7463 | LY_CHECK_RET(rc); |
| 7464 | |
| 7465 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7466 | } |
| 7467 | |
| 7468 | /* restore the state as it was before the predicate */ |
| 7469 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7470 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7471 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7472 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7473 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7474 | } |
| 7475 | } |
| 7476 | |
| 7477 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7478 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7479 | set_fill_set(&set2, set); |
| 7480 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7481 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7482 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7483 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7484 | return rc; |
| 7485 | } |
| 7486 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7487 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7488 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7489 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7490 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7491 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7492 | } |
| 7493 | |
| 7494 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7495 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7496 | 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] | 7497 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7498 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7499 | |
| 7500 | return LY_SUCCESS; |
| 7501 | } |
| 7502 | |
| 7503 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7504 | * @brief Evaluate Literal. Logs directly on error. |
| 7505 | * |
| 7506 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7507 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7508 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7509 | */ |
| 7510 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7511 | 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] | 7512 | { |
| 7513 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7514 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7515 | set_fill_string(set, "", 0); |
| 7516 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7517 | 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] | 7518 | } |
| 7519 | } |
| 7520 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7521 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7522 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7523 | } |
| 7524 | |
| 7525 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7526 | * @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] | 7527 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7528 | * @param[in] exp Full parsed XPath expression. |
| 7529 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7530 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 7531 | * @param[in] cur_mod Current module for the expression. |
| 7532 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7533 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7534 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7535 | * @param[out] predicates Parsed predicates. |
| 7536 | * @param[out] pred_type Type of @p predicates. |
| 7537 | * @return LY_SUCCESS on success, |
| 7538 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7539 | */ |
| 7540 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7541 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7542 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_VALUE_FORMAT format, void *prefix_data, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7543 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7544 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7545 | LY_ERR ret = LY_SUCCESS; |
| 7546 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7547 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7548 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7549 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7550 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7551 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7552 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7553 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7554 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7555 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7556 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7557 | return LY_EINVAL; |
| 7558 | } |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 7559 | for (key_count = 0, key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7560 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7561 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7562 | /* learn where the predicates end */ |
| 7563 | e_idx = *tok_idx; |
| 7564 | while (key_count) { |
| 7565 | /* '[' */ |
| 7566 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7567 | return LY_EINVAL; |
| 7568 | } |
| 7569 | ++e_idx; |
| 7570 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7571 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
| 7572 | /* definitely not a key */ |
| 7573 | return LY_EINVAL; |
| 7574 | } |
| 7575 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7576 | /* ']' */ |
| 7577 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7578 | ++e_idx; |
| 7579 | } |
| 7580 | ++e_idx; |
| 7581 | |
| 7582 | /* another presumably key predicate parsed */ |
| 7583 | --key_count; |
| 7584 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | } else { |
| 7586 | /* learn just where this single predicate ends */ |
| 7587 | e_idx = *tok_idx; |
| 7588 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7589 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7590 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7591 | return LY_EINVAL; |
| 7592 | } |
| 7593 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7594 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7595 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
| 7596 | /* definitely not the value */ |
| 7597 | return LY_EINVAL; |
| 7598 | } |
| 7599 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7600 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7601 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7602 | ++e_idx; |
| 7603 | } |
| 7604 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7605 | } |
| 7606 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7607 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 7608 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 7609 | |
| 7610 | /* turn logging off */ |
| 7611 | prev_lo = ly_log_options(0); |
| 7612 | |
| 7613 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7614 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 7615 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7616 | |
| 7617 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7618 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 7619 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7620 | LY_CHECK_GOTO(ret, cleanup); |
| 7621 | |
| 7622 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7623 | *tok_idx = e_idx; |
| 7624 | |
| 7625 | cleanup: |
| 7626 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7627 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7628 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7629 | } |
| 7630 | |
| 7631 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7632 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7633 | * data node(s) could be found using a single hash-based search. |
| 7634 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7635 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7636 | * @param[in] node Next context node to check. |
| 7637 | * @param[in] name Expected node name. |
| 7638 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7639 | * @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] | 7640 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7641 | * @param[in] format Prefix format. |
| 7642 | * @param[in,out] found Previously found node, is updated. |
| 7643 | * @return LY_SUCCESS on success, |
| 7644 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7645 | */ |
| 7646 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7647 | 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] | 7648 | 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] | 7649 | const struct lysc_node **found) |
| 7650 | { |
| 7651 | const struct lysc_node *scnode; |
| 7652 | const struct lys_module *mod; |
| 7653 | uint32_t idx = 0; |
| 7654 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7655 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7656 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7657 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7658 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7659 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7660 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7661 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7662 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7663 | if (!mod->implemented) { |
| 7664 | continue; |
| 7665 | } |
| 7666 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7667 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7668 | if (scnode) { |
| 7669 | /* we have found a match */ |
| 7670 | break; |
| 7671 | } |
| 7672 | } |
| 7673 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7674 | if (!scnode) { |
| 7675 | /* all modules searched */ |
| 7676 | idx = 0; |
| 7677 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7678 | } else { |
| 7679 | /* search in top-level */ |
| 7680 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7681 | } |
| 7682 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7683 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7684 | /* we must adjust the module to inherit the one from the context node */ |
| 7685 | moveto_mod = node->schema->module; |
| 7686 | } |
| 7687 | |
| 7688 | /* search in children, do not repeat the same search */ |
| 7689 | 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] | 7690 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7691 | |
| 7692 | /* additional context check */ |
| 7693 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7694 | scnode = NULL; |
| 7695 | } |
| 7696 | |
| 7697 | if (scnode) { |
| 7698 | if (*found) { |
| 7699 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7700 | * (more hash-based searches would be required, not supported) */ |
| 7701 | return LY_ENOT; |
| 7702 | } else { |
| 7703 | /* remember the found schema node and continue to make sure it can be used */ |
| 7704 | *found = scnode; |
| 7705 | } |
| 7706 | } |
| 7707 | |
| 7708 | if (idx) { |
| 7709 | /* continue searching all the following models */ |
| 7710 | goto continue_search; |
| 7711 | } |
| 7712 | |
| 7713 | return LY_SUCCESS; |
| 7714 | } |
| 7715 | |
| 7716 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7717 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7718 | * |
| 7719 | * @param[in] set XPath set. |
| 7720 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7721 | * @param[in] ncname XPath NCName being evaluated. |
| 7722 | * @param[in] ncname_len Length of @p ncname. |
| 7723 | * @param[in] expr Whole XPath expression. |
| 7724 | * @param[in] options XPath options. |
| 7725 | */ |
| 7726 | static void |
| 7727 | eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname, |
| 7728 | uint16_t ncname_len, const char *expr, uint32_t options) |
| 7729 | { |
| 7730 | const char *format; |
| 7731 | char *path = NULL, *ppath = NULL; |
| 7732 | |
| 7733 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7734 | if (scparent) { |
| 7735 | /* generate path for the parent */ |
| 7736 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7737 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7738 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7739 | ppath = strdup("<root>"); |
| 7740 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7741 | ppath = strdup("<config-root>"); |
| 7742 | } |
| 7743 | } |
| 7744 | if (ppath) { |
| 7745 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7746 | if (options & LYXP_SCNODE_ERROR) { |
| 7747 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7748 | } else { |
| 7749 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7750 | } |
| 7751 | } else { |
| 7752 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7753 | if (options & LYXP_SCNODE_ERROR) { |
| 7754 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7755 | } else { |
| 7756 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7757 | } |
| 7758 | } |
| 7759 | free(path); |
| 7760 | free(ppath); |
| 7761 | } |
| 7762 | |
| 7763 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7764 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7765 | * |
| 7766 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7767 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7768 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7769 | * |
| 7770 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7771 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7772 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7773 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7774 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7775 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7776 | * @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] | 7777 | */ |
| 7778 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7779 | 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] | 7780 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7781 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7782 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7783 | const char *ncname, *ncname_dict = NULL; |
| 7784 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7785 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7786 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7787 | struct ly_path_predicate *predicates = NULL; |
| 7788 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7789 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7790 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7791 | 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] | 7792 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7793 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7794 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7795 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7796 | goto moveto; |
| 7797 | } |
| 7798 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7799 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7800 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7801 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7802 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 7803 | /* all nodes will match */ |
| 7804 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7805 | } |
| 7806 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7807 | /* parse (and skip) module name */ |
| 7808 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7809 | LY_CHECK_GOTO(rc, cleanup); |
| 7810 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7811 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 7812 | /* all nodes from the module will match */ |
| 7813 | goto moveto; |
| 7814 | } |
| 7815 | |
| 7816 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 7817 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7818 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7819 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7820 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 7821 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7822 | /* check failed */ |
| 7823 | scnode = NULL; |
| 7824 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7825 | } |
| 7826 | } |
| 7827 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7828 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7829 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7830 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7831 | set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7832 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7833 | scnode = NULL; |
| 7834 | } |
| 7835 | } |
| 7836 | } |
| 7837 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7838 | if (!scnode) { |
| 7839 | /* 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] | 7840 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7841 | 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] | 7842 | } |
| 7843 | |
| 7844 | moveto: |
| 7845 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7846 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7847 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7848 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7849 | } else { |
| 7850 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7851 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7852 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7853 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7854 | } |
| 7855 | LY_CHECK_GOTO(rc, cleanup); |
| 7856 | } |
| 7857 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7858 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7859 | int64_t i; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7860 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7861 | |
| 7862 | /* remember parent if there is only one, to print in the warning */ |
| 7863 | for (i = 0; i < set->used; ++i) { |
| 7864 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 7865 | if (!scparent) { |
| 7866 | /* remember the context node */ |
| 7867 | scparent = &set->val.scnodes[i]; |
| 7868 | } else { |
| 7869 | /* several context nodes, no reasonable error possible */ |
| 7870 | scparent = NULL; |
| 7871 | break; |
| 7872 | } |
| 7873 | } |
| 7874 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7875 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7876 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 7877 | /* efficient evaluation that does not add all the descendants into the set */ |
| 7878 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7879 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7880 | if (all_desc) { |
| 7881 | /* "//" == "/descendant-or-self::node()/" */ |
| 7882 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 7883 | LY_CHECK_GOTO(rc, cleanup); |
| 7884 | } |
| 7885 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7886 | } |
| 7887 | LY_CHECK_GOTO(rc, cleanup); |
| 7888 | |
| 7889 | for (i = set->used - 1; i > -1; --i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7890 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7891 | break; |
| 7892 | } |
| 7893 | } |
| 7894 | if (i == -1) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7895 | /* generate message */ |
| 7896 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 7897 | |
| 7898 | if (options & LYXP_SCNODE_ERROR) { |
| 7899 | /* error */ |
| 7900 | rc = LY_EVALID; |
| 7901 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7902 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7903 | |
| 7904 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 7905 | rc = LY_ENOT; |
| 7906 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7907 | } |
| 7908 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7909 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 7910 | /* efficient evaluation */ |
| 7911 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 7912 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 7913 | /* we can find the child nodes using hashes */ |
| 7914 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7915 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7916 | if (all_desc) { |
| 7917 | /* "//" == "/descendant-or-self::node()/" */ |
| 7918 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 7919 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7920 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7921 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7922 | } |
| 7923 | LY_CHECK_GOTO(rc, cleanup); |
| 7924 | } |
| 7925 | } |
| 7926 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7927 | if (scnode_skip_pred) { |
| 7928 | /* skip predicates */ |
| 7929 | options |= LYXP_SKIP_EXPR; |
| 7930 | } |
| 7931 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7932 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7933 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7934 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7935 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7936 | } |
| 7937 | |
| 7938 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7939 | if (scnode_skip_pred) { |
| 7940 | /* restore options */ |
| 7941 | options &= ~LYXP_SKIP_EXPR; |
| 7942 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7943 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7944 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 7945 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7946 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7947 | return rc; |
| 7948 | } |
| 7949 | |
| 7950 | /** |
| 7951 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7952 | * |
| 7953 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7954 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7955 | * [8] NodeType ::= 'text' | 'node' |
| 7956 | * |
| 7957 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7958 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7959 | * @param[in] axis Axis to search on. |
| 7960 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7961 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7962 | * @param[in] options XPath options. |
| 7963 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7964 | */ |
| 7965 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7966 | 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] | 7967 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7968 | { |
| 7969 | LY_ERR rc; |
| 7970 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7971 | (void)all_desc; |
| 7972 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7973 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7974 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7975 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 7976 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7977 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7978 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 7979 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7980 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7981 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7982 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7983 | 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] | 7984 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7985 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7986 | |
| 7987 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7988 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7989 | 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] | 7990 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7991 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7992 | |
| 7993 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7994 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7995 | 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] | 7996 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7997 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7998 | |
| 7999 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8000 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8001 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8002 | LY_CHECK_RET(rc); |
| 8003 | } |
| 8004 | |
| 8005 | return LY_SUCCESS; |
| 8006 | } |
| 8007 | |
| 8008 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8009 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8010 | * |
| 8011 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8012 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8013 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8014 | * |
| 8015 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8016 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8017 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8018 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8019 | * @param[in] options XPath options. |
| 8020 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8021 | */ |
| 8022 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8023 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 8024 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8025 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8026 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8027 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8028 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8029 | |
| 8030 | goto step; |
| 8031 | do { |
| 8032 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8033 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8034 | all_desc = 0; |
| 8035 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8036 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8037 | all_desc = 1; |
| 8038 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8039 | 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] | 8040 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8041 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8042 | |
| 8043 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8044 | /* AxisSpecifier */ |
| 8045 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8046 | 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] | 8047 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8048 | 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] | 8049 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8050 | ++(*tok_idx); |
| 8051 | |
| 8052 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8053 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8054 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8055 | ++(*tok_idx); |
| 8056 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8057 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8058 | |
| 8059 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8060 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8061 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8062 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8063 | /* default */ |
| 8064 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8065 | } |
| 8066 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8067 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8068 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8069 | case LYXP_TOKEN_DOT: |
| 8070 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8071 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8072 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8073 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8074 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8075 | rc = LY_EVALID; |
| 8076 | goto cleanup; |
| 8077 | } |
| 8078 | |
| 8079 | if (all_desc) { |
| 8080 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8081 | LY_CHECK_GOTO(rc, cleanup); |
| 8082 | } |
| 8083 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8084 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8085 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8086 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8087 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8088 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8089 | break; |
| 8090 | |
| 8091 | case LYXP_TOKEN_DDOT: |
| 8092 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8093 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8094 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8095 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8096 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8097 | rc = LY_EVALID; |
| 8098 | goto cleanup; |
| 8099 | } |
| 8100 | |
| 8101 | if (all_desc) { |
| 8102 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8103 | LY_CHECK_GOTO(rc, cleanup); |
| 8104 | } |
| 8105 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8106 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8107 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8108 | 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] | 8109 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8110 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8111 | break; |
| 8112 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8113 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8114 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8115 | 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] | 8116 | if (rc == LY_ENOT) { |
| 8117 | assert(options & LYXP_SCNODE_ALL); |
| 8118 | /* skip the rest of this path */ |
| 8119 | rc = LY_SUCCESS; |
| 8120 | scnode_skip_path = 1; |
| 8121 | options |= LYXP_SKIP_EXPR; |
| 8122 | } |
| 8123 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8124 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8126 | case LYXP_TOKEN_NODETYPE: |
| 8127 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8128 | 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] | 8129 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8130 | break; |
| 8131 | |
| 8132 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8133 | LOGINT(set->ctx); |
| 8134 | rc = LY_EINT; |
| 8135 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8136 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8137 | } 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] | 8138 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8139 | cleanup: |
| 8140 | if (scnode_skip_path) { |
| 8141 | options &= ~LYXP_SKIP_EXPR; |
| 8142 | } |
| 8143 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8144 | } |
| 8145 | |
| 8146 | /** |
| 8147 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8148 | * |
| 8149 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8150 | * |
| 8151 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8152 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8153 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8154 | * @param[in] options XPath options. |
| 8155 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8156 | */ |
| 8157 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8158 | 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] | 8159 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8160 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8161 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8162 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8163 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8164 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8165 | } |
| 8166 | |
| 8167 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8168 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8169 | /* evaluate '/' - deferred */ |
| 8170 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8171 | 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] | 8172 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8173 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8174 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8175 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8176 | return LY_SUCCESS; |
| 8177 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8178 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8179 | case LYXP_TOKEN_DOT: |
| 8180 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8181 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8182 | case LYXP_TOKEN_AT: |
| 8183 | case LYXP_TOKEN_NAMETEST: |
| 8184 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8185 | 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] | 8186 | break; |
| 8187 | default: |
| 8188 | break; |
| 8189 | } |
| 8190 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8191 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8192 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8193 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8194 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8195 | 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] | 8196 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8197 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8198 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8199 | 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] | 8200 | } |
| 8201 | |
| 8202 | return LY_SUCCESS; |
| 8203 | } |
| 8204 | |
| 8205 | /** |
| 8206 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8207 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8208 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8209 | * |
| 8210 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8211 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8212 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8213 | * @param[in] options XPath options. |
| 8214 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8215 | */ |
| 8216 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8217 | 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] | 8218 | { |
| 8219 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8220 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8221 | 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] | 8222 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8223 | struct lyxp_set **args = NULL, **args_aux; |
| 8224 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8225 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8226 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8227 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8228 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8229 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8230 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8231 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8232 | xpath_func = &xpath_sum; |
| 8233 | } |
| 8234 | break; |
| 8235 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8236 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8237 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8238 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8239 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8240 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8241 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8242 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8243 | xpath_func = &xpath_true; |
| 8244 | } |
| 8245 | break; |
| 8246 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8247 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8249 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8250 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8251 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8252 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8253 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8255 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8256 | xpath_func = &xpath_deref; |
| 8257 | } |
| 8258 | break; |
| 8259 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8260 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8261 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8262 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8264 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8265 | xpath_func = &xpath_string; |
| 8266 | } |
| 8267 | break; |
| 8268 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8269 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8270 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8271 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8272 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8273 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8274 | xpath_func = &xpath_current; |
| 8275 | } |
| 8276 | break; |
| 8277 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8278 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8279 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8280 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8281 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8282 | } 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] | 8283 | xpath_func = &xpath_re_match; |
| 8284 | } |
| 8285 | break; |
| 8286 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8287 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8288 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8289 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8290 | xpath_func = &xpath_translate; |
| 8291 | } |
| 8292 | break; |
| 8293 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8294 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8295 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8296 | } 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] | 8297 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8298 | } 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] | 8299 | xpath_func = &xpath_bit_is_set; |
| 8300 | } |
| 8301 | break; |
| 8302 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8303 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8304 | xpath_func = &xpath_starts_with; |
| 8305 | } |
| 8306 | break; |
| 8307 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8308 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8309 | xpath_func = &xpath_derived_from; |
| 8310 | } |
| 8311 | break; |
| 8312 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8313 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8314 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8315 | } 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] | 8316 | xpath_func = &xpath_string_length; |
| 8317 | } |
| 8318 | break; |
| 8319 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8320 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8321 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8322 | } 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] | 8323 | xpath_func = &xpath_substring_after; |
| 8324 | } |
| 8325 | break; |
| 8326 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8327 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8328 | xpath_func = &xpath_substring_before; |
| 8329 | } |
| 8330 | break; |
| 8331 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8332 | 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] | 8333 | xpath_func = &xpath_derived_from_or_self; |
| 8334 | } |
| 8335 | break; |
| 8336 | } |
| 8337 | |
| 8338 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8339 | 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] | 8340 | return LY_EVALID; |
| 8341 | } |
| 8342 | } |
| 8343 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8344 | 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] | 8345 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8346 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8347 | |
| 8348 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8349 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8350 | 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] | 8351 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8352 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8353 | |
| 8354 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8355 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8356 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8357 | args = malloc(sizeof *args); |
| 8358 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8359 | arg_count = 1; |
| 8360 | args[0] = set_copy(set); |
| 8361 | if (!args[0]) { |
| 8362 | rc = LY_EMEM; |
| 8363 | goto cleanup; |
| 8364 | } |
| 8365 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8366 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8367 | LY_CHECK_GOTO(rc, cleanup); |
| 8368 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8369 | 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] | 8370 | LY_CHECK_GOTO(rc, cleanup); |
| 8371 | } |
| 8372 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8373 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8374 | 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] | 8375 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8376 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8377 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8378 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8379 | ++arg_count; |
| 8380 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8381 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8382 | args = args_aux; |
| 8383 | args[arg_count - 1] = set_copy(set); |
| 8384 | if (!args[arg_count - 1]) { |
| 8385 | rc = LY_EMEM; |
| 8386 | goto cleanup; |
| 8387 | } |
| 8388 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8389 | 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] | 8390 | LY_CHECK_GOTO(rc, cleanup); |
| 8391 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8392 | 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] | 8393 | LY_CHECK_GOTO(rc, cleanup); |
| 8394 | } |
| 8395 | } |
| 8396 | |
| 8397 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8398 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8399 | 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] | 8400 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8401 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8402 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8403 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8404 | /* evaluate function */ |
| 8405 | rc = xpath_func(args, arg_count, set, options); |
| 8406 | |
| 8407 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8408 | /* merge all nodes from arg evaluations */ |
| 8409 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8410 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8411 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8412 | } |
| 8413 | } |
| 8414 | } else { |
| 8415 | rc = LY_SUCCESS; |
| 8416 | } |
| 8417 | |
| 8418 | cleanup: |
| 8419 | for (i = 0; i < arg_count; ++i) { |
| 8420 | lyxp_set_free(args[i]); |
| 8421 | } |
| 8422 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8423 | return rc; |
| 8424 | } |
| 8425 | |
| 8426 | /** |
| 8427 | * @brief Evaluate Number. Logs directly on error. |
| 8428 | * |
| 8429 | * @param[in] ctx Context for errors. |
| 8430 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8431 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8432 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8433 | * @return LY_ERR |
| 8434 | */ |
| 8435 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8436 | 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] | 8437 | { |
| 8438 | long double num; |
| 8439 | char *endptr; |
| 8440 | |
| 8441 | if (set) { |
| 8442 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8443 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8444 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8445 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8446 | 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] | 8447 | 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] | 8448 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8449 | } 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] | 8450 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8451 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8452 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8453 | return LY_EVALID; |
| 8454 | } |
| 8455 | |
| 8456 | set_fill_number(set, num); |
| 8457 | } |
| 8458 | |
| 8459 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8460 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8461 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8462 | return LY_SUCCESS; |
| 8463 | } |
| 8464 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8465 | LY_ERR |
| 8466 | lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var) |
| 8467 | { |
| 8468 | LY_ERR ret = LY_ENOTFOUND; |
| 8469 | LY_ARRAY_COUNT_TYPE u; |
| 8470 | |
| 8471 | assert(vars && name); |
| 8472 | |
| 8473 | name_len = name_len ? name_len : strlen(name); |
| 8474 | |
| 8475 | LY_ARRAY_FOR(vars, u) { |
| 8476 | if (!strncmp(vars[u].name, name, name_len)) { |
| 8477 | ret = LY_SUCCESS; |
| 8478 | break; |
| 8479 | } |
| 8480 | } |
| 8481 | |
| 8482 | if (var && !ret) { |
| 8483 | *var = &vars[u]; |
| 8484 | } |
| 8485 | |
| 8486 | return ret; |
| 8487 | } |
| 8488 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8489 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8490 | * @brief Evaluate VariableReference. |
| 8491 | * |
| 8492 | * @param[in] exp Parsed XPath expression. |
| 8493 | * @param[in] tok_idx Position in the expression @p exp. |
| 8494 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8495 | * @param[in,out] set Context and result set. |
| 8496 | * @param[in] options XPath options. |
| 8497 | * @return LY_ERR value. |
| 8498 | */ |
| 8499 | static LY_ERR |
| 8500 | eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
| 8501 | { |
| 8502 | LY_ERR ret; |
| 8503 | const char *name; |
| 8504 | struct lyxp_var *var; |
| 8505 | const struct lyxp_var *vars; |
| 8506 | struct lyxp_expr *tokens = NULL; |
| 8507 | uint16_t token_index; |
| 8508 | |
| 8509 | vars = set->vars; |
| 8510 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8511 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8512 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
| 8513 | ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var); |
| 8514 | LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret, |
| 8515 | "XPath variable \"%s\" not defined.", name), ret); |
| 8516 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8517 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8518 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8519 | LY_CHECK_GOTO(ret, cleanup); |
| 8520 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8521 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8522 | token_index = 0; |
| 8523 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8524 | LY_CHECK_GOTO(ret, cleanup); |
| 8525 | |
| 8526 | cleanup: |
| 8527 | lyxp_expr_free(set->ctx, tokens); |
| 8528 | |
| 8529 | return ret; |
| 8530 | } |
| 8531 | |
| 8532 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8534 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8535 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8536 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8537 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8538 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8539 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8540 | * |
| 8541 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8542 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8543 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8544 | * @param[in] options XPath options. |
| 8545 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8546 | */ |
| 8547 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8548 | 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] | 8549 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8550 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8551 | LY_ERR rc; |
| 8552 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8553 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8554 | case LYXP_TOKEN_PAR1: |
| 8555 | /* '(' Expr ')' */ |
| 8556 | |
| 8557 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8558 | 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] | 8559 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8560 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8561 | |
| 8562 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8563 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8564 | LY_CHECK_RET(rc); |
| 8565 | |
| 8566 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8567 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8568 | 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] | 8569 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8570 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8572 | goto predicate; |
| 8573 | |
| 8574 | case LYXP_TOKEN_DOT: |
| 8575 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8576 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8577 | case LYXP_TOKEN_AT: |
| 8578 | case LYXP_TOKEN_NAMETEST: |
| 8579 | case LYXP_TOKEN_NODETYPE: |
| 8580 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8581 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8582 | LY_CHECK_RET(rc); |
| 8583 | break; |
| 8584 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8585 | case LYXP_TOKEN_VARREF: |
| 8586 | /* VariableReference */ |
| 8587 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8588 | LY_CHECK_RET(rc); |
| 8589 | ++(*tok_idx); |
| 8590 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8591 | goto predicate; |
| 8592 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8593 | case LYXP_TOKEN_FUNCNAME: |
| 8594 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8595 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8596 | LY_CHECK_RET(rc); |
| 8597 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8598 | goto predicate; |
| 8599 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8600 | case LYXP_TOKEN_OPER_PATH: |
| 8601 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8602 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8603 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8604 | LY_CHECK_RET(rc); |
| 8605 | break; |
| 8606 | |
| 8607 | case LYXP_TOKEN_LITERAL: |
| 8608 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8609 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8610 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8611 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8612 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8613 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8614 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8615 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8616 | } |
| 8617 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8618 | goto predicate; |
| 8619 | |
| 8620 | case LYXP_TOKEN_NUMBER: |
| 8621 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8622 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8623 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8624 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8625 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8626 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8627 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8628 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8629 | } |
| 8630 | LY_CHECK_RET(rc); |
| 8631 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8632 | goto predicate; |
| 8633 | |
| 8634 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8635 | 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] | 8636 | return LY_EVALID; |
| 8637 | } |
| 8638 | |
| 8639 | return LY_SUCCESS; |
| 8640 | |
| 8641 | predicate: |
| 8642 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8643 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8644 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8645 | LY_CHECK_RET(rc); |
| 8646 | } |
| 8647 | |
| 8648 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8649 | 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] | 8650 | |
| 8651 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8652 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8653 | all_desc = 0; |
| 8654 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8655 | all_desc = 1; |
| 8656 | } |
| 8657 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8658 | 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] | 8659 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8660 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8661 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8662 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8663 | LY_CHECK_RET(rc); |
| 8664 | } |
| 8665 | |
| 8666 | return LY_SUCCESS; |
| 8667 | } |
| 8668 | |
| 8669 | /** |
| 8670 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8671 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8672 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8673 | * |
| 8674 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8675 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8676 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8677 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8678 | * @param[in] options XPath options. |
| 8679 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8680 | */ |
| 8681 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8682 | 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] | 8683 | { |
| 8684 | LY_ERR rc = LY_SUCCESS; |
| 8685 | struct lyxp_set orig_set, set2; |
| 8686 | uint16_t i; |
| 8687 | |
| 8688 | assert(repeat); |
| 8689 | |
| 8690 | set_init(&orig_set, set); |
| 8691 | set_init(&set2, set); |
| 8692 | |
| 8693 | set_fill_set(&orig_set, set); |
| 8694 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8695 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8696 | LY_CHECK_GOTO(rc, cleanup); |
| 8697 | |
| 8698 | /* ('|' PathExpr)* */ |
| 8699 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8700 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8701 | 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] | 8702 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8703 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8704 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8705 | if (options & LYXP_SKIP_EXPR) { |
| 8706 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8707 | LY_CHECK_GOTO(rc, cleanup); |
| 8708 | continue; |
| 8709 | } |
| 8710 | |
| 8711 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8712 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8713 | LY_CHECK_GOTO(rc, cleanup); |
| 8714 | |
| 8715 | /* eval */ |
| 8716 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8717 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8718 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8719 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8720 | LY_CHECK_GOTO(rc, cleanup); |
| 8721 | } |
| 8722 | } |
| 8723 | |
| 8724 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8725 | lyxp_set_free_content(&orig_set); |
| 8726 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8727 | return rc; |
| 8728 | } |
| 8729 | |
| 8730 | /** |
| 8731 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8732 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8733 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8734 | * |
| 8735 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8736 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8737 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8738 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8739 | * @param[in] options XPath options. |
| 8740 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8741 | */ |
| 8742 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8743 | 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] | 8744 | { |
| 8745 | LY_ERR rc; |
| 8746 | uint16_t this_op, i; |
| 8747 | |
| 8748 | assert(repeat); |
| 8749 | |
| 8750 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8751 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8752 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8753 | 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] | 8754 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8755 | 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] | 8756 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8757 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8758 | } |
| 8759 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8760 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8761 | LY_CHECK_RET(rc); |
| 8762 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8763 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8764 | if (options & LYXP_SCNODE_ALL) { |
| 8765 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 8766 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8767 | 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] | 8768 | LY_CHECK_RET(rc); |
| 8769 | } |
| 8770 | } |
| 8771 | |
| 8772 | return LY_SUCCESS; |
| 8773 | } |
| 8774 | |
| 8775 | /** |
| 8776 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 8777 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8778 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8779 | * | MultiplicativeExpr '*' UnaryExpr |
| 8780 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8781 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8782 | * |
| 8783 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8784 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8785 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8786 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8787 | * @param[in] options XPath options. |
| 8788 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8789 | */ |
| 8790 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8791 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 8792 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8793 | { |
| 8794 | LY_ERR rc; |
| 8795 | uint16_t this_op; |
| 8796 | struct lyxp_set orig_set, set2; |
| 8797 | uint16_t i; |
| 8798 | |
| 8799 | assert(repeat); |
| 8800 | |
| 8801 | set_init(&orig_set, set); |
| 8802 | set_init(&set2, set); |
| 8803 | |
| 8804 | set_fill_set(&orig_set, set); |
| 8805 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8806 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8807 | LY_CHECK_GOTO(rc, cleanup); |
| 8808 | |
| 8809 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 8810 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8811 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8812 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8813 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8814 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8815 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8816 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8817 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8818 | if (options & LYXP_SKIP_EXPR) { |
| 8819 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8820 | LY_CHECK_GOTO(rc, cleanup); |
| 8821 | continue; |
| 8822 | } |
| 8823 | |
| 8824 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8825 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8826 | LY_CHECK_GOTO(rc, cleanup); |
| 8827 | |
| 8828 | /* eval */ |
| 8829 | if (options & LYXP_SCNODE_ALL) { |
| 8830 | 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] | 8831 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8832 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8833 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8834 | 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] | 8835 | LY_CHECK_GOTO(rc, cleanup); |
| 8836 | } |
| 8837 | } |
| 8838 | |
| 8839 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8840 | lyxp_set_free_content(&orig_set); |
| 8841 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8842 | return rc; |
| 8843 | } |
| 8844 | |
| 8845 | /** |
| 8846 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 8847 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8848 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8849 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8850 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8851 | * |
| 8852 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8853 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8854 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8855 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8856 | * @param[in] options XPath options. |
| 8857 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8858 | */ |
| 8859 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8860 | 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] | 8861 | { |
| 8862 | LY_ERR rc; |
| 8863 | uint16_t this_op; |
| 8864 | struct lyxp_set orig_set, set2; |
| 8865 | uint16_t i; |
| 8866 | |
| 8867 | assert(repeat); |
| 8868 | |
| 8869 | set_init(&orig_set, set); |
| 8870 | set_init(&set2, set); |
| 8871 | |
| 8872 | set_fill_set(&orig_set, set); |
| 8873 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8874 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8875 | LY_CHECK_GOTO(rc, cleanup); |
| 8876 | |
| 8877 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8878 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8879 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8880 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8881 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8882 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8883 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8884 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8885 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8886 | if (options & LYXP_SKIP_EXPR) { |
| 8887 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8888 | LY_CHECK_GOTO(rc, cleanup); |
| 8889 | continue; |
| 8890 | } |
| 8891 | |
| 8892 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8893 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8894 | LY_CHECK_GOTO(rc, cleanup); |
| 8895 | |
| 8896 | /* eval */ |
| 8897 | if (options & LYXP_SCNODE_ALL) { |
| 8898 | 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] | 8899 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8900 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8901 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8902 | 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] | 8903 | LY_CHECK_GOTO(rc, cleanup); |
| 8904 | } |
| 8905 | } |
| 8906 | |
| 8907 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8908 | lyxp_set_free_content(&orig_set); |
| 8909 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8910 | return rc; |
| 8911 | } |
| 8912 | |
| 8913 | /** |
| 8914 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8915 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8916 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8917 | * | RelationalExpr '<' AdditiveExpr |
| 8918 | * | RelationalExpr '>' AdditiveExpr |
| 8919 | * | RelationalExpr '<=' AdditiveExpr |
| 8920 | * | RelationalExpr '>=' AdditiveExpr |
| 8921 | * |
| 8922 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8923 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8924 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8925 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8926 | * @param[in] options XPath options. |
| 8927 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8928 | */ |
| 8929 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8930 | 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] | 8931 | { |
| 8932 | LY_ERR rc; |
| 8933 | uint16_t this_op; |
| 8934 | struct lyxp_set orig_set, set2; |
| 8935 | uint16_t i; |
| 8936 | |
| 8937 | assert(repeat); |
| 8938 | |
| 8939 | set_init(&orig_set, set); |
| 8940 | set_init(&set2, set); |
| 8941 | |
| 8942 | set_fill_set(&orig_set, set); |
| 8943 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8944 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8945 | LY_CHECK_GOTO(rc, cleanup); |
| 8946 | |
| 8947 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8948 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8949 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8950 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8951 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8952 | 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] | 8953 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8954 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8955 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8956 | if (options & LYXP_SKIP_EXPR) { |
| 8957 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8958 | LY_CHECK_GOTO(rc, cleanup); |
| 8959 | continue; |
| 8960 | } |
| 8961 | |
| 8962 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8963 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8964 | LY_CHECK_GOTO(rc, cleanup); |
| 8965 | |
| 8966 | /* eval */ |
| 8967 | if (options & LYXP_SCNODE_ALL) { |
| 8968 | 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] | 8969 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8970 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8971 | } else { |
| 8972 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8973 | LY_CHECK_GOTO(rc, cleanup); |
| 8974 | } |
| 8975 | } |
| 8976 | |
| 8977 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8978 | lyxp_set_free_content(&orig_set); |
| 8979 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8980 | return rc; |
| 8981 | } |
| 8982 | |
| 8983 | /** |
| 8984 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8985 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8986 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8987 | * | EqualityExpr '!=' RelationalExpr |
| 8988 | * |
| 8989 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8990 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8991 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8992 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8993 | * @param[in] options XPath options. |
| 8994 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8995 | */ |
| 8996 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8997 | 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] | 8998 | { |
| 8999 | LY_ERR rc; |
| 9000 | uint16_t this_op; |
| 9001 | struct lyxp_set orig_set, set2; |
| 9002 | uint16_t i; |
| 9003 | |
| 9004 | assert(repeat); |
| 9005 | |
| 9006 | set_init(&orig_set, set); |
| 9007 | set_init(&set2, set); |
| 9008 | |
| 9009 | set_fill_set(&orig_set, set); |
| 9010 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9011 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9012 | LY_CHECK_GOTO(rc, cleanup); |
| 9013 | |
| 9014 | /* ('=' / '!=' RelationalExpr)* */ |
| 9015 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9016 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9017 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9018 | 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] | 9019 | 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] | 9020 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9021 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9022 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9023 | if (options & LYXP_SKIP_EXPR) { |
| 9024 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9025 | LY_CHECK_GOTO(rc, cleanup); |
| 9026 | continue; |
| 9027 | } |
| 9028 | |
| 9029 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9030 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9031 | LY_CHECK_GOTO(rc, cleanup); |
| 9032 | |
| 9033 | /* eval */ |
| 9034 | if (options & LYXP_SCNODE_ALL) { |
| 9035 | 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] | 9036 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9037 | 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] | 9038 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9039 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9040 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9041 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 9042 | LY_CHECK_GOTO(rc, cleanup); |
| 9043 | } |
| 9044 | } |
| 9045 | |
| 9046 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9047 | lyxp_set_free_content(&orig_set); |
| 9048 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9049 | return rc; |
| 9050 | } |
| 9051 | |
| 9052 | /** |
| 9053 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9054 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9055 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9056 | * |
| 9057 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9058 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9059 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9060 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9061 | * @param[in] options XPath options. |
| 9062 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9063 | */ |
| 9064 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9065 | 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] | 9066 | { |
| 9067 | LY_ERR rc; |
| 9068 | struct lyxp_set orig_set, set2; |
| 9069 | uint16_t i; |
| 9070 | |
| 9071 | assert(repeat); |
| 9072 | |
| 9073 | set_init(&orig_set, set); |
| 9074 | set_init(&set2, set); |
| 9075 | |
| 9076 | set_fill_set(&orig_set, set); |
| 9077 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9078 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9079 | LY_CHECK_GOTO(rc, cleanup); |
| 9080 | |
| 9081 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9082 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9083 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9084 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9085 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9086 | } |
| 9087 | |
| 9088 | /* ('and' EqualityExpr)* */ |
| 9089 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9090 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9091 | 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] | 9092 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9093 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9094 | |
| 9095 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9096 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9097 | 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] | 9098 | LY_CHECK_GOTO(rc, cleanup); |
| 9099 | continue; |
| 9100 | } |
| 9101 | |
| 9102 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9103 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9104 | LY_CHECK_GOTO(rc, cleanup); |
| 9105 | |
| 9106 | /* eval - just get boolean value actually */ |
| 9107 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9108 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9109 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9110 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9111 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9112 | set_fill_set(set, &set2); |
| 9113 | } |
| 9114 | } |
| 9115 | |
| 9116 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9117 | lyxp_set_free_content(&orig_set); |
| 9118 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9119 | return rc; |
| 9120 | } |
| 9121 | |
| 9122 | /** |
| 9123 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9124 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9125 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9126 | * |
| 9127 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9128 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9129 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9130 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9131 | * @param[in] options XPath options. |
| 9132 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9133 | */ |
| 9134 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9135 | 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] | 9136 | { |
| 9137 | LY_ERR rc; |
| 9138 | struct lyxp_set orig_set, set2; |
| 9139 | uint16_t i; |
| 9140 | |
| 9141 | assert(repeat); |
| 9142 | |
| 9143 | set_init(&orig_set, set); |
| 9144 | set_init(&set2, set); |
| 9145 | |
| 9146 | set_fill_set(&orig_set, set); |
| 9147 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9148 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9149 | LY_CHECK_GOTO(rc, cleanup); |
| 9150 | |
| 9151 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9152 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9153 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9154 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9155 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9156 | } |
| 9157 | |
| 9158 | /* ('or' AndExpr)* */ |
| 9159 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9160 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9161 | 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] | 9162 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9163 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9164 | |
| 9165 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9166 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9167 | 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] | 9168 | LY_CHECK_GOTO(rc, cleanup); |
| 9169 | continue; |
| 9170 | } |
| 9171 | |
| 9172 | set_fill_set(&set2, &orig_set); |
| 9173 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9174 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9175 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9176 | LY_CHECK_GOTO(rc, cleanup); |
| 9177 | |
| 9178 | /* eval - just get boolean value actually */ |
| 9179 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9180 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9181 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9182 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9183 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9184 | set_fill_set(set, &set2); |
| 9185 | } |
| 9186 | } |
| 9187 | |
| 9188 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9189 | lyxp_set_free_content(&orig_set); |
| 9190 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9191 | return rc; |
| 9192 | } |
| 9193 | |
| 9194 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9195 | * @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] | 9196 | * |
| 9197 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9198 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9199 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9200 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9201 | * @param[in] options XPath options. |
| 9202 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9203 | */ |
| 9204 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9205 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 9206 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9207 | { |
| 9208 | uint16_t i, count; |
| 9209 | enum lyxp_expr_type next_etype; |
| 9210 | LY_ERR rc; |
| 9211 | |
| 9212 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9213 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9214 | next_etype = LYXP_EXPR_NONE; |
| 9215 | } else { |
| 9216 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9217 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9218 | |
| 9219 | /* select one-priority lower because etype expression called us */ |
| 9220 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9221 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9222 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9223 | 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] | 9224 | } else { |
| 9225 | next_etype = LYXP_EXPR_NONE; |
| 9226 | } |
| 9227 | } |
| 9228 | |
| 9229 | /* decide what expression are we parsing based on the repeat */ |
| 9230 | switch (next_etype) { |
| 9231 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9232 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9233 | break; |
| 9234 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9235 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9236 | break; |
| 9237 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9238 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9239 | break; |
| 9240 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9241 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9242 | break; |
| 9243 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9244 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9245 | break; |
| 9246 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9247 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9248 | break; |
| 9249 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9250 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9251 | break; |
| 9252 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9253 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9254 | break; |
| 9255 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9256 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9257 | break; |
| 9258 | default: |
| 9259 | LOGINT_RET(set->ctx); |
| 9260 | } |
| 9261 | |
| 9262 | return rc; |
| 9263 | } |
| 9264 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9265 | /** |
| 9266 | * @brief Get root type. |
| 9267 | * |
| 9268 | * @param[in] ctx_node Context node. |
| 9269 | * @param[in] ctx_scnode Schema context node. |
| 9270 | * @param[in] options XPath options. |
| 9271 | * @return Root type. |
| 9272 | */ |
| 9273 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9274 | 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] | 9275 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9276 | const struct lysc_node *op; |
| 9277 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9278 | /* explicit */ |
| 9279 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9280 | return LYXP_NODE_ROOT; |
| 9281 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9282 | return LYXP_NODE_ROOT_CONFIG; |
| 9283 | } |
| 9284 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9285 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9286 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9287 | 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] | 9288 | |
| 9289 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9290 | /* general root that can access everything */ |
| 9291 | return LYXP_NODE_ROOT; |
| 9292 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9293 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9294 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9295 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9296 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9297 | } |
| 9298 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9299 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9300 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9301 | 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] | 9302 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9303 | if (op || !(options & LYXP_SCHEMA)) { |
| 9304 | /* general root that can access everything */ |
| 9305 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9306 | } 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] | 9307 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9308 | return LYXP_NODE_ROOT_CONFIG; |
| 9309 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9310 | return LYXP_NODE_ROOT; |
| 9311 | } |
| 9312 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9313 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9314 | lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9315 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree, |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9316 | const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9317 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9318 | uint16_t tok_idx = 0; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9319 | const struct lysc_node *snode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9320 | LY_ERR rc; |
| 9321 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9322 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9323 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9324 | 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] | 9325 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9326 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9327 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9328 | if (tree) { |
| 9329 | /* adjust the pointer to be the first top-level sibling */ |
| 9330 | while (tree->parent) { |
| 9331 | tree = lyd_parent(tree); |
| 9332 | } |
| 9333 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9334 | |
| 9335 | for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {} |
| 9336 | if (snode) { |
| 9337 | /* unable to evaluate absolute paths */ |
| 9338 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9339 | LYD_NAME(tree)); |
| 9340 | return LY_EINVAL; |
| 9341 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9342 | } |
| 9343 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9344 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9345 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9346 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9347 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9348 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9349 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9350 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9351 | set->cur_node = ctx_node; |
| 9352 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9353 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9354 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9355 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9356 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9357 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9358 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9359 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9360 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9361 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9362 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9363 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9364 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9365 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9366 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9367 | } |
| 9368 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9369 | if (set->cur_node) { |
| 9370 | LOG_LOCBACK(0, 1, 0, 0); |
| 9371 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9372 | return rc; |
| 9373 | } |
| 9374 | |
| 9375 | #if 0 |
| 9376 | |
| 9377 | /* full xml printing of set elements, not used currently */ |
| 9378 | |
| 9379 | void |
| 9380 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9381 | { |
| 9382 | uint32_t i; |
| 9383 | char *str_num; |
| 9384 | struct lyout out; |
| 9385 | |
| 9386 | memset(&out, 0, sizeof out); |
| 9387 | |
| 9388 | out.type = LYOUT_STREAM; |
| 9389 | out.method.f = f; |
| 9390 | |
| 9391 | switch (set->type) { |
| 9392 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9393 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9394 | break; |
| 9395 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9396 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9397 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9398 | break; |
| 9399 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9400 | ly_print_(&out, "String XPath set:\n"); |
| 9401 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9402 | break; |
| 9403 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9404 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9405 | |
| 9406 | if (isnan(set->value.num)) { |
| 9407 | str_num = strdup("NaN"); |
| 9408 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9409 | str_num = strdup("0"); |
| 9410 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9411 | str_num = strdup("Infinity"); |
| 9412 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9413 | str_num = strdup("-Infinity"); |
| 9414 | } else if ((long long)set->value.num == set->value.num) { |
| 9415 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9416 | str_num = NULL; |
| 9417 | } |
| 9418 | } else { |
| 9419 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9420 | str_num = NULL; |
| 9421 | } |
| 9422 | } |
| 9423 | if (!str_num) { |
| 9424 | LOGMEM; |
| 9425 | return; |
| 9426 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9427 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9428 | free(str_num); |
| 9429 | break; |
| 9430 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9431 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9432 | |
| 9433 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9434 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9435 | switch (set->node_type[i]) { |
| 9436 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9437 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9438 | break; |
| 9439 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9440 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9441 | break; |
| 9442 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9443 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9444 | break; |
| 9445 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9446 | 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] | 9447 | break; |
| 9448 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9449 | 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] | 9450 | break; |
| 9451 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9452 | 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] | 9453 | break; |
| 9454 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9455 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9456 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9457 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9458 | break; |
| 9459 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9460 | 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] | 9461 | break; |
| 9462 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9463 | 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] | 9464 | break; |
| 9465 | } |
| 9466 | } |
| 9467 | break; |
| 9468 | } |
| 9469 | } |
| 9470 | |
| 9471 | #endif |
| 9472 | |
| 9473 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9474 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9475 | { |
| 9476 | long double num; |
| 9477 | char *str; |
| 9478 | LY_ERR rc; |
| 9479 | |
| 9480 | if (!set || (set->type == target)) { |
| 9481 | return LY_SUCCESS; |
| 9482 | } |
| 9483 | |
| 9484 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9485 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9486 | |
| 9487 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9488 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9489 | return LY_EINVAL; |
| 9490 | } |
| 9491 | |
| 9492 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9493 | 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] | 9494 | switch (set->type) { |
| 9495 | case LYXP_SET_NUMBER: |
| 9496 | if (isnan(set->val.num)) { |
| 9497 | set->val.str = strdup("NaN"); |
| 9498 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9499 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9500 | set->val.str = strdup("0"); |
| 9501 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9502 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9503 | set->val.str = strdup("Infinity"); |
| 9504 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9505 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9506 | set->val.str = strdup("-Infinity"); |
| 9507 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9508 | } else if ((long long)set->val.num == set->val.num) { |
| 9509 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9510 | LOGMEM_RET(set->ctx); |
| 9511 | } |
| 9512 | set->val.str = str; |
| 9513 | } else { |
| 9514 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9515 | LOGMEM_RET(set->ctx); |
| 9516 | } |
| 9517 | set->val.str = str; |
| 9518 | } |
| 9519 | break; |
| 9520 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9521 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9522 | set->val.str = strdup("true"); |
| 9523 | } else { |
| 9524 | set->val.str = strdup("false"); |
| 9525 | } |
| 9526 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9527 | break; |
| 9528 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9529 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9530 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9531 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9532 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9533 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9534 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9535 | set->val.str = str; |
| 9536 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9537 | default: |
| 9538 | LOGINT_RET(set->ctx); |
| 9539 | } |
| 9540 | set->type = LYXP_SET_STRING; |
| 9541 | } |
| 9542 | |
| 9543 | /* to NUMBER */ |
| 9544 | if (target == LYXP_SET_NUMBER) { |
| 9545 | switch (set->type) { |
| 9546 | case LYXP_SET_STRING: |
| 9547 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9548 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9549 | set->val.num = num; |
| 9550 | break; |
| 9551 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9552 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9553 | set->val.num = 1; |
| 9554 | } else { |
| 9555 | set->val.num = 0; |
| 9556 | } |
| 9557 | break; |
| 9558 | default: |
| 9559 | LOGINT_RET(set->ctx); |
| 9560 | } |
| 9561 | set->type = LYXP_SET_NUMBER; |
| 9562 | } |
| 9563 | |
| 9564 | /* to BOOLEAN */ |
| 9565 | if (target == LYXP_SET_BOOLEAN) { |
| 9566 | switch (set->type) { |
| 9567 | case LYXP_SET_NUMBER: |
| 9568 | 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] | 9569 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9570 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9571 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9572 | } |
| 9573 | break; |
| 9574 | case LYXP_SET_STRING: |
| 9575 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9576 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9577 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9578 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9579 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9580 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9581 | } |
| 9582 | break; |
| 9583 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9584 | if (set->used) { |
| 9585 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9586 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9587 | } else { |
| 9588 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9589 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9590 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9591 | break; |
| 9592 | default: |
| 9593 | LOGINT_RET(set->ctx); |
| 9594 | } |
| 9595 | set->type = LYXP_SET_BOOLEAN; |
| 9596 | } |
| 9597 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9598 | return LY_SUCCESS; |
| 9599 | } |
| 9600 | |
| 9601 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9602 | lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9603 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set, |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9604 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9605 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9606 | LY_ERR ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9607 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9608 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9609 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9610 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9611 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9612 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9613 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9614 | |
| 9615 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9616 | memset(set, 0, sizeof *set); |
| 9617 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9618 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 9619 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL)); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 9620 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9621 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9622 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9623 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9624 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9625 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9626 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9627 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9628 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9629 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9630 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9631 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9632 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9633 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9634 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9635 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9636 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9637 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9638 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9639 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9640 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9641 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9642 | { |
| 9643 | if (!path) { |
| 9644 | return NULL; |
| 9645 | } |
| 9646 | |
| 9647 | return path->expr; |
| 9648 | } |