Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file xpath.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG XPath evaluation functions |
| 5 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 17 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 19 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include <errno.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 25 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 26 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 28 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 32 | #include "out.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 33 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 34 | #include "path.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 35 | #include "plugins_exts/metadata.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 36 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 37 | #include "printer_data.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 38 | #include "schema_compile_node.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 39 | #include "tree.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 40 | #include "tree_data.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 42 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 43 | #include "tree_schema_internal.h" |
| 44 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 45 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 46 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth); |
| 47 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 48 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 49 | static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set, |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 50 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 51 | static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, |
| 52 | 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] | 53 | static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 54 | enum lyxp_axis axis, uint32_t options); |
| 55 | static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 56 | enum lyxp_axis axis, uint32_t options); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 57 | static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 58 | |
aPiecek | 96dc1e3 | 2021-10-08 15:45:59 +0200 | [diff] [blame] | 59 | /* Functions are divided into the following basic classes: |
| 60 | * |
| 61 | * (re)parse functions: |
| 62 | * Parse functions parse the expression into |
| 63 | * tokens (syntactic analysis). |
| 64 | * Reparse functions perform semantic analysis |
| 65 | * (do not save the result, just a check) of |
| 66 | * the expression and fill repeat indices. |
| 67 | * |
| 68 | * warn functions: |
| 69 | * Warn functions check specific reasonable conditions for schema XPath |
| 70 | * and print a warning if they are not satisfied. |
| 71 | * |
| 72 | * moveto functions: |
| 73 | * They and only they actually change the context (set). |
| 74 | * |
| 75 | * eval functions: |
| 76 | * They execute a parsed XPath expression on some data subtree. |
| 77 | */ |
| 78 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 79 | /** |
| 80 | * @brief Print the type of an XPath \p set. |
| 81 | * |
| 82 | * @param[in] set Set to use. |
| 83 | * @return Set type string. |
| 84 | */ |
| 85 | static const char * |
| 86 | print_set_type(struct lyxp_set *set) |
| 87 | { |
| 88 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 89 | case LYXP_SET_NODE_SET: |
| 90 | return "node set"; |
| 91 | case LYXP_SET_SCNODE_SET: |
| 92 | return "schema node set"; |
| 93 | case LYXP_SET_BOOLEAN: |
| 94 | return "boolean"; |
| 95 | case LYXP_SET_NUMBER: |
| 96 | return "number"; |
| 97 | case LYXP_SET_STRING: |
| 98 | return "string"; |
| 99 | } |
| 100 | |
| 101 | return NULL; |
| 102 | } |
| 103 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 104 | const char * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 105 | lyxp_token2str(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 106 | { |
| 107 | switch (tok) { |
| 108 | case LYXP_TOKEN_PAR1: |
| 109 | return "("; |
| 110 | case LYXP_TOKEN_PAR2: |
| 111 | return ")"; |
| 112 | case LYXP_TOKEN_BRACK1: |
| 113 | return "["; |
| 114 | case LYXP_TOKEN_BRACK2: |
| 115 | return "]"; |
| 116 | case LYXP_TOKEN_DOT: |
| 117 | return "."; |
| 118 | case LYXP_TOKEN_DDOT: |
| 119 | return ".."; |
| 120 | case LYXP_TOKEN_AT: |
| 121 | return "@"; |
| 122 | case LYXP_TOKEN_COMMA: |
| 123 | return ","; |
Michal Vasko | e0a8ce7 | 2023-04-04 08:30:44 +0200 | [diff] [blame] | 124 | case LYXP_TOKEN_DCOLON: |
| 125 | return "::"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 126 | case LYXP_TOKEN_NAMETEST: |
| 127 | return "NameTest"; |
| 128 | case LYXP_TOKEN_NODETYPE: |
| 129 | return "NodeType"; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 130 | case LYXP_TOKEN_VARREF: |
| 131 | return "VariableReference"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 132 | case LYXP_TOKEN_FUNCNAME: |
| 133 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 134 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 135 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 136 | case LYXP_TOKEN_OPER_EQUAL: |
| 137 | return "Operator(Equal)"; |
| 138 | case LYXP_TOKEN_OPER_NEQUAL: |
| 139 | return "Operator(Non-equal)"; |
| 140 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 141 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 142 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 143 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 144 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 145 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 146 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 147 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 148 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 149 | return "Operator(Recursive Path)"; |
Michal Vasko | e0a8ce7 | 2023-04-04 08:30:44 +0200 | [diff] [blame] | 150 | case LYXP_TOKEN_AXISNAME: |
| 151 | return "AxisName"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 152 | case LYXP_TOKEN_LITERAL: |
| 153 | return "Literal"; |
| 154 | case LYXP_TOKEN_NUMBER: |
| 155 | return "Number"; |
| 156 | default: |
| 157 | LOGINT(NULL); |
| 158 | return ""; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 163 | * @brief Transform string into an axis. |
| 164 | * |
| 165 | * @param[in] str String to transform. |
| 166 | * @param[in] str_len Length of @p str. |
| 167 | * @return Transformed axis. |
| 168 | */ |
| 169 | static enum lyxp_axis |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 170 | str2axis(const char *str, uint32_t str_len) |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 171 | { |
| 172 | switch (str_len) { |
| 173 | case 4: |
| 174 | assert(!strncmp("self", str, str_len)); |
| 175 | return LYXP_AXIS_SELF; |
| 176 | case 5: |
| 177 | assert(!strncmp("child", str, str_len)); |
| 178 | return LYXP_AXIS_CHILD; |
| 179 | case 6: |
| 180 | assert(!strncmp("parent", str, str_len)); |
| 181 | return LYXP_AXIS_PARENT; |
| 182 | case 8: |
| 183 | assert(!strncmp("ancestor", str, str_len)); |
| 184 | return LYXP_AXIS_ANCESTOR; |
| 185 | case 9: |
| 186 | if (str[0] == 'a') { |
| 187 | assert(!strncmp("attribute", str, str_len)); |
| 188 | return LYXP_AXIS_ATTRIBUTE; |
| 189 | } else if (str[0] == 'f') { |
| 190 | assert(!strncmp("following", str, str_len)); |
| 191 | return LYXP_AXIS_FOLLOWING; |
| 192 | } else { |
| 193 | assert(!strncmp("preceding", str, str_len)); |
| 194 | return LYXP_AXIS_PRECEDING; |
| 195 | } |
| 196 | break; |
| 197 | case 10: |
| 198 | assert(!strncmp("descendant", str, str_len)); |
| 199 | return LYXP_AXIS_DESCENDANT; |
| 200 | case 16: |
| 201 | assert(!strncmp("ancestor-or-self", str, str_len)); |
| 202 | return LYXP_AXIS_ANCESTOR_OR_SELF; |
| 203 | case 17: |
| 204 | if (str[0] == 'f') { |
| 205 | assert(!strncmp("following-sibling", str, str_len)); |
| 206 | return LYXP_AXIS_FOLLOWING_SIBLING; |
| 207 | } else { |
| 208 | assert(!strncmp("preceding-sibling", str, str_len)); |
| 209 | return LYXP_AXIS_PRECEDING_SIBLING; |
| 210 | } |
| 211 | break; |
| 212 | case 18: |
| 213 | assert(!strncmp("descendant-or-self", str, str_len)); |
| 214 | return LYXP_AXIS_DESCENDANT_OR_SELF; |
| 215 | } |
| 216 | |
| 217 | LOGINT(NULL); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 222 | * @brief Print the whole expression \p exp to debug output. |
| 223 | * |
| 224 | * @param[in] exp Expression to use. |
| 225 | */ |
| 226 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 227 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 228 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 229 | #define MSG_BUFFER_SIZE 128 |
| 230 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 231 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 232 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 233 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | |
| 237 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 238 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 239 | 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] | 240 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 241 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 242 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 243 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 244 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 245 | } |
| 246 | strcat(tmp, ")"); |
| 247 | } |
| 248 | LOGDBG(LY_LDGXPATH, tmp); |
| 249 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 250 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | #ifndef NDEBUG |
| 254 | |
| 255 | /** |
| 256 | * @brief Print XPath set content to debug output. |
| 257 | * |
| 258 | * @param[in] set Set to print. |
| 259 | */ |
| 260 | static void |
| 261 | print_set_debug(struct lyxp_set *set) |
| 262 | { |
| 263 | uint32_t i; |
| 264 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 265 | struct lyxp_set_node *item; |
| 266 | struct lyxp_set_scnode *sitem; |
| 267 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 268 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
| 272 | switch (set->type) { |
| 273 | case LYXP_SET_NODE_SET: |
| 274 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 275 | for (i = 0; i < set->used; ++i) { |
| 276 | item = &set->val.nodes[i]; |
| 277 | |
| 278 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 279 | case LYXP_NODE_NONE: |
| 280 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 281 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 282 | case LYXP_NODE_ROOT: |
| 283 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 284 | break; |
| 285 | case LYXP_NODE_ROOT_CONFIG: |
| 286 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 287 | break; |
| 288 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 289 | 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] | 290 | 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] | 291 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 292 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 293 | 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] | 294 | item->node->schema->name, lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 295 | } else { |
| 296 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 297 | } |
| 298 | break; |
| 299 | case LYXP_NODE_TEXT: |
| 300 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 301 | 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] | 302 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 303 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 304 | 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] | 305 | } |
| 306 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 307 | case LYXP_NODE_META: |
| 308 | 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] | 309 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | } |
| 313 | break; |
| 314 | |
| 315 | case LYXP_SET_SCNODE_SET: |
| 316 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 317 | for (i = 0; i < set->used; ++i) { |
| 318 | sitem = &set->val.scnodes[i]; |
| 319 | |
| 320 | switch (sitem->type) { |
| 321 | case LYXP_NODE_ROOT: |
| 322 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 323 | break; |
| 324 | case LYXP_NODE_ROOT_CONFIG: |
| 325 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 326 | break; |
| 327 | case LYXP_NODE_ELEM: |
| 328 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 329 | break; |
| 330 | default: |
| 331 | LOGINT(NULL); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | break; |
| 336 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 337 | case LYXP_SET_BOOLEAN: |
| 338 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 339 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 340 | break; |
| 341 | |
| 342 | case LYXP_SET_STRING: |
| 343 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 344 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 345 | break; |
| 346 | |
| 347 | case LYXP_SET_NUMBER: |
| 348 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 349 | |
| 350 | if (isnan(set->val.num)) { |
| 351 | str = strdup("NaN"); |
| 352 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 353 | str = strdup("0"); |
| 354 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 355 | str = strdup("Infinity"); |
| 356 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 357 | str = strdup("-Infinity"); |
| 358 | } else if ((long long)set->val.num == set->val.num) { |
| 359 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 360 | str = NULL; |
| 361 | } |
| 362 | } else { |
| 363 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 364 | str = NULL; |
| 365 | } |
| 366 | } |
| 367 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 368 | |
| 369 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 370 | free(str); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | #endif |
| 375 | |
| 376 | /** |
| 377 | * @brief Realloc the string \p str. |
| 378 | * |
| 379 | * @param[in] ctx libyang context for logging. |
| 380 | * @param[in] needed How much free space is required. |
| 381 | * @param[in,out] str Pointer to the string to use. |
| 382 | * @param[in,out] used Used bytes in \p str. |
| 383 | * @param[in,out] size Allocated bytes in \p str. |
| 384 | * @return LY_ERR |
| 385 | */ |
| 386 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 387 | cast_string_realloc(const struct ly_ctx *ctx, uint64_t needed, char **str, uint32_t *used, uint32_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 388 | { |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 389 | if (*size - (unsigned)*used < needed) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 390 | do { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 391 | if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 392 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 393 | return LY_EINVAL; |
| 394 | } |
| 395 | *size += LYXP_STRING_CAST_SIZE_STEP; |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 396 | } while (*size - (unsigned)*used < needed); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 397 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 398 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 399 | } |
| 400 | |
| 401 | return LY_SUCCESS; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * @brief Cast nodes recursively to one string @p str. |
| 406 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 407 | * @param[in] node Node to cast, NULL if root. |
| 408 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 409 | * @param[in] indent Current indent. |
| 410 | * @param[in,out] str Resulting string. |
| 411 | * @param[in,out] used Used bytes in @p str. |
| 412 | * @param[in,out] size Allocated bytes in @p str. |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 413 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 414 | */ |
| 415 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 416 | cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used, |
| 417 | uint32_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 419 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 420 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 421 | const struct lyd_node *child; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 422 | enum lyxp_node_type child_type; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 423 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 424 | struct lyd_node_any *any; |
| 425 | LY_ERR rc; |
| 426 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 427 | 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] | 428 | return LY_SUCCESS; |
| 429 | } |
| 430 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 431 | if (!node) { |
| 432 | /* fake container */ |
| 433 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 434 | strcpy(*str + (*used - 1), "\n"); |
| 435 | ++(*used); |
| 436 | |
| 437 | ++indent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 438 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 439 | /* print all the top-level nodes */ |
| 440 | child = NULL; |
| 441 | child_type = 0; |
| 442 | while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) { |
| 443 | LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 446 | /* end fake container */ |
| 447 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 448 | strcpy(*str + (*used - 1), "\n"); |
| 449 | ++(*used); |
| 450 | |
| 451 | --indent; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 452 | } else { |
| 453 | switch (node->schema->nodetype) { |
| 454 | case LYS_CONTAINER: |
| 455 | case LYS_LIST: |
| 456 | case LYS_RPC: |
| 457 | case LYS_NOTIF: |
| 458 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
| 459 | strcpy(*str + (*used - 1), "\n"); |
| 460 | ++(*used); |
| 461 | |
| 462 | for (child = lyd_child(node); child; child = child->next) { |
| 463 | LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size)); |
| 464 | } |
| 465 | |
| 466 | break; |
| 467 | |
| 468 | case LYS_LEAF: |
| 469 | case LYS_LEAFLIST: |
| 470 | value_str = lyd_get_value(node); |
| 471 | |
| 472 | /* print indent */ |
| 473 | LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size)); |
| 474 | memset(*str + (*used - 1), ' ', indent * 2); |
| 475 | *used += indent * 2; |
| 476 | |
| 477 | /* print value */ |
| 478 | if (*used == 1) { |
| 479 | sprintf(*str + (*used - 1), "%s", value_str); |
| 480 | *used += strlen(value_str); |
| 481 | } else { |
| 482 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 483 | *used += strlen(value_str) + 1; |
| 484 | } |
| 485 | |
| 486 | break; |
| 487 | |
| 488 | case LYS_ANYXML: |
| 489 | case LYS_ANYDATA: |
| 490 | any = (struct lyd_node_any *)node; |
| 491 | if (!(void *)any->value.tree) { |
| 492 | /* no content */ |
| 493 | buf = strdup(""); |
| 494 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 495 | } else { |
| 496 | struct ly_out *out; |
| 497 | |
| 498 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 499 | /* try to parse it into a data tree */ |
| 500 | if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB, |
| 501 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
| 502 | /* successfully parsed */ |
| 503 | free(any->value.mem); |
| 504 | any->value.tree = tree; |
| 505 | any->value_type = LYD_ANYDATA_DATATREE; |
| 506 | } |
| 507 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
| 508 | } |
| 509 | |
| 510 | switch (any->value_type) { |
| 511 | case LYD_ANYDATA_STRING: |
| 512 | case LYD_ANYDATA_XML: |
| 513 | case LYD_ANYDATA_JSON: |
| 514 | buf = strdup(any->value.json); |
| 515 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 516 | break; |
| 517 | case LYD_ANYDATA_DATATREE: |
| 518 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
| 519 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
| 520 | ly_out_free(out, NULL, 0); |
| 521 | LY_CHECK_RET(rc < 0, -rc); |
| 522 | break; |
| 523 | case LYD_ANYDATA_LYB: |
| 524 | LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 525 | return LY_EINVAL; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | line = strtok_r(buf, "\n", &ptr); |
| 530 | do { |
| 531 | rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size); |
| 532 | if (rc != LY_SUCCESS) { |
| 533 | free(buf); |
| 534 | return rc; |
| 535 | } |
| 536 | memset(*str + (*used - 1), ' ', indent * 2); |
| 537 | *used += indent * 2; |
| 538 | |
| 539 | strcpy(*str + (*used - 1), line); |
| 540 | *used += strlen(line); |
| 541 | |
| 542 | strcpy(*str + (*used - 1), "\n"); |
| 543 | *used += 1; |
| 544 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 545 | |
| 546 | free(buf); |
| 547 | break; |
| 548 | |
| 549 | default: |
| 550 | LOGINT_RET(set->ctx); |
| 551 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | return LY_SUCCESS; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @brief Cast an element into a string. |
| 559 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 560 | * @param[in] node Node to cast, NULL if root. |
| 561 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 562 | * @param[out] str Element cast to dynamically-allocated string. |
| 563 | * @return LY_ERR |
| 564 | */ |
| 565 | static LY_ERR |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 566 | 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] | 567 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 568 | uint32_t used, size; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 569 | LY_ERR rc; |
| 570 | |
| 571 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 572 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 573 | (*str)[0] = '\0'; |
| 574 | used = 1; |
| 575 | size = LYXP_STRING_CAST_SIZE_START; |
| 576 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 577 | rc = cast_string_recursive(node, set, 0, str, &used, &size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 578 | if (rc != LY_SUCCESS) { |
| 579 | free(*str); |
| 580 | return rc; |
| 581 | } |
| 582 | |
| 583 | if (size > used) { |
| 584 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 585 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 586 | } |
| 587 | return LY_SUCCESS; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 592 | * Context position aware. |
| 593 | * |
| 594 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 595 | * @param[out] str Cast dynamically-allocated string. |
| 596 | * @return LY_ERR |
| 597 | */ |
| 598 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 599 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 600 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 601 | if (!set->used) { |
| 602 | *str = strdup(""); |
| 603 | if (!*str) { |
| 604 | LOGMEM_RET(set->ctx); |
| 605 | } |
| 606 | return LY_SUCCESS; |
| 607 | } |
| 608 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 609 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 610 | case LYXP_NODE_NONE: |
| 611 | /* invalid */ |
| 612 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 613 | case LYXP_NODE_ROOT: |
| 614 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 615 | case LYXP_NODE_ELEM: |
| 616 | case LYXP_NODE_TEXT: |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 617 | return cast_string_elem(set->val.nodes[0].node, set, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 618 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 619 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 620 | if (!*str) { |
| 621 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 622 | } |
| 623 | return LY_SUCCESS; |
| 624 | } |
| 625 | |
| 626 | LOGINT_RET(set->ctx); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * @brief Cast a string into an XPath number. |
| 631 | * |
| 632 | * @param[in] str String to use. |
| 633 | * @return Cast number. |
| 634 | */ |
| 635 | static long double |
| 636 | cast_string_to_number(const char *str) |
| 637 | { |
| 638 | long double num; |
| 639 | char *ptr; |
| 640 | |
| 641 | errno = 0; |
| 642 | num = strtold(str, &ptr); |
| 643 | if (errno || *ptr) { |
| 644 | num = NAN; |
| 645 | } |
| 646 | return num; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * @brief Callback for checking value equality. |
| 651 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 652 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 653 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 654 | * @param[in] val1_p First value. |
| 655 | * @param[in] val2_p Second value. |
| 656 | * @param[in] mod Whether hash table is being modified. |
| 657 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 658 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 659 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 660 | static ly_bool |
| 661 | 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] | 662 | { |
| 663 | struct lyxp_set_hash_node *val1, *val2; |
| 664 | |
| 665 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 666 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 667 | |
| 668 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 669 | return 1; |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * @brief Insert node and its hash into set. |
| 677 | * |
| 678 | * @param[in] set et to insert to. |
| 679 | * @param[in] node Node with hash. |
| 680 | * @param[in] type Node type. |
| 681 | */ |
| 682 | static void |
| 683 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 684 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 685 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 686 | uint32_t i, hash; |
| 687 | struct lyxp_set_hash_node hnode; |
| 688 | |
| 689 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 690 | /* create hash table and add all the nodes */ |
| 691 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 692 | for (i = 0; i < set->used; ++i) { |
| 693 | hnode.node = set->val.nodes[i].node; |
| 694 | hnode.type = set->val.nodes[i].type; |
| 695 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 696 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 697 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 698 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 699 | |
| 700 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 701 | assert(!r); |
| 702 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 703 | |
Michal Vasko | 513b87d | 2023-08-04 14:03:40 +0200 | [diff] [blame] | 704 | if ((hnode.node == node) && (hnode.type == type)) { |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 705 | /* it was just added, do not add it twice */ |
Michal Vasko | 513b87d | 2023-08-04 14:03:40 +0200 | [diff] [blame] | 706 | return; |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
Michal Vasko | 513b87d | 2023-08-04 14:03:40 +0200 | [diff] [blame] | 711 | if (set->ht) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 712 | /* add the new node into hash table */ |
| 713 | hnode.node = node; |
| 714 | hnode.type = type; |
| 715 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 716 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 717 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 718 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 719 | |
| 720 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 721 | assert(!r); |
| 722 | (void)r; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * @brief Remove node and its hash from set. |
| 728 | * |
| 729 | * @param[in] set Set to remove from. |
| 730 | * @param[in] node Node to remove. |
| 731 | * @param[in] type Node type. |
| 732 | */ |
| 733 | static void |
| 734 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 735 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 736 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 737 | struct lyxp_set_hash_node hnode; |
| 738 | uint32_t hash; |
| 739 | |
| 740 | if (set->ht) { |
| 741 | hnode.node = node; |
| 742 | hnode.type = type; |
| 743 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 744 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 745 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 746 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 747 | |
| 748 | r = lyht_remove(set->ht, &hnode, hash); |
| 749 | assert(!r); |
| 750 | (void)r; |
| 751 | |
| 752 | if (!set->ht->used) { |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 753 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 754 | set->ht = NULL; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * @brief Check whether node is in set based on its hash. |
| 761 | * |
| 762 | * @param[in] set Set to search in. |
| 763 | * @param[in] node Node to search for. |
| 764 | * @param[in] type Node type. |
| 765 | * @param[in] skip_idx Index in @p set to skip. |
| 766 | * @return LY_ERR |
| 767 | */ |
| 768 | static LY_ERR |
| 769 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 770 | { |
| 771 | struct lyxp_set_hash_node hnode, *match_p; |
| 772 | uint32_t hash; |
| 773 | |
| 774 | hnode.node = node; |
| 775 | hnode.type = type; |
| 776 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 777 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 778 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 779 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 780 | |
| 781 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 782 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 783 | /* we found it on the index that should be skipped, find another */ |
| 784 | hnode = *match_p; |
| 785 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 786 | /* none other found */ |
| 787 | return LY_SUCCESS; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | return LY_EEXIST; |
| 792 | } |
| 793 | |
| 794 | /* not found */ |
| 795 | return LY_SUCCESS; |
| 796 | } |
| 797 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 798 | void |
| 799 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 800 | { |
| 801 | if (!set) { |
| 802 | return; |
| 803 | } |
| 804 | |
| 805 | if (set->type == LYXP_SET_NODE_SET) { |
| 806 | free(set->val.nodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 807 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 808 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 809 | free(set->val.scnodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 810 | lyht_free(set->ht, NULL); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 811 | } else { |
| 812 | if (set->type == LYXP_SET_STRING) { |
| 813 | free(set->val.str); |
| 814 | } |
| 815 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 816 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 817 | |
| 818 | set->val.nodes = NULL; |
| 819 | set->used = 0; |
| 820 | set->size = 0; |
| 821 | set->ht = NULL; |
| 822 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 823 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 826 | /** |
| 827 | * @brief Free dynamically-allocated set. |
| 828 | * |
| 829 | * @param[in] set Set to free. |
| 830 | */ |
| 831 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 832 | lyxp_set_free(struct lyxp_set *set) |
| 833 | { |
| 834 | if (!set) { |
| 835 | return; |
| 836 | } |
| 837 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 838 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 839 | free(set); |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * @brief Initialize set context. |
| 844 | * |
| 845 | * @param[in] new Set to initialize. |
| 846 | * @param[in] set Arbitrary initialized set. |
| 847 | */ |
| 848 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 849 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 850 | { |
| 851 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 852 | if (set) { |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 853 | new->non_child_axis = set->non_child_axis; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 854 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 855 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 856 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 857 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 858 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 859 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 860 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 861 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 862 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 863 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /** |
| 867 | * @brief Create a deep copy of a set. |
| 868 | * |
| 869 | * @param[in] set Set to copy. |
| 870 | * @return Copy of @p set. |
| 871 | */ |
| 872 | static struct lyxp_set * |
| 873 | set_copy(struct lyxp_set *set) |
| 874 | { |
| 875 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 876 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 877 | |
| 878 | if (!set) { |
| 879 | return NULL; |
| 880 | } |
| 881 | |
| 882 | ret = malloc(sizeof *ret); |
| 883 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 884 | set_init(ret, set); |
| 885 | |
| 886 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 887 | ret->type = set->type; |
| 888 | |
| 889 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 890 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 891 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 892 | uint32_t idx; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 893 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 894 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 895 | set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 896 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 897 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 898 | lyxp_set_free(ret); |
| 899 | return NULL; |
| 900 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 901 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 905 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 906 | if (set->used) { |
| 907 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 908 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 909 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 910 | } else { |
| 911 | ret->val.nodes = NULL; |
| 912 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 913 | |
| 914 | ret->used = ret->size = set->used; |
| 915 | ret->ctx_pos = set->ctx_pos; |
| 916 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 917 | if (set->ht) { |
| 918 | ret->ht = lyht_dup(set->ht); |
| 919 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 920 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 921 | memcpy(ret, set, sizeof *ret); |
| 922 | if (set->type == LYXP_SET_STRING) { |
| 923 | ret->val.str = strdup(set->val.str); |
| 924 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 925 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | return ret; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 933 | * |
| 934 | * @param[in] set Set to fill. |
| 935 | * @param[in] string String to fill into \p set. |
| 936 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 937 | */ |
| 938 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 939 | set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 940 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 941 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 942 | |
| 943 | set->type = LYXP_SET_STRING; |
| 944 | if ((str_len == 0) && (string[0] != '\0')) { |
| 945 | string = ""; |
| 946 | } |
| 947 | set->val.str = strndup(string, str_len); |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 952 | * |
| 953 | * @param[in] set Set to fill. |
| 954 | * @param[in] number Number to fill into \p set. |
| 955 | */ |
| 956 | static void |
| 957 | set_fill_number(struct lyxp_set *set, long double number) |
| 958 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 959 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 960 | |
| 961 | set->type = LYXP_SET_NUMBER; |
| 962 | set->val.num = number; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 967 | * |
| 968 | * @param[in] set Set to fill. |
| 969 | * @param[in] boolean Boolean to fill into \p set. |
| 970 | */ |
| 971 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 972 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 973 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 974 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 975 | |
| 976 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 977 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /** |
| 981 | * @brief Fill XPath set with the value from another set (deep assign). |
| 982 | * Any current data are disposed of. |
| 983 | * |
| 984 | * @param[in] trg Set to fill. |
| 985 | * @param[in] src Source set to copy into \p trg. |
| 986 | */ |
| 987 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 988 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 989 | { |
| 990 | if (!trg || !src) { |
| 991 | return; |
| 992 | } |
| 993 | |
Michal Vasko | f06d19d | 2023-08-04 14:04:08 +0200 | [diff] [blame] | 994 | lyxp_set_free_content(trg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 995 | set_init(trg, src); |
| 996 | |
| 997 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 998 | trg->type = LYXP_SET_SCNODE_SET; |
| 999 | trg->used = src->used; |
| 1000 | trg->size = src->used; |
| 1001 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1002 | if (trg->size) { |
| 1003 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 1004 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1005 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 1006 | } else { |
| 1007 | trg->val.scnodes = NULL; |
| 1008 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1009 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1010 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1011 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1012 | set_fill_number(trg, src->val.num); |
| 1013 | } else if (src->type == LYXP_SET_STRING) { |
| 1014 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 1015 | } else { |
| 1016 | if (trg->type == LYXP_SET_NODE_SET) { |
| 1017 | free(trg->val.nodes); |
| 1018 | } else if (trg->type == LYXP_SET_STRING) { |
| 1019 | free(trg->val.str); |
| 1020 | } |
| 1021 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1022 | assert(src->type == LYXP_SET_NODE_SET); |
| 1023 | |
| 1024 | trg->type = LYXP_SET_NODE_SET; |
| 1025 | trg->used = src->used; |
| 1026 | trg->size = src->used; |
| 1027 | trg->ctx_pos = src->ctx_pos; |
| 1028 | trg->ctx_size = src->ctx_size; |
| 1029 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1030 | if (trg->size) { |
| 1031 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 1032 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1033 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 1034 | } else { |
| 1035 | trg->val.nodes = NULL; |
| 1036 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1037 | if (src->ht) { |
| 1038 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1039 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1040 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * @brief Clear context of all schema nodes. |
| 1047 | * |
| 1048 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1049 | * @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] | 1050 | */ |
| 1051 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1052 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1053 | { |
| 1054 | uint32_t i; |
| 1055 | |
| 1056 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1057 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1058 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1059 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 1060 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | /** |
| 1066 | * @brief Remove a node from a set. Removing last node changes |
| 1067 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1068 | * |
| 1069 | * @param[in] set Set to use. |
| 1070 | * @param[in] idx Index from @p set of the node to be removed. |
| 1071 | */ |
| 1072 | static void |
| 1073 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1074 | { |
| 1075 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1076 | assert(idx < set->used); |
| 1077 | |
| 1078 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1079 | |
| 1080 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1081 | if (idx < set->used) { |
| 1082 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1083 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1084 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1089 | * @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] | 1090 | * |
| 1091 | * @param[in] set Set to use. |
| 1092 | * @param[in] idx Index from @p set of the node to be removed. |
| 1093 | */ |
| 1094 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1095 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1096 | { |
| 1097 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1098 | assert(idx < set->used); |
| 1099 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1100 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1101 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1102 | } |
| 1103 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1107 | * @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] | 1108 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1109 | * |
| 1110 | * @param[in] set Set to consolidate. |
| 1111 | */ |
| 1112 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1113 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1114 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1115 | uint32_t i, orig_used, end = 0; |
| 1116 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1117 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1118 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1119 | |
| 1120 | orig_used = set->used; |
| 1121 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1122 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1123 | start = -1; |
| 1124 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1125 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1126 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1127 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1128 | end = i; |
| 1129 | ++i; |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | ++i; |
| 1134 | if (i == orig_used) { |
| 1135 | end = i; |
| 1136 | } |
| 1137 | } while (i < orig_used); |
| 1138 | |
| 1139 | if (start > -1) { |
| 1140 | /* move the whole chunk of valid nodes together */ |
| 1141 | if (set->used != (unsigned)start) { |
| 1142 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1143 | } |
| 1144 | set->used += end - start; |
| 1145 | } |
| 1146 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1150 | * @brief Check for duplicates in a node set. |
| 1151 | * |
| 1152 | * @param[in] set Set to check. |
| 1153 | * @param[in] node Node to look for in @p set. |
| 1154 | * @param[in] node_type Type of @p node. |
| 1155 | * @param[in] skip_idx Index from @p set to skip. |
| 1156 | * @return LY_ERR |
| 1157 | */ |
| 1158 | static LY_ERR |
| 1159 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1160 | { |
| 1161 | uint32_t i; |
| 1162 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1163 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1164 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1165 | } |
| 1166 | |
| 1167 | for (i = 0; i < set->used; ++i) { |
| 1168 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1169 | continue; |
| 1170 | } |
| 1171 | |
| 1172 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1173 | return LY_EEXIST; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | return LY_SUCCESS; |
| 1178 | } |
| 1179 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1180 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1181 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1182 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1183 | { |
| 1184 | uint32_t i; |
| 1185 | |
| 1186 | for (i = 0; i < set->used; ++i) { |
| 1187 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1188 | continue; |
| 1189 | } |
| 1190 | |
| 1191 | 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] | 1192 | if (index_p) { |
| 1193 | *index_p = i; |
| 1194 | } |
| 1195 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1199 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1202 | void |
| 1203 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1204 | { |
| 1205 | uint32_t orig_used, i, j; |
| 1206 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1207 | 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] | 1208 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1209 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1210 | return; |
| 1211 | } |
| 1212 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1213 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1214 | /* release hidden allocated data (lyxp_set.size) */ |
| 1215 | lyxp_set_free_content(set1); |
| 1216 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1217 | memcpy(set1, set2, sizeof *set1); |
| 1218 | return; |
| 1219 | } |
| 1220 | |
| 1221 | if (set1->used + set2->used > set1->size) { |
| 1222 | set1->size = set1->used + set2->used; |
| 1223 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1224 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1225 | } |
| 1226 | |
| 1227 | orig_used = set1->used; |
| 1228 | |
| 1229 | for (i = 0; i < set2->used; ++i) { |
| 1230 | for (j = 0; j < orig_used; ++j) { |
| 1231 | /* detect duplicities */ |
| 1232 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1233 | break; |
| 1234 | } |
| 1235 | } |
| 1236 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1237 | if (j < orig_used) { |
| 1238 | /* node is there, but update its status if needed */ |
| 1239 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1240 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1241 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1242 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1243 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1244 | } |
| 1245 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1246 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1247 | ++set1->used; |
| 1248 | } |
| 1249 | } |
| 1250 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1251 | lyxp_set_free_content(set2); |
| 1252 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * @brief Insert a node into a set. Context position aware. |
| 1257 | * |
| 1258 | * @param[in] set Set to use. |
| 1259 | * @param[in] node Node to insert to @p set. |
| 1260 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1261 | * @param[in] node_type Node type of @p node. |
| 1262 | * @param[in] idx Index in @p set to insert into. |
| 1263 | */ |
| 1264 | static void |
| 1265 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1266 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1267 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1268 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1269 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1270 | /* first item */ |
| 1271 | if (idx) { |
| 1272 | /* no real harm done, but it is a bug */ |
| 1273 | LOGINT(set->ctx); |
| 1274 | idx = 0; |
| 1275 | } |
| 1276 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1277 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1278 | set->type = LYXP_SET_NODE_SET; |
| 1279 | set->used = 0; |
| 1280 | set->size = LYXP_SET_SIZE_START; |
| 1281 | set->ctx_pos = 1; |
| 1282 | set->ctx_size = 1; |
| 1283 | set->ht = NULL; |
| 1284 | } else { |
| 1285 | /* not an empty set */ |
| 1286 | if (set->used == set->size) { |
| 1287 | |
| 1288 | /* set is full */ |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1289 | 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] | 1290 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1291 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | if (idx > set->used) { |
| 1295 | LOGINT(set->ctx); |
| 1296 | idx = set->used; |
| 1297 | } |
| 1298 | |
| 1299 | /* make space for the new node */ |
| 1300 | if (idx < set->used) { |
| 1301 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | /* finally assign the value */ |
| 1306 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1307 | set->val.nodes[idx].type = node_type; |
| 1308 | set->val.nodes[idx].pos = pos; |
| 1309 | ++set->used; |
| 1310 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1311 | /* add into hash table */ |
| 1312 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1313 | } |
| 1314 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 1315 | LY_ERR |
| 1316 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1317 | enum lyxp_axis axis, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1318 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1319 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1320 | |
| 1321 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1322 | |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1323 | if (!set->size) { |
| 1324 | /* first item */ |
| 1325 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
| 1326 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
| 1327 | set->type = LYXP_SET_SCNODE_SET; |
| 1328 | set->used = 0; |
| 1329 | set->size = LYXP_SET_SIZE_START; |
| 1330 | set->ctx_pos = 1; |
| 1331 | set->ctx_size = 1; |
| 1332 | set->ht = NULL; |
| 1333 | } |
| 1334 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1335 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 1336 | /* BUG if axes differ, this new one is thrown away */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1337 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1338 | } else { |
| 1339 | if (set->used == set->size) { |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1340 | 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] | 1341 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1342 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1345 | index = set->used; |
| 1346 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1347 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1348 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1349 | set->val.scnodes[index].axis = axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1350 | ++set->used; |
| 1351 | } |
| 1352 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1353 | if (index_p) { |
| 1354 | *index_p = index; |
| 1355 | } |
| 1356 | |
| 1357 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1361 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1362 | * |
| 1363 | * @param[in] set Set to modify. |
| 1364 | * @return New context value. |
| 1365 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1366 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1367 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1368 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1369 | uint32_t i; |
| 1370 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1371 | |
| 1372 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1373 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1374 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1375 | retry: |
| 1376 | for (i = 0; i < set->used; ++i) { |
| 1377 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1378 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1379 | goto retry; |
| 1380 | } |
| 1381 | } |
| 1382 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1383 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1384 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | return ret_ctx; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
| 1392 | * @brief Get unique @p node position in the data. |
| 1393 | * |
| 1394 | * @param[in] node Node to find. |
| 1395 | * @param[in] node_type Node type of @p node. |
| 1396 | * @param[in] root Root node. |
| 1397 | * @param[in] root_type Type of the XPath @p root node. |
| 1398 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1399 | * be used to increase efficiency and start the DFS from this node. |
| 1400 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1401 | * @return Node position. |
| 1402 | */ |
| 1403 | static uint32_t |
| 1404 | 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] | 1405 | 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] | 1406 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1407 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1408 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1409 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1410 | |
| 1411 | assert(prev && prev_pos && !root->prev->next); |
| 1412 | |
| 1413 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | if (*prev) { |
| 1418 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1419 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1420 | 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] | 1421 | goto dfs_search; |
| 1422 | } |
| 1423 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1424 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1425 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1426 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1427 | LYD_TREE_DFS_continue = 0; |
| 1428 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1429 | if (*prev && !elem) { |
| 1430 | /* resume previous DFS */ |
| 1431 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1432 | LYD_TREE_DFS_continue = 0; |
| 1433 | } |
| 1434 | |
Michal Vasko | 482a682 | 2022-05-06 08:56:12 +0200 | [diff] [blame] | 1435 | 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] | 1436 | /* skip */ |
| 1437 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1438 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1439 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1440 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1441 | break; |
| 1442 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1443 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1444 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1445 | |
| 1446 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1450 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1451 | break; |
| 1452 | } |
| 1453 | } |
| 1454 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1455 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1456 | if (!(*prev)) { |
| 1457 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1458 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1459 | return 0; |
| 1460 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1461 | /* start the search again from the beginning */ |
| 1462 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1463 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1464 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1465 | pos = 1; |
| 1466 | goto dfs_search; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | /* remember the last found node for next time */ |
| 1471 | *prev = node; |
| 1472 | *prev_pos = pos; |
| 1473 | |
| 1474 | return pos; |
| 1475 | } |
| 1476 | |
| 1477 | /** |
| 1478 | * @brief Assign (fill) missing node positions. |
| 1479 | * |
| 1480 | * @param[in] set Set to fill positions in. |
| 1481 | * @param[in] root Context root node. |
| 1482 | * @param[in] root_type Context root type. |
| 1483 | * @return LY_ERR |
| 1484 | */ |
| 1485 | static LY_ERR |
| 1486 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1487 | { |
| 1488 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1489 | uint32_t i, tmp_pos = 0; |
| 1490 | |
| 1491 | for (i = 0; i < set->used; ++i) { |
| 1492 | if (!set->val.nodes[i].pos) { |
| 1493 | tmp_node = NULL; |
| 1494 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1495 | case LYXP_NODE_META: |
| 1496 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1497 | if (!tmp_node) { |
| 1498 | LOGINT_RET(root->schema->module->ctx); |
| 1499 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1500 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1501 | case LYXP_NODE_ELEM: |
| 1502 | case LYXP_NODE_TEXT: |
| 1503 | if (!tmp_node) { |
| 1504 | tmp_node = set->val.nodes[i].node; |
| 1505 | } |
| 1506 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1507 | break; |
| 1508 | default: |
| 1509 | /* all roots have position 0 */ |
| 1510 | break; |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | return LY_SUCCESS; |
| 1516 | } |
| 1517 | |
| 1518 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1519 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1520 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1521 | * @param[in] meta Metadata to use. |
| 1522 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1523 | */ |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1524 | static uint32_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1525 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1526 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1527 | uint32_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1528 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1529 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1530 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1531 | ++pos; |
| 1532 | } |
| 1533 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1534 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1535 | return pos; |
| 1536 | } |
| 1537 | |
| 1538 | /** |
| 1539 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1540 | * |
| 1541 | * @param[in] item1 1st node. |
| 1542 | * @param[in] item2 2nd node. |
| 1543 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1544 | */ |
| 1545 | static int |
| 1546 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1547 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1548 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1549 | |
| 1550 | if (item1->pos < item2->pos) { |
| 1551 | return -1; |
| 1552 | } |
| 1553 | |
| 1554 | if (item1->pos > item2->pos) { |
| 1555 | return 1; |
| 1556 | } |
| 1557 | |
| 1558 | /* node positions are equal, the fun case */ |
| 1559 | |
| 1560 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1561 | /* special case since text nodes are actually saved as their parents */ |
| 1562 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1563 | if (item1->type == LYXP_NODE_ELEM) { |
| 1564 | assert(item2->type == LYXP_NODE_TEXT); |
| 1565 | return -1; |
| 1566 | } else { |
| 1567 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1568 | return 1; |
| 1569 | } |
| 1570 | } |
| 1571 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1572 | /* we need meta positions now */ |
| 1573 | if (item1->type == LYXP_NODE_META) { |
| 1574 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1575 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1576 | if (item2->type == LYXP_NODE_META) { |
| 1577 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1578 | } |
| 1579 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1580 | /* 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] | 1581 | /* check for duplicates */ |
| 1582 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1583 | 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] | 1584 | return 0; |
| 1585 | } |
| 1586 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1587 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1588 | /* elem is always first, 2nd node is after it */ |
| 1589 | if (item1->type == LYXP_NODE_ELEM) { |
| 1590 | assert(item2->type != LYXP_NODE_ELEM); |
| 1591 | return -1; |
| 1592 | } |
| 1593 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1594 | /* 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] | 1595 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1596 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1597 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1598 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1599 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1600 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1601 | return 1; |
| 1602 | } |
| 1603 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1604 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1605 | /* 2nd is after 1st */ |
| 1606 | return -1; |
| 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * @brief Set cast for comparisons. |
| 1611 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1612 | * @param[in,out] trg Target set to cast source into. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1613 | * @param[in] src Source set. |
| 1614 | * @param[in] type Target set type. |
| 1615 | * @param[in] src_idx Source set node index. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1616 | * @return LY_SUCCESS on success. |
| 1617 | * @return LY_ERR value on error. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1618 | */ |
| 1619 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1620 | set_comp_cast(struct lyxp_set *trg, const struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1621 | { |
| 1622 | assert(src->type == LYXP_SET_NODE_SET); |
| 1623 | |
| 1624 | set_init(trg, src); |
| 1625 | |
| 1626 | /* insert node into target set */ |
| 1627 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1628 | |
| 1629 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1630 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1631 | } |
| 1632 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1633 | /** |
| 1634 | * @brief Set content canonization for comparisons. |
| 1635 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1636 | * @param[in,out] set Set to canonize. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1637 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1638 | * @return LY_SUCCESS on success. |
| 1639 | * @return LY_ERR value on error. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1640 | */ |
| 1641 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1642 | set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node) |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1643 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1644 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1645 | struct lyd_value val; |
| 1646 | struct ly_err_item *err = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1647 | LY_ERR rc; |
| 1648 | |
| 1649 | /* is there anything to canonize even? */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1650 | if (set->type == LYXP_SET_STRING) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1651 | /* do we have a type to use for canonization? */ |
| 1652 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1653 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1654 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1655 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1656 | } |
| 1657 | } |
| 1658 | if (!type) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1659 | /* no canonization needed/possible */ |
| 1660 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1661 | } |
| 1662 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1663 | /* check for built-in types without required canonization */ |
| 1664 | if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) { |
| 1665 | /* string */ |
| 1666 | return LY_SUCCESS; |
| 1667 | } |
| 1668 | if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) { |
| 1669 | /* boolean */ |
| 1670 | return LY_SUCCESS; |
| 1671 | } |
| 1672 | if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) { |
| 1673 | /* enumeration */ |
| 1674 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1675 | } |
| 1676 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1677 | /* print canonized string, ignore errors, the value may not satisfy schema constraints */ |
| 1678 | rc = type->plugin->store(set->ctx, type, set->val.str, strlen(set->val.str), 0, set->format, set->prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1679 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1680 | ly_err_free(err); |
| 1681 | if (rc) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1682 | /* invalid value, function store automaticaly dealloc value when fail */ |
| 1683 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1684 | } |
| 1685 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1686 | /* use the canonized string value */ |
| 1687 | free(set->val.str); |
| 1688 | set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val)); |
| 1689 | type->plugin->free(set->ctx, &val); |
| 1690 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1691 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 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 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 1775 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1776 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1777 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1913 | lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1934 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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 | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1946 | exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok1, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 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 |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1968 | lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok1, |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 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 | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 1983 | * @param[in] tok_idx Position in the expresion @p exp. |
| 1984 | * @param[in] repeat_expr_type Repeated expression type, this value is pushed. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | */ |
| 1986 | static void |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 1987 | exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_expr_type repeat_expr_type) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1988 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1989 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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), ); |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 1995 | exp->repeat[tok_idx][i] = repeat_expr_type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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), ); |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2000 | exp->repeat[tok_idx][0] = repeat_expr_type; |
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 |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2016 | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2048 | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2139 | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2190 | reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2191 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2192 | int8_t min_arg_count = -1; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2193 | uint32_t arg_count, max_arg_count = 0, func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2194 | LY_ERR rc; |
| 2195 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2197 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2198 | func_tok_idx = *tok_idx; |
| 2199 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2201 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2202 | min_arg_count = 1; |
| 2203 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2204 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2205 | min_arg_count = 1; |
| 2206 | max_arg_count = 1; |
| 2207 | } |
| 2208 | break; |
| 2209 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2210 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2211 | min_arg_count = 1; |
| 2212 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2213 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2214 | min_arg_count = 0; |
| 2215 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2216 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2217 | min_arg_count = 0; |
| 2218 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2219 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2220 | min_arg_count = 0; |
| 2221 | max_arg_count = 0; |
| 2222 | } |
| 2223 | break; |
| 2224 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2225 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2226 | min_arg_count = 1; |
| 2227 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2228 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2229 | min_arg_count = 0; |
| 2230 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2231 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2232 | min_arg_count = 1; |
| 2233 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2234 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2235 | min_arg_count = 1; |
| 2236 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2237 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2238 | min_arg_count = 1; |
| 2239 | max_arg_count = 1; |
| 2240 | } |
| 2241 | break; |
| 2242 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2243 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2244 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2245 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2246 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2247 | min_arg_count = 0; |
| 2248 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2249 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2250 | min_arg_count = 0; |
| 2251 | max_arg_count = 1; |
| 2252 | } |
| 2253 | break; |
| 2254 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2255 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2256 | min_arg_count = 1; |
| 2257 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2258 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2259 | min_arg_count = 1; |
| 2260 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2261 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2262 | min_arg_count = 0; |
| 2263 | max_arg_count = 0; |
| 2264 | } |
| 2265 | break; |
| 2266 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2267 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2268 | min_arg_count = 2; |
| 2269 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2270 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2271 | min_arg_count = 0; |
| 2272 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2273 | } 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] | 2274 | min_arg_count = 2; |
| 2275 | max_arg_count = 2; |
| 2276 | } |
| 2277 | break; |
| 2278 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2279 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2280 | min_arg_count = 2; |
| 2281 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2282 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2283 | min_arg_count = 3; |
| 2284 | max_arg_count = 3; |
| 2285 | } |
| 2286 | break; |
| 2287 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2288 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2289 | min_arg_count = 0; |
| 2290 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2291 | } 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] | 2292 | min_arg_count = 1; |
| 2293 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2294 | } 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] | 2295 | min_arg_count = 2; |
| 2296 | max_arg_count = 2; |
| 2297 | } |
| 2298 | break; |
| 2299 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2300 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2301 | min_arg_count = 2; |
| 2302 | max_arg_count = 2; |
| 2303 | } |
| 2304 | break; |
| 2305 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2306 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2307 | min_arg_count = 2; |
| 2308 | max_arg_count = 2; |
| 2309 | } |
| 2310 | break; |
| 2311 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2312 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2313 | min_arg_count = 0; |
| 2314 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2315 | } 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] | 2316 | min_arg_count = 0; |
| 2317 | max_arg_count = 1; |
| 2318 | } |
| 2319 | break; |
| 2320 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2321 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2322 | min_arg_count = 0; |
| 2323 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2324 | } 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] | 2325 | min_arg_count = 2; |
| 2326 | max_arg_count = 2; |
| 2327 | } |
| 2328 | break; |
| 2329 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2330 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2331 | min_arg_count = 2; |
| 2332 | max_arg_count = 2; |
| 2333 | } |
| 2334 | break; |
| 2335 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2336 | 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] | 2337 | min_arg_count = 2; |
| 2338 | max_arg_count = 2; |
| 2339 | } |
| 2340 | break; |
| 2341 | } |
| 2342 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2343 | 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] | 2344 | return LY_EINVAL; |
| 2345 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2346 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2347 | |
| 2348 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2349 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2350 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2351 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2352 | |
| 2353 | /* ( Expr ( ',' Expr )* )? */ |
| 2354 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2355 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2356 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2357 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2358 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2359 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2360 | LY_CHECK_RET(rc); |
| 2361 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2362 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2363 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2364 | |
| 2365 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2366 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2367 | LY_CHECK_RET(rc); |
| 2368 | } |
| 2369 | |
| 2370 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2371 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2372 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2373 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2374 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2375 | 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] | 2376 | 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] | 2377 | return LY_EVALID; |
| 2378 | } |
| 2379 | |
| 2380 | return LY_SUCCESS; |
| 2381 | } |
| 2382 | |
| 2383 | /** |
| 2384 | * @brief Reparse PathExpr. Logs directly on error. |
| 2385 | * |
| 2386 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2387 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2388 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2389 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2390 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2391 | * |
| 2392 | * @param[in] ctx Context for logging. |
| 2393 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2394 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2395 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2396 | * @return LY_ERR |
| 2397 | */ |
| 2398 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2399 | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2400 | { |
| 2401 | LY_ERR rc; |
| 2402 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2403 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2404 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2405 | } |
| 2406 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2407 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2408 | case LYXP_TOKEN_PAR1: |
| 2409 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2410 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2411 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2412 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2413 | LY_CHECK_RET(rc); |
| 2414 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2415 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2416 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2417 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2418 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2419 | case LYXP_TOKEN_DOT: |
| 2420 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2421 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2422 | case LYXP_TOKEN_AT: |
| 2423 | case LYXP_TOKEN_NAMETEST: |
| 2424 | case LYXP_TOKEN_NODETYPE: |
| 2425 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2426 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2427 | LY_CHECK_RET(rc); |
| 2428 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2429 | case LYXP_TOKEN_VARREF: |
| 2430 | /* VariableReference */ |
| 2431 | ++(*tok_idx); |
| 2432 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2433 | case LYXP_TOKEN_FUNCNAME: |
| 2434 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2435 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2436 | LY_CHECK_RET(rc); |
| 2437 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2438 | case LYXP_TOKEN_OPER_PATH: |
| 2439 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2440 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2441 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2442 | LY_CHECK_RET(rc); |
| 2443 | break; |
| 2444 | case LYXP_TOKEN_LITERAL: |
| 2445 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2446 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2447 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2448 | case LYXP_TOKEN_NUMBER: |
| 2449 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2450 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2451 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2452 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2453 | 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] | 2454 | return LY_EVALID; |
| 2455 | } |
| 2456 | |
| 2457 | return LY_SUCCESS; |
| 2458 | |
| 2459 | predicate: |
| 2460 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2461 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2462 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2463 | LY_CHECK_RET(rc); |
| 2464 | } |
| 2465 | |
| 2466 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2467 | 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] | 2468 | |
| 2469 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2470 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2471 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2472 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2473 | LY_CHECK_RET(rc); |
| 2474 | } |
| 2475 | |
| 2476 | return LY_SUCCESS; |
| 2477 | } |
| 2478 | |
| 2479 | /** |
| 2480 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2481 | * |
| 2482 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2483 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2484 | * |
| 2485 | * @param[in] ctx Context for logging. |
| 2486 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2487 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2488 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2489 | * @return LY_ERR |
| 2490 | */ |
| 2491 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2492 | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2493 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2494 | uint32_t prev_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2495 | LY_ERR rc; |
| 2496 | |
| 2497 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2498 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2499 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2500 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2501 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2502 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2506 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2507 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2508 | LY_CHECK_RET(rc); |
| 2509 | |
| 2510 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2512 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2513 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2514 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2515 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2516 | LY_CHECK_RET(rc); |
| 2517 | } |
| 2518 | |
| 2519 | return LY_SUCCESS; |
| 2520 | } |
| 2521 | |
| 2522 | /** |
| 2523 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2524 | * |
| 2525 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2526 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2527 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2528 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2529 | * | MultiplicativeExpr '*' UnaryExpr |
| 2530 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2531 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2532 | * |
| 2533 | * @param[in] ctx Context for logging. |
| 2534 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2535 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2536 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2537 | * @return LY_ERR |
| 2538 | */ |
| 2539 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2540 | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2541 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2542 | uint32_t prev_add_exp, prev_mul_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2543 | LY_ERR rc; |
| 2544 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2545 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2546 | goto reparse_multiplicative_expr; |
| 2547 | |
| 2548 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2549 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2550 | ((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] | 2551 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2552 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2553 | |
| 2554 | reparse_multiplicative_expr: |
| 2555 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2556 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2557 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2558 | LY_CHECK_RET(rc); |
| 2559 | |
| 2560 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2561 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2562 | ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2563 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2564 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2565 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2566 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2567 | LY_CHECK_RET(rc); |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | return LY_SUCCESS; |
| 2572 | } |
| 2573 | |
| 2574 | /** |
| 2575 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2576 | * |
| 2577 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2578 | * | EqualityExpr '!=' RelationalExpr |
| 2579 | * [14] RelationalExpr ::= AdditiveExpr |
| 2580 | * | RelationalExpr '<' AdditiveExpr |
| 2581 | * | RelationalExpr '>' AdditiveExpr |
| 2582 | * | RelationalExpr '<=' AdditiveExpr |
| 2583 | * | RelationalExpr '>=' AdditiveExpr |
| 2584 | * |
| 2585 | * @param[in] ctx Context for logging. |
| 2586 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2587 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2588 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2589 | * @return LY_ERR |
| 2590 | */ |
| 2591 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2592 | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2593 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2594 | uint32_t prev_eq_exp, prev_rel_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2595 | LY_ERR rc; |
| 2596 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2597 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2598 | goto reparse_additive_expr; |
| 2599 | |
| 2600 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2601 | 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] | 2602 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2603 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2604 | |
| 2605 | reparse_additive_expr: |
| 2606 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2607 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2608 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2609 | LY_CHECK_RET(rc); |
| 2610 | |
| 2611 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2612 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2613 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2614 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2615 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2616 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | LY_CHECK_RET(rc); |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | return LY_SUCCESS; |
| 2622 | } |
| 2623 | |
| 2624 | /** |
| 2625 | * @brief Reparse OrExpr. Logs directly on error. |
| 2626 | * |
| 2627 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2628 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2629 | * |
| 2630 | * @param[in] ctx Context for logging. |
| 2631 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2632 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2633 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2634 | * @return LY_ERR |
| 2635 | */ |
| 2636 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2637 | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2638 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2639 | uint32_t prev_or_exp, prev_and_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2640 | LY_ERR rc; |
| 2641 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2642 | ++depth; |
| 2643 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2644 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2645 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2646 | goto reparse_equality_expr; |
| 2647 | |
| 2648 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2649 | 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] | 2650 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2651 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2652 | |
| 2653 | reparse_equality_expr: |
| 2654 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2655 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2656 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2657 | LY_CHECK_RET(rc); |
| 2658 | |
| 2659 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2660 | 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] | 2661 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2662 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2663 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2664 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2665 | LY_CHECK_RET(rc); |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | return LY_SUCCESS; |
| 2670 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2671 | |
| 2672 | /** |
| 2673 | * @brief Parse NCName. |
| 2674 | * |
| 2675 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2676 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2677 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2678 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2679 | parse_ncname(const char *ncname) |
| 2680 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2681 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2682 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2683 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2684 | |
| 2685 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2686 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2687 | return len; |
| 2688 | } |
| 2689 | |
| 2690 | do { |
| 2691 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2692 | if (!*ncname) { |
| 2693 | break; |
| 2694 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2695 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2696 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2697 | |
| 2698 | return len; |
| 2699 | } |
| 2700 | |
| 2701 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2702 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2703 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2704 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2705 | * @param[in] exp Expression to use. |
| 2706 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2707 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2708 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2709 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2710 | */ |
| 2711 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2712 | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint32_t tok_pos, uint32_t tok_len) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2713 | { |
| 2714 | uint32_t prev; |
| 2715 | |
| 2716 | if (exp->used == exp->size) { |
| 2717 | prev = exp->size; |
| 2718 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2719 | if (prev > exp->size) { |
| 2720 | LOGINT(ctx); |
| 2721 | return LY_EINT; |
| 2722 | } |
| 2723 | |
| 2724 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2725 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2726 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2727 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2728 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2729 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2730 | } |
| 2731 | |
| 2732 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2733 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2734 | exp->tok_len[exp->used] = tok_len; |
| 2735 | ++exp->used; |
| 2736 | return LY_SUCCESS; |
| 2737 | } |
| 2738 | |
| 2739 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2740 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2741 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2742 | uint32_t i; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2743 | |
| 2744 | if (!expr) { |
| 2745 | return; |
| 2746 | } |
| 2747 | |
| 2748 | lydict_remove(ctx, expr->expr); |
| 2749 | free(expr->tokens); |
| 2750 | free(expr->tok_pos); |
| 2751 | free(expr->tok_len); |
| 2752 | if (expr->repeat) { |
| 2753 | for (i = 0; i < expr->used; ++i) { |
| 2754 | free(expr->repeat[i]); |
| 2755 | } |
| 2756 | } |
| 2757 | free(expr->repeat); |
| 2758 | free(expr); |
| 2759 | } |
| 2760 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2761 | /** |
| 2762 | * @brief Parse Axis name. |
| 2763 | * |
| 2764 | * @param[in] str String to parse. |
| 2765 | * @param[in] str_len Length of @p str. |
| 2766 | * @return LY_SUCCESS if an axis. |
| 2767 | * @return LY_ENOT otherwise. |
| 2768 | */ |
| 2769 | static LY_ERR |
| 2770 | expr_parse_axis(const char *str, size_t str_len) |
| 2771 | { |
| 2772 | switch (str_len) { |
| 2773 | case 4: |
| 2774 | if (!strncmp("self", str, str_len)) { |
| 2775 | return LY_SUCCESS; |
| 2776 | } |
| 2777 | break; |
| 2778 | case 5: |
| 2779 | if (!strncmp("child", str, str_len)) { |
| 2780 | return LY_SUCCESS; |
| 2781 | } |
| 2782 | break; |
| 2783 | case 6: |
| 2784 | if (!strncmp("parent", str, str_len)) { |
| 2785 | return LY_SUCCESS; |
| 2786 | } |
| 2787 | break; |
| 2788 | case 8: |
| 2789 | if (!strncmp("ancestor", str, str_len)) { |
| 2790 | return LY_SUCCESS; |
| 2791 | } |
| 2792 | break; |
| 2793 | case 9: |
| 2794 | if (!strncmp("attribute", str, str_len)) { |
| 2795 | return LY_SUCCESS; |
| 2796 | } else if (!strncmp("following", str, str_len)) { |
| 2797 | return LY_SUCCESS; |
| 2798 | } else if (!strncmp("namespace", str, str_len)) { |
| 2799 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2800 | return LY_ENOT; |
| 2801 | } else if (!strncmp("preceding", str, str_len)) { |
| 2802 | return LY_SUCCESS; |
| 2803 | } |
| 2804 | break; |
| 2805 | case 10: |
| 2806 | if (!strncmp("descendant", str, str_len)) { |
| 2807 | return LY_SUCCESS; |
| 2808 | } |
| 2809 | break; |
| 2810 | case 16: |
| 2811 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2812 | return LY_SUCCESS; |
| 2813 | } |
| 2814 | break; |
| 2815 | case 17: |
| 2816 | if (!strncmp("following-sibling", str, str_len)) { |
| 2817 | return LY_SUCCESS; |
| 2818 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2819 | return LY_SUCCESS; |
| 2820 | } |
| 2821 | break; |
| 2822 | case 18: |
| 2823 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2824 | return LY_SUCCESS; |
| 2825 | } |
| 2826 | break; |
| 2827 | } |
| 2828 | |
| 2829 | return LY_ENOT; |
| 2830 | } |
| 2831 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2832 | LY_ERR |
| 2833 | 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] | 2834 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2835 | LY_ERR ret = LY_SUCCESS; |
| 2836 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2837 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2838 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2839 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2840 | uint32_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2841 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2842 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2843 | assert(expr_p); |
| 2844 | |
| 2845 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2846 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2847 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2851 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2852 | } |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2853 | if (expr_len > UINT32_MAX) { |
| 2854 | LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2855 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2859 | expr = calloc(1, sizeof *expr); |
| 2860 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2861 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2862 | expr->used = 0; |
| 2863 | expr->size = LYXP_EXPR_SIZE_START; |
| 2864 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2865 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2866 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2867 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2868 | 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] | 2869 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2870 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2871 | 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] | 2872 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2873 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2874 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2875 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2876 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2877 | ++parsed; |
| 2878 | } |
| 2879 | |
| 2880 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2881 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2882 | |
| 2883 | /* '(' */ |
| 2884 | tok_len = 1; |
| 2885 | tok_type = LYXP_TOKEN_PAR1; |
| 2886 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2887 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2888 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2889 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2890 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2891 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2892 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2893 | /* it is NodeType after all */ |
| 2894 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2895 | |
| 2896 | prev_ntype_check = 0; |
| 2897 | prev_func_check = 0; |
| 2898 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2899 | /* it is FunctionName after all */ |
| 2900 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2901 | |
| 2902 | prev_ntype_check = 0; |
| 2903 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2904 | } |
| 2905 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2906 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2907 | |
| 2908 | /* ')' */ |
| 2909 | tok_len = 1; |
| 2910 | tok_type = LYXP_TOKEN_PAR2; |
| 2911 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2912 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2913 | |
| 2914 | /* '[' */ |
| 2915 | tok_len = 1; |
| 2916 | tok_type = LYXP_TOKEN_BRACK1; |
| 2917 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2918 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2919 | |
| 2920 | /* ']' */ |
| 2921 | tok_len = 1; |
| 2922 | tok_type = LYXP_TOKEN_BRACK2; |
| 2923 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2924 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2925 | |
| 2926 | /* '..' */ |
| 2927 | tok_len = 2; |
| 2928 | tok_type = LYXP_TOKEN_DDOT; |
| 2929 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2930 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2931 | |
| 2932 | /* '.' */ |
| 2933 | tok_len = 1; |
| 2934 | tok_type = LYXP_TOKEN_DOT; |
| 2935 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2936 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2937 | |
| 2938 | /* '@' */ |
| 2939 | tok_len = 1; |
| 2940 | tok_type = LYXP_TOKEN_AT; |
| 2941 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2942 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2943 | |
| 2944 | /* ',' */ |
| 2945 | tok_len = 1; |
| 2946 | tok_type = LYXP_TOKEN_COMMA; |
| 2947 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2948 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2949 | |
| 2950 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2951 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2952 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2953 | 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] | 2954 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2955 | ++tok_len; |
| 2956 | tok_type = LYXP_TOKEN_LITERAL; |
| 2957 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2958 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2959 | |
| 2960 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2961 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2962 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2963 | 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] | 2964 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2965 | ++tok_len; |
| 2966 | tok_type = LYXP_TOKEN_LITERAL; |
| 2967 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2968 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2969 | |
| 2970 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2971 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2972 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2973 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2974 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2975 | } |
| 2976 | tok_type = LYXP_TOKEN_NUMBER; |
| 2977 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2978 | } else if (expr_str[parsed] == '$') { |
| 2979 | |
| 2980 | /* VariableReference */ |
| 2981 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2982 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2983 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 2984 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2985 | tok_len = ncname_len; |
| 2986 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 2987 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 2988 | error); |
| 2989 | tok_type = LYXP_TOKEN_VARREF; |
| 2990 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2991 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2992 | |
| 2993 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2994 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2995 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2996 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2997 | } else { |
| 2998 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2999 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3000 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3001 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3002 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3003 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3004 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3005 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3006 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 3007 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3008 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3009 | |
| 3010 | /* Operator '<=', '>=' */ |
| 3011 | tok_len = 2; |
| 3012 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3013 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3014 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3015 | |
| 3016 | /* Operator '|' */ |
| 3017 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3018 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3019 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3020 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3021 | |
| 3022 | /* Operator '+', '-' */ |
| 3023 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3024 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3025 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3026 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3027 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3028 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3029 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3030 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3031 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3032 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3033 | |
| 3034 | /* Operator '<', '>' */ |
| 3035 | tok_len = 1; |
| 3036 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3037 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3038 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3039 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3040 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3041 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3042 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3043 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3044 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3045 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3046 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3047 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3048 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3049 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3050 | |
| 3051 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3052 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3053 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3054 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3055 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3056 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3057 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3058 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3059 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3060 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3061 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3062 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3063 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3064 | } 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] | 3065 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3066 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3067 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3068 | } else if (prev_ntype_check || prev_func_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3069 | 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] | 3070 | 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] | 3071 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3072 | goto error; |
| 3073 | } else { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3074 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3075 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3076 | goto error; |
| 3077 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3078 | } else { |
| 3079 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3080 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3081 | if (expr_str[parsed] == '*') { |
| 3082 | ncname_len = 1; |
| 3083 | } else { |
| 3084 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3085 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3086 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3087 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3088 | tok_len = ncname_len; |
| 3089 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3090 | has_axis = 0; |
| 3091 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3092 | /* axis */ |
| 3093 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3094 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); ret = LY_EVALID, |
| 3095 | error); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 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], |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3113 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 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 | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3127 | (uint32_t)(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 |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3176 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t start_idx, uint32_t end_idx, |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3177 | struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3178 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3179 | LY_ERR ret = LY_SUCCESS; |
| 3180 | struct lyxp_expr *dup = NULL; |
Michal Vasko | d59039d | 2022-09-09 09:07:30 +0200 | [diff] [blame] | 3181 | uint32_t used = 0, i, j, expr_len; |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3182 | const char *expr_start; |
| 3183 | |
| 3184 | assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx))); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3185 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3186 | if (!exp) { |
| 3187 | goto cleanup; |
| 3188 | } |
| 3189 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3190 | if (!start_idx && !end_idx) { |
| 3191 | end_idx = exp->used - 1; |
| 3192 | } |
| 3193 | |
| 3194 | expr_start = exp->expr + exp->tok_pos[start_idx]; |
| 3195 | expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx]; |
| 3196 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3197 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3198 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3199 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3200 | if (exp->used) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3201 | used = (end_idx - start_idx) + 1; |
| 3202 | |
| 3203 | dup->tokens = malloc(used * sizeof *dup->tokens); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3204 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3205 | memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3206 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3207 | dup->tok_pos = malloc(used * sizeof *dup->tok_pos); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3208 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3209 | memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3210 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3211 | if (start_idx) { |
| 3212 | /* fix the indices in the expression */ |
| 3213 | for (i = 0; i < used; ++i) { |
| 3214 | dup->tok_pos[i] -= expr_start - exp->expr; |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | dup->tok_len = malloc(used * sizeof *dup->tok_len); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3219 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3220 | memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3221 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3222 | if (exp->repeat) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3223 | dup->repeat = malloc(used * sizeof *dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3224 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3225 | for (i = start_idx; i <= end_idx; ++i) { |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3226 | if (!exp->repeat[i]) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3227 | dup->repeat[i - start_idx] = NULL; |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3228 | } else { |
| 3229 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3230 | /* the ending 0 as well */ |
| 3231 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3232 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3233 | dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat); |
| 3234 | LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3235 | memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3236 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3237 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3241 | dup->used = used; |
| 3242 | dup->size = used; |
| 3243 | |
| 3244 | /* copy only subexpression */ |
| 3245 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3246 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3247 | cleanup: |
| 3248 | if (ret) { |
| 3249 | lyxp_expr_free(ctx, dup); |
| 3250 | } else { |
| 3251 | *dup_p = dup; |
| 3252 | } |
| 3253 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3254 | } |
| 3255 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3256 | /** |
| 3257 | * @brief Get the last-added schema node that is currently in the context. |
| 3258 | * |
| 3259 | * @param[in] set Set to search in. |
| 3260 | * @return Last-added schema context node, NULL if no node is in context. |
| 3261 | */ |
| 3262 | static struct lysc_node * |
| 3263 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3264 | { |
| 3265 | uint32_t i; |
| 3266 | |
| 3267 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3268 | return NULL; |
| 3269 | } |
| 3270 | |
| 3271 | i = set->used; |
| 3272 | do { |
| 3273 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3274 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3275 | /* if there are more, simply return the first found (last added) */ |
| 3276 | return set->val.scnodes[i].scnode; |
| 3277 | } |
| 3278 | } while (i); |
| 3279 | |
| 3280 | return NULL; |
| 3281 | } |
| 3282 | |
| 3283 | /** |
| 3284 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3285 | * |
| 3286 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3287 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3288 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3289 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3290 | warn_is_numeric_type(struct lysc_type *type) |
| 3291 | { |
| 3292 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3293 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3294 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3295 | |
| 3296 | switch (type->basetype) { |
| 3297 | case LY_TYPE_DEC64: |
| 3298 | case LY_TYPE_INT8: |
| 3299 | case LY_TYPE_UINT8: |
| 3300 | case LY_TYPE_INT16: |
| 3301 | case LY_TYPE_UINT16: |
| 3302 | case LY_TYPE_INT32: |
| 3303 | case LY_TYPE_UINT32: |
| 3304 | case LY_TYPE_INT64: |
| 3305 | case LY_TYPE_UINT64: |
| 3306 | return 1; |
| 3307 | case LY_TYPE_UNION: |
| 3308 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3309 | LY_ARRAY_FOR(uni->types, u) { |
| 3310 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3311 | if (ret) { |
| 3312 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3313 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3314 | } |
| 3315 | } |
| 3316 | /* did not find any suitable type */ |
| 3317 | return 0; |
| 3318 | case LY_TYPE_LEAFREF: |
| 3319 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3320 | default: |
| 3321 | return 0; |
| 3322 | } |
| 3323 | } |
| 3324 | |
| 3325 | /** |
| 3326 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3327 | * |
| 3328 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3329 | * @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] | 3330 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3331 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3332 | warn_is_string_type(struct lysc_type *type) |
| 3333 | { |
| 3334 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3335 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3336 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3337 | |
| 3338 | switch (type->basetype) { |
| 3339 | case LY_TYPE_BITS: |
| 3340 | case LY_TYPE_ENUM: |
| 3341 | case LY_TYPE_IDENT: |
| 3342 | case LY_TYPE_INST: |
| 3343 | case LY_TYPE_STRING: |
| 3344 | return 1; |
| 3345 | case LY_TYPE_UNION: |
| 3346 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3347 | LY_ARRAY_FOR(uni->types, u) { |
| 3348 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3349 | if (ret) { |
| 3350 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3351 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3352 | } |
| 3353 | } |
| 3354 | /* did not find any suitable type */ |
| 3355 | return 0; |
| 3356 | case LY_TYPE_LEAFREF: |
| 3357 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3358 | default: |
| 3359 | return 0; |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | /** |
| 3364 | * @brief Test whether a type is one specific type. |
| 3365 | * |
| 3366 | * @param[in] type Type to test. |
| 3367 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3368 | * @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] | 3369 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3370 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3371 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3372 | { |
| 3373 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3374 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3375 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3376 | |
| 3377 | if (type->basetype == base) { |
| 3378 | return 1; |
| 3379 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3380 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3381 | LY_ARRAY_FOR(uni->types, u) { |
| 3382 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3383 | if (ret) { |
| 3384 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3385 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | /* did not find any suitable type */ |
| 3389 | return 0; |
| 3390 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3391 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3392 | } |
| 3393 | |
| 3394 | return 0; |
| 3395 | } |
| 3396 | |
| 3397 | /** |
| 3398 | * @brief Get next type of a (union) type. |
| 3399 | * |
| 3400 | * @param[in] type Base type. |
| 3401 | * @param[in] prev_type Previously returned type. |
| 3402 | * @return Next type or NULL. |
| 3403 | */ |
| 3404 | static struct lysc_type * |
| 3405 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3406 | { |
| 3407 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3408 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3409 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3410 | |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3411 | if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3412 | uni = (struct lysc_type_union *)type; |
| 3413 | if (!prev_type) { |
| 3414 | return uni->types[0]; |
| 3415 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3416 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3417 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3418 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3419 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3420 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3421 | found = 1; |
| 3422 | } |
| 3423 | } |
| 3424 | return NULL; |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3425 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3426 | if (prev_type) { |
| 3427 | assert(type == prev_type); |
| 3428 | return NULL; |
| 3429 | } else { |
| 3430 | return type; |
| 3431 | } |
| 3432 | } |
| 3433 | } |
| 3434 | |
| 3435 | /** |
| 3436 | * @brief Test whether 2 types have a common type. |
| 3437 | * |
| 3438 | * @param[in] type1 First type. |
| 3439 | * @param[in] type2 Second type. |
| 3440 | * @return 1 if they do, 0 otherwise. |
| 3441 | */ |
| 3442 | static int |
| 3443 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3444 | { |
| 3445 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3446 | |
| 3447 | t1 = NULL; |
| 3448 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3449 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3450 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3451 | } else { |
| 3452 | rt1 = t1; |
| 3453 | } |
| 3454 | |
| 3455 | t2 = NULL; |
| 3456 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3457 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3458 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3459 | } else { |
| 3460 | rt2 = t2; |
| 3461 | } |
| 3462 | |
| 3463 | if (rt2->basetype == rt1->basetype) { |
| 3464 | /* match found */ |
| 3465 | return 1; |
| 3466 | } |
| 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | return 0; |
| 3471 | } |
| 3472 | |
| 3473 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3474 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3475 | * |
| 3476 | * @param[in] ctx Context for logging. |
| 3477 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3478 | * @param[in] subexpr Subexpression start. |
| 3479 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3480 | * @param[in] cur_scnode Expression context node. |
| 3481 | */ |
| 3482 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3483 | warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len, |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3484 | const struct lysc_node *cur_scnode) |
| 3485 | { |
| 3486 | char *path; |
| 3487 | |
| 3488 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3489 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".", |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3490 | tok_pos, subexpr_len, subexpr, path); |
| 3491 | free(path); |
| 3492 | } |
| 3493 | |
| 3494 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3495 | * @brief Check both operands of comparison operators. |
| 3496 | * |
| 3497 | * @param[in] ctx Context for errors. |
| 3498 | * @param[in] set1 First operand set. |
| 3499 | * @param[in] set2 Second operand set. |
| 3500 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3501 | * @param[in] expr Start of the expression to print with the warning. |
| 3502 | * @param[in] tok_pos Token position. |
| 3503 | */ |
| 3504 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3505 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3506 | uint32_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3507 | { |
| 3508 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3509 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3510 | |
| 3511 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3512 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3513 | |
| 3514 | if (!node1 && !node2) { |
| 3515 | /* no node-sets involved, nothing to do */ |
| 3516 | return; |
| 3517 | } |
| 3518 | |
| 3519 | if (node1) { |
| 3520 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3521 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3522 | warning = 1; |
| 3523 | leaves = 0; |
| 3524 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3525 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3526 | warning = 1; |
| 3527 | } |
| 3528 | } |
| 3529 | |
| 3530 | if (node2) { |
| 3531 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3532 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3533 | warning = 1; |
| 3534 | leaves = 0; |
| 3535 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3536 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3537 | warning = 1; |
| 3538 | } |
| 3539 | } |
| 3540 | |
| 3541 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3542 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3543 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3544 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3545 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3546 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3547 | warning = 1; |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3552 | 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] | 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | /** |
| 3557 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3558 | * |
| 3559 | * @param[in] exp Parsed XPath expression. |
| 3560 | * @param[in] set Set with the leaf/leaf-list. |
| 3561 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3562 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3563 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3564 | */ |
| 3565 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3566 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp, |
| 3567 | uint32_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3568 | { |
| 3569 | struct lysc_node *scnode; |
| 3570 | struct lysc_type *type; |
| 3571 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3572 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3573 | LY_ERR rc; |
| 3574 | struct ly_err_item *err = NULL; |
| 3575 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3576 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3577 | ((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] | 3578 | /* check that the node can have the specified value */ |
| 3579 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3580 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3581 | } else { |
| 3582 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3583 | } |
| 3584 | if (!value) { |
| 3585 | LOGMEM(set->ctx); |
| 3586 | return; |
| 3587 | } |
| 3588 | |
| 3589 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3590 | 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] | 3591 | " 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] | 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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3598 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3599 | 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] | 3600 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3601 | if (rc == LY_EINCOMPLETE) { |
| 3602 | rc = LY_SUCCESS; |
| 3603 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3604 | |
| 3605 | if (err) { |
| 3606 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3607 | ly_err_free(err); |
| 3608 | } else if (rc != LY_SUCCESS) { |
| 3609 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3610 | } |
| 3611 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3612 | warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp], |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3613 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3614 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3615 | } else { |
| 3616 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | free(value); |
| 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | /* |
| 3624 | * XPath functions |
| 3625 | */ |
| 3626 | |
| 3627 | /** |
| 3628 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3629 | * depending on whether the first node bit value from the second argument is set. |
| 3630 | * |
| 3631 | * @param[in] args Array of arguments. |
| 3632 | * @param[in] arg_count Count of elements in @p args. |
| 3633 | * @param[in,out] set Context and result set at the same time. |
| 3634 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3635 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3636 | */ |
| 3637 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3638 | xpath_bit_is_set(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3639 | { |
| 3640 | struct lyd_node_term *leaf; |
| 3641 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3642 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3643 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3644 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3645 | |
| 3646 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3647 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3648 | 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] | 3649 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3650 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3651 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3652 | sleaf->name); |
| 3653 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3654 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3655 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3659 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3660 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3661 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3662 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3663 | 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] | 3664 | } |
| 3665 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3666 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3667 | return rc; |
| 3668 | } |
| 3669 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3670 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3671 | 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] | 3672 | return LY_EVALID; |
| 3673 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3674 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3675 | LY_CHECK_RET(rc); |
| 3676 | |
| 3677 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3678 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3679 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3680 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3681 | LYD_VALUE_GET(&leaf->value, bits); |
| 3682 | LY_ARRAY_FOR(bits->items, u) { |
| 3683 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3684 | set_fill_boolean(set, 1); |
| 3685 | break; |
| 3686 | } |
| 3687 | } |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | return LY_SUCCESS; |
| 3692 | } |
| 3693 | |
| 3694 | /** |
| 3695 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3696 | * with the argument converted to boolean. |
| 3697 | * |
| 3698 | * @param[in] args Array of arguments. |
| 3699 | * @param[in] arg_count Count of elements in @p args. |
| 3700 | * @param[in,out] set Context and result set at the same time. |
| 3701 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3702 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3703 | */ |
| 3704 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3705 | xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3706 | { |
| 3707 | LY_ERR rc; |
| 3708 | |
| 3709 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3710 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3711 | return LY_SUCCESS; |
| 3712 | } |
| 3713 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3714 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3715 | LY_CHECK_RET(rc); |
| 3716 | set_fill_set(set, args[0]); |
| 3717 | |
| 3718 | return LY_SUCCESS; |
| 3719 | } |
| 3720 | |
| 3721 | /** |
| 3722 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3723 | * with the first argument rounded up to the nearest integer. |
| 3724 | * |
| 3725 | * @param[in] args Array of arguments. |
| 3726 | * @param[in] arg_count Count of elements in @p args. |
| 3727 | * @param[in,out] set Context and result set at the same time. |
| 3728 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3729 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3730 | */ |
| 3731 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3732 | xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3733 | { |
| 3734 | struct lysc_node_leaf *sleaf; |
| 3735 | LY_ERR rc = LY_SUCCESS; |
| 3736 | |
| 3737 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3738 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3739 | 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] | 3740 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3741 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3742 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3743 | sleaf->name); |
| 3744 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3745 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3746 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3747 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3748 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3749 | return rc; |
| 3750 | } |
| 3751 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3752 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3753 | LY_CHECK_RET(rc); |
| 3754 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3755 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3756 | } else { |
| 3757 | set_fill_number(set, args[0]->val.num); |
| 3758 | } |
| 3759 | |
| 3760 | return LY_SUCCESS; |
| 3761 | } |
| 3762 | |
| 3763 | /** |
| 3764 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3765 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3766 | * |
| 3767 | * @param[in] args Array of arguments. |
| 3768 | * @param[in] arg_count Count of elements in @p args. |
| 3769 | * @param[in,out] set Context and result set at the same time. |
| 3770 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3771 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3772 | */ |
| 3773 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3774 | xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3775 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3776 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3777 | char *str = NULL; |
| 3778 | size_t used = 1; |
| 3779 | LY_ERR rc = LY_SUCCESS; |
| 3780 | struct lysc_node_leaf *sleaf; |
| 3781 | |
| 3782 | if (options & LYXP_SCNODE_ALL) { |
| 3783 | for (i = 0; i < arg_count; ++i) { |
| 3784 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3785 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3786 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3787 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3788 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3789 | 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] | 3790 | } |
| 3791 | } |
| 3792 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3793 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3794 | return rc; |
| 3795 | } |
| 3796 | |
| 3797 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3798 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3799 | if (rc != LY_SUCCESS) { |
| 3800 | free(str); |
| 3801 | return rc; |
| 3802 | } |
| 3803 | |
| 3804 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3805 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3806 | strcpy(str + used - 1, args[i]->val.str); |
| 3807 | used += strlen(args[i]->val.str); |
| 3808 | } |
| 3809 | |
| 3810 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3811 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3812 | set->type = LYXP_SET_STRING; |
| 3813 | set->val.str = str; |
| 3814 | |
| 3815 | return LY_SUCCESS; |
| 3816 | } |
| 3817 | |
| 3818 | /** |
| 3819 | * @brief Execute the XPath contains(string, string) function. |
| 3820 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3821 | * be found in the first or not. |
| 3822 | * |
| 3823 | * @param[in] args Array of arguments. |
| 3824 | * @param[in] arg_count Count of elements in @p args. |
| 3825 | * @param[in,out] set Context and result set at the same time. |
| 3826 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3827 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3828 | */ |
| 3829 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3830 | xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3831 | { |
| 3832 | struct lysc_node_leaf *sleaf; |
| 3833 | LY_ERR rc = LY_SUCCESS; |
| 3834 | |
| 3835 | if (options & LYXP_SCNODE_ALL) { |
| 3836 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3837 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3838 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3839 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3840 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3841 | 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] | 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3846 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3847 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3848 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3849 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3850 | 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] | 3851 | } |
| 3852 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3853 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3854 | return rc; |
| 3855 | } |
| 3856 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3857 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3858 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3859 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3860 | LY_CHECK_RET(rc); |
| 3861 | |
| 3862 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3863 | set_fill_boolean(set, 1); |
| 3864 | } else { |
| 3865 | set_fill_boolean(set, 0); |
| 3866 | } |
| 3867 | |
| 3868 | return LY_SUCCESS; |
| 3869 | } |
| 3870 | |
| 3871 | /** |
| 3872 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3873 | * with the size of the node-set from the argument. |
| 3874 | * |
| 3875 | * @param[in] args Array of arguments. |
| 3876 | * @param[in] arg_count Count of elements in @p args. |
| 3877 | * @param[in,out] set Context and result set at the same time. |
| 3878 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3879 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3880 | */ |
| 3881 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3882 | xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3883 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3884 | LY_ERR rc = LY_SUCCESS; |
| 3885 | |
| 3886 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3887 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3888 | 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] | 3889 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3890 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3891 | return rc; |
| 3892 | } |
| 3893 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3894 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3895 | 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] | 3896 | return LY_EVALID; |
| 3897 | } |
| 3898 | |
| 3899 | set_fill_number(set, args[0]->used); |
| 3900 | return LY_SUCCESS; |
| 3901 | } |
| 3902 | |
| 3903 | /** |
| 3904 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3905 | * with the context with the intial node. |
| 3906 | * |
| 3907 | * @param[in] args Array of arguments. |
| 3908 | * @param[in] arg_count Count of elements in @p args. |
| 3909 | * @param[in,out] set Context and result set at the same time. |
| 3910 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3911 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3912 | */ |
| 3913 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3914 | xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3915 | { |
| 3916 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3917 | 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] | 3918 | return LY_EVALID; |
| 3919 | } |
| 3920 | |
| 3921 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3922 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3923 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3924 | if (set->cur_scnode) { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 3925 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3926 | } else { |
| 3927 | /* root node */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 3928 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3929 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3930 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3931 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3932 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3933 | if (set->cur_node) { |
| 3934 | /* position is filled later */ |
| 3935 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3936 | } else { |
| 3937 | /* root node */ |
| 3938 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3939 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3940 | } |
| 3941 | |
| 3942 | return LY_SUCCESS; |
| 3943 | } |
| 3944 | |
| 3945 | /** |
| 3946 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3947 | * leafref or instance-identifier target node(s). |
| 3948 | * |
| 3949 | * @param[in] args Array of arguments. |
| 3950 | * @param[in] arg_count Count of elements in @p args. |
| 3951 | * @param[in,out] set Context and result set at the same time. |
| 3952 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3953 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3954 | */ |
| 3955 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3956 | xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3957 | { |
| 3958 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3959 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3960 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3961 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3962 | struct ly_path *p; |
| 3963 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3964 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3965 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3966 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3967 | |
| 3968 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3969 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3970 | 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] | 3971 | } 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] | 3972 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3973 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3974 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3975 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3976 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3977 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3978 | __func__, sleaf->name); |
| 3979 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3980 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3981 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 3982 | 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] | 3983 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 3984 | 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] | 3985 | |
| 3986 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3987 | 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] | 3988 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3989 | if (!r) { |
| 3990 | /* get the target node */ |
| 3991 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 3992 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3993 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 3994 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3995 | } /* 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] | 3996 | } |
| 3997 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3998 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3999 | } |
| 4000 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4001 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4002 | 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] | 4003 | return LY_EVALID; |
| 4004 | } |
| 4005 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4006 | lyxp_set_free_content(set); |
| 4007 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4008 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4009 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4010 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4011 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 4012 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4013 | 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] | 4014 | &node, &errmsg)) { |
Michal Vasko | 1a2b8ee | 2022-12-05 10:41:55 +0100 | [diff] [blame] | 4015 | LOGERR(set->ctx, LY_EVALID, "%s", errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4016 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4017 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4018 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4019 | } else { |
| 4020 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 4021 | if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4022 | 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] | 4023 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4024 | return LY_EVALID; |
| 4025 | } |
| 4026 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4027 | |
| 4028 | /* insert it */ |
| 4029 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | return LY_SUCCESS; |
| 4034 | } |
| 4035 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4036 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4037 | 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] | 4038 | { |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 4039 | uint32_t i, id_len; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4040 | LY_ARRAY_COUNT_TYPE u; |
| 4041 | struct lyd_node_term *leaf; |
| 4042 | struct lysc_node_leaf *sleaf; |
| 4043 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4044 | struct lyd_value *val; |
| 4045 | const struct lys_module *mod; |
| 4046 | const char *id_name; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4047 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4048 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4049 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4050 | |
| 4051 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4052 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4053 | 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] | 4054 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4055 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4056 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4057 | sleaf->name); |
| 4058 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4059 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4060 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4064 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4065 | 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] | 4066 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4067 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4068 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4069 | } |
| 4070 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4071 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4072 | return rc; |
| 4073 | } |
| 4074 | |
| 4075 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4076 | 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] | 4077 | return LY_EVALID; |
| 4078 | } |
| 4079 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4080 | LY_CHECK_RET(rc); |
| 4081 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4082 | /* parse the identity */ |
| 4083 | id_name = args[1]->val.str; |
| 4084 | id_len = strlen(id_name); |
| 4085 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4086 | LY_CHECK_RET(rc); |
| 4087 | if (!mod) { |
| 4088 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4089 | return LY_EVALID; |
| 4090 | } |
| 4091 | |
| 4092 | /* find the identity */ |
| 4093 | found = 0; |
| 4094 | LY_ARRAY_FOR(mod->identities, u) { |
| 4095 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4096 | /* we have match */ |
| 4097 | found = 1; |
| 4098 | break; |
| 4099 | } |
| 4100 | } |
| 4101 | if (!found) { |
| 4102 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4103 | return LY_EVALID; |
| 4104 | } |
| 4105 | id = &mod->identities[u]; |
| 4106 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4107 | set_fill_boolean(set, 0); |
| 4108 | found = 0; |
| 4109 | for (i = 0; i < args[0]->used; ++i) { |
| 4110 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4111 | continue; |
| 4112 | } |
| 4113 | |
| 4114 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4115 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4116 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4117 | val = &leaf->value; |
| 4118 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 4119 | /* uninteresting */ |
| 4120 | continue; |
| 4121 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4122 | } else { |
| 4123 | meta = args[0]->val.meta[i].meta; |
| 4124 | val = &meta->value; |
| 4125 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4126 | /* uninteresting */ |
| 4127 | continue; |
| 4128 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4129 | } |
| 4130 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4131 | /* check the identity itself */ |
| 4132 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4133 | set_fill_boolean(set, 1); |
| 4134 | found = 1; |
| 4135 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4136 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4137 | set_fill_boolean(set, 1); |
| 4138 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4139 | } |
| 4140 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4141 | if (found) { |
| 4142 | break; |
| 4143 | } |
| 4144 | } |
| 4145 | |
| 4146 | return LY_SUCCESS; |
| 4147 | } |
| 4148 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4149 | /** |
| 4150 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4151 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4152 | * argument identity. |
| 4153 | * |
| 4154 | * @param[in] args Array of arguments. |
| 4155 | * @param[in] arg_count Count of elements in @p args. |
| 4156 | * @param[in,out] set Context and result set at the same time. |
| 4157 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4158 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4159 | */ |
| 4160 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4161 | xpath_derived_from(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4162 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4163 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | /** |
| 4167 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4168 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4169 | * the second argument identity. |
| 4170 | * |
| 4171 | * @param[in] args Array of arguments. |
| 4172 | * @param[in] arg_count Count of elements in @p args. |
| 4173 | * @param[in,out] set Context and result set at the same time. |
| 4174 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4175 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4176 | */ |
| 4177 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4178 | xpath_derived_from_or_self(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4179 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4180 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | /** |
| 4184 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4185 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4186 | * |
| 4187 | * @param[in] args Array of arguments. |
| 4188 | * @param[in] arg_count Count of elements in @p args. |
| 4189 | * @param[in,out] set Context and result set at the same time. |
| 4190 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4191 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4192 | */ |
| 4193 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4194 | xpath_enum_value(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4195 | { |
| 4196 | struct lyd_node_term *leaf; |
| 4197 | struct lysc_node_leaf *sleaf; |
| 4198 | LY_ERR rc = LY_SUCCESS; |
| 4199 | |
| 4200 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4201 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4202 | 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] | 4203 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4204 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4205 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4206 | sleaf->name); |
| 4207 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4208 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4209 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4210 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4211 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4212 | return rc; |
| 4213 | } |
| 4214 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4215 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4216 | 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] | 4217 | return LY_EVALID; |
| 4218 | } |
| 4219 | |
| 4220 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4221 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4223 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4224 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4225 | set_fill_number(set, leaf->value.enum_item->value); |
| 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | return LY_SUCCESS; |
| 4230 | } |
| 4231 | |
| 4232 | /** |
| 4233 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4234 | * with false value. |
| 4235 | * |
| 4236 | * @param[in] args Array of arguments. |
| 4237 | * @param[in] arg_count Count of elements in @p args. |
| 4238 | * @param[in,out] set Context and result set at the same time. |
| 4239 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4240 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4241 | */ |
| 4242 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4243 | xpath_false(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4244 | { |
| 4245 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4246 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4247 | return LY_SUCCESS; |
| 4248 | } |
| 4249 | |
| 4250 | set_fill_boolean(set, 0); |
| 4251 | return LY_SUCCESS; |
| 4252 | } |
| 4253 | |
| 4254 | /** |
| 4255 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4256 | * with the first argument floored (truncated). |
| 4257 | * |
| 4258 | * @param[in] args Array of arguments. |
| 4259 | * @param[in] arg_count Count of elements in @p args. |
| 4260 | * @param[in,out] set Context and result set at the same time. |
| 4261 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4262 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4263 | */ |
| 4264 | static LY_ERR |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4265 | xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4266 | { |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4267 | struct lysc_node_leaf *sleaf; |
| 4268 | LY_ERR rc = LY_SUCCESS; |
| 4269 | |
| 4270 | if (options & LYXP_SCNODE_ALL) { |
| 4271 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
| 4272 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 4273 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4274 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4275 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4276 | sleaf->name); |
| 4277 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4278 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4279 | } |
| 4280 | } |
| 4281 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 4282 | return rc; |
| 4283 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4284 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4285 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4286 | LY_CHECK_RET(rc); |
| 4287 | if (isfinite(args[0]->val.num)) { |
| 4288 | set_fill_number(set, (long long)args[0]->val.num); |
| 4289 | } |
| 4290 | |
| 4291 | return LY_SUCCESS; |
| 4292 | } |
| 4293 | |
| 4294 | /** |
| 4295 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4296 | * whether the language of the text matches the one from the argument. |
| 4297 | * |
| 4298 | * @param[in] args Array of arguments. |
| 4299 | * @param[in] arg_count Count of elements in @p args. |
| 4300 | * @param[in,out] set Context and result set at the same time. |
| 4301 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4302 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4303 | */ |
| 4304 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4305 | xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4306 | { |
| 4307 | const struct lyd_node *node; |
| 4308 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4309 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4310 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4311 | LY_ERR rc = LY_SUCCESS; |
| 4312 | |
| 4313 | if (options & LYXP_SCNODE_ALL) { |
| 4314 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4315 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4316 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4317 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4318 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4319 | 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] | 4320 | } |
| 4321 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4322 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4323 | return rc; |
| 4324 | } |
| 4325 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4326 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4327 | LY_CHECK_RET(rc); |
| 4328 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4329 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4330 | 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] | 4331 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4332 | } else if (!set->used) { |
| 4333 | set_fill_boolean(set, 0); |
| 4334 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4335 | } |
| 4336 | |
| 4337 | switch (set->val.nodes[0].type) { |
| 4338 | case LYXP_NODE_ELEM: |
| 4339 | case LYXP_NODE_TEXT: |
| 4340 | node = set->val.nodes[0].node; |
| 4341 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4342 | case LYXP_NODE_META: |
| 4343 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4344 | break; |
| 4345 | default: |
| 4346 | /* nothing to do with roots */ |
| 4347 | set_fill_boolean(set, 0); |
| 4348 | return LY_SUCCESS; |
| 4349 | } |
| 4350 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4351 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4352 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4353 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4354 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4355 | 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] | 4356 | break; |
| 4357 | } |
| 4358 | } |
| 4359 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4360 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4361 | break; |
| 4362 | } |
| 4363 | } |
| 4364 | |
| 4365 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4366 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4367 | set_fill_boolean(set, 0); |
| 4368 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4369 | uint64_t i; |
| 4370 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4371 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4372 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4373 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4374 | set_fill_boolean(set, 0); |
| 4375 | break; |
| 4376 | } |
| 4377 | } |
| 4378 | if (!args[0]->val.str[i]) { |
| 4379 | if (!val[i] || (val[i] == '-')) { |
| 4380 | set_fill_boolean(set, 1); |
| 4381 | } else { |
| 4382 | set_fill_boolean(set, 0); |
| 4383 | } |
| 4384 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | return LY_SUCCESS; |
| 4388 | } |
| 4389 | |
| 4390 | /** |
| 4391 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4392 | * with the context size. |
| 4393 | * |
| 4394 | * @param[in] args Array of arguments. |
| 4395 | * @param[in] arg_count Count of elements in @p args. |
| 4396 | * @param[in,out] set Context and result set at the same time. |
| 4397 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4398 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4399 | */ |
| 4400 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4401 | xpath_last(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4408 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4409 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4410 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4411 | } else if (!set->used) { |
| 4412 | set_fill_number(set, 0); |
| 4413 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | set_fill_number(set, set->ctx_size); |
| 4417 | return LY_SUCCESS; |
| 4418 | } |
| 4419 | |
| 4420 | /** |
| 4421 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4422 | * with the node name without namespace from the argument or the context. |
| 4423 | * |
| 4424 | * @param[in] args Array of arguments. |
| 4425 | * @param[in] arg_count Count of elements in @p args. |
| 4426 | * @param[in,out] set Context and result set at the same time. |
| 4427 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4428 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4429 | */ |
| 4430 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4431 | xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4432 | { |
| 4433 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4434 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4435 | /* suppress unused variable warning */ |
| 4436 | (void)options; |
| 4437 | |
| 4438 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4439 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4440 | return LY_SUCCESS; |
| 4441 | } |
| 4442 | |
| 4443 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4444 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4445 | 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] | 4446 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4447 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4448 | } else if (!args[0]->used) { |
| 4449 | set_fill_string(set, "", 0); |
| 4450 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4454 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4455 | |
| 4456 | item = &args[0]->val.nodes[0]; |
| 4457 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4458 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4459 | 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] | 4460 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4461 | } else if (!set->used) { |
| 4462 | set_fill_string(set, "", 0); |
| 4463 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4467 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4468 | |
| 4469 | item = &set->val.nodes[0]; |
| 4470 | } |
| 4471 | |
| 4472 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4473 | case LYXP_NODE_NONE: |
| 4474 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4475 | case LYXP_NODE_ROOT: |
| 4476 | case LYXP_NODE_ROOT_CONFIG: |
| 4477 | case LYXP_NODE_TEXT: |
| 4478 | set_fill_string(set, "", 0); |
| 4479 | break; |
| 4480 | case LYXP_NODE_ELEM: |
| 4481 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4482 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4483 | case LYXP_NODE_META: |
| 4484 | 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] | 4485 | break; |
| 4486 | } |
| 4487 | |
| 4488 | return LY_SUCCESS; |
| 4489 | } |
| 4490 | |
| 4491 | /** |
| 4492 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4493 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4494 | * |
| 4495 | * @param[in] args Array of arguments. |
| 4496 | * @param[in] arg_count Count of elements in @p args. |
| 4497 | * @param[in,out] set Context and result set at the same time. |
| 4498 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4499 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4500 | */ |
| 4501 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4502 | xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4503 | { |
| 4504 | struct lyxp_set_node *item; |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame^] | 4505 | const struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4506 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4507 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4508 | |
| 4509 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4510 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4511 | return LY_SUCCESS; |
| 4512 | } |
| 4513 | |
| 4514 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4515 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4516 | 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] | 4517 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4518 | } else if (!args[0]->used) { |
| 4519 | set_fill_string(set, "", 0); |
| 4520 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4524 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4525 | |
| 4526 | item = &args[0]->val.nodes[0]; |
| 4527 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4528 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4529 | 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] | 4530 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4531 | } else if (!set->used) { |
| 4532 | set_fill_string(set, "", 0); |
| 4533 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4537 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4538 | |
| 4539 | item = &set->val.nodes[0]; |
| 4540 | } |
| 4541 | |
| 4542 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4543 | case LYXP_NODE_NONE: |
| 4544 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4545 | case LYXP_NODE_ROOT: |
| 4546 | case LYXP_NODE_ROOT_CONFIG: |
| 4547 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4548 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4549 | break; |
| 4550 | case LYXP_NODE_ELEM: |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame^] | 4551 | mod = lyd_node_module(item->node); |
| 4552 | name = LYD_NAME(item->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4553 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4554 | case LYXP_NODE_META: |
| 4555 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4556 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4557 | break; |
| 4558 | } |
| 4559 | |
| 4560 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4561 | int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 4562 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4563 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4564 | set_fill_string(set, str, strlen(str)); |
| 4565 | free(str); |
| 4566 | } else { |
| 4567 | set_fill_string(set, "", 0); |
| 4568 | } |
| 4569 | |
| 4570 | return LY_SUCCESS; |
| 4571 | } |
| 4572 | |
| 4573 | /** |
| 4574 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4575 | * with the namespace of the node from the argument or the context. |
| 4576 | * |
| 4577 | * @param[in] args Array of arguments. |
| 4578 | * @param[in] arg_count Count of elements in @p args. |
| 4579 | * @param[in,out] set Context and result set at the same time. |
| 4580 | * @param[in] options XPath options. |
| 4581 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4582 | */ |
| 4583 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4584 | xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4585 | { |
| 4586 | struct lyxp_set_node *item; |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame^] | 4587 | const struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4588 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4589 | /* suppress unused variable warning */ |
| 4590 | (void)options; |
| 4591 | |
| 4592 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4593 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4594 | return LY_SUCCESS; |
| 4595 | } |
| 4596 | |
| 4597 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4598 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4599 | 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] | 4600 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4601 | return LY_EVALID; |
| 4602 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4603 | set_fill_string(set, "", 0); |
| 4604 | return LY_SUCCESS; |
| 4605 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4606 | |
| 4607 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4608 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4609 | |
| 4610 | item = &args[0]->val.nodes[0]; |
| 4611 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4612 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4613 | 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] | 4614 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4615 | } else if (!set->used) { |
| 4616 | set_fill_string(set, "", 0); |
| 4617 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4621 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4622 | |
| 4623 | item = &set->val.nodes[0]; |
| 4624 | } |
| 4625 | |
| 4626 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4627 | case LYXP_NODE_NONE: |
| 4628 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4629 | case LYXP_NODE_ROOT: |
| 4630 | case LYXP_NODE_ROOT_CONFIG: |
| 4631 | case LYXP_NODE_TEXT: |
| 4632 | set_fill_string(set, "", 0); |
| 4633 | break; |
| 4634 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4635 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4636 | if (item->type == LYXP_NODE_ELEM) { |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame^] | 4637 | mod = lyd_node_module(item->node); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4638 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4639 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4640 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4644 | break; |
| 4645 | } |
| 4646 | |
| 4647 | return LY_SUCCESS; |
| 4648 | } |
| 4649 | |
| 4650 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4651 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4652 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4653 | * from the argument or the context. |
| 4654 | * |
| 4655 | * @param[in] args Array of arguments. |
| 4656 | * @param[in] arg_count Count of elements in @p args. |
| 4657 | * @param[in,out] set Context and result set at the same time. |
| 4658 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4659 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4660 | */ |
| 4661 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4662 | xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4663 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4664 | uint32_t i, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4665 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4666 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4667 | struct lysc_node_leaf *sleaf; |
| 4668 | LY_ERR rc = LY_SUCCESS; |
| 4669 | |
| 4670 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4671 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4672 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4673 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4674 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4675 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4676 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4677 | 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] | 4678 | } |
| 4679 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4680 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4681 | return rc; |
| 4682 | } |
| 4683 | |
| 4684 | if (arg_count) { |
| 4685 | set_fill_set(set, args[0]); |
| 4686 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4687 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4688 | LY_CHECK_RET(rc); |
| 4689 | |
| 4690 | /* is there any normalization necessary? */ |
| 4691 | for (i = 0; set->val.str[i]; ++i) { |
| 4692 | if (is_xmlws(set->val.str[i])) { |
| 4693 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4694 | have_spaces = 1; |
| 4695 | break; |
| 4696 | } |
| 4697 | space_before = 1; |
| 4698 | } else { |
| 4699 | space_before = 0; |
| 4700 | } |
| 4701 | } |
| 4702 | |
| 4703 | /* yep, there is */ |
| 4704 | if (have_spaces) { |
| 4705 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4706 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4707 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4708 | new_used = 0; |
| 4709 | |
| 4710 | space_before = 0; |
| 4711 | for (i = 0; set->val.str[i]; ++i) { |
| 4712 | if (is_xmlws(set->val.str[i])) { |
| 4713 | if ((i == 0) || space_before) { |
| 4714 | space_before = 1; |
| 4715 | continue; |
| 4716 | } else { |
| 4717 | space_before = 1; |
| 4718 | } |
| 4719 | } else { |
| 4720 | space_before = 0; |
| 4721 | } |
| 4722 | |
| 4723 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4724 | ++new_used; |
| 4725 | } |
| 4726 | |
| 4727 | /* at worst there is one trailing space now */ |
| 4728 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4729 | --new_used; |
| 4730 | } |
| 4731 | |
| 4732 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4733 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4734 | new[new_used] = '\0'; |
| 4735 | |
| 4736 | free(set->val.str); |
| 4737 | set->val.str = new; |
| 4738 | } |
| 4739 | |
| 4740 | return LY_SUCCESS; |
| 4741 | } |
| 4742 | |
| 4743 | /** |
| 4744 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4745 | * with the argument converted to boolean and logically inverted. |
| 4746 | * |
| 4747 | * @param[in] args Array of arguments. |
| 4748 | * @param[in] arg_count Count of elements in @p args. |
| 4749 | * @param[in,out] set Context and result set at the same time. |
| 4750 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4751 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4752 | */ |
| 4753 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4754 | xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4755 | { |
| 4756 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4757 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4758 | return LY_SUCCESS; |
| 4759 | } |
| 4760 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4761 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4762 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4763 | set_fill_boolean(set, 0); |
| 4764 | } else { |
| 4765 | set_fill_boolean(set, 1); |
| 4766 | } |
| 4767 | |
| 4768 | return LY_SUCCESS; |
| 4769 | } |
| 4770 | |
| 4771 | /** |
| 4772 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4773 | * with the number representation of either the argument or the context. |
| 4774 | * |
| 4775 | * @param[in] args Array of arguments. |
| 4776 | * @param[in] arg_count Count of elements in @p args. |
| 4777 | * @param[in,out] set Context and result set at the same time. |
| 4778 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4779 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4780 | */ |
| 4781 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4782 | xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4783 | { |
| 4784 | LY_ERR rc; |
| 4785 | |
| 4786 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4787 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4788 | return LY_SUCCESS; |
| 4789 | } |
| 4790 | |
| 4791 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4792 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4793 | LY_CHECK_RET(rc); |
| 4794 | set_fill_set(set, args[0]); |
| 4795 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4796 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4797 | LY_CHECK_RET(rc); |
| 4798 | } |
| 4799 | |
| 4800 | return LY_SUCCESS; |
| 4801 | } |
| 4802 | |
| 4803 | /** |
| 4804 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4805 | * with the context position. |
| 4806 | * |
| 4807 | * @param[in] args Array of arguments. |
| 4808 | * @param[in] arg_count Count of elements in @p args. |
| 4809 | * @param[in,out] set Context and result set at the same time. |
| 4810 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4811 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4812 | */ |
| 4813 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4814 | xpath_position(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4815 | { |
| 4816 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4817 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4818 | return LY_SUCCESS; |
| 4819 | } |
| 4820 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4821 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4822 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4823 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4824 | } else if (!set->used) { |
| 4825 | set_fill_number(set, 0); |
| 4826 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | set_fill_number(set, set->ctx_pos); |
| 4830 | |
| 4831 | /* UNUSED in 'Release' build type */ |
| 4832 | (void)options; |
| 4833 | return LY_SUCCESS; |
| 4834 | } |
| 4835 | |
| 4836 | /** |
| 4837 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4838 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4839 | * YANG 1.1 RFC section 10.2.1. |
| 4840 | * |
| 4841 | * @param[in] args Array of arguments. |
| 4842 | * @param[in] arg_count Count of elements in @p args. |
| 4843 | * @param[in,out] set Context and result set at the same time. |
| 4844 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4845 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4846 | */ |
| 4847 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4848 | xpath_re_match(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4849 | { |
| 4850 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4851 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4852 | LY_ERR rc = LY_SUCCESS; |
| 4853 | struct ly_err_item *err; |
| 4854 | |
| 4855 | if (options & LYXP_SCNODE_ALL) { |
| 4856 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4857 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4858 | 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] | 4859 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4860 | 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] | 4861 | } |
| 4862 | } |
| 4863 | |
| 4864 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4865 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4866 | 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] | 4867 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4868 | 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] | 4869 | } |
| 4870 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4871 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4872 | return rc; |
| 4873 | } |
| 4874 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4875 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4876 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4877 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4878 | LY_CHECK_RET(rc); |
| 4879 | |
| 4880 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4881 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4882 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4883 | 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] | 4884 | if (set->cur_node) { |
| 4885 | LOG_LOCBACK(0, 1, 0, 0); |
| 4886 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4887 | if (rc != LY_SUCCESS) { |
| 4888 | LY_ARRAY_FREE(patterns); |
| 4889 | return rc; |
| 4890 | } |
| 4891 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4892 | 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] | 4893 | pcre2_code_free((*pattern)->code); |
| 4894 | free(*pattern); |
| 4895 | LY_ARRAY_FREE(patterns); |
| 4896 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4897 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4898 | ly_err_free(err); |
| 4899 | return rc; |
| 4900 | } |
| 4901 | |
| 4902 | if (rc == LY_EVALID) { |
| 4903 | ly_err_free(err); |
| 4904 | set_fill_boolean(set, 0); |
| 4905 | } else { |
| 4906 | set_fill_boolean(set, 1); |
| 4907 | } |
| 4908 | |
| 4909 | return LY_SUCCESS; |
| 4910 | } |
| 4911 | |
| 4912 | /** |
| 4913 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4914 | * with the rounded first argument. For details refer to |
| 4915 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4916 | * |
| 4917 | * @param[in] args Array of arguments. |
| 4918 | * @param[in] arg_count Count of elements in @p args. |
| 4919 | * @param[in,out] set Context and result set at the same time. |
| 4920 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4921 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4922 | */ |
| 4923 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4924 | xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4925 | { |
| 4926 | struct lysc_node_leaf *sleaf; |
| 4927 | LY_ERR rc = LY_SUCCESS; |
| 4928 | |
| 4929 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4930 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4931 | 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] | 4932 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4933 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4934 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4935 | sleaf->name); |
| 4936 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4937 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4938 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4939 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4940 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4941 | return rc; |
| 4942 | } |
| 4943 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4944 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4945 | LY_CHECK_RET(rc); |
| 4946 | |
| 4947 | /* cover only the cases where floor can't be used */ |
| 4948 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4949 | set_fill_number(set, -0.0f); |
| 4950 | } else { |
| 4951 | args[0]->val.num += 0.5; |
| 4952 | rc = xpath_floor(args, 1, args[0], options); |
| 4953 | LY_CHECK_RET(rc); |
| 4954 | set_fill_number(set, args[0]->val.num); |
| 4955 | } |
| 4956 | |
| 4957 | return LY_SUCCESS; |
| 4958 | } |
| 4959 | |
| 4960 | /** |
| 4961 | * @brief Execute the XPath starts-with(string, string) function. |
| 4962 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4963 | * the prefix of the first or not. |
| 4964 | * |
| 4965 | * @param[in] args Array of arguments. |
| 4966 | * @param[in] arg_count Count of elements in @p args. |
| 4967 | * @param[in,out] set Context and result set at the same time. |
| 4968 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4969 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4970 | */ |
| 4971 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4972 | xpath_starts_with(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4973 | { |
| 4974 | struct lysc_node_leaf *sleaf; |
| 4975 | LY_ERR rc = LY_SUCCESS; |
| 4976 | |
| 4977 | if (options & LYXP_SCNODE_ALL) { |
| 4978 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4979 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4980 | 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] | 4981 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4982 | 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] | 4983 | } |
| 4984 | } |
| 4985 | |
| 4986 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4987 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4988 | 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] | 4989 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4990 | 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] | 4991 | } |
| 4992 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4993 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4994 | return rc; |
| 4995 | } |
| 4996 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4997 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4998 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4999 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5000 | LY_CHECK_RET(rc); |
| 5001 | |
| 5002 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 5003 | set_fill_boolean(set, 0); |
| 5004 | } else { |
| 5005 | set_fill_boolean(set, 1); |
| 5006 | } |
| 5007 | |
| 5008 | return LY_SUCCESS; |
| 5009 | } |
| 5010 | |
| 5011 | /** |
| 5012 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 5013 | * with the string representation of either the argument or the context. |
| 5014 | * |
| 5015 | * @param[in] args Array of arguments. |
| 5016 | * @param[in] arg_count Count of elements in @p args. |
| 5017 | * @param[in,out] set Context and result set at the same time. |
| 5018 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5019 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5020 | */ |
| 5021 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5022 | xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5023 | { |
| 5024 | LY_ERR rc; |
| 5025 | |
| 5026 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5027 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5028 | return LY_SUCCESS; |
| 5029 | } |
| 5030 | |
| 5031 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5032 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5033 | LY_CHECK_RET(rc); |
| 5034 | set_fill_set(set, args[0]); |
| 5035 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5036 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5037 | LY_CHECK_RET(rc); |
| 5038 | } |
| 5039 | |
| 5040 | return LY_SUCCESS; |
| 5041 | } |
| 5042 | |
| 5043 | /** |
| 5044 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5045 | * with the length of the string in either the argument or the context. |
| 5046 | * |
| 5047 | * @param[in] args Array of arguments. |
| 5048 | * @param[in] arg_count Count of elements in @p args. |
| 5049 | * @param[in,out] set Context and result set at the same time. |
| 5050 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5051 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5052 | */ |
| 5053 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5054 | xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5055 | { |
| 5056 | struct lysc_node_leaf *sleaf; |
| 5057 | LY_ERR rc = LY_SUCCESS; |
| 5058 | |
| 5059 | if (options & LYXP_SCNODE_ALL) { |
| 5060 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5061 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5062 | 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] | 5063 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5064 | 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] | 5065 | } |
| 5066 | } |
| 5067 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5068 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5069 | 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] | 5070 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5071 | 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] | 5072 | } |
| 5073 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5074 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5075 | return rc; |
| 5076 | } |
| 5077 | |
| 5078 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5079 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5080 | LY_CHECK_RET(rc); |
| 5081 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5082 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5083 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5084 | LY_CHECK_RET(rc); |
| 5085 | set_fill_number(set, strlen(set->val.str)); |
| 5086 | } |
| 5087 | |
| 5088 | return LY_SUCCESS; |
| 5089 | } |
| 5090 | |
| 5091 | /** |
| 5092 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5093 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5094 | * on the second argument index ending on the third argument index, |
| 5095 | * indexed from 1. For exact definition refer to |
| 5096 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5097 | * |
| 5098 | * @param[in] args Array of arguments. |
| 5099 | * @param[in] arg_count Count of elements in @p args. |
| 5100 | * @param[in,out] set Context and result set at the same time. |
| 5101 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5102 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5103 | */ |
| 5104 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5105 | xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5106 | { |
Michal Vasko | 6db996e | 2022-07-28 10:28:04 +0200 | [diff] [blame] | 5107 | int64_t start; |
| 5108 | int32_t len; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5109 | uint32_t str_start, str_len, pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5110 | struct lysc_node_leaf *sleaf; |
| 5111 | LY_ERR rc = LY_SUCCESS; |
| 5112 | |
| 5113 | if (options & LYXP_SCNODE_ALL) { |
| 5114 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5115 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5116 | 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] | 5117 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5118 | 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] | 5119 | } |
| 5120 | } |
| 5121 | |
| 5122 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5123 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5124 | 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] | 5125 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5126 | 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] | 5127 | } |
| 5128 | } |
| 5129 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5130 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5131 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5132 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5133 | 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] | 5134 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5135 | 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] | 5136 | } |
| 5137 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5138 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5139 | return rc; |
| 5140 | } |
| 5141 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5142 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5143 | LY_CHECK_RET(rc); |
| 5144 | |
| 5145 | /* start */ |
| 5146 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5147 | return -1; |
| 5148 | } |
| 5149 | if (isfinite(args[1]->val.num)) { |
| 5150 | start = args[1]->val.num - 1; |
| 5151 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5152 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5153 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5154 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5155 | } |
| 5156 | |
| 5157 | /* len */ |
| 5158 | if (arg_count == 3) { |
| 5159 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5160 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5161 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5162 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5163 | } else if (isfinite(args[2]->val.num)) { |
| 5164 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5165 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5166 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5167 | } |
| 5168 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5169 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | /* find matching character positions */ |
| 5173 | str_start = 0; |
| 5174 | str_len = 0; |
| 5175 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5176 | if (pos < start) { |
| 5177 | ++str_start; |
| 5178 | } else if (pos < start + len) { |
| 5179 | ++str_len; |
| 5180 | } else { |
| 5181 | break; |
| 5182 | } |
| 5183 | } |
| 5184 | |
| 5185 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5186 | return LY_SUCCESS; |
| 5187 | } |
| 5188 | |
| 5189 | /** |
| 5190 | * @brief Execute the XPath substring-after(string, string) function. |
| 5191 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5192 | * of the second argument in the first or an empty string. |
| 5193 | * |
| 5194 | * @param[in] args Array of arguments. |
| 5195 | * @param[in] arg_count Count of elements in @p args. |
| 5196 | * @param[in,out] set Context and result set at the same time. |
| 5197 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5198 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5199 | */ |
| 5200 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5201 | xpath_substring_after(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5202 | { |
| 5203 | char *ptr; |
| 5204 | struct lysc_node_leaf *sleaf; |
| 5205 | LY_ERR rc = LY_SUCCESS; |
| 5206 | |
| 5207 | if (options & LYXP_SCNODE_ALL) { |
| 5208 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5209 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5210 | 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] | 5211 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5212 | 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] | 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5217 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5218 | 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] | 5219 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5220 | 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] | 5221 | } |
| 5222 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5223 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5224 | return rc; |
| 5225 | } |
| 5226 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5227 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5228 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5229 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5230 | LY_CHECK_RET(rc); |
| 5231 | |
| 5232 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5233 | if (ptr) { |
| 5234 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5235 | } else { |
| 5236 | set_fill_string(set, "", 0); |
| 5237 | } |
| 5238 | |
| 5239 | return LY_SUCCESS; |
| 5240 | } |
| 5241 | |
| 5242 | /** |
| 5243 | * @brief Execute the XPath substring-before(string, string) function. |
| 5244 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5245 | * of the second argument in the first or an empty string. |
| 5246 | * |
| 5247 | * @param[in] args Array of arguments. |
| 5248 | * @param[in] arg_count Count of elements in @p args. |
| 5249 | * @param[in,out] set Context and result set at the same time. |
| 5250 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5251 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5252 | */ |
| 5253 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5254 | xpath_substring_before(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5255 | { |
| 5256 | char *ptr; |
| 5257 | struct lysc_node_leaf *sleaf; |
| 5258 | LY_ERR rc = LY_SUCCESS; |
| 5259 | |
| 5260 | if (options & LYXP_SCNODE_ALL) { |
| 5261 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5262 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5263 | 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] | 5264 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5265 | 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] | 5266 | } |
| 5267 | } |
| 5268 | |
| 5269 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5270 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5271 | 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] | 5272 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5273 | 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] | 5274 | } |
| 5275 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5276 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5277 | return rc; |
| 5278 | } |
| 5279 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5280 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5281 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5282 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5283 | LY_CHECK_RET(rc); |
| 5284 | |
| 5285 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5286 | if (ptr) { |
| 5287 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5288 | } else { |
| 5289 | set_fill_string(set, "", 0); |
| 5290 | } |
| 5291 | |
| 5292 | return LY_SUCCESS; |
| 5293 | } |
| 5294 | |
| 5295 | /** |
| 5296 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5297 | * with the sum of all the nodes in the context. |
| 5298 | * |
| 5299 | * @param[in] args Array of arguments. |
| 5300 | * @param[in] arg_count Count of elements in @p args. |
| 5301 | * @param[in,out] set Context and result set at the same time. |
| 5302 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5303 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5304 | */ |
| 5305 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5306 | xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5307 | { |
| 5308 | long double num; |
| 5309 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5310 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5311 | struct lyxp_set set_item; |
| 5312 | struct lysc_node_leaf *sleaf; |
| 5313 | LY_ERR rc = LY_SUCCESS; |
| 5314 | |
| 5315 | if (options & LYXP_SCNODE_ALL) { |
| 5316 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5317 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5318 | 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] | 5319 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5320 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5321 | 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] | 5322 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5323 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5324 | 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] | 5325 | } |
| 5326 | } |
| 5327 | } |
| 5328 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5329 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5330 | return rc; |
| 5331 | } |
| 5332 | |
| 5333 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5334 | |
| 5335 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5336 | 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] | 5337 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5338 | } else if (!args[0]->used) { |
| 5339 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5340 | } |
| 5341 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5342 | set_init(&set_item, set); |
| 5343 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5344 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5345 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5346 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5347 | |
| 5348 | set_item.used = 1; |
| 5349 | set_item.size = 1; |
| 5350 | |
| 5351 | for (i = 0; i < args[0]->used; ++i) { |
| 5352 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5353 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5354 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5355 | LY_CHECK_RET(rc); |
| 5356 | num = cast_string_to_number(str); |
| 5357 | free(str); |
| 5358 | set->val.num += num; |
| 5359 | } |
| 5360 | |
| 5361 | free(set_item.val.nodes); |
| 5362 | |
| 5363 | return LY_SUCCESS; |
| 5364 | } |
| 5365 | |
| 5366 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5367 | * @brief Execute the XPath translate(string, string, string) function. |
| 5368 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5369 | * from the second argument replaced by those on the corresponding |
| 5370 | * positions in the third argument. |
| 5371 | * |
| 5372 | * @param[in] args Array of arguments. |
| 5373 | * @param[in] arg_count Count of elements in @p args. |
| 5374 | * @param[in,out] set Context and result set at the same time. |
| 5375 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5376 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5377 | */ |
| 5378 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5379 | xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5380 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5381 | uint32_t i, j, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5382 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5383 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5384 | struct lysc_node_leaf *sleaf; |
| 5385 | LY_ERR rc = LY_SUCCESS; |
| 5386 | |
| 5387 | if (options & LYXP_SCNODE_ALL) { |
| 5388 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5389 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5390 | 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] | 5391 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5392 | 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] | 5393 | } |
| 5394 | } |
| 5395 | |
| 5396 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5397 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5398 | 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] | 5399 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5400 | 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] | 5401 | } |
| 5402 | } |
| 5403 | |
| 5404 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5405 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5406 | 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] | 5407 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5408 | 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] | 5409 | } |
| 5410 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5411 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5412 | return rc; |
| 5413 | } |
| 5414 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5415 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5416 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5417 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5418 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5419 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5420 | LY_CHECK_RET(rc); |
| 5421 | |
| 5422 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5423 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5424 | new_used = 0; |
| 5425 | |
| 5426 | have_removed = 0; |
| 5427 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5428 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5429 | |
| 5430 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5431 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5432 | /* removing this char */ |
| 5433 | if (j >= strlen(args[2]->val.str)) { |
| 5434 | have_removed = 1; |
| 5435 | found = 1; |
| 5436 | break; |
| 5437 | } |
| 5438 | /* replacing this char */ |
| 5439 | new[new_used] = args[2]->val.str[j]; |
| 5440 | ++new_used; |
| 5441 | found = 1; |
| 5442 | break; |
| 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | /* copying this char */ |
| 5447 | if (!found) { |
| 5448 | new[new_used] = args[0]->val.str[i]; |
| 5449 | ++new_used; |
| 5450 | } |
| 5451 | } |
| 5452 | |
| 5453 | if (have_removed) { |
| 5454 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5455 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5456 | } |
| 5457 | new[new_used] = '\0'; |
| 5458 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5459 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5460 | set->type = LYXP_SET_STRING; |
| 5461 | set->val.str = new; |
| 5462 | |
| 5463 | return LY_SUCCESS; |
| 5464 | } |
| 5465 | |
| 5466 | /** |
| 5467 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5468 | * with true value. |
| 5469 | * |
| 5470 | * @param[in] args Array of arguments. |
| 5471 | * @param[in] arg_count Count of elements in @p args. |
| 5472 | * @param[in,out] set Context and result set at the same time. |
| 5473 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5474 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5475 | */ |
| 5476 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5477 | xpath_true(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5478 | { |
| 5479 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5480 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5481 | return LY_SUCCESS; |
| 5482 | } |
| 5483 | |
| 5484 | set_fill_boolean(set, 1); |
| 5485 | return LY_SUCCESS; |
| 5486 | } |
| 5487 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5488 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5489 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5490 | * with only nodes from the context. |
| 5491 | * |
| 5492 | * @param[in,out] set Context and result set at the same time. |
| 5493 | * @param[in] axis Axis to search on. |
| 5494 | * @param[in] options XPath options. |
| 5495 | * @return LY_ERR |
| 5496 | */ |
| 5497 | static LY_ERR |
| 5498 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5499 | { |
| 5500 | if (options & LYXP_SCNODE_ALL) { |
| 5501 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5502 | } |
| 5503 | |
| 5504 | if (set->type != LYXP_SET_NODE_SET) { |
| 5505 | lyxp_set_free_content(set); |
| 5506 | return LY_SUCCESS; |
| 5507 | } |
| 5508 | |
| 5509 | /* just like moving to a node with no restrictions */ |
| 5510 | return moveto_node(set, NULL, NULL, axis, options); |
| 5511 | } |
| 5512 | |
| 5513 | /** |
| 5514 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5515 | * with the text content of the nodes in the context. |
| 5516 | * |
| 5517 | * @param[in,out] set Context and result set at the same time. |
| 5518 | * @param[in] axis Axis to search on. |
| 5519 | * @param[in] options XPath options. |
| 5520 | * @return LY_ERR |
| 5521 | */ |
| 5522 | static LY_ERR |
| 5523 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5524 | { |
| 5525 | uint32_t i; |
| 5526 | |
| 5527 | if (options & LYXP_SCNODE_ALL) { |
| 5528 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5529 | return LY_SUCCESS; |
| 5530 | } |
| 5531 | |
| 5532 | if (set->type != LYXP_SET_NODE_SET) { |
| 5533 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5534 | return LY_EVALID; |
| 5535 | } |
| 5536 | |
| 5537 | if (axis != LYXP_AXIS_CHILD) { |
| 5538 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5539 | lyxp_set_free_content(set); |
| 5540 | return LY_SUCCESS; |
| 5541 | } |
| 5542 | |
| 5543 | for (i = 0; i < set->used; ++i) { |
| 5544 | switch (set->val.nodes[i].type) { |
| 5545 | case LYXP_NODE_NONE: |
| 5546 | LOGINT_RET(set->ctx); |
| 5547 | case LYXP_NODE_ELEM: |
| 5548 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5549 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5550 | break; |
| 5551 | } |
| 5552 | /* fall through */ |
| 5553 | case LYXP_NODE_ROOT: |
| 5554 | case LYXP_NODE_ROOT_CONFIG: |
| 5555 | case LYXP_NODE_TEXT: |
| 5556 | case LYXP_NODE_META: |
| 5557 | set_remove_node_none(set, i); |
| 5558 | break; |
| 5559 | } |
| 5560 | } |
| 5561 | set_remove_nodes_none(set); |
| 5562 | |
| 5563 | return LY_SUCCESS; |
| 5564 | } |
| 5565 | |
| 5566 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5567 | * @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] | 5568 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5569 | * XPath @p set is expected to be a (sc)node set! |
| 5570 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5571 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5572 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5573 | * @param[in] set Set with general XPath context. |
| 5574 | * @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] | 5575 | * @param[out] moveto_mod Expected module of a matching node. |
| 5576 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5577 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5578 | static LY_ERR |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 5579 | moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5580 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5581 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5582 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5583 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5584 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5585 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5586 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5587 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5588 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5589 | /* specific module */ |
| 5590 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5591 | 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] | 5592 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5593 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5594 | if (!mod || !mod->implemented) { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 5595 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, (int)pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5596 | return LY_EVALID; |
| 5597 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5598 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5599 | *qname += pref_len + 1; |
| 5600 | *qname_len -= pref_len + 1; |
| 5601 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5602 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5603 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5604 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5605 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5606 | case LY_VALUE_SCHEMA: |
| 5607 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5608 | /* current module */ |
| 5609 | mod = set->cur_mod; |
| 5610 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5611 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5612 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5613 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5614 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5615 | /* inherit parent (context node) module */ |
| 5616 | if (ctx_scnode) { |
| 5617 | mod = ctx_scnode->module; |
| 5618 | } else { |
| 5619 | mod = NULL; |
| 5620 | } |
| 5621 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5622 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5623 | /* all nodes need to be prefixed */ |
| 5624 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5625 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5626 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5627 | } |
| 5628 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5629 | *moveto_mod = mod; |
| 5630 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5634 | * @brief Move context @p set to the root. Handles absolute path. |
| 5635 | * Result is LYXP_SET_NODE_SET. |
| 5636 | * |
| 5637 | * @param[in,out] set Set to use. |
| 5638 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5639 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5640 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5641 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5642 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5643 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5644 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5645 | |
| 5646 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5647 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 5648 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5649 | } else { |
Michal Vasko | ef71b39 | 2023-08-16 09:45:23 +0200 | [diff] [blame] | 5650 | lyxp_set_free_content(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5651 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 5652 | set->non_child_axis = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5653 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5654 | |
| 5655 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5656 | } |
| 5657 | |
| 5658 | /** |
| 5659 | * @brief Check @p node as a part of NameTest processing. |
| 5660 | * |
| 5661 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5662 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5663 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5664 | * @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] | 5665 | * @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] | 5666 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5667 | * @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] | 5668 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5669 | */ |
| 5670 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5671 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5672 | 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] | 5673 | { |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5674 | const struct lysc_node *schema; |
| 5675 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5676 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5677 | assert(node_type == set->root_type); |
| 5678 | |
| 5679 | if (node_name || moveto_mod) { |
| 5680 | /* root will not match a specific node */ |
| 5681 | return LY_ENOT; |
| 5682 | } |
| 5683 | return LY_SUCCESS; |
| 5684 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5685 | /* other types will not match */ |
| 5686 | return LY_ENOT; |
| 5687 | } |
| 5688 | |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5689 | /* get schema node even of an opaque node */ |
| 5690 | schema = lyd_node_schema(node); |
| 5691 | if (!schema) { |
| 5692 | /* unknown opaque node never matches */ |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5693 | return LY_ENOT; |
| 5694 | } |
| 5695 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5696 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5697 | if (moveto_mod) { |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5698 | if ((set->ctx == LYD_CTX(node)) && (schema->module != moveto_mod)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5699 | return LY_ENOT; |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5700 | } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->module->name, moveto_mod->name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5701 | return LY_ENOT; |
| 5702 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5703 | } |
| 5704 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5705 | /* context check */ |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5706 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5707 | return LY_EINVAL; |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5708 | } else if (set->context_op && (schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5709 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5710 | } |
| 5711 | |
| 5712 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5713 | if (node_name) { |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5714 | if ((set->ctx == LYD_CTX(node)) && (schema->name != node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5715 | return LY_ENOT; |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5716 | } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->name, node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5717 | return LY_ENOT; |
| 5718 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5719 | } |
| 5720 | |
Michal Vasko | 6d65712 | 2022-10-19 14:38:35 +0200 | [diff] [blame] | 5721 | /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */ |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5722 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(schema) && !(node->flags & LYD_WHEN_TRUE) && |
Michal Vasko | 6d65712 | 2022-10-19 14:38:35 +0200 | [diff] [blame] | 5723 | (node != set->cur_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5724 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5725 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5726 | |
| 5727 | /* match */ |
| 5728 | return LY_SUCCESS; |
| 5729 | } |
| 5730 | |
| 5731 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5732 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5733 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5734 | * @param[in] iter Last returned node. |
| 5735 | * @param[in] stop Node to stop the search on and not return. |
| 5736 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5737 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5738 | static const struct lyd_node * |
| 5739 | 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] | 5740 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5741 | const struct lyd_node *next = NULL; |
| 5742 | |
| 5743 | /* 1) child */ |
| 5744 | next = lyd_child(iter); |
| 5745 | if (!next) { |
| 5746 | if (iter == stop) { |
| 5747 | /* reached stop, no more descendants */ |
| 5748 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5749 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5750 | /* 2) child next sibling */ |
| 5751 | next = iter->next; |
| 5752 | } |
| 5753 | while (!next) { |
| 5754 | iter = lyd_parent(iter); |
| 5755 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5756 | return NULL; |
| 5757 | } |
| 5758 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5759 | } |
| 5760 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5761 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5765 | * @brief Get the next node in a backward DFS. |
| 5766 | * |
| 5767 | * @param[in] iter Last returned node. |
| 5768 | * @param[in] stop Node to stop the search on and not return. |
| 5769 | * @return Next node, NULL if there are no more. |
| 5770 | */ |
| 5771 | static const struct lyd_node * |
| 5772 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5773 | { |
| 5774 | const struct lyd_node *next = NULL; |
| 5775 | |
| 5776 | /* 1) previous sibling innermost last child */ |
| 5777 | next = iter->prev->next ? iter->prev : NULL; |
| 5778 | while (next && lyd_child(next)) { |
| 5779 | next = lyd_child(next); |
| 5780 | next = next->prev; |
| 5781 | } |
| 5782 | |
| 5783 | if (!next) { |
| 5784 | /* 2) parent */ |
| 5785 | iter = lyd_parent(iter); |
| 5786 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5787 | return NULL; |
| 5788 | } |
| 5789 | next = iter; |
| 5790 | } |
| 5791 | |
| 5792 | return next; |
| 5793 | } |
| 5794 | |
| 5795 | /** |
| 5796 | * @brief Get the first node on an axis for a context node. |
| 5797 | * |
| 5798 | * @param[in,out] iter NULL, updated to the next node. |
| 5799 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5800 | * @param[in] node Context node. |
| 5801 | * @param[in] node_type Type of @p node. |
| 5802 | * @param[in] axis Axis to use. |
| 5803 | * @param[in] set XPath set with the general context. |
| 5804 | * @return LY_SUCCESS on success. |
| 5805 | * @return LY_ENOTFOUND if no next node found. |
| 5806 | */ |
| 5807 | static LY_ERR |
| 5808 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5809 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5810 | { |
| 5811 | const struct lyd_node *next = NULL; |
| 5812 | enum lyxp_node_type next_type = 0; |
| 5813 | |
| 5814 | assert(!*iter); |
| 5815 | assert(!*iter_type); |
| 5816 | |
| 5817 | switch (axis) { |
| 5818 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5819 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5820 | case LYXP_AXIS_SELF: |
| 5821 | /* return the context node */ |
| 5822 | next = node; |
| 5823 | next_type = node_type; |
| 5824 | break; |
| 5825 | |
| 5826 | case LYXP_AXIS_ANCESTOR: |
| 5827 | case LYXP_AXIS_PARENT: |
| 5828 | if (node_type == LYXP_NODE_ELEM) { |
| 5829 | next = lyd_parent(node); |
| 5830 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5831 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5832 | next = node; |
| 5833 | next_type = LYXP_NODE_ELEM; |
| 5834 | } else if (node_type == LYXP_NODE_META) { |
| 5835 | next = ((struct lyd_meta *)node)->parent; |
| 5836 | next_type = LYXP_NODE_ELEM; |
| 5837 | } /* else root does not have a parent */ |
| 5838 | break; |
| 5839 | |
| 5840 | case LYXP_AXIS_CHILD: |
| 5841 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5842 | assert(!node); |
| 5843 | |
| 5844 | /* search in all the trees */ |
| 5845 | next = set->tree; |
| 5846 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5847 | } else { |
| 5848 | /* search in children */ |
| 5849 | next = lyd_child(node); |
| 5850 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5851 | } |
| 5852 | break; |
| 5853 | |
| 5854 | case LYXP_AXIS_DESCENDANT: |
| 5855 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5856 | /* top-level nodes */ |
| 5857 | next = set->tree; |
| 5858 | next_type = LYXP_NODE_ELEM; |
| 5859 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5860 | /* start from the context node */ |
| 5861 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5862 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5863 | } /* else no children */ |
| 5864 | break; |
| 5865 | |
| 5866 | case LYXP_AXIS_FOLLOWING: |
| 5867 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5868 | if (node_type == LYXP_NODE_ELEM) { |
| 5869 | /* first next sibling */ |
| 5870 | next = node->next; |
| 5871 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5872 | } /* else no sibling */ |
| 5873 | break; |
| 5874 | |
| 5875 | case LYXP_AXIS_PRECEDING: |
| 5876 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5877 | /* skip ancestors */ |
| 5878 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5879 | assert(next); |
| 5880 | next_type = LYXP_NODE_ELEM; |
| 5881 | } /* else no sibling */ |
| 5882 | break; |
| 5883 | |
| 5884 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5885 | if (node_type == LYXP_NODE_ELEM) { |
| 5886 | /* first previous sibling */ |
| 5887 | next = node->prev->next ? node->prev : NULL; |
| 5888 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5889 | } /* else no sibling */ |
| 5890 | break; |
| 5891 | |
| 5892 | case LYXP_AXIS_ATTRIBUTE: |
| 5893 | /* handled specially */ |
| 5894 | assert(0); |
| 5895 | LOGINT(set->ctx); |
| 5896 | break; |
| 5897 | } |
| 5898 | |
| 5899 | *iter = next; |
| 5900 | *iter_type = next_type; |
| 5901 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5902 | } |
| 5903 | |
| 5904 | /** |
| 5905 | * @brief Iterate over all nodes on an axis for a context node. |
| 5906 | * |
| 5907 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5908 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5909 | * @param[in] node Context node. |
| 5910 | * @param[in] node_type Type of @p node. |
| 5911 | * @param[in] axis Axis to use. |
| 5912 | * @param[in] set XPath set with the general context. |
| 5913 | * @return LY_SUCCESS on success. |
| 5914 | * @return LY_ENOTFOUND if no next node found. |
| 5915 | */ |
| 5916 | static LY_ERR |
| 5917 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5918 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5919 | { |
| 5920 | const struct lyd_node *next = NULL; |
| 5921 | enum lyxp_node_type next_type = 0; |
| 5922 | |
| 5923 | if (!*iter_type) { |
| 5924 | /* first returned node */ |
| 5925 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5926 | } |
| 5927 | |
| 5928 | switch (axis) { |
| 5929 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5930 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5931 | /* fake first ancestor, we returned self before */ |
| 5932 | *iter = NULL; |
| 5933 | *iter_type = 0; |
| 5934 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 5935 | } /* else continue ancestor */ |
| 5936 | |
| 5937 | /* fallthrough */ |
| 5938 | case LYXP_AXIS_ANCESTOR: |
| 5939 | if (*iter_type == LYXP_NODE_ELEM) { |
| 5940 | /* iter parent */ |
| 5941 | next = lyd_parent(*iter); |
| 5942 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5943 | } /* else root, no ancestors */ |
| 5944 | break; |
| 5945 | |
| 5946 | case LYXP_AXIS_CHILD: |
| 5947 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5948 | |
| 5949 | /* next sibling (child) */ |
| 5950 | next = (*iter)->next; |
| 5951 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5952 | break; |
| 5953 | |
| 5954 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5955 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5956 | /* fake first descendant, we returned self before */ |
| 5957 | *iter = NULL; |
| 5958 | *iter_type = 0; |
| 5959 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 5960 | } /* else continue descendant */ |
| 5961 | |
| 5962 | /* fallthrough */ |
| 5963 | case LYXP_AXIS_DESCENDANT: |
| 5964 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5965 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 5966 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5967 | break; |
| 5968 | |
| 5969 | case LYXP_AXIS_FOLLOWING: |
| 5970 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5971 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 5972 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5973 | break; |
| 5974 | |
| 5975 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5976 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5977 | |
| 5978 | /* next sibling */ |
| 5979 | next = (*iter)->next; |
| 5980 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5981 | break; |
| 5982 | |
| 5983 | case LYXP_AXIS_PARENT: |
| 5984 | case LYXP_AXIS_SELF: |
| 5985 | /* parent/self was returned before */ |
| 5986 | break; |
| 5987 | |
| 5988 | case LYXP_AXIS_PRECEDING: |
| 5989 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5990 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 5991 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5992 | break; |
| 5993 | |
| 5994 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5995 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5996 | |
| 5997 | /* previous sibling */ |
| 5998 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 5999 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6000 | break; |
| 6001 | |
| 6002 | case LYXP_AXIS_ATTRIBUTE: |
| 6003 | /* handled specially */ |
| 6004 | assert(0); |
| 6005 | LOGINT(set->ctx); |
| 6006 | break; |
| 6007 | } |
| 6008 | |
| 6009 | *iter = next; |
| 6010 | *iter_type = next_type; |
| 6011 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6012 | } |
| 6013 | |
| 6014 | /** |
| 6015 | * @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] | 6016 | * |
| 6017 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6018 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6019 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6020 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6021 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6022 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6023 | */ |
| 6024 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6025 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6026 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6027 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6028 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6029 | const struct lyd_node *iter; |
| 6030 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6031 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6032 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6033 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6034 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6035 | return LY_SUCCESS; |
| 6036 | } |
| 6037 | |
| 6038 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6039 | 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] | 6040 | return LY_EVALID; |
| 6041 | } |
| 6042 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6043 | /* init result set */ |
| 6044 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6045 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6046 | for (i = 0; i < set->used; ++i) { |
| 6047 | /* iterate over all the nodes on the axis of the node */ |
| 6048 | iter = NULL; |
| 6049 | iter_type = 0; |
| 6050 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6051 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6052 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6053 | rc = r; |
| 6054 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6055 | } else if (r) { |
| 6056 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6057 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6058 | |
| 6059 | /* check for duplicates if they are possible */ |
| 6060 | switch (axis) { |
| 6061 | case LYXP_AXIS_ANCESTOR: |
| 6062 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6063 | case LYXP_AXIS_DESCENDANT: |
| 6064 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6065 | case LYXP_AXIS_FOLLOWING: |
| 6066 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6067 | case LYXP_AXIS_PARENT: |
| 6068 | case LYXP_AXIS_PRECEDING: |
| 6069 | case LYXP_AXIS_PRECEDING_SIBLING: |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6070 | result.non_child_axis = 1; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6071 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6072 | continue; |
| 6073 | } |
| 6074 | break; |
| 6075 | case LYXP_AXIS_CHILD: |
| 6076 | case LYXP_AXIS_SELF: |
| 6077 | break; |
| 6078 | case LYXP_AXIS_ATTRIBUTE: |
| 6079 | /* handled specially */ |
| 6080 | assert(0); |
| 6081 | LOGINT(set->ctx); |
| 6082 | break; |
| 6083 | } |
| 6084 | |
| 6085 | /* matching node */ |
| 6086 | set_insert_node(&result, iter, 0, iter_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6087 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6088 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6089 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6090 | /* move result to the set */ |
| 6091 | lyxp_set_free_content(set); |
| 6092 | *set = result; |
| 6093 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6094 | |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6095 | /* sort the final set if the document order could have been broken */ |
| 6096 | if (set->non_child_axis) { |
| 6097 | set_sort(set); |
| 6098 | } else { |
| 6099 | assert(!set_sort(set)); |
| 6100 | } |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6101 | |
| 6102 | cleanup: |
| 6103 | lyxp_set_free_content(&result); |
| 6104 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6105 | } |
| 6106 | |
| 6107 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6108 | * @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] | 6109 | * |
| 6110 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6111 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6112 | * @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] | 6113 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6114 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6115 | */ |
| 6116 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6117 | 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] | 6118 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6119 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6120 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6121 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6122 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6123 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6124 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6125 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6126 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6127 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6128 | /* init result set */ |
| 6129 | set_init(&result, set); |
| 6130 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6131 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6132 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6133 | } |
| 6134 | |
| 6135 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6136 | 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] | 6137 | ret = LY_EVALID; |
| 6138 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6139 | } |
| 6140 | |
| 6141 | /* context check for all the nodes since we have the schema node */ |
| 6142 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6143 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6144 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6145 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6146 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6147 | lyxp_set_free_content(set); |
| 6148 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | /* create specific data instance if needed */ |
| 6152 | if (scnode->nodetype == LYS_LIST) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 6153 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6154 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6155 | 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] | 6156 | } |
| 6157 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6158 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6159 | siblings = NULL; |
| 6160 | |
| 6161 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6162 | assert(!set->val.nodes[i].node); |
| 6163 | |
| 6164 | /* search in all the trees */ |
| 6165 | siblings = set->tree; |
| 6166 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6167 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6168 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6169 | } |
| 6170 | |
| 6171 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6172 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6173 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6174 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6175 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6176 | } |
Michal Vasko | d8a5201 | 2023-08-15 11:38:10 +0200 | [diff] [blame] | 6177 | if (r == LY_ENOTFOUND) { |
| 6178 | /* may still be an opaque node */ |
| 6179 | r = lyd_find_sibling_opaq_next(siblings, scnode->name, &sub); |
| 6180 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6181 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6182 | |
| 6183 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6184 | 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] | 6185 | ret = LY_EINCOMPLETE; |
| 6186 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6187 | } |
| 6188 | |
| 6189 | if (sub) { |
| 6190 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6191 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6192 | } |
| 6193 | } |
| 6194 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6195 | /* move result to the set */ |
| 6196 | lyxp_set_free_content(set); |
| 6197 | *set = result; |
| 6198 | result.type = LYXP_SET_NUMBER; |
| 6199 | assert(!set_sort(set)); |
| 6200 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6201 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6202 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6203 | lyd_free_tree(inst); |
| 6204 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6208 | * @brief Check @p node as a part of schema NameTest processing. |
| 6209 | * |
| 6210 | * @param[in] node Schema node to check. |
| 6211 | * @param[in] ctx_scnode Context node. |
| 6212 | * @param[in] set Set to read general context from. |
| 6213 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6214 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6215 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6216 | */ |
| 6217 | static LY_ERR |
| 6218 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6219 | const char *node_name, const struct lys_module *moveto_mod) |
| 6220 | { |
| 6221 | if (!moveto_mod && node_name) { |
| 6222 | switch (set->format) { |
| 6223 | case LY_VALUE_SCHEMA: |
| 6224 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6225 | /* use current module */ |
| 6226 | moveto_mod = set->cur_mod; |
| 6227 | break; |
| 6228 | case LY_VALUE_JSON: |
| 6229 | case LY_VALUE_LYB: |
| 6230 | case LY_VALUE_STR_NS: |
| 6231 | /* inherit module of the context node, if any */ |
| 6232 | if (ctx_scnode) { |
| 6233 | moveto_mod = ctx_scnode->module; |
| 6234 | } |
| 6235 | break; |
| 6236 | case LY_VALUE_CANON: |
| 6237 | case LY_VALUE_XML: |
| 6238 | /* not defined */ |
| 6239 | LOGINT(set->ctx); |
| 6240 | return LY_EINVAL; |
| 6241 | } |
| 6242 | } |
| 6243 | |
| 6244 | if (!node) { |
| 6245 | /* root will not match a specific node */ |
| 6246 | if (node_name || moveto_mod) { |
| 6247 | return LY_ENOT; |
| 6248 | } |
| 6249 | return LY_SUCCESS; |
| 6250 | } |
| 6251 | |
| 6252 | /* module check */ |
| 6253 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6254 | return LY_ENOT; |
| 6255 | } |
| 6256 | |
| 6257 | /* context check */ |
| 6258 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6259 | return LY_EINVAL; |
| 6260 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6261 | return LY_EINVAL; |
| 6262 | } |
| 6263 | |
| 6264 | /* name check */ |
| 6265 | if (node_name && (node->name != node_name)) { |
| 6266 | return LY_ENOT; |
| 6267 | } |
| 6268 | |
| 6269 | /* match */ |
| 6270 | return LY_SUCCESS; |
| 6271 | } |
| 6272 | |
| 6273 | /** |
| 6274 | * @brief Get the next node in a forward schema node DFS. |
| 6275 | * |
| 6276 | * @param[in] iter Last returned node. |
| 6277 | * @param[in] stop Node to stop the search on and not return. |
| 6278 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6279 | * @return Next node, NULL if there are no more. |
| 6280 | */ |
| 6281 | static const struct lysc_node * |
| 6282 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6283 | { |
| 6284 | const struct lysc_node *next = NULL; |
| 6285 | |
| 6286 | next = lysc_node_child(iter); |
| 6287 | if (!next) { |
| 6288 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6289 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6290 | /* we are done, no next element to process */ |
| 6291 | return NULL; |
| 6292 | } |
| 6293 | |
| 6294 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6295 | } |
| 6296 | while (!next && iter) { |
| 6297 | /* parent is already processed, go to its sibling */ |
| 6298 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6299 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6300 | /* we are done, no next element to process */ |
| 6301 | return NULL; |
| 6302 | } |
| 6303 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6304 | } |
| 6305 | |
| 6306 | return next; |
| 6307 | } |
| 6308 | |
| 6309 | /** |
| 6310 | * @brief Consider schema node based on its in_ctx enum value. |
| 6311 | * |
| 6312 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6313 | * @param[in] axis Axis to use. |
| 6314 | * @return LY_SUCCESS on success. |
| 6315 | * @return LY_ENOT if the node should not be returned. |
| 6316 | */ |
| 6317 | static LY_ERR |
| 6318 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6319 | { |
| 6320 | switch (axis) { |
| 6321 | case LYXP_AXIS_SELF: |
| 6322 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6323 | /* additionally put the start node into context */ |
| 6324 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6325 | return LY_SUCCESS; |
| 6326 | } |
| 6327 | break; |
| 6328 | case LYXP_AXIS_PARENT: |
| 6329 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6330 | case LYXP_AXIS_ANCESTOR: |
| 6331 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6332 | case LYXP_AXIS_DESCENDANT: |
| 6333 | case LYXP_AXIS_FOLLOWING: |
| 6334 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6335 | case LYXP_AXIS_PRECEDING: |
| 6336 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6337 | case LYXP_AXIS_CHILD: |
| 6338 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6339 | /* remember that context node was used */ |
| 6340 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6341 | return LY_SUCCESS; |
| 6342 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6343 | /* traversed */ |
| 6344 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6345 | return LY_SUCCESS; |
| 6346 | } |
| 6347 | break; |
| 6348 | case LYXP_AXIS_ATTRIBUTE: |
| 6349 | /* unreachable */ |
| 6350 | assert(0); |
| 6351 | LOGINT(NULL); |
| 6352 | break; |
| 6353 | } |
| 6354 | |
| 6355 | return LY_ENOT; |
| 6356 | } |
| 6357 | |
| 6358 | /** |
| 6359 | * @brief Get previous sibling for a schema node. |
| 6360 | * |
| 6361 | * @param[in] scnode Schema node. |
| 6362 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6363 | * @return Previous sibling, NULL if none. |
| 6364 | */ |
| 6365 | static const struct lysc_node * |
| 6366 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6367 | { |
| 6368 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6369 | |
| 6370 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6371 | if (next == scnode) { |
| 6372 | break; |
| 6373 | } |
| 6374 | |
| 6375 | prev = next; |
| 6376 | } |
| 6377 | |
| 6378 | return prev; |
| 6379 | } |
| 6380 | |
| 6381 | /** |
| 6382 | * @brief Get the first schema node on an axis for a context node. |
| 6383 | * |
| 6384 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6385 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6386 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6387 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6388 | * @param[in] scnode Context node. |
| 6389 | * @param[in] node_type Type of @p scnode. |
| 6390 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6391 | * @param[in] axis Axis to use. |
| 6392 | * @param[in] set XPath set with the general context. |
| 6393 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6394 | * @return LY_SUCCESS on success. |
| 6395 | * @return LY_ENOTFOUND if no next node found. |
| 6396 | */ |
| 6397 | static LY_ERR |
| 6398 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6399 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6400 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6401 | { |
| 6402 | const struct lysc_node *next = NULL; |
| 6403 | enum lyxp_node_type next_type = 0; |
| 6404 | |
| 6405 | assert(!*iter); |
| 6406 | assert(!*iter_type); |
| 6407 | |
| 6408 | *iter_mod = NULL; |
| 6409 | *iter_mod_idx = 0; |
| 6410 | |
| 6411 | switch (axis) { |
| 6412 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6413 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6414 | case LYXP_AXIS_SELF: |
| 6415 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6416 | /* just return the node */ |
| 6417 | next = scnode; |
| 6418 | next_type = node_type; |
| 6419 | } |
| 6420 | break; |
| 6421 | |
| 6422 | case LYXP_AXIS_ANCESTOR: |
| 6423 | case LYXP_AXIS_PARENT: |
| 6424 | if (node_type == LYXP_NODE_ELEM) { |
| 6425 | next = lysc_data_parent(scnode); |
| 6426 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6427 | } /* else no parent */ |
| 6428 | break; |
| 6429 | |
| 6430 | case LYXP_AXIS_DESCENDANT: |
| 6431 | case LYXP_AXIS_CHILD: |
| 6432 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6433 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6434 | * it can be in a top-level augment */ |
| 6435 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6436 | /* module may not be implemented or not compiled yet */ |
| 6437 | if (!(*iter_mod)->compiled) { |
| 6438 | continue; |
| 6439 | } |
| 6440 | |
| 6441 | /* get next node */ |
| 6442 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6443 | next_type = LYXP_NODE_ELEM; |
| 6444 | break; |
| 6445 | } |
| 6446 | } |
| 6447 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6448 | /* get next node */ |
| 6449 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6450 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6451 | } |
| 6452 | break; |
| 6453 | |
| 6454 | case LYXP_AXIS_FOLLOWING: |
| 6455 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6456 | if (node_type == LYXP_NODE_ELEM) { |
| 6457 | /* first next sibling */ |
| 6458 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6459 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6460 | } /* else no sibling */ |
| 6461 | break; |
| 6462 | |
| 6463 | case LYXP_AXIS_PRECEDING: |
| 6464 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6465 | if (node_type == LYXP_NODE_ELEM) { |
| 6466 | /* first parent sibling */ |
| 6467 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6468 | if (next == scnode) { |
| 6469 | /* no preceding sibling */ |
| 6470 | next = NULL; |
| 6471 | } |
| 6472 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6473 | } /* else no sibling */ |
| 6474 | break; |
| 6475 | |
| 6476 | case LYXP_AXIS_ATTRIBUTE: |
| 6477 | /* unreachable */ |
| 6478 | assert(0); |
| 6479 | LOGINT(set->ctx); |
| 6480 | break; |
| 6481 | } |
| 6482 | |
| 6483 | *iter = next; |
| 6484 | *iter_type = next_type; |
| 6485 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6486 | } |
| 6487 | |
| 6488 | /** |
| 6489 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6490 | * |
| 6491 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6492 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6493 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6494 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6495 | * @param[in] scnode Context node. |
| 6496 | * @param[in] node_type Type of @p scnode. |
| 6497 | * @param[in] axis Axis to use. |
| 6498 | * @param[in] set XPath set with the general context. |
| 6499 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6500 | * @return LY_SUCCESS on success. |
| 6501 | * @return LY_ENOTFOUND if no next node found. |
| 6502 | */ |
| 6503 | static LY_ERR |
| 6504 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6505 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6506 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6507 | { |
| 6508 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6509 | enum lyxp_node_type next_type = 0; |
| 6510 | |
| 6511 | if (!*iter_type) { |
| 6512 | /* first returned node */ |
| 6513 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6514 | getnext_opts); |
| 6515 | } |
| 6516 | |
| 6517 | switch (axis) { |
| 6518 | case LYXP_AXIS_PARENT: |
| 6519 | case LYXP_AXIS_SELF: |
| 6520 | /* parent/self was returned before */ |
| 6521 | break; |
| 6522 | |
| 6523 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6524 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6525 | /* fake first ancestor, we returned self before */ |
| 6526 | *iter = NULL; |
| 6527 | *iter_type = 0; |
| 6528 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6529 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
| 6530 | } /* else continue ancestor */ |
| 6531 | |
| 6532 | /* fallthrough */ |
| 6533 | case LYXP_AXIS_ANCESTOR: |
| 6534 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6535 | next = lysc_data_parent(*iter); |
| 6536 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6537 | } /* else no ancestor */ |
| 6538 | break; |
| 6539 | |
| 6540 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6541 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6542 | /* fake first descendant, we returned self before */ |
| 6543 | *iter = NULL; |
| 6544 | *iter_type = 0; |
| 6545 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6546 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6547 | } /* else DFS until context node */ |
| 6548 | dfs_stop = scnode; |
| 6549 | |
| 6550 | /* fallthrough */ |
| 6551 | case LYXP_AXIS_DESCENDANT: |
| 6552 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6553 | /* DFS until the context node */ |
| 6554 | dfs_stop = scnode; |
| 6555 | } |
| 6556 | |
| 6557 | /* fallthrough */ |
| 6558 | case LYXP_AXIS_PRECEDING: |
| 6559 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6560 | /* DFS until the previous sibling */ |
| 6561 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6562 | assert(dfs_stop); |
| 6563 | |
| 6564 | if (*iter == dfs_stop) { |
| 6565 | /* we are done */ |
| 6566 | break; |
| 6567 | } |
| 6568 | } |
| 6569 | |
| 6570 | /* fallthrough */ |
| 6571 | case LYXP_AXIS_FOLLOWING: |
| 6572 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6573 | /* DFS through the whole module */ |
| 6574 | dfs_stop = NULL; |
| 6575 | } |
| 6576 | |
| 6577 | /* nested nodes */ |
| 6578 | assert(*iter); |
| 6579 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6580 | if (next) { |
| 6581 | next_type = LYXP_NODE_ELEM; |
| 6582 | break; |
| 6583 | } /* else get next top-level node just like a child */ |
| 6584 | |
| 6585 | /* fallthrough */ |
| 6586 | case LYXP_AXIS_CHILD: |
| 6587 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6588 | if (!*iter_mod) { |
| 6589 | /* nodes from a single module */ |
| 6590 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6591 | next_type = LYXP_NODE_ELEM; |
| 6592 | break; |
| 6593 | } |
| 6594 | |
| 6595 | assert(scnode); |
Michal Vasko | a353cce | 2022-11-14 10:09:55 +0100 | [diff] [blame] | 6596 | if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6597 | /* iterating over top-level nodes, find next */ |
| 6598 | while (lysc_data_parent(*iter)) { |
| 6599 | *iter = lysc_data_parent(*iter); |
| 6600 | } |
| 6601 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6602 | next_type = LYXP_NODE_ELEM; |
| 6603 | break; |
| 6604 | } |
| 6605 | } |
| 6606 | } |
| 6607 | |
| 6608 | while (*iter_mod) { |
| 6609 | /* module top-level nodes */ |
| 6610 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6611 | next_type = LYXP_NODE_ELEM; |
| 6612 | break; |
| 6613 | } |
| 6614 | |
| 6615 | /* get next module */ |
| 6616 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6617 | /* module may not be implemented or not compiled yet */ |
| 6618 | if ((*iter_mod)->compiled) { |
| 6619 | break; |
| 6620 | } |
| 6621 | } |
| 6622 | |
| 6623 | /* new module, start over */ |
| 6624 | *iter = NULL; |
| 6625 | } |
| 6626 | break; |
| 6627 | |
| 6628 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6629 | assert(*iter); |
| 6630 | |
| 6631 | /* next parent sibling until scnode */ |
| 6632 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6633 | if (next == scnode) { |
| 6634 | /* no previous sibling */ |
| 6635 | next = NULL; |
| 6636 | } |
| 6637 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6638 | break; |
| 6639 | |
| 6640 | case LYXP_AXIS_ATTRIBUTE: |
| 6641 | /* unreachable */ |
| 6642 | assert(0); |
| 6643 | LOGINT(set->ctx); |
| 6644 | break; |
| 6645 | } |
| 6646 | |
| 6647 | *iter = next; |
| 6648 | *iter_type = next_type; |
| 6649 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6650 | } |
| 6651 | |
| 6652 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6653 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6654 | * |
| 6655 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6656 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6657 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6658 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6659 | * @param[in] options XPath options. |
| 6660 | * @return LY_ERR |
| 6661 | */ |
| 6662 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6663 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6664 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6665 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6666 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6667 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 6668 | const struct lys_module *mod = NULL; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6669 | const struct lysc_node *iter; |
| 6670 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6671 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6672 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6673 | return LY_SUCCESS; |
| 6674 | } |
| 6675 | |
| 6676 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6677 | 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] | 6678 | return LY_EVALID; |
| 6679 | } |
| 6680 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6681 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6682 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6683 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6684 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6685 | } |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 6686 | if (options & LYXP_SCNODE_SCHEMAMOUNT) { |
| 6687 | getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT; |
| 6688 | } |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6689 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6690 | orig_used = set->used; |
| 6691 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6692 | /* update in_ctx first */ |
| 6693 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6694 | /* not usable, skip */ |
| 6695 | continue; |
| 6696 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6697 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6698 | iter = NULL; |
| 6699 | iter_type = 0; |
| 6700 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6701 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6702 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6703 | continue; |
| 6704 | } |
| 6705 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6706 | /* insert */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6707 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, axis, &idx)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6708 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6709 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6710 | if ((idx < orig_used) && (idx > i)) { |
| 6711 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6712 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6713 | } |
| 6714 | } |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 6715 | |
| 6716 | if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) && |
| 6717 | (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode, |
| 6718 | moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) { |
| 6719 | /* there is a matching node from an extension, use it */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6720 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx)); |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 6721 | if ((idx < orig_used) && (idx > i)) { |
| 6722 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6723 | temp_ctx = 1; |
| 6724 | } |
| 6725 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6726 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6727 | |
| 6728 | /* correct temporary in_ctx values */ |
| 6729 | if (temp_ctx) { |
| 6730 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6731 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6732 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6733 | } |
| 6734 | } |
| 6735 | } |
| 6736 | |
| 6737 | return LY_SUCCESS; |
| 6738 | } |
| 6739 | |
| 6740 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6741 | * @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] | 6742 | * Context position aware. |
| 6743 | * |
| 6744 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6745 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6746 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6747 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6748 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6749 | */ |
| 6750 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6751 | 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] | 6752 | { |
| 6753 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6754 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6755 | struct lyxp_set ret_set; |
| 6756 | LY_ERR rc; |
| 6757 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6758 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6759 | return LY_SUCCESS; |
| 6760 | } |
| 6761 | |
| 6762 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6763 | 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] | 6764 | return LY_EVALID; |
| 6765 | } |
| 6766 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6767 | /* 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] | 6768 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6769 | LY_CHECK_RET(rc); |
| 6770 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6771 | /* 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] | 6772 | set_init(&ret_set, set); |
| 6773 | for (i = 0; i < set->used; ++i) { |
| 6774 | |
| 6775 | /* TREE DFS */ |
| 6776 | start = set->val.nodes[i].node; |
| 6777 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6778 | 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] | 6779 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | /* add matching node into result set */ |
| 6781 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6782 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6783 | /* the node is a duplicate, we'll process it later in the set */ |
| 6784 | goto skip_children; |
| 6785 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6786 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6787 | return rc; |
| 6788 | } else if (rc == LY_EINVAL) { |
| 6789 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6790 | } |
| 6791 | |
| 6792 | /* TREE DFS NEXT ELEM */ |
| 6793 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6794 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6795 | if (!next) { |
| 6796 | skip_children: |
| 6797 | /* no children, so try siblings, but only if it's not the start, |
| 6798 | * that is considered to be the root and it's siblings are not traversed */ |
| 6799 | if (elem != start) { |
| 6800 | next = elem->next; |
| 6801 | } else { |
| 6802 | break; |
| 6803 | } |
| 6804 | } |
| 6805 | while (!next) { |
| 6806 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6807 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6808 | /* we are done, no next element to process */ |
| 6809 | break; |
| 6810 | } |
| 6811 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6812 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6813 | next = elem->next; |
| 6814 | } |
| 6815 | } |
| 6816 | } |
| 6817 | |
| 6818 | /* make the temporary set the current one */ |
| 6819 | ret_set.ctx_pos = set->ctx_pos; |
| 6820 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6821 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6822 | memcpy(set, &ret_set, sizeof *set); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6823 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6824 | |
| 6825 | return LY_SUCCESS; |
| 6826 | } |
| 6827 | |
| 6828 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6829 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6830 | * Result is LYXP_SET_NODE_SET. |
| 6831 | * |
| 6832 | * @param[in] set Set to use. |
| 6833 | * @param[in] start Start node whose subtree to add. |
| 6834 | * @param[in] start_idx Index of @p start in @p set. |
| 6835 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6836 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6837 | * @param[in] options XPath options. |
| 6838 | * @return LY_ERR value. |
| 6839 | */ |
| 6840 | static LY_ERR |
| 6841 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6842 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6843 | { |
| 6844 | const struct lysc_node *next, *elem; |
| 6845 | uint32_t idx; |
| 6846 | LY_ERR rc; |
| 6847 | |
| 6848 | /* TREE DFS */ |
| 6849 | for (elem = next = start; elem; elem = next) { |
| 6850 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6851 | /* schema-only nodes, skip root */ |
| 6852 | goto next_iter; |
| 6853 | } |
| 6854 | |
| 6855 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6856 | if (!rc) { |
| 6857 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6858 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6859 | if (idx > start_idx) { |
| 6860 | /* we will process it later in the set */ |
| 6861 | goto skip_children; |
| 6862 | } |
| 6863 | } else { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6864 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL)); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6865 | } |
| 6866 | } else if (rc == LY_EINVAL) { |
| 6867 | goto skip_children; |
| 6868 | } |
| 6869 | |
| 6870 | next_iter: |
| 6871 | /* TREE DFS NEXT ELEM */ |
| 6872 | /* select element for the next run - children first */ |
| 6873 | next = lysc_node_child(elem); |
| 6874 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6875 | next = next->next; |
| 6876 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6877 | next = next->next; |
| 6878 | } |
| 6879 | if (!next) { |
| 6880 | skip_children: |
| 6881 | /* no children, so try siblings, but only if it's not the start, |
| 6882 | * that is considered to be the root and it's siblings are not traversed */ |
| 6883 | if (elem != start) { |
| 6884 | next = elem->next; |
| 6885 | } else { |
| 6886 | break; |
| 6887 | } |
| 6888 | } |
| 6889 | while (!next) { |
| 6890 | /* no siblings, go back through the parents */ |
| 6891 | if (elem->parent == start) { |
| 6892 | /* we are done, no next element to process */ |
| 6893 | break; |
| 6894 | } |
| 6895 | /* parent is already processed, go to its sibling */ |
| 6896 | elem = elem->parent; |
| 6897 | next = elem->next; |
| 6898 | } |
| 6899 | } |
| 6900 | |
| 6901 | return LY_SUCCESS; |
| 6902 | } |
| 6903 | |
| 6904 | /** |
| 6905 | * @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] | 6906 | * |
| 6907 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6908 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6909 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6910 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6911 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6912 | */ |
| 6913 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6914 | 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] | 6915 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6916 | uint32_t i, orig_used, mod_idx; |
| 6917 | const struct lys_module *mod; |
| 6918 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6919 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6920 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6921 | return LY_SUCCESS; |
| 6922 | } |
| 6923 | |
| 6924 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6925 | 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] | 6926 | return LY_EVALID; |
| 6927 | } |
| 6928 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6929 | orig_used = set->used; |
| 6930 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6931 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6932 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6933 | continue; |
| 6934 | } |
| 6935 | |
| 6936 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6937 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6938 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6939 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6940 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6941 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6942 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6943 | /* traverse all top-level nodes in all the modules */ |
| 6944 | mod_idx = 0; |
| 6945 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6946 | /* module may not be implemented or not compiled yet */ |
| 6947 | if (!mod->compiled) { |
| 6948 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6949 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6950 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6951 | root = NULL; |
| 6952 | /* no getnext opts needed */ |
| 6953 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 6954 | 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] | 6955 | } |
| 6956 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6957 | |
| 6958 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6959 | /* add all the descendants recursively */ |
| 6960 | 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] | 6961 | } |
| 6962 | } |
| 6963 | |
| 6964 | return LY_SUCCESS; |
| 6965 | } |
| 6966 | |
| 6967 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6968 | * @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] | 6969 | * Indirectly context position aware. |
| 6970 | * |
| 6971 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6972 | * @param[in] mod Matching metadata module, NULL for any. |
| 6973 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6974 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6975 | * @return LY_ERR |
| 6976 | */ |
| 6977 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6978 | 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] | 6979 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6980 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6981 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6982 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6983 | return LY_SUCCESS; |
| 6984 | } |
| 6985 | |
| 6986 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6987 | 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] | 6988 | return LY_EVALID; |
| 6989 | } |
| 6990 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6991 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6992 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6993 | |
| 6994 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 6995 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6996 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6997 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6998 | |
| 6999 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7000 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7001 | continue; |
| 7002 | } |
| 7003 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7004 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7005 | /* match */ |
| 7006 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7007 | set->val.meta[i].meta = sub; |
| 7008 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7009 | /* pos does not change */ |
| 7010 | replaced = 1; |
| 7011 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7012 | 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] | 7013 | } |
| 7014 | ++i; |
| 7015 | } |
| 7016 | } |
| 7017 | } |
| 7018 | |
| 7019 | if (!replaced) { |
| 7020 | /* no match */ |
| 7021 | set_remove_node(set, i); |
| 7022 | } |
| 7023 | } |
| 7024 | |
| 7025 | return LY_SUCCESS; |
| 7026 | } |
| 7027 | |
| 7028 | /** |
| 7029 | * @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] | 7030 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7031 | * |
| 7032 | * @param[in,out] set1 Set to use for the result. |
| 7033 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7034 | * @return LY_ERR |
| 7035 | */ |
| 7036 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7037 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7038 | { |
| 7039 | LY_ERR rc; |
| 7040 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7041 | 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] | 7042 | 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] | 7043 | return LY_EVALID; |
| 7044 | } |
| 7045 | |
| 7046 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7047 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7048 | return LY_SUCCESS; |
| 7049 | } |
| 7050 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7051 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 7052 | /* release hidden allocated data (lyxp_set.size) */ |
| 7053 | lyxp_set_free_content(set1); |
| 7054 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7055 | memcpy(set1, set2, sizeof *set1); |
| 7056 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7057 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7058 | return LY_SUCCESS; |
| 7059 | } |
| 7060 | |
| 7061 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7062 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7063 | |
| 7064 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7065 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7066 | LY_CHECK_RET(rc); |
| 7067 | |
| 7068 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7069 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7070 | |
| 7071 | return LY_SUCCESS; |
| 7072 | } |
| 7073 | |
| 7074 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7075 | * @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] | 7076 | * Context position aware. |
| 7077 | * |
| 7078 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7079 | * @param[in] mod Matching metadata module, NULL for any. |
| 7080 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7081 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7082 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7083 | */ |
| 7084 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7085 | 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] | 7086 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7087 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7088 | struct lyxp_set *set_all_desc = NULL; |
| 7089 | LY_ERR rc; |
| 7090 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7091 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7092 | return LY_SUCCESS; |
| 7093 | } |
| 7094 | |
| 7095 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7096 | 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] | 7097 | return LY_EVALID; |
| 7098 | } |
| 7099 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7100 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7101 | * but it likely won't be used much, so it's a waste of time */ |
| 7102 | /* copy the context */ |
| 7103 | set_all_desc = set_copy(set); |
| 7104 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7105 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7106 | if (rc != LY_SUCCESS) { |
| 7107 | lyxp_set_free(set_all_desc); |
| 7108 | return rc; |
| 7109 | } |
| 7110 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7111 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7112 | if (rc != LY_SUCCESS) { |
| 7113 | lyxp_set_free(set_all_desc); |
| 7114 | return rc; |
| 7115 | } |
| 7116 | lyxp_set_free(set_all_desc); |
| 7117 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7118 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7119 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7120 | |
| 7121 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7122 | * we have all attributes qualified in lyd tree */ |
| 7123 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7124 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7125 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7126 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7127 | continue; |
| 7128 | } |
| 7129 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7130 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7131 | /* match */ |
| 7132 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7133 | set->val.meta[i].meta = sub; |
| 7134 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7135 | /* pos does not change */ |
| 7136 | replaced = 1; |
| 7137 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7138 | 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] | 7139 | } |
| 7140 | ++i; |
| 7141 | } |
| 7142 | } |
| 7143 | } |
| 7144 | |
| 7145 | if (!replaced) { |
| 7146 | /* no match */ |
| 7147 | set_remove_node(set, i); |
| 7148 | } |
| 7149 | } |
| 7150 | |
| 7151 | return LY_SUCCESS; |
| 7152 | } |
| 7153 | |
| 7154 | /** |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7155 | * @brief Move context @p set1 single item to the result of a comparison. |
| 7156 | * |
| 7157 | * @param[in] set1 First set with the item to compare. |
| 7158 | * @param[in] idx1 Index of the item in @p set1. |
| 7159 | * @param[in] set2 Second set. |
| 7160 | * @param[in] op Comparison operator to process. |
| 7161 | * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`. |
| 7162 | * @param[out] result Result of the comparison. |
| 7163 | * @return LY_ERR value. |
| 7164 | */ |
| 7165 | static LY_ERR |
| 7166 | moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op, |
| 7167 | ly_bool switch_operands, ly_bool *result) |
| 7168 | { |
| 7169 | struct lyxp_set tmp1 = {0}; |
| 7170 | LY_ERR rc = LY_SUCCESS; |
| 7171 | |
| 7172 | assert(set1->type == LYXP_SET_NODE_SET); |
| 7173 | |
| 7174 | /* cast set1 */ |
| 7175 | switch (set2->type) { |
| 7176 | case LYXP_SET_NUMBER: |
| 7177 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); |
| 7178 | break; |
| 7179 | case LYXP_SET_BOOLEAN: |
| 7180 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); |
| 7181 | break; |
| 7182 | default: |
| 7183 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); |
| 7184 | break; |
| 7185 | } |
| 7186 | LY_CHECK_GOTO(rc, cleanup); |
| 7187 | |
| 7188 | /* canonize set2 */ |
| 7189 | LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup); |
| 7190 | |
| 7191 | /* compare recursively and store the result */ |
| 7192 | if (switch_operands) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7193 | LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7194 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7195 | LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7196 | } |
| 7197 | |
| 7198 | cleanup: |
| 7199 | lyxp_set_free_content(&tmp1); |
| 7200 | return rc; |
| 7201 | } |
| 7202 | |
| 7203 | /** |
| 7204 | * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7205 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7206 | * |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7207 | * @param[in] set1 Set acting as the first operand for @p op. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7208 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7209 | * @param[in] op Comparison operator to process. |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7210 | * @param[out] result Result of the comparison. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7211 | * @return LY_ERR |
| 7212 | */ |
| 7213 | static LY_ERR |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7214 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7215 | { |
| 7216 | /* |
| 7217 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7218 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7219 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7220 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7221 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7222 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7223 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7224 | * |
| 7225 | * '=' or '!=' |
| 7226 | * BOOLEAN + BOOLEAN |
| 7227 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7228 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7229 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7230 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7231 | * NUMBER + NUMBER |
| 7232 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7233 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7234 | * STRING + STRING |
| 7235 | * |
| 7236 | * '<=', '<', '>=', '>' |
| 7237 | * NUMBER + NUMBER |
| 7238 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7239 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7240 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7241 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7242 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7243 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7244 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7245 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7246 | */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7247 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7248 | LY_ERR rc; |
| 7249 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7250 | /* iterative evaluation with node-sets */ |
| 7251 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7252 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7253 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7254 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7255 | LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7256 | |
| 7257 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7258 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7259 | return LY_SUCCESS; |
| 7260 | } |
| 7261 | } |
| 7262 | } else { |
| 7263 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7264 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7265 | LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7266 | |
| 7267 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7268 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7269 | return LY_SUCCESS; |
| 7270 | } |
| 7271 | } |
| 7272 | } |
| 7273 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7274 | /* false for all the nodes */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7275 | *result = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7276 | return LY_SUCCESS; |
| 7277 | } |
| 7278 | |
| 7279 | /* first convert properly */ |
| 7280 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7281 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7282 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7283 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7284 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7285 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7286 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7287 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7288 | LY_CHECK_RET(rc); |
| 7289 | } /* else we have 2 strings */ |
| 7290 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7291 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7292 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7293 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7294 | LY_CHECK_RET(rc); |
| 7295 | } |
| 7296 | |
| 7297 | assert(set1->type == set2->type); |
| 7298 | |
| 7299 | /* compute result */ |
| 7300 | if (op[0] == '=') { |
| 7301 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7302 | *result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7303 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7304 | *result = (set1->val.num == set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7305 | } else { |
| 7306 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7307 | *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7308 | } |
| 7309 | } else if (op[0] == '!') { |
| 7310 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7311 | *result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7312 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7313 | *result = (set1->val.num != set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7314 | } else { |
| 7315 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7316 | *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7317 | } |
| 7318 | } else { |
| 7319 | assert(set1->type == LYXP_SET_NUMBER); |
| 7320 | if (op[0] == '<') { |
| 7321 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7322 | *result = (set1->val.num <= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7323 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7324 | *result = (set1->val.num < set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7325 | } |
| 7326 | } else { |
| 7327 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7328 | *result = (set1->val.num >= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7329 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7330 | *result = (set1->val.num > set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7331 | } |
| 7332 | } |
| 7333 | } |
| 7334 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7335 | return LY_SUCCESS; |
| 7336 | } |
| 7337 | |
| 7338 | /** |
| 7339 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7340 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7341 | * |
| 7342 | * @param[in,out] set1 Set to use for the result. |
| 7343 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7344 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7345 | * @return LY_ERR |
| 7346 | */ |
| 7347 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7348 | 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] | 7349 | { |
| 7350 | LY_ERR rc; |
| 7351 | |
| 7352 | /* unary '-' */ |
| 7353 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7354 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7355 | LY_CHECK_RET(rc); |
| 7356 | set1->val.num *= -1; |
| 7357 | lyxp_set_free(set2); |
| 7358 | return LY_SUCCESS; |
| 7359 | } |
| 7360 | |
| 7361 | assert(set1 && set2); |
| 7362 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7363 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7364 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7365 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7366 | LY_CHECK_RET(rc); |
| 7367 | |
| 7368 | switch (op[0]) { |
| 7369 | /* '+' */ |
| 7370 | case '+': |
| 7371 | set1->val.num += set2->val.num; |
| 7372 | break; |
| 7373 | |
| 7374 | /* '-' */ |
| 7375 | case '-': |
| 7376 | set1->val.num -= set2->val.num; |
| 7377 | break; |
| 7378 | |
| 7379 | /* '*' */ |
| 7380 | case '*': |
| 7381 | set1->val.num *= set2->val.num; |
| 7382 | break; |
| 7383 | |
| 7384 | /* 'div' */ |
| 7385 | case 'd': |
| 7386 | set1->val.num /= set2->val.num; |
| 7387 | break; |
| 7388 | |
| 7389 | /* 'mod' */ |
| 7390 | case 'm': |
| 7391 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7392 | break; |
| 7393 | |
| 7394 | default: |
| 7395 | LOGINT_RET(set1->ctx); |
| 7396 | } |
| 7397 | |
| 7398 | return LY_SUCCESS; |
| 7399 | } |
| 7400 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7401 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7402 | * @brief Evaluate Predicate. Logs directly on error. |
| 7403 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7404 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7405 | * |
| 7406 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7407 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7408 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7409 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7410 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7411 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7412 | */ |
| 7413 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7414 | eval_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options, enum lyxp_axis axis) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7415 | { |
| 7416 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7417 | uint32_t i, orig_exp, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7418 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7419 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7420 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7421 | |
| 7422 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7423 | 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] | 7424 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7425 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7426 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7427 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7428 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7429 | 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] | 7430 | LY_CHECK_RET(rc); |
| 7431 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7432 | /* 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] | 7433 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7434 | |
| 7435 | /* empty set, nothing to evaluate */ |
| 7436 | if (!set->used) { |
| 7437 | goto only_parse; |
| 7438 | } |
| 7439 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7440 | /* decide forward or reverse axis */ |
| 7441 | switch (axis) { |
| 7442 | case LYXP_AXIS_ANCESTOR: |
| 7443 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7444 | case LYXP_AXIS_PRECEDING: |
| 7445 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7446 | reverse_axis = 1; |
| 7447 | break; |
| 7448 | case LYXP_AXIS_DESCENDANT: |
| 7449 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7450 | case LYXP_AXIS_FOLLOWING: |
| 7451 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7452 | case LYXP_AXIS_PARENT: |
| 7453 | case LYXP_AXIS_CHILD: |
| 7454 | case LYXP_AXIS_SELF: |
| 7455 | case LYXP_AXIS_ATTRIBUTE: |
| 7456 | reverse_axis = 0; |
| 7457 | break; |
| 7458 | } |
| 7459 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7460 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7461 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7462 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7463 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7464 | set_init(&set2, set); |
| 7465 | 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] | 7466 | |
| 7467 | /* remember the node context position for position() and context size for last() */ |
| 7468 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7469 | |
| 7470 | set2.ctx_pos = orig_pos; |
| 7471 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7472 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7474 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7476 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7477 | return rc; |
| 7478 | } |
| 7479 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7480 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7481 | if (set2.type == LYXP_SET_NUMBER) { |
| 7482 | if ((long long)set2.val.num == orig_pos) { |
| 7483 | set2.val.num = 1; |
| 7484 | } else { |
| 7485 | set2.val.num = 0; |
| 7486 | } |
| 7487 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7488 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7489 | |
| 7490 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7491 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7492 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7493 | } |
| 7494 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7495 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7496 | |
| 7497 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7498 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7499 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7500 | /* there is a currently-valid node */ |
| 7501 | break; |
| 7502 | } |
| 7503 | } |
| 7504 | /* empty set, nothing to evaluate */ |
| 7505 | if (i == set->used) { |
| 7506 | goto only_parse; |
| 7507 | } |
| 7508 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7509 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7510 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7511 | /* set special in_ctx to all the valid snodes */ |
| 7512 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7513 | |
| 7514 | /* use the valid snodes one-by-one */ |
| 7515 | for (i = 0; i < set->used; ++i) { |
| 7516 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7517 | continue; |
| 7518 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7519 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7520 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7521 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7522 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7523 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7524 | LY_CHECK_RET(rc); |
| 7525 | |
| 7526 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7527 | } |
| 7528 | |
| 7529 | /* restore the state as it was before the predicate */ |
| 7530 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7531 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7532 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7533 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7534 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7535 | } |
| 7536 | } |
| 7537 | |
| 7538 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7539 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7540 | set_fill_set(&set2, set); |
| 7541 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7542 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7543 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7544 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | return rc; |
| 7546 | } |
| 7547 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7548 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7549 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7550 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7551 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7552 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7553 | } |
| 7554 | |
| 7555 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7556 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7557 | 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] | 7558 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7559 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7560 | |
| 7561 | return LY_SUCCESS; |
| 7562 | } |
| 7563 | |
| 7564 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7565 | * @brief Evaluate Literal. Logs directly on error. |
| 7566 | * |
| 7567 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7568 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7569 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7570 | */ |
| 7571 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7572 | eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7573 | { |
| 7574 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7575 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7576 | set_fill_string(set, "", 0); |
| 7577 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7578 | 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] | 7579 | } |
| 7580 | } |
| 7581 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7582 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7583 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7584 | } |
| 7585 | |
| 7586 | /** |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7587 | * @brief Check that a nametest in a predicate matches a key node. |
| 7588 | * |
| 7589 | * @param[in] nametest Nametest to check. |
| 7590 | * @param[in] len Length of @p nametest. |
| 7591 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7592 | * @param[in] set Context set. |
| 7593 | * @param[in] key Expected key node. |
| 7594 | * @return LY_SUCCESS on success, |
| 7595 | * @return LY_ENOT if a predicate could not be compiled. |
| 7596 | * @return LY_ERR on any error. |
| 7597 | */ |
| 7598 | static LY_ERR |
| 7599 | eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode, |
| 7600 | const struct lyxp_set *set, const struct lysc_node *key) |
| 7601 | { |
| 7602 | const struct lys_module *mod; |
| 7603 | |
| 7604 | /* prefix (module) */ |
| 7605 | LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod)); |
| 7606 | if (mod != key->module) { |
| 7607 | return LY_ENOT; |
| 7608 | } |
| 7609 | |
| 7610 | /* node name */ |
| 7611 | if (ly_strncmp(key->name, nametest, len)) { |
| 7612 | return LY_ENOT; |
| 7613 | } |
| 7614 | |
| 7615 | return LY_SUCCESS; |
| 7616 | } |
| 7617 | |
| 7618 | /** |
| 7619 | * @brief Append a simple predicate for the node. |
| 7620 | * |
| 7621 | * @param[in] exp Full parsed XPath expression. |
| 7622 | * @param[in] tok_idx Predicate start index in @p exp. |
| 7623 | * @param[in] end_tok_idx Predicate end index in @p exp. |
| 7624 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7625 | * @param[in] set Context set. |
| 7626 | * @param[in] pred_node Node with the value referenced in the predicate. |
| 7627 | * @param[in,out] pred Predicate to append to. |
| 7628 | * @param[in,out] pred_len Length of @p pred, is updated. |
| 7629 | * @return LY_SUCCESS on success, |
| 7630 | * @return LY_ENOT if a predicate could not be compiled. |
| 7631 | * @return LY_ERR on any error. |
| 7632 | */ |
| 7633 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7634 | eval_name_test_try_compile_predicate_append(const struct lyxp_expr *exp, uint32_t tok_idx, uint32_t end_tok_idx, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7635 | const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred, |
| 7636 | uint32_t *pred_len) |
| 7637 | { |
| 7638 | LY_ERR rc = LY_SUCCESS; |
| 7639 | uint32_t i; |
| 7640 | const struct lyd_node *siblings; |
| 7641 | struct lyd_node *ctx_node; |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7642 | const struct lysc_node *sparent, *cur_scnode; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7643 | struct lyxp_expr *val_exp = NULL; |
| 7644 | struct lyxp_set set2 = {0}; |
| 7645 | char quot; |
| 7646 | |
| 7647 | /* duplicate the value expression */ |
| 7648 | LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup); |
| 7649 | |
| 7650 | /* get its atoms */ |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7651 | cur_scnode = set->cur_node ? set->cur_node->schema : NULL; |
| 7652 | LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode, |
| 7653 | ctx_scnode, &set2, LYXP_SCNODE), cleanup); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7654 | |
| 7655 | /* check whether we can compile a single predicate (evaluation result value is always the same) */ |
| 7656 | for (i = 0; i < set2.used; ++i) { |
| 7657 | if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) { |
| 7658 | /* skip root and context node */ |
| 7659 | continue; |
| 7660 | } |
| 7661 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7662 | /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the |
| 7663 | * context node instance */ |
| 7664 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) { |
| 7665 | /* 1.1) context node child was accessed on the child axis, certain dependency */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7666 | rc = LY_ENOT; |
| 7667 | goto cleanup; |
| 7668 | } |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7669 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) { |
| 7670 | for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {} |
| 7671 | if (sparent) { |
| 7672 | /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */ |
| 7673 | rc = LY_ENOT; |
| 7674 | goto cleanup; |
| 7675 | } |
| 7676 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7677 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7678 | /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered, |
| 7679 | * but the current node can be safely ignored, it is always the same data instance */ |
| 7680 | if ((set2.val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (cur_scnode != set2.val.scnodes[i].scnode)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7681 | rc = LY_ENOT; |
| 7682 | goto cleanup; |
| 7683 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7684 | } |
| 7685 | |
| 7686 | /* get any data instance of the context node, we checked it makes no difference */ |
| 7687 | siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree; |
| 7688 | LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup); |
| 7689 | |
| 7690 | /* evaluate the value subexpression with the root context node */ |
| 7691 | lyxp_set_free_content(&set2); |
| 7692 | LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node, |
| 7693 | ctx_node, set->tree, NULL, &set2, 0), cleanup); |
| 7694 | |
| 7695 | /* cast it into a string */ |
| 7696 | LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup); |
| 7697 | |
| 7698 | /* append the JSON predicate */ |
| 7699 | *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3); |
| 7700 | LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7701 | quot = strchr(set2.val.str, '\'') ? '\"' : '\''; |
| 7702 | *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot); |
| 7703 | |
| 7704 | cleanup: |
| 7705 | lyxp_expr_free(set->ctx, val_exp); |
| 7706 | lyxp_set_free_content(&set2); |
| 7707 | return rc; |
| 7708 | } |
| 7709 | |
| 7710 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7711 | * @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] | 7712 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7713 | * @param[in] exp Full parsed XPath expression. |
| 7714 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7715 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7716 | * @param[in] set Context set. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7717 | * @param[out] predicates Parsed predicates. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7718 | * @return LY_SUCCESS on success, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7719 | * @return LY_ENOT if a predicate could not be compiled. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7720 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7721 | */ |
| 7722 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7723 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode, |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 7724 | const struct lyxp_set *set, struct ly_path_predicate **predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7725 | { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7726 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7727 | uint32_t e_idx, val_start_idx, pred_idx = 0, temp_lo = 0, pred_len = 0, nested_pred; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7728 | const struct lysc_node *key; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7729 | char *pred = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7730 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7731 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7732 | assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7733 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7734 | /* turn logging off */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7735 | ly_temp_log_options(&temp_lo); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7736 | |
| 7737 | if (ctx_scnode->nodetype == LYS_LIST) { |
| 7738 | /* check for predicates "[key1=...][key2=...]..." */ |
| 7739 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7740 | /* get key count */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7741 | if (ctx_scnode->flags & LYS_KEYLESS) { |
| 7742 | rc = LY_ENOT; |
| 7743 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7744 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7745 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7746 | /* learn where the predicates end */ |
| 7747 | e_idx = *tok_idx; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7748 | for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7749 | /* '[' */ |
| 7750 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7751 | rc = LY_ENOT; |
| 7752 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7753 | } |
| 7754 | ++e_idx; |
| 7755 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7756 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7757 | /* not a key */ |
| 7758 | rc = LY_ENOT; |
| 7759 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7760 | } |
| 7761 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7762 | /* check key */ |
| 7763 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx], |
| 7764 | exp->tok_len[e_idx], ctx_scnode, set, key), cleanup); |
| 7765 | |
| 7766 | ++e_idx; |
| 7767 | |
| 7768 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7769 | /* not '=' */ |
| 7770 | rc = LY_ENOT; |
| 7771 | goto cleanup; |
| 7772 | } |
| 7773 | ++e_idx; |
| 7774 | |
| 7775 | /* value start */ |
| 7776 | val_start_idx = e_idx; |
| 7777 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7778 | /* ']' */ |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7779 | nested_pred = 1; |
| 7780 | do { |
| 7781 | ++e_idx; |
| 7782 | |
| 7783 | if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7784 | /* higher priority than '=' */ |
| 7785 | rc = LY_ENOT; |
| 7786 | goto cleanup; |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7787 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7788 | /* nested predicate */ |
| 7789 | ++nested_pred; |
| 7790 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7791 | /* predicate end */ |
| 7792 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7793 | } |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7794 | } while (nested_pred); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7795 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7796 | /* try to evaluate the value */ |
| 7797 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, |
| 7798 | set, key, &pred, &pred_len), cleanup); |
| 7799 | |
| 7800 | ++e_idx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7801 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7802 | } else { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7803 | /* check for predicate "[.=...]" */ |
| 7804 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7805 | /* learn just where this single predicate ends */ |
| 7806 | e_idx = *tok_idx; |
| 7807 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7808 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7809 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7810 | rc = LY_ENOT; |
| 7811 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7812 | } |
| 7813 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7814 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7815 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7816 | /* not the node value */ |
| 7817 | rc = LY_ENOT; |
| 7818 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7819 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7820 | ++e_idx; |
| 7821 | |
| 7822 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7823 | /* not '=' */ |
| 7824 | rc = LY_ENOT; |
| 7825 | goto cleanup; |
| 7826 | } |
| 7827 | ++e_idx; |
| 7828 | |
| 7829 | /* value start */ |
| 7830 | val_start_idx = e_idx; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7831 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7832 | /* ']' */ |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7833 | nested_pred = 1; |
| 7834 | do { |
| 7835 | ++e_idx; |
| 7836 | |
| 7837 | if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7838 | /* higher priority than '=' */ |
| 7839 | rc = LY_ENOT; |
| 7840 | goto cleanup; |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7841 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7842 | /* nested predicate */ |
| 7843 | ++nested_pred; |
| 7844 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7845 | /* predicate end */ |
| 7846 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7847 | } |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7848 | } while (nested_pred); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7849 | |
| 7850 | /* try to evaluate the value */ |
| 7851 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set, |
| 7852 | ctx_scnode, &pred, &pred_len), cleanup); |
| 7853 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7854 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7855 | } |
| 7856 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7857 | /* parse the predicate(s) */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7858 | LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL, |
| 7859 | LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7860 | |
| 7861 | /* compile */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7862 | rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2, |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 7863 | &pred_idx, LY_VALUE_JSON, NULL, predicates); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7864 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7865 | |
| 7866 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7867 | *tok_idx = e_idx; |
| 7868 | |
| 7869 | cleanup: |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7870 | ly_temp_log_options(NULL); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7871 | lyxp_expr_free(set->ctx, exp2); |
| 7872 | free(pred); |
| 7873 | return rc; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7874 | } |
| 7875 | |
| 7876 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7877 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7878 | * data node(s) could be found using a single hash-based search. |
| 7879 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7880 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7881 | * @param[in] node Next context node to check. |
| 7882 | * @param[in] name Expected node name. |
| 7883 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7884 | * @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] | 7885 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7886 | * @param[in] format Prefix format. |
| 7887 | * @param[in,out] found Previously found node, is updated. |
| 7888 | * @return LY_SUCCESS on success, |
| 7889 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7890 | */ |
| 7891 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7892 | eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7893 | uint32_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7894 | const struct lysc_node **found) |
| 7895 | { |
| 7896 | const struct lysc_node *scnode; |
| 7897 | const struct lys_module *mod; |
| 7898 | uint32_t idx = 0; |
| 7899 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7900 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7901 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7902 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7903 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7904 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7905 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7906 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7907 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7908 | if (!mod->implemented) { |
| 7909 | continue; |
| 7910 | } |
| 7911 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7912 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7913 | if (scnode) { |
| 7914 | /* we have found a match */ |
| 7915 | break; |
| 7916 | } |
| 7917 | } |
| 7918 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7919 | if (!scnode) { |
| 7920 | /* all modules searched */ |
| 7921 | idx = 0; |
| 7922 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7923 | } else { |
| 7924 | /* search in top-level */ |
| 7925 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7926 | } |
| 7927 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7928 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7929 | /* we must adjust the module to inherit the one from the context node */ |
| 7930 | moveto_mod = node->schema->module; |
| 7931 | } |
| 7932 | |
| 7933 | /* search in children, do not repeat the same search */ |
| 7934 | 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] | 7935 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7936 | |
| 7937 | /* additional context check */ |
| 7938 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7939 | scnode = NULL; |
| 7940 | } |
| 7941 | |
| 7942 | if (scnode) { |
| 7943 | if (*found) { |
| 7944 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7945 | * (more hash-based searches would be required, not supported) */ |
| 7946 | return LY_ENOT; |
| 7947 | } else { |
| 7948 | /* remember the found schema node and continue to make sure it can be used */ |
| 7949 | *found = scnode; |
| 7950 | } |
| 7951 | } |
| 7952 | |
| 7953 | if (idx) { |
| 7954 | /* continue searching all the following models */ |
| 7955 | goto continue_search; |
| 7956 | } |
| 7957 | |
| 7958 | return LY_SUCCESS; |
| 7959 | } |
| 7960 | |
| 7961 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7962 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7963 | * |
| 7964 | * @param[in] set XPath set. |
| 7965 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7966 | * @param[in] ncname XPath NCName being evaluated. |
| 7967 | * @param[in] ncname_len Length of @p ncname. |
| 7968 | * @param[in] expr Whole XPath expression. |
| 7969 | * @param[in] options XPath options. |
| 7970 | */ |
| 7971 | static void |
| 7972 | eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7973 | uint32_t ncname_len, const char *expr, uint32_t options) |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7974 | { |
| 7975 | const char *format; |
| 7976 | char *path = NULL, *ppath = NULL; |
| 7977 | |
| 7978 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7979 | if (scparent) { |
| 7980 | /* generate path for the parent */ |
| 7981 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7982 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7983 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7984 | ppath = strdup("<root>"); |
| 7985 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7986 | ppath = strdup("<config-root>"); |
| 7987 | } |
| 7988 | } |
| 7989 | if (ppath) { |
| 7990 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7991 | if (options & LYXP_SCNODE_ERROR) { |
| 7992 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7993 | } else { |
| 7994 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7995 | } |
| 7996 | } else { |
| 7997 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7998 | if (options & LYXP_SCNODE_ERROR) { |
| 7999 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 8000 | } else { |
| 8001 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 8002 | } |
| 8003 | } |
| 8004 | free(path); |
| 8005 | free(ppath); |
| 8006 | } |
| 8007 | |
| 8008 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8009 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 8010 | * |
| 8011 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8012 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8013 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 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 | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8017 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8018 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8019 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8020 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8021 | * @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] | 8022 | */ |
| 8023 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8024 | eval_name_test_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8025 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8026 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8027 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8028 | const char *ncname, *ncname_dict = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8029 | uint32_t i, ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8030 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8031 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8032 | struct ly_path_predicate *predicates = NULL; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8033 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8034 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8035 | 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] | 8036 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8037 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8038 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8039 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8040 | goto moveto; |
| 8041 | } |
| 8042 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8043 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 8044 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8045 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8046 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8047 | /* all nodes will match */ |
| 8048 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8049 | } |
| 8050 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8051 | /* parse (and skip) module name */ |
| 8052 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8053 | LY_CHECK_GOTO(rc, cleanup); |
| 8054 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8055 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8056 | /* all nodes from the module will match */ |
| 8057 | goto moveto; |
| 8058 | } |
| 8059 | |
| 8060 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 8061 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8062 | /* find the matching schema node in some parent in the context */ |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8063 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8064 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 8065 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8066 | /* check failed */ |
| 8067 | scnode = NULL; |
| 8068 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8069 | } |
| 8070 | } |
| 8071 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8072 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 8073 | /* try to create the predicates */ |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8074 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8075 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8076 | scnode = NULL; |
| 8077 | } |
| 8078 | } |
| 8079 | } |
| 8080 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8081 | if (!scnode) { |
| 8082 | /* 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] | 8083 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 8084 | 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] | 8085 | } |
| 8086 | |
| 8087 | moveto: |
| 8088 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8089 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8090 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8091 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8092 | } else { |
| 8093 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 8094 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8095 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8096 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8097 | } |
| 8098 | LY_CHECK_GOTO(rc, cleanup); |
| 8099 | } |
| 8100 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8101 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8102 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8103 | ly_bool found = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8104 | |
| 8105 | /* remember parent if there is only one, to print in the warning */ |
| 8106 | for (i = 0; i < set->used; ++i) { |
| 8107 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 8108 | if (!scparent) { |
| 8109 | /* remember the context node */ |
| 8110 | scparent = &set->val.scnodes[i]; |
| 8111 | } else { |
| 8112 | /* several context nodes, no reasonable error possible */ |
| 8113 | scparent = NULL; |
| 8114 | break; |
| 8115 | } |
| 8116 | } |
| 8117 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8118 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8119 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8120 | /* efficient evaluation that does not add all the descendants into the set */ |
| 8121 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8122 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8123 | if (all_desc) { |
| 8124 | /* "//" == "/descendant-or-self::node()/" */ |
| 8125 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8126 | LY_CHECK_GOTO(rc, cleanup); |
| 8127 | } |
| 8128 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8129 | } |
| 8130 | LY_CHECK_GOTO(rc, cleanup); |
| 8131 | |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8132 | i = set->used; |
| 8133 | do { |
| 8134 | --i; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8135 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8136 | found = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8137 | break; |
| 8138 | } |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8139 | } while (i); |
| 8140 | if (!found) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8141 | /* generate message */ |
| 8142 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 8143 | |
| 8144 | if (options & LYXP_SCNODE_ERROR) { |
| 8145 | /* error */ |
| 8146 | rc = LY_EVALID; |
| 8147 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8148 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8149 | |
| 8150 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 8151 | rc = LY_ENOT; |
| 8152 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8153 | } |
| 8154 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8155 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8156 | /* efficient evaluation */ |
| 8157 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 8158 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 8159 | /* we can find the child nodes using hashes */ |
| 8160 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8161 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8162 | if (all_desc) { |
| 8163 | /* "//" == "/descendant-or-self::node()/" */ |
| 8164 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8165 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8166 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8167 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8168 | } |
| 8169 | LY_CHECK_GOTO(rc, cleanup); |
| 8170 | } |
| 8171 | } |
| 8172 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8173 | if (scnode_skip_pred) { |
| 8174 | /* skip predicates */ |
| 8175 | options |= LYXP_SKIP_EXPR; |
| 8176 | } |
| 8177 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8178 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8179 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8180 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8181 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8182 | } |
| 8183 | |
| 8184 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8185 | if (scnode_skip_pred) { |
| 8186 | /* restore options */ |
| 8187 | options &= ~LYXP_SKIP_EXPR; |
| 8188 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8189 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8190 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8191 | ly_path_predicates_free(set->ctx, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 8192 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8193 | return rc; |
| 8194 | } |
| 8195 | |
| 8196 | /** |
| 8197 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 8198 | * |
| 8199 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8200 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8201 | * [8] NodeType ::= 'text' | 'node' |
| 8202 | * |
| 8203 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8204 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8205 | * @param[in] axis Axis to search on. |
| 8206 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8207 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8208 | * @param[in] options XPath options. |
| 8209 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8210 | */ |
| 8211 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8212 | eval_node_type_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8213 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8214 | { |
| 8215 | LY_ERR rc; |
| 8216 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8217 | (void)all_desc; |
| 8218 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8219 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8220 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8221 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 8222 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8223 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8224 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 8225 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8226 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8227 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8228 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8229 | 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] | 8230 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8231 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8232 | |
| 8233 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8234 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8235 | 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] | 8236 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8237 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8238 | |
| 8239 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8240 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8241 | 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] | 8242 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8243 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8244 | |
| 8245 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8246 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8247 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8248 | LY_CHECK_RET(rc); |
| 8249 | } |
| 8250 | |
| 8251 | return LY_SUCCESS; |
| 8252 | } |
| 8253 | |
| 8254 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8255 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8256 | * |
| 8257 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8258 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8259 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8260 | * |
| 8261 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8262 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8264 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8265 | * @param[in] options XPath options. |
| 8266 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8267 | */ |
| 8268 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8269 | eval_relative_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8270 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8271 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8272 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8273 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8274 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8275 | |
| 8276 | goto step; |
| 8277 | do { |
| 8278 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8279 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8280 | all_desc = 0; |
| 8281 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8282 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8283 | all_desc = 1; |
| 8284 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8285 | 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] | 8286 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8287 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8288 | |
| 8289 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8290 | /* AxisSpecifier */ |
| 8291 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8292 | 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] | 8293 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8294 | 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] | 8295 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8296 | ++(*tok_idx); |
| 8297 | |
| 8298 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8299 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8300 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8301 | ++(*tok_idx); |
| 8302 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8303 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8304 | |
| 8305 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8306 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8307 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8308 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8309 | /* default */ |
| 8310 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8311 | } |
| 8312 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8313 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8314 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8315 | case LYXP_TOKEN_DOT: |
| 8316 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8317 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8318 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8319 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8320 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8321 | rc = LY_EVALID; |
| 8322 | goto cleanup; |
| 8323 | } |
| 8324 | |
| 8325 | if (all_desc) { |
| 8326 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8327 | LY_CHECK_GOTO(rc, cleanup); |
| 8328 | } |
| 8329 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8330 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8331 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8332 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8333 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8334 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8335 | break; |
| 8336 | |
| 8337 | case LYXP_TOKEN_DDOT: |
| 8338 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8339 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8340 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8341 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8342 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8343 | rc = LY_EVALID; |
| 8344 | goto cleanup; |
| 8345 | } |
| 8346 | |
| 8347 | if (all_desc) { |
| 8348 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8349 | LY_CHECK_GOTO(rc, cleanup); |
| 8350 | } |
| 8351 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8352 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8353 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8354 | 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] | 8355 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8356 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8357 | break; |
| 8358 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8359 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8360 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8361 | 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] | 8362 | if (rc == LY_ENOT) { |
| 8363 | assert(options & LYXP_SCNODE_ALL); |
| 8364 | /* skip the rest of this path */ |
| 8365 | rc = LY_SUCCESS; |
| 8366 | scnode_skip_path = 1; |
| 8367 | options |= LYXP_SKIP_EXPR; |
| 8368 | } |
| 8369 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8370 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8371 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8372 | case LYXP_TOKEN_NODETYPE: |
| 8373 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8374 | 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] | 8375 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8376 | break; |
| 8377 | |
| 8378 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8379 | LOGINT(set->ctx); |
| 8380 | rc = LY_EINT; |
| 8381 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8382 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8383 | } 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] | 8384 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8385 | cleanup: |
| 8386 | if (scnode_skip_path) { |
| 8387 | options &= ~LYXP_SKIP_EXPR; |
| 8388 | } |
| 8389 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8390 | } |
| 8391 | |
| 8392 | /** |
| 8393 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8394 | * |
| 8395 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8396 | * |
| 8397 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8398 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8399 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8400 | * @param[in] options XPath options. |
| 8401 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8402 | */ |
| 8403 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8404 | eval_absolute_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8405 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8406 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8407 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8408 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8409 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8410 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8411 | } |
| 8412 | |
| 8413 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8414 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8415 | /* evaluate '/' - deferred */ |
| 8416 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8417 | 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] | 8418 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8419 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8420 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8421 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8422 | return LY_SUCCESS; |
| 8423 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8424 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8425 | case LYXP_TOKEN_DOT: |
| 8426 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8427 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8428 | case LYXP_TOKEN_AT: |
| 8429 | case LYXP_TOKEN_NAMETEST: |
| 8430 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8431 | 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] | 8432 | break; |
| 8433 | default: |
| 8434 | break; |
| 8435 | } |
| 8436 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8437 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8438 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8439 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8440 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8441 | 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] | 8442 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8443 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8444 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8445 | 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] | 8446 | } |
| 8447 | |
| 8448 | return LY_SUCCESS; |
| 8449 | } |
| 8450 | |
| 8451 | /** |
| 8452 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8453 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8454 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8455 | * |
| 8456 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8457 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8458 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8459 | * @param[in] options XPath options. |
| 8460 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8461 | */ |
| 8462 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8463 | eval_function_call(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8464 | { |
| 8465 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8466 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8467 | LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL; |
| 8468 | uint32_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8469 | struct lyxp_set **args = NULL, **args_aux; |
| 8470 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8471 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8472 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8473 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8474 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8475 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8476 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8477 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8478 | xpath_func = &xpath_sum; |
| 8479 | } |
| 8480 | break; |
| 8481 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8482 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8483 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8484 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8485 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8486 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8487 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8488 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8489 | xpath_func = &xpath_true; |
| 8490 | } |
| 8491 | break; |
| 8492 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8493 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8494 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8495 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8496 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8497 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8498 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8499 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8500 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8501 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8502 | xpath_func = &xpath_deref; |
| 8503 | } |
| 8504 | break; |
| 8505 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8506 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8507 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8508 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8509 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8510 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8511 | xpath_func = &xpath_string; |
| 8512 | } |
| 8513 | break; |
| 8514 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8515 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8516 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8517 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8518 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8519 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8520 | xpath_func = &xpath_current; |
| 8521 | } |
| 8522 | break; |
| 8523 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8524 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8525 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8526 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8527 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8528 | } 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] | 8529 | xpath_func = &xpath_re_match; |
| 8530 | } |
| 8531 | break; |
| 8532 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8533 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8534 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8535 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8536 | xpath_func = &xpath_translate; |
| 8537 | } |
| 8538 | break; |
| 8539 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8540 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8541 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8542 | } 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] | 8543 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8544 | } 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] | 8545 | xpath_func = &xpath_bit_is_set; |
| 8546 | } |
| 8547 | break; |
| 8548 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8549 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8550 | xpath_func = &xpath_starts_with; |
| 8551 | } |
| 8552 | break; |
| 8553 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8554 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8555 | xpath_func = &xpath_derived_from; |
| 8556 | } |
| 8557 | break; |
| 8558 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8559 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8560 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8561 | } 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] | 8562 | xpath_func = &xpath_string_length; |
| 8563 | } |
| 8564 | break; |
| 8565 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8566 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8567 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8568 | } 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] | 8569 | xpath_func = &xpath_substring_after; |
| 8570 | } |
| 8571 | break; |
| 8572 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8573 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8574 | xpath_func = &xpath_substring_before; |
| 8575 | } |
| 8576 | break; |
| 8577 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8578 | 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] | 8579 | xpath_func = &xpath_derived_from_or_self; |
| 8580 | } |
| 8581 | break; |
| 8582 | } |
| 8583 | |
| 8584 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8585 | 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] | 8586 | return LY_EVALID; |
| 8587 | } |
| 8588 | } |
| 8589 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8590 | 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] | 8591 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8592 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8593 | |
| 8594 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8595 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8596 | 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] | 8597 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8598 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8599 | |
| 8600 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8601 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8602 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8603 | args = malloc(sizeof *args); |
| 8604 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8605 | arg_count = 1; |
| 8606 | args[0] = set_copy(set); |
| 8607 | if (!args[0]) { |
| 8608 | rc = LY_EMEM; |
| 8609 | goto cleanup; |
| 8610 | } |
| 8611 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8612 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8613 | LY_CHECK_GOTO(rc, cleanup); |
| 8614 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8615 | 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] | 8616 | LY_CHECK_GOTO(rc, cleanup); |
| 8617 | } |
| 8618 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8619 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8620 | 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] | 8621 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8622 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8623 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8624 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8625 | ++arg_count; |
| 8626 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8627 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8628 | args = args_aux; |
| 8629 | args[arg_count - 1] = set_copy(set); |
| 8630 | if (!args[arg_count - 1]) { |
| 8631 | rc = LY_EMEM; |
| 8632 | goto cleanup; |
| 8633 | } |
| 8634 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8635 | 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] | 8636 | LY_CHECK_GOTO(rc, cleanup); |
| 8637 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8638 | 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] | 8639 | LY_CHECK_GOTO(rc, cleanup); |
| 8640 | } |
| 8641 | } |
| 8642 | |
| 8643 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8644 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8645 | 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] | 8646 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8647 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8648 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8649 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8650 | /* evaluate function */ |
| 8651 | rc = xpath_func(args, arg_count, set, options); |
| 8652 | |
| 8653 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8654 | /* merge all nodes from arg evaluations */ |
| 8655 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8656 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8657 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8658 | } |
| 8659 | } |
| 8660 | } else { |
| 8661 | rc = LY_SUCCESS; |
| 8662 | } |
| 8663 | |
| 8664 | cleanup: |
| 8665 | for (i = 0; i < arg_count; ++i) { |
| 8666 | lyxp_set_free(args[i]); |
| 8667 | } |
| 8668 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8669 | return rc; |
| 8670 | } |
| 8671 | |
| 8672 | /** |
| 8673 | * @brief Evaluate Number. Logs directly on error. |
| 8674 | * |
| 8675 | * @param[in] ctx Context for errors. |
| 8676 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8677 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8678 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8679 | * @return LY_ERR |
| 8680 | */ |
| 8681 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8682 | eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8683 | { |
| 8684 | long double num; |
| 8685 | char *endptr; |
| 8686 | |
| 8687 | if (set) { |
| 8688 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8689 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8690 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8691 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8692 | 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] | 8693 | 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] | 8694 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8695 | } 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] | 8696 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8697 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8698 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8699 | return LY_EVALID; |
| 8700 | } |
| 8701 | |
| 8702 | set_fill_number(set, num); |
| 8703 | } |
| 8704 | |
| 8705 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8706 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8707 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8708 | return LY_SUCCESS; |
| 8709 | } |
| 8710 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8711 | LY_ERR |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8712 | lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len, |
| 8713 | struct lyxp_var **var) |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8714 | { |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8715 | LY_ARRAY_COUNT_TYPE u; |
| 8716 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8717 | assert(name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8718 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8719 | if (!name_len) { |
| 8720 | name_len = strlen(name); |
| 8721 | } |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8722 | |
| 8723 | LY_ARRAY_FOR(vars, u) { |
| 8724 | if (!strncmp(vars[u].name, name, name_len)) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8725 | if (var) { |
| 8726 | *var = (struct lyxp_var *)&vars[u]; |
| 8727 | } |
| 8728 | return LY_SUCCESS; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8729 | } |
| 8730 | } |
| 8731 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8732 | if (ctx) { |
| 8733 | LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8734 | } |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8735 | return LY_ENOTFOUND; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8736 | } |
| 8737 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8738 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8739 | * @brief Evaluate VariableReference. |
| 8740 | * |
| 8741 | * @param[in] exp Parsed XPath expression. |
| 8742 | * @param[in] tok_idx Position in the expression @p exp. |
| 8743 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8744 | * @param[in,out] set Context and result set. |
| 8745 | * @param[in] options XPath options. |
| 8746 | * @return LY_ERR value. |
| 8747 | */ |
| 8748 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8749 | eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8750 | { |
| 8751 | LY_ERR ret; |
| 8752 | const char *name; |
| 8753 | struct lyxp_var *var; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8754 | struct lyxp_expr *tokens = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8755 | uint32_t token_index, name_len; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8756 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8757 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8758 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
Michal Vasko | e68b540 | 2022-07-29 14:58:15 +0200 | [diff] [blame] | 8759 | name_len = exp->tok_len[*tok_idx]; |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8760 | ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var); |
| 8761 | LY_CHECK_RET(ret); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8762 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8763 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8764 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8765 | LY_CHECK_GOTO(ret, cleanup); |
| 8766 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8767 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8768 | token_index = 0; |
| 8769 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8770 | LY_CHECK_GOTO(ret, cleanup); |
| 8771 | |
| 8772 | cleanup: |
| 8773 | lyxp_expr_free(set->ctx, tokens); |
| 8774 | |
| 8775 | return ret; |
| 8776 | } |
| 8777 | |
| 8778 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8779 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8780 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8781 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8782 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8783 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8784 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8785 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8786 | * |
| 8787 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8788 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8789 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8790 | * @param[in] options XPath options. |
| 8791 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8792 | */ |
| 8793 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8794 | eval_path_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8795 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8796 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8797 | LY_ERR rc; |
| 8798 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8799 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8800 | case LYXP_TOKEN_PAR1: |
| 8801 | /* '(' Expr ')' */ |
| 8802 | |
| 8803 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8804 | 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] | 8805 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8806 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8807 | |
| 8808 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8809 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8810 | LY_CHECK_RET(rc); |
| 8811 | |
| 8812 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8813 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
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 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8818 | goto predicate; |
| 8819 | |
| 8820 | case LYXP_TOKEN_DOT: |
| 8821 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8822 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8823 | case LYXP_TOKEN_AT: |
| 8824 | case LYXP_TOKEN_NAMETEST: |
| 8825 | case LYXP_TOKEN_NODETYPE: |
| 8826 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8827 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8828 | LY_CHECK_RET(rc); |
| 8829 | break; |
| 8830 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8831 | case LYXP_TOKEN_VARREF: |
| 8832 | /* VariableReference */ |
| 8833 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8834 | LY_CHECK_RET(rc); |
| 8835 | ++(*tok_idx); |
| 8836 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8837 | goto predicate; |
| 8838 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8839 | case LYXP_TOKEN_FUNCNAME: |
| 8840 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8841 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8842 | LY_CHECK_RET(rc); |
| 8843 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8844 | goto predicate; |
| 8845 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8846 | case LYXP_TOKEN_OPER_PATH: |
| 8847 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8848 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8849 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8850 | LY_CHECK_RET(rc); |
| 8851 | break; |
| 8852 | |
| 8853 | case LYXP_TOKEN_LITERAL: |
| 8854 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8855 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8856 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8857 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8858 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8859 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8860 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8861 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8862 | } |
| 8863 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8864 | goto predicate; |
| 8865 | |
| 8866 | case LYXP_TOKEN_NUMBER: |
| 8867 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8868 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8869 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8870 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8871 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8872 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8873 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8874 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8875 | } |
| 8876 | LY_CHECK_RET(rc); |
| 8877 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8878 | goto predicate; |
| 8879 | |
| 8880 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8881 | 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] | 8882 | return LY_EVALID; |
| 8883 | } |
| 8884 | |
| 8885 | return LY_SUCCESS; |
| 8886 | |
| 8887 | predicate: |
| 8888 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8889 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8890 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8891 | LY_CHECK_RET(rc); |
| 8892 | } |
| 8893 | |
| 8894 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8895 | 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] | 8896 | |
| 8897 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8898 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8899 | all_desc = 0; |
| 8900 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8901 | all_desc = 1; |
| 8902 | } |
| 8903 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8904 | 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] | 8905 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8906 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8907 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8908 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8909 | LY_CHECK_RET(rc); |
| 8910 | } |
| 8911 | |
| 8912 | return LY_SUCCESS; |
| 8913 | } |
| 8914 | |
| 8915 | /** |
| 8916 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8917 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8918 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8919 | * |
| 8920 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8921 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8922 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8923 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8924 | * @param[in] options XPath options. |
| 8925 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8926 | */ |
| 8927 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8928 | eval_union_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8929 | { |
| 8930 | LY_ERR rc = LY_SUCCESS; |
| 8931 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8932 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8933 | |
| 8934 | assert(repeat); |
| 8935 | |
| 8936 | set_init(&orig_set, set); |
| 8937 | set_init(&set2, set); |
| 8938 | |
| 8939 | set_fill_set(&orig_set, set); |
| 8940 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8941 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8942 | LY_CHECK_GOTO(rc, cleanup); |
| 8943 | |
| 8944 | /* ('|' PathExpr)* */ |
| 8945 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8946 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8947 | 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] | 8948 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8949 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8950 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8951 | if (options & LYXP_SKIP_EXPR) { |
| 8952 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8953 | LY_CHECK_GOTO(rc, cleanup); |
| 8954 | continue; |
| 8955 | } |
| 8956 | |
| 8957 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8958 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8959 | LY_CHECK_GOTO(rc, cleanup); |
| 8960 | |
| 8961 | /* eval */ |
| 8962 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8963 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8964 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8965 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8966 | LY_CHECK_GOTO(rc, cleanup); |
| 8967 | } |
| 8968 | } |
| 8969 | |
| 8970 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8971 | lyxp_set_free_content(&orig_set); |
| 8972 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8973 | return rc; |
| 8974 | } |
| 8975 | |
| 8976 | /** |
| 8977 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8978 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8979 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8980 | * |
| 8981 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8982 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8983 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8984 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8985 | * @param[in] options XPath options. |
| 8986 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8987 | */ |
| 8988 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8989 | eval_unary_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8990 | { |
| 8991 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8992 | uint32_t this_op, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8993 | |
| 8994 | assert(repeat); |
| 8995 | |
| 8996 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8997 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8998 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8999 | 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] | 9000 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9001 | 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] | 9002 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9003 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9004 | } |
| 9005 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9006 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9007 | LY_CHECK_RET(rc); |
| 9008 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9009 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9010 | if (options & LYXP_SCNODE_ALL) { |
| 9011 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 9012 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9013 | 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] | 9014 | LY_CHECK_RET(rc); |
| 9015 | } |
| 9016 | } |
| 9017 | |
| 9018 | return LY_SUCCESS; |
| 9019 | } |
| 9020 | |
| 9021 | /** |
| 9022 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 9023 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9024 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9025 | * | MultiplicativeExpr '*' UnaryExpr |
| 9026 | * | MultiplicativeExpr 'div' UnaryExpr |
| 9027 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 9028 | * |
| 9029 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9030 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9031 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9032 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9033 | * @param[in] options XPath options. |
| 9034 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9035 | */ |
| 9036 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9037 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9038 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9039 | { |
| 9040 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9041 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9042 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9043 | |
| 9044 | assert(repeat); |
| 9045 | |
| 9046 | set_init(&orig_set, set); |
| 9047 | set_init(&set2, set); |
| 9048 | |
| 9049 | set_fill_set(&orig_set, set); |
| 9050 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9051 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9052 | LY_CHECK_GOTO(rc, cleanup); |
| 9053 | |
| 9054 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 9055 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9056 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9057 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9058 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9059 | 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] | 9060 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9061 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9062 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9063 | if (options & LYXP_SKIP_EXPR) { |
| 9064 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9065 | LY_CHECK_GOTO(rc, cleanup); |
| 9066 | continue; |
| 9067 | } |
| 9068 | |
| 9069 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9070 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9071 | LY_CHECK_GOTO(rc, cleanup); |
| 9072 | |
| 9073 | /* eval */ |
| 9074 | if (options & LYXP_SCNODE_ALL) { |
| 9075 | 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] | 9076 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9077 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9078 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9079 | 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] | 9080 | LY_CHECK_GOTO(rc, cleanup); |
| 9081 | } |
| 9082 | } |
| 9083 | |
| 9084 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9085 | lyxp_set_free_content(&orig_set); |
| 9086 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9087 | return rc; |
| 9088 | } |
| 9089 | |
| 9090 | /** |
| 9091 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 9092 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9093 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9094 | * | AdditiveExpr '+' MultiplicativeExpr |
| 9095 | * | AdditiveExpr '-' MultiplicativeExpr |
| 9096 | * |
| 9097 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9098 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9099 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9100 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9101 | * @param[in] options XPath options. |
| 9102 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9103 | */ |
| 9104 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9105 | eval_additive_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9106 | { |
| 9107 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9108 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9109 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9110 | |
| 9111 | assert(repeat); |
| 9112 | |
| 9113 | set_init(&orig_set, set); |
| 9114 | set_init(&set2, set); |
| 9115 | |
| 9116 | set_fill_set(&orig_set, set); |
| 9117 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9118 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9119 | LY_CHECK_GOTO(rc, cleanup); |
| 9120 | |
| 9121 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 9122 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9123 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9124 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9125 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9126 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9127 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9128 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9129 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9130 | if (options & LYXP_SKIP_EXPR) { |
| 9131 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9132 | LY_CHECK_GOTO(rc, cleanup); |
| 9133 | continue; |
| 9134 | } |
| 9135 | |
| 9136 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9137 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9138 | LY_CHECK_GOTO(rc, cleanup); |
| 9139 | |
| 9140 | /* eval */ |
| 9141 | if (options & LYXP_SCNODE_ALL) { |
| 9142 | 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] | 9143 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9144 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9145 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9146 | 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] | 9147 | LY_CHECK_GOTO(rc, cleanup); |
| 9148 | } |
| 9149 | } |
| 9150 | |
| 9151 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9152 | lyxp_set_free_content(&orig_set); |
| 9153 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9154 | return rc; |
| 9155 | } |
| 9156 | |
| 9157 | /** |
| 9158 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 9159 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9160 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9161 | * | RelationalExpr '<' AdditiveExpr |
| 9162 | * | RelationalExpr '>' AdditiveExpr |
| 9163 | * | RelationalExpr '<=' AdditiveExpr |
| 9164 | * | RelationalExpr '>=' AdditiveExpr |
| 9165 | * |
| 9166 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9167 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9168 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9169 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9170 | * @param[in] options XPath options. |
| 9171 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9172 | */ |
| 9173 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9174 | eval_relational_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9175 | { |
| 9176 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9177 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9178 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9179 | |
| 9180 | assert(repeat); |
| 9181 | |
| 9182 | set_init(&orig_set, set); |
| 9183 | set_init(&set2, set); |
| 9184 | |
| 9185 | set_fill_set(&orig_set, set); |
| 9186 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9187 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9188 | LY_CHECK_GOTO(rc, cleanup); |
| 9189 | |
| 9190 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 9191 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9192 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9193 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9194 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9195 | 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] | 9196 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9197 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9198 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9199 | if (options & LYXP_SKIP_EXPR) { |
| 9200 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9201 | LY_CHECK_GOTO(rc, cleanup); |
| 9202 | continue; |
| 9203 | } |
| 9204 | |
| 9205 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9206 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9207 | LY_CHECK_GOTO(rc, cleanup); |
| 9208 | |
| 9209 | /* eval */ |
| 9210 | if (options & LYXP_SCNODE_ALL) { |
| 9211 | 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] | 9212 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9213 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9214 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9215 | ly_bool result; |
| 9216 | |
| 9217 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9218 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9219 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9220 | } |
| 9221 | } |
| 9222 | |
| 9223 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9224 | lyxp_set_free_content(&orig_set); |
| 9225 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9226 | return rc; |
| 9227 | } |
| 9228 | |
| 9229 | /** |
| 9230 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 9231 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9232 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9233 | * | EqualityExpr '!=' RelationalExpr |
| 9234 | * |
| 9235 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9236 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9237 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9238 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9239 | * @param[in] options XPath options. |
| 9240 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9241 | */ |
| 9242 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9243 | eval_equality_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9244 | { |
| 9245 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9246 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9247 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9248 | |
| 9249 | assert(repeat); |
| 9250 | |
| 9251 | set_init(&orig_set, set); |
| 9252 | set_init(&set2, set); |
| 9253 | |
| 9254 | set_fill_set(&orig_set, set); |
| 9255 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9256 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9257 | LY_CHECK_GOTO(rc, cleanup); |
| 9258 | |
| 9259 | /* ('=' / '!=' RelationalExpr)* */ |
| 9260 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9261 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9262 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9263 | 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] | 9264 | 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] | 9265 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9266 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9267 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9268 | if (options & LYXP_SKIP_EXPR) { |
| 9269 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9270 | LY_CHECK_GOTO(rc, cleanup); |
| 9271 | continue; |
| 9272 | } |
| 9273 | |
| 9274 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9275 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9276 | LY_CHECK_GOTO(rc, cleanup); |
| 9277 | |
| 9278 | /* eval */ |
| 9279 | if (options & LYXP_SCNODE_ALL) { |
| 9280 | 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] | 9281 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9282 | 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] | 9283 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9284 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9285 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9286 | ly_bool result; |
| 9287 | |
| 9288 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9289 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9290 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9291 | } |
| 9292 | } |
| 9293 | |
| 9294 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9295 | lyxp_set_free_content(&orig_set); |
| 9296 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9297 | return rc; |
| 9298 | } |
| 9299 | |
| 9300 | /** |
| 9301 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9302 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9303 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9304 | * |
| 9305 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9306 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9307 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9308 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9309 | * @param[in] options XPath options. |
| 9310 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9311 | */ |
| 9312 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9313 | eval_and_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9314 | { |
| 9315 | LY_ERR rc; |
| 9316 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9317 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9318 | |
| 9319 | assert(repeat); |
| 9320 | |
| 9321 | set_init(&orig_set, set); |
| 9322 | set_init(&set2, set); |
| 9323 | |
| 9324 | set_fill_set(&orig_set, set); |
| 9325 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9326 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9327 | LY_CHECK_GOTO(rc, cleanup); |
| 9328 | |
Michal Vasko | 3ff82dd | 2023-05-09 14:09:21 +0200 | [diff] [blame] | 9329 | if (!(options & LYXP_SKIP_EXPR)) { |
| 9330 | if (options & LYXP_SCNODE_ALL) { |
| 9331 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
| 9332 | } else { |
| 9333 | /* cast to boolean, we know that will be the final result */ |
| 9334 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
| 9335 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9336 | } |
| 9337 | |
| 9338 | /* ('and' EqualityExpr)* */ |
| 9339 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9340 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9341 | 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] | 9342 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9343 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9344 | |
| 9345 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9346 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9347 | 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] | 9348 | LY_CHECK_GOTO(rc, cleanup); |
| 9349 | continue; |
| 9350 | } |
| 9351 | |
| 9352 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9353 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9354 | LY_CHECK_GOTO(rc, cleanup); |
| 9355 | |
| 9356 | /* eval - just get boolean value actually */ |
| 9357 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9358 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9359 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9360 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9361 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9362 | set_fill_set(set, &set2); |
| 9363 | } |
| 9364 | } |
| 9365 | |
| 9366 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9367 | lyxp_set_free_content(&orig_set); |
| 9368 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9369 | return rc; |
| 9370 | } |
| 9371 | |
| 9372 | /** |
| 9373 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9374 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9375 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9376 | * |
| 9377 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9378 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9379 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9380 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9381 | * @param[in] options XPath options. |
| 9382 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9383 | */ |
| 9384 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9385 | eval_or_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9386 | { |
| 9387 | LY_ERR rc; |
| 9388 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9389 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9390 | |
| 9391 | assert(repeat); |
| 9392 | |
| 9393 | set_init(&orig_set, set); |
| 9394 | set_init(&set2, set); |
| 9395 | |
| 9396 | set_fill_set(&orig_set, set); |
| 9397 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9398 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9399 | LY_CHECK_GOTO(rc, cleanup); |
| 9400 | |
Michal Vasko | 3ff82dd | 2023-05-09 14:09:21 +0200 | [diff] [blame] | 9401 | if (!(options & LYXP_SKIP_EXPR)) { |
| 9402 | if (options & LYXP_SCNODE_ALL) { |
| 9403 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
| 9404 | } else { |
| 9405 | /* cast to boolean, we know that will be the final result */ |
| 9406 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
| 9407 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9408 | } |
| 9409 | |
| 9410 | /* ('or' AndExpr)* */ |
| 9411 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9412 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9413 | 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] | 9414 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9415 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9416 | |
| 9417 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9418 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9419 | 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] | 9420 | LY_CHECK_GOTO(rc, cleanup); |
| 9421 | continue; |
| 9422 | } |
| 9423 | |
| 9424 | set_fill_set(&set2, &orig_set); |
| 9425 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9426 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9427 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9428 | LY_CHECK_GOTO(rc, cleanup); |
| 9429 | |
| 9430 | /* eval - just get boolean value actually */ |
| 9431 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9432 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9433 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9434 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9435 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9436 | set_fill_set(set, &set2); |
| 9437 | } |
| 9438 | } |
| 9439 | |
| 9440 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9441 | lyxp_set_free_content(&orig_set); |
| 9442 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9443 | return rc; |
| 9444 | } |
| 9445 | |
| 9446 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9447 | * @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] | 9448 | * |
| 9449 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9450 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9451 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9452 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9453 | * @param[in] options XPath options. |
| 9454 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9455 | */ |
| 9456 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9457 | eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9458 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9459 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9460 | uint32_t i, count; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9461 | enum lyxp_expr_type next_etype; |
| 9462 | LY_ERR rc; |
| 9463 | |
| 9464 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9465 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9466 | next_etype = LYXP_EXPR_NONE; |
| 9467 | } else { |
| 9468 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9469 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9470 | |
| 9471 | /* select one-priority lower because etype expression called us */ |
| 9472 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9473 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9474 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9475 | 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] | 9476 | } else { |
| 9477 | next_etype = LYXP_EXPR_NONE; |
| 9478 | } |
| 9479 | } |
| 9480 | |
| 9481 | /* decide what expression are we parsing based on the repeat */ |
| 9482 | switch (next_etype) { |
| 9483 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9484 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9485 | break; |
| 9486 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9487 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9488 | break; |
| 9489 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9490 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9491 | break; |
| 9492 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9493 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9494 | break; |
| 9495 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9496 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9497 | break; |
| 9498 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9499 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9500 | break; |
| 9501 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9502 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9503 | break; |
| 9504 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9505 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9506 | break; |
| 9507 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9508 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9509 | break; |
| 9510 | default: |
| 9511 | LOGINT_RET(set->ctx); |
| 9512 | } |
| 9513 | |
| 9514 | return rc; |
| 9515 | } |
| 9516 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9517 | /** |
| 9518 | * @brief Get root type. |
| 9519 | * |
| 9520 | * @param[in] ctx_node Context node. |
| 9521 | * @param[in] ctx_scnode Schema context node. |
| 9522 | * @param[in] options XPath options. |
| 9523 | * @return Root type. |
| 9524 | */ |
| 9525 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9526 | 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] | 9527 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9528 | const struct lysc_node *op; |
| 9529 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9530 | /* explicit */ |
| 9531 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9532 | return LYXP_NODE_ROOT; |
| 9533 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9534 | return LYXP_NODE_ROOT_CONFIG; |
| 9535 | } |
| 9536 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9537 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9538 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9539 | 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] | 9540 | |
| 9541 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9542 | /* general root that can access everything */ |
| 9543 | return LYXP_NODE_ROOT; |
| 9544 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9545 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9546 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9547 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9548 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9549 | } |
| 9550 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9551 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9552 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9553 | 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] | 9554 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9555 | if (op || !(options & LYXP_SCHEMA)) { |
| 9556 | /* general root that can access everything */ |
| 9557 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9558 | } 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] | 9559 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9560 | return LYXP_NODE_ROOT_CONFIG; |
| 9561 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9562 | return LYXP_NODE_ROOT; |
| 9563 | } |
| 9564 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9565 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9566 | lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9567 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node, |
| 9568 | const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9569 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9570 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9571 | LY_ERR rc; |
| 9572 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9573 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9574 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9575 | 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] | 9576 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9577 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9578 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9579 | if (tree) { |
| 9580 | /* adjust the pointer to be the first top-level sibling */ |
| 9581 | while (tree->parent) { |
| 9582 | tree = lyd_parent(tree); |
| 9583 | } |
| 9584 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9585 | |
Michal Vasko | 6f78a77 | 2022-11-10 08:13:52 +0100 | [diff] [blame] | 9586 | if (lysc_data_parent(tree->schema)) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9587 | /* unable to evaluate absolute paths */ |
| 9588 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9589 | LYD_NAME(tree)); |
| 9590 | return LY_EINVAL; |
| 9591 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9592 | } |
| 9593 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9594 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9595 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9596 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9597 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9598 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9599 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9600 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9601 | set->cur_node = cur_node; |
| 9602 | for (set->context_op = cur_node ? cur_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9603 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9604 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9605 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9606 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9607 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9608 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9609 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9610 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9611 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9612 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9613 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9614 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9615 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9616 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9617 | } |
| 9618 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9619 | if (set->cur_node) { |
| 9620 | LOG_LOCBACK(0, 1, 0, 0); |
| 9621 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9622 | return rc; |
| 9623 | } |
| 9624 | |
| 9625 | #if 0 |
| 9626 | |
| 9627 | /* full xml printing of set elements, not used currently */ |
| 9628 | |
| 9629 | void |
| 9630 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9631 | { |
| 9632 | uint32_t i; |
| 9633 | char *str_num; |
| 9634 | struct lyout out; |
| 9635 | |
| 9636 | memset(&out, 0, sizeof out); |
| 9637 | |
| 9638 | out.type = LYOUT_STREAM; |
| 9639 | out.method.f = f; |
| 9640 | |
| 9641 | switch (set->type) { |
| 9642 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9643 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9644 | break; |
| 9645 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9646 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9647 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9648 | break; |
| 9649 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9650 | ly_print_(&out, "String XPath set:\n"); |
| 9651 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9652 | break; |
| 9653 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9654 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9655 | |
| 9656 | if (isnan(set->value.num)) { |
| 9657 | str_num = strdup("NaN"); |
| 9658 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9659 | str_num = strdup("0"); |
| 9660 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9661 | str_num = strdup("Infinity"); |
| 9662 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9663 | str_num = strdup("-Infinity"); |
| 9664 | } else if ((long long)set->value.num == set->value.num) { |
| 9665 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9666 | str_num = NULL; |
| 9667 | } |
| 9668 | } else { |
| 9669 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9670 | str_num = NULL; |
| 9671 | } |
| 9672 | } |
| 9673 | if (!str_num) { |
| 9674 | LOGMEM; |
| 9675 | return; |
| 9676 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9677 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9678 | free(str_num); |
| 9679 | break; |
| 9680 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9681 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9682 | |
| 9683 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9684 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9685 | switch (set->node_type[i]) { |
| 9686 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9687 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9688 | break; |
| 9689 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9690 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9691 | break; |
| 9692 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9693 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9694 | break; |
| 9695 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9696 | 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] | 9697 | break; |
| 9698 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9699 | 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] | 9700 | break; |
| 9701 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9702 | 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] | 9703 | break; |
| 9704 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9705 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9706 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9707 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9708 | break; |
| 9709 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9710 | 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] | 9711 | break; |
| 9712 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9713 | 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] | 9714 | break; |
| 9715 | } |
| 9716 | } |
| 9717 | break; |
| 9718 | } |
| 9719 | } |
| 9720 | |
| 9721 | #endif |
| 9722 | |
| 9723 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9724 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9725 | { |
| 9726 | long double num; |
| 9727 | char *str; |
| 9728 | LY_ERR rc; |
| 9729 | |
| 9730 | if (!set || (set->type == target)) { |
| 9731 | return LY_SUCCESS; |
| 9732 | } |
| 9733 | |
| 9734 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9735 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9736 | |
| 9737 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9738 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9739 | return LY_EINVAL; |
| 9740 | } |
| 9741 | |
| 9742 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9743 | 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] | 9744 | switch (set->type) { |
| 9745 | case LYXP_SET_NUMBER: |
| 9746 | if (isnan(set->val.num)) { |
| 9747 | set->val.str = strdup("NaN"); |
| 9748 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9749 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9750 | set->val.str = strdup("0"); |
| 9751 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9752 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9753 | set->val.str = strdup("Infinity"); |
| 9754 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9755 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9756 | set->val.str = strdup("-Infinity"); |
| 9757 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9758 | } else if ((long long)set->val.num == set->val.num) { |
| 9759 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9760 | LOGMEM_RET(set->ctx); |
| 9761 | } |
| 9762 | set->val.str = str; |
| 9763 | } else { |
| 9764 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9765 | LOGMEM_RET(set->ctx); |
| 9766 | } |
| 9767 | set->val.str = str; |
| 9768 | } |
| 9769 | break; |
| 9770 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9771 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9772 | set->val.str = strdup("true"); |
| 9773 | } else { |
| 9774 | set->val.str = strdup("false"); |
| 9775 | } |
| 9776 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9777 | break; |
| 9778 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9779 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9780 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9781 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9782 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9783 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9784 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9785 | set->val.str = str; |
| 9786 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9787 | default: |
| 9788 | LOGINT_RET(set->ctx); |
| 9789 | } |
| 9790 | set->type = LYXP_SET_STRING; |
| 9791 | } |
| 9792 | |
| 9793 | /* to NUMBER */ |
| 9794 | if (target == LYXP_SET_NUMBER) { |
| 9795 | switch (set->type) { |
| 9796 | case LYXP_SET_STRING: |
| 9797 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9798 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9799 | set->val.num = num; |
| 9800 | break; |
| 9801 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9802 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9803 | set->val.num = 1; |
| 9804 | } else { |
| 9805 | set->val.num = 0; |
| 9806 | } |
| 9807 | break; |
| 9808 | default: |
| 9809 | LOGINT_RET(set->ctx); |
| 9810 | } |
| 9811 | set->type = LYXP_SET_NUMBER; |
| 9812 | } |
| 9813 | |
| 9814 | /* to BOOLEAN */ |
| 9815 | if (target == LYXP_SET_BOOLEAN) { |
| 9816 | switch (set->type) { |
| 9817 | case LYXP_SET_NUMBER: |
| 9818 | 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] | 9819 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9820 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9821 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9822 | } |
| 9823 | break; |
| 9824 | case LYXP_SET_STRING: |
| 9825 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9826 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9827 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9828 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9829 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9830 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9831 | } |
| 9832 | break; |
| 9833 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9834 | if (set->used) { |
| 9835 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9836 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9837 | } else { |
| 9838 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9839 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9840 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9841 | break; |
| 9842 | default: |
| 9843 | LOGINT_RET(set->ctx); |
| 9844 | } |
| 9845 | set->type = LYXP_SET_BOOLEAN; |
| 9846 | } |
| 9847 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9848 | return LY_SUCCESS; |
| 9849 | } |
| 9850 | |
| 9851 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9852 | lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9853 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode, |
| 9854 | const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9855 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9856 | LY_ERR ret; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9857 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9858 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9859 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9860 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9861 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9862 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9863 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9864 | |
| 9865 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9866 | memset(set, 0, sizeof *set); |
| 9867 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9868 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 9869 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, LYXP_AXIS_SELF, NULL)); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 9870 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9871 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9872 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9873 | set->cur_scnode = cur_scnode; |
| 9874 | for (set->context_op = cur_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9875 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9876 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9877 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9878 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9879 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9880 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9881 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9882 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9883 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9884 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9885 | |
Michal Vasko | f69e455 | 2022-12-02 10:27:42 +0100 | [diff] [blame] | 9886 | LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9887 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9888 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9889 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9890 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9891 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9892 | { |
| 9893 | if (!path) { |
| 9894 | return NULL; |
| 9895 | } |
| 9896 | |
| 9897 | return path->expr; |
| 9898 | } |