Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file xpath.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG XPath evaluation functions |
| 5 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 17 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 19 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include <errno.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 25 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 26 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 28 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 32 | #include "out.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 33 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 34 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 35 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "printer_data.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 37 | #include "schema_compile_node.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 39 | #include "tree_data.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 40 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 41 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 42 | #include "tree_schema_internal.h" |
| 43 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 44 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 45 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 46 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, |
| 47 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 48 | static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 49 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 50 | static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 51 | enum lyxp_axis axis, uint32_t options); |
| 52 | static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 53 | enum lyxp_axis axis, uint32_t options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 54 | |
aPiecek | 96dc1e3 | 2021-10-08 15:45:59 +0200 | [diff] [blame] | 55 | /* Functions are divided into the following basic classes: |
| 56 | * |
| 57 | * (re)parse functions: |
| 58 | * Parse functions parse the expression into |
| 59 | * tokens (syntactic analysis). |
| 60 | * Reparse functions perform semantic analysis |
| 61 | * (do not save the result, just a check) of |
| 62 | * the expression and fill repeat indices. |
| 63 | * |
| 64 | * warn functions: |
| 65 | * Warn functions check specific reasonable conditions for schema XPath |
| 66 | * and print a warning if they are not satisfied. |
| 67 | * |
| 68 | * moveto functions: |
| 69 | * They and only they actually change the context (set). |
| 70 | * |
| 71 | * eval functions: |
| 72 | * They execute a parsed XPath expression on some data subtree. |
| 73 | */ |
| 74 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 75 | /** |
| 76 | * @brief Print the type of an XPath \p set. |
| 77 | * |
| 78 | * @param[in] set Set to use. |
| 79 | * @return Set type string. |
| 80 | */ |
| 81 | static const char * |
| 82 | print_set_type(struct lyxp_set *set) |
| 83 | { |
| 84 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 85 | case LYXP_SET_NODE_SET: |
| 86 | return "node set"; |
| 87 | case LYXP_SET_SCNODE_SET: |
| 88 | return "schema node set"; |
| 89 | case LYXP_SET_BOOLEAN: |
| 90 | return "boolean"; |
| 91 | case LYXP_SET_NUMBER: |
| 92 | return "number"; |
| 93 | case LYXP_SET_STRING: |
| 94 | return "string"; |
| 95 | } |
| 96 | |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 100 | const char * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 101 | lyxp_token2str(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 102 | { |
| 103 | switch (tok) { |
| 104 | case LYXP_TOKEN_PAR1: |
| 105 | return "("; |
| 106 | case LYXP_TOKEN_PAR2: |
| 107 | return ")"; |
| 108 | case LYXP_TOKEN_BRACK1: |
| 109 | return "["; |
| 110 | case LYXP_TOKEN_BRACK2: |
| 111 | return "]"; |
| 112 | case LYXP_TOKEN_DOT: |
| 113 | return "."; |
| 114 | case LYXP_TOKEN_DDOT: |
| 115 | return ".."; |
| 116 | case LYXP_TOKEN_AT: |
| 117 | return "@"; |
| 118 | case LYXP_TOKEN_COMMA: |
| 119 | return ","; |
| 120 | case LYXP_TOKEN_NAMETEST: |
| 121 | return "NameTest"; |
| 122 | case LYXP_TOKEN_NODETYPE: |
| 123 | return "NodeType"; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 124 | case LYXP_TOKEN_VARREF: |
| 125 | return "VariableReference"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 126 | case LYXP_TOKEN_FUNCNAME: |
| 127 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 128 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 129 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 130 | case LYXP_TOKEN_OPER_EQUAL: |
| 131 | return "Operator(Equal)"; |
| 132 | case LYXP_TOKEN_OPER_NEQUAL: |
| 133 | return "Operator(Non-equal)"; |
| 134 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 135 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 136 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 137 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 138 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 139 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 140 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 141 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 142 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 143 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 144 | case LYXP_TOKEN_LITERAL: |
| 145 | return "Literal"; |
| 146 | case LYXP_TOKEN_NUMBER: |
| 147 | return "Number"; |
| 148 | default: |
| 149 | LOGINT(NULL); |
| 150 | return ""; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 155 | * @brief Transform string into an axis. |
| 156 | * |
| 157 | * @param[in] str String to transform. |
| 158 | * @param[in] str_len Length of @p str. |
| 159 | * @return Transformed axis. |
| 160 | */ |
| 161 | static enum lyxp_axis |
| 162 | str2axis(const char *str, uint16_t str_len) |
| 163 | { |
| 164 | switch (str_len) { |
| 165 | case 4: |
| 166 | assert(!strncmp("self", str, str_len)); |
| 167 | return LYXP_AXIS_SELF; |
| 168 | case 5: |
| 169 | assert(!strncmp("child", str, str_len)); |
| 170 | return LYXP_AXIS_CHILD; |
| 171 | case 6: |
| 172 | assert(!strncmp("parent", str, str_len)); |
| 173 | return LYXP_AXIS_PARENT; |
| 174 | case 8: |
| 175 | assert(!strncmp("ancestor", str, str_len)); |
| 176 | return LYXP_AXIS_ANCESTOR; |
| 177 | case 9: |
| 178 | if (str[0] == 'a') { |
| 179 | assert(!strncmp("attribute", str, str_len)); |
| 180 | return LYXP_AXIS_ATTRIBUTE; |
| 181 | } else if (str[0] == 'f') { |
| 182 | assert(!strncmp("following", str, str_len)); |
| 183 | return LYXP_AXIS_FOLLOWING; |
| 184 | } else { |
| 185 | assert(!strncmp("preceding", str, str_len)); |
| 186 | return LYXP_AXIS_PRECEDING; |
| 187 | } |
| 188 | break; |
| 189 | case 10: |
| 190 | assert(!strncmp("descendant", str, str_len)); |
| 191 | return LYXP_AXIS_DESCENDANT; |
| 192 | case 16: |
| 193 | assert(!strncmp("ancestor-or-self", str, str_len)); |
| 194 | return LYXP_AXIS_ANCESTOR_OR_SELF; |
| 195 | case 17: |
| 196 | if (str[0] == 'f') { |
| 197 | assert(!strncmp("following-sibling", str, str_len)); |
| 198 | return LYXP_AXIS_FOLLOWING_SIBLING; |
| 199 | } else { |
| 200 | assert(!strncmp("preceding-sibling", str, str_len)); |
| 201 | return LYXP_AXIS_PRECEDING_SIBLING; |
| 202 | } |
| 203 | break; |
| 204 | case 18: |
| 205 | assert(!strncmp("descendant-or-self", str, str_len)); |
| 206 | return LYXP_AXIS_DESCENDANT_OR_SELF; |
| 207 | } |
| 208 | |
| 209 | LOGINT(NULL); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 214 | * @brief Print the whole expression \p exp to debug output. |
| 215 | * |
| 216 | * @param[in] exp Expression to use. |
| 217 | */ |
| 218 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 219 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 220 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 221 | #define MSG_BUFFER_SIZE 128 |
| 222 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 223 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 224 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 225 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | |
| 229 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 230 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 231 | 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] | 232 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 233 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 234 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 235 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 236 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 237 | } |
| 238 | strcat(tmp, ")"); |
| 239 | } |
| 240 | LOGDBG(LY_LDGXPATH, tmp); |
| 241 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 242 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | #ifndef NDEBUG |
| 246 | |
| 247 | /** |
| 248 | * @brief Print XPath set content to debug output. |
| 249 | * |
| 250 | * @param[in] set Set to print. |
| 251 | */ |
| 252 | static void |
| 253 | print_set_debug(struct lyxp_set *set) |
| 254 | { |
| 255 | uint32_t i; |
| 256 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 257 | struct lyxp_set_node *item; |
| 258 | struct lyxp_set_scnode *sitem; |
| 259 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 260 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 261 | return; |
| 262 | } |
| 263 | |
| 264 | switch (set->type) { |
| 265 | case LYXP_SET_NODE_SET: |
| 266 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 267 | for (i = 0; i < set->used; ++i) { |
| 268 | item = &set->val.nodes[i]; |
| 269 | |
| 270 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 271 | case LYXP_NODE_NONE: |
| 272 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 273 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 274 | case LYXP_NODE_ROOT: |
| 275 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 276 | break; |
| 277 | case LYXP_NODE_ROOT_CONFIG: |
| 278 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 279 | break; |
| 280 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 281 | 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] | 282 | 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] | 283 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 284 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 285 | 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] | 286 | item->node->schema->name, lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 287 | } else { |
| 288 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 289 | } |
| 290 | break; |
| 291 | case LYXP_NODE_TEXT: |
| 292 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 293 | 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] | 294 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 295 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 296 | 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] | 297 | } |
| 298 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 299 | case LYXP_NODE_META: |
| 300 | 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] | 301 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | } |
| 305 | break; |
| 306 | |
| 307 | case LYXP_SET_SCNODE_SET: |
| 308 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 309 | for (i = 0; i < set->used; ++i) { |
| 310 | sitem = &set->val.scnodes[i]; |
| 311 | |
| 312 | switch (sitem->type) { |
| 313 | case LYXP_NODE_ROOT: |
| 314 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 315 | break; |
| 316 | case LYXP_NODE_ROOT_CONFIG: |
| 317 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 318 | break; |
| 319 | case LYXP_NODE_ELEM: |
| 320 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 321 | break; |
| 322 | default: |
| 323 | LOGINT(NULL); |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | break; |
| 328 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 329 | case LYXP_SET_BOOLEAN: |
| 330 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 331 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 332 | break; |
| 333 | |
| 334 | case LYXP_SET_STRING: |
| 335 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 336 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 337 | break; |
| 338 | |
| 339 | case LYXP_SET_NUMBER: |
| 340 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 341 | |
| 342 | if (isnan(set->val.num)) { |
| 343 | str = strdup("NaN"); |
| 344 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 345 | str = strdup("0"); |
| 346 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 347 | str = strdup("Infinity"); |
| 348 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 349 | str = strdup("-Infinity"); |
| 350 | } else if ((long long)set->val.num == set->val.num) { |
| 351 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 352 | str = NULL; |
| 353 | } |
| 354 | } else { |
| 355 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 356 | str = NULL; |
| 357 | } |
| 358 | } |
| 359 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 360 | |
| 361 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 362 | free(str); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | #endif |
| 367 | |
| 368 | /** |
| 369 | * @brief Realloc the string \p str. |
| 370 | * |
| 371 | * @param[in] ctx libyang context for logging. |
| 372 | * @param[in] needed How much free space is required. |
| 373 | * @param[in,out] str Pointer to the string to use. |
| 374 | * @param[in,out] used Used bytes in \p str. |
| 375 | * @param[in,out] size Allocated bytes in \p str. |
| 376 | * @return LY_ERR |
| 377 | */ |
| 378 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 379 | cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 380 | { |
| 381 | if (*size - *used < needed) { |
| 382 | do { |
| 383 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 384 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 385 | return LY_EINVAL; |
| 386 | } |
| 387 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 388 | } while (*size - *used < needed); |
| 389 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 390 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 391 | } |
| 392 | |
| 393 | return LY_SUCCESS; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * @brief Cast nodes recursively to one string @p str. |
| 398 | * |
| 399 | * @param[in] node Node to cast. |
| 400 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 401 | * @param[in] root_type Type of the XPath root. |
| 402 | * @param[in] indent Current indent. |
| 403 | * @param[in,out] str Resulting string. |
| 404 | * @param[in,out] used Used bytes in @p str. |
| 405 | * @param[in,out] size Allocated bytes in @p str. |
| 406 | * @return LY_ERR |
| 407 | */ |
| 408 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 409 | cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, |
| 410 | char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 411 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 412 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 413 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 414 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 415 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 416 | struct lyd_node_any *any; |
| 417 | LY_ERR rc; |
| 418 | |
| 419 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 420 | return LY_SUCCESS; |
| 421 | } |
| 422 | |
| 423 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 424 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 425 | LY_CHECK_RET(rc); |
| 426 | strcpy(*str + (*used - 1), "\n"); |
| 427 | ++(*used); |
| 428 | |
| 429 | ++indent; |
| 430 | } |
| 431 | |
| 432 | switch (node->schema->nodetype) { |
| 433 | case LYS_CONTAINER: |
| 434 | case LYS_LIST: |
| 435 | case LYS_RPC: |
| 436 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 437 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 438 | LY_CHECK_RET(rc); |
| 439 | strcpy(*str + (*used - 1), "\n"); |
| 440 | ++(*used); |
| 441 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 442 | for (child = lyd_child(node); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 443 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 444 | LY_CHECK_RET(rc); |
| 445 | } |
| 446 | |
| 447 | break; |
| 448 | |
| 449 | case LYS_LEAF: |
| 450 | case LYS_LEAFLIST: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 451 | value_str = lyd_get_value(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 452 | |
| 453 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 454 | LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 455 | memset(*str + (*used - 1), ' ', indent * 2); |
| 456 | *used += indent * 2; |
| 457 | |
| 458 | /* print value */ |
| 459 | if (*used == 1) { |
| 460 | sprintf(*str + (*used - 1), "%s", value_str); |
| 461 | *used += strlen(value_str); |
| 462 | } else { |
| 463 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 464 | *used += strlen(value_str) + 1; |
| 465 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 466 | |
| 467 | break; |
| 468 | |
| 469 | case LYS_ANYXML: |
| 470 | case LYS_ANYDATA: |
| 471 | any = (struct lyd_node_any *)node; |
| 472 | if (!(void *)any->value.tree) { |
| 473 | /* no content */ |
| 474 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 475 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 476 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 477 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 478 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 479 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 480 | /* try to parse it into a data tree */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 481 | if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, |
| 482 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 483 | /* successfully parsed */ |
| 484 | free(any->value.mem); |
| 485 | any->value.tree = tree; |
| 486 | any->value_type = LYD_ANYDATA_DATATREE; |
| 487 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 488 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 491 | switch (any->value_type) { |
| 492 | case LYD_ANYDATA_STRING: |
| 493 | case LYD_ANYDATA_XML: |
| 494 | case LYD_ANYDATA_JSON: |
| 495 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 496 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 497 | break; |
| 498 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 499 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 500 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 501 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 502 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 503 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 504 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 505 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 506 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | line = strtok_r(buf, "\n", &ptr); |
| 511 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 512 | rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 513 | if (rc != LY_SUCCESS) { |
| 514 | free(buf); |
| 515 | return rc; |
| 516 | } |
| 517 | memset(*str + (*used - 1), ' ', indent * 2); |
| 518 | *used += indent * 2; |
| 519 | |
| 520 | strcpy(*str + (*used - 1), line); |
| 521 | *used += strlen(line); |
| 522 | |
| 523 | strcpy(*str + (*used - 1), "\n"); |
| 524 | *used += 1; |
| 525 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 526 | |
| 527 | free(buf); |
| 528 | break; |
| 529 | |
| 530 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 531 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 535 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 536 | LY_CHECK_RET(rc); |
| 537 | strcpy(*str + (*used - 1), "\n"); |
| 538 | ++(*used); |
| 539 | |
| 540 | --indent; |
| 541 | } |
| 542 | |
| 543 | return LY_SUCCESS; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * @brief Cast an element into a string. |
| 548 | * |
| 549 | * @param[in] node Node to cast. |
| 550 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 551 | * @param[in] root_type Type of the XPath root. |
| 552 | * @param[out] str Element cast to dynamically-allocated string. |
| 553 | * @return LY_ERR |
| 554 | */ |
| 555 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 556 | cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 557 | { |
| 558 | uint16_t used, size; |
| 559 | LY_ERR rc; |
| 560 | |
| 561 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 562 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 563 | (*str)[0] = '\0'; |
| 564 | used = 1; |
| 565 | size = LYXP_STRING_CAST_SIZE_START; |
| 566 | |
| 567 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 568 | if (rc != LY_SUCCESS) { |
| 569 | free(*str); |
| 570 | return rc; |
| 571 | } |
| 572 | |
| 573 | if (size > used) { |
| 574 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 575 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 576 | } |
| 577 | return LY_SUCCESS; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 582 | * Context position aware. |
| 583 | * |
| 584 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 585 | * @param[out] str Cast dynamically-allocated string. |
| 586 | * @return LY_ERR |
| 587 | */ |
| 588 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 589 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 590 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 591 | if (!set->used) { |
| 592 | *str = strdup(""); |
| 593 | if (!*str) { |
| 594 | LOGMEM_RET(set->ctx); |
| 595 | } |
| 596 | return LY_SUCCESS; |
| 597 | } |
| 598 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 599 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 600 | case LYXP_NODE_NONE: |
| 601 | /* invalid */ |
| 602 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 603 | case LYXP_NODE_ROOT: |
| 604 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 605 | return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 606 | case LYXP_NODE_ELEM: |
| 607 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 608 | return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 609 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 610 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 611 | if (!*str) { |
| 612 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 613 | } |
| 614 | return LY_SUCCESS; |
| 615 | } |
| 616 | |
| 617 | LOGINT_RET(set->ctx); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * @brief Cast a string into an XPath number. |
| 622 | * |
| 623 | * @param[in] str String to use. |
| 624 | * @return Cast number. |
| 625 | */ |
| 626 | static long double |
| 627 | cast_string_to_number(const char *str) |
| 628 | { |
| 629 | long double num; |
| 630 | char *ptr; |
| 631 | |
| 632 | errno = 0; |
| 633 | num = strtold(str, &ptr); |
| 634 | if (errno || *ptr) { |
| 635 | num = NAN; |
| 636 | } |
| 637 | return num; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * @brief Callback for checking value equality. |
| 642 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 643 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 644 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 645 | * @param[in] val1_p First value. |
| 646 | * @param[in] val2_p Second value. |
| 647 | * @param[in] mod Whether hash table is being modified. |
| 648 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 649 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 650 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 651 | static ly_bool |
| 652 | 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] | 653 | { |
| 654 | struct lyxp_set_hash_node *val1, *val2; |
| 655 | |
| 656 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 657 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 658 | |
| 659 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 660 | return 1; |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * @brief Insert node and its hash into set. |
| 668 | * |
| 669 | * @param[in] set et to insert to. |
| 670 | * @param[in] node Node with hash. |
| 671 | * @param[in] type Node type. |
| 672 | */ |
| 673 | static void |
| 674 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 675 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 676 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 677 | uint32_t i, hash; |
| 678 | struct lyxp_set_hash_node hnode; |
| 679 | |
| 680 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 681 | /* create hash table and add all the nodes */ |
| 682 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 683 | for (i = 0; i < set->used; ++i) { |
| 684 | hnode.node = set->val.nodes[i].node; |
| 685 | hnode.type = set->val.nodes[i].type; |
| 686 | |
| 687 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 688 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 689 | hash = dict_hash_multi(hash, NULL, 0); |
| 690 | |
| 691 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 692 | assert(!r); |
| 693 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 694 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 695 | if (hnode.node == node) { |
| 696 | /* it was just added, do not add it twice */ |
| 697 | node = NULL; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 703 | /* add the new node into hash table */ |
| 704 | hnode.node = node; |
| 705 | hnode.type = type; |
| 706 | |
| 707 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 708 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 709 | hash = dict_hash_multi(hash, NULL, 0); |
| 710 | |
| 711 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 712 | assert(!r); |
| 713 | (void)r; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * @brief Remove node and its hash from set. |
| 719 | * |
| 720 | * @param[in] set Set to remove from. |
| 721 | * @param[in] node Node to remove. |
| 722 | * @param[in] type Node type. |
| 723 | */ |
| 724 | static void |
| 725 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 726 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 727 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 728 | struct lyxp_set_hash_node hnode; |
| 729 | uint32_t hash; |
| 730 | |
| 731 | if (set->ht) { |
| 732 | hnode.node = node; |
| 733 | hnode.type = type; |
| 734 | |
| 735 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 736 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 737 | hash = dict_hash_multi(hash, NULL, 0); |
| 738 | |
| 739 | r = lyht_remove(set->ht, &hnode, hash); |
| 740 | assert(!r); |
| 741 | (void)r; |
| 742 | |
| 743 | if (!set->ht->used) { |
| 744 | lyht_free(set->ht); |
| 745 | set->ht = NULL; |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * @brief Check whether node is in set based on its hash. |
| 752 | * |
| 753 | * @param[in] set Set to search in. |
| 754 | * @param[in] node Node to search for. |
| 755 | * @param[in] type Node type. |
| 756 | * @param[in] skip_idx Index in @p set to skip. |
| 757 | * @return LY_ERR |
| 758 | */ |
| 759 | static LY_ERR |
| 760 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 761 | { |
| 762 | struct lyxp_set_hash_node hnode, *match_p; |
| 763 | uint32_t hash; |
| 764 | |
| 765 | hnode.node = node; |
| 766 | hnode.type = type; |
| 767 | |
| 768 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 769 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 770 | hash = dict_hash_multi(hash, NULL, 0); |
| 771 | |
| 772 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 773 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 774 | /* we found it on the index that should be skipped, find another */ |
| 775 | hnode = *match_p; |
| 776 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 777 | /* none other found */ |
| 778 | return LY_SUCCESS; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | return LY_EEXIST; |
| 783 | } |
| 784 | |
| 785 | /* not found */ |
| 786 | return LY_SUCCESS; |
| 787 | } |
| 788 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 789 | void |
| 790 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 791 | { |
| 792 | if (!set) { |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | if (set->type == LYXP_SET_NODE_SET) { |
| 797 | free(set->val.nodes); |
| 798 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 799 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 800 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 801 | lyht_free(set->ht); |
| 802 | } else { |
| 803 | if (set->type == LYXP_SET_STRING) { |
| 804 | free(set->val.str); |
| 805 | } |
| 806 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 807 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 808 | |
| 809 | set->val.nodes = NULL; |
| 810 | set->used = 0; |
| 811 | set->size = 0; |
| 812 | set->ht = NULL; |
| 813 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 814 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 815 | } |
| 816 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 817 | /** |
| 818 | * @brief Free dynamically-allocated set. |
| 819 | * |
| 820 | * @param[in] set Set to free. |
| 821 | */ |
| 822 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 823 | lyxp_set_free(struct lyxp_set *set) |
| 824 | { |
| 825 | if (!set) { |
| 826 | return; |
| 827 | } |
| 828 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 829 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 830 | free(set); |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * @brief Initialize set context. |
| 835 | * |
| 836 | * @param[in] new Set to initialize. |
| 837 | * @param[in] set Arbitrary initialized set. |
| 838 | */ |
| 839 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 840 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 841 | { |
| 842 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 843 | if (set) { |
| 844 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 845 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 846 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 847 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 848 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 849 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 850 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 851 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 852 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 853 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /** |
| 857 | * @brief Create a deep copy of a set. |
| 858 | * |
| 859 | * @param[in] set Set to copy. |
| 860 | * @return Copy of @p set. |
| 861 | */ |
| 862 | static struct lyxp_set * |
| 863 | set_copy(struct lyxp_set *set) |
| 864 | { |
| 865 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 866 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 867 | |
| 868 | if (!set) { |
| 869 | return NULL; |
| 870 | } |
| 871 | |
| 872 | ret = malloc(sizeof *ret); |
| 873 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 874 | set_init(ret, set); |
| 875 | |
| 876 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 877 | ret->type = set->type; |
| 878 | |
| 879 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 880 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 881 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 882 | uint32_t idx; |
| 883 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 884 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 885 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 886 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 887 | lyxp_set_free(ret); |
| 888 | return NULL; |
| 889 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 890 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 894 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 895 | if (set->used) { |
| 896 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 897 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 898 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 899 | } else { |
| 900 | ret->val.nodes = NULL; |
| 901 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 902 | |
| 903 | ret->used = ret->size = set->used; |
| 904 | ret->ctx_pos = set->ctx_pos; |
| 905 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 906 | if (set->ht) { |
| 907 | ret->ht = lyht_dup(set->ht); |
| 908 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 909 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 910 | memcpy(ret, set, sizeof *ret); |
| 911 | if (set->type == LYXP_SET_STRING) { |
| 912 | ret->val.str = strdup(set->val.str); |
| 913 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 914 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | return ret; |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 922 | * |
| 923 | * @param[in] set Set to fill. |
| 924 | * @param[in] string String to fill into \p set. |
| 925 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 926 | */ |
| 927 | static void |
| 928 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 929 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 930 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 931 | |
| 932 | set->type = LYXP_SET_STRING; |
| 933 | if ((str_len == 0) && (string[0] != '\0')) { |
| 934 | string = ""; |
| 935 | } |
| 936 | set->val.str = strndup(string, str_len); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 941 | * |
| 942 | * @param[in] set Set to fill. |
| 943 | * @param[in] number Number to fill into \p set. |
| 944 | */ |
| 945 | static void |
| 946 | set_fill_number(struct lyxp_set *set, long double number) |
| 947 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 948 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 949 | |
| 950 | set->type = LYXP_SET_NUMBER; |
| 951 | set->val.num = number; |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 956 | * |
| 957 | * @param[in] set Set to fill. |
| 958 | * @param[in] boolean Boolean to fill into \p set. |
| 959 | */ |
| 960 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 961 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 962 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 963 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 964 | |
| 965 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 966 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /** |
| 970 | * @brief Fill XPath set with the value from another set (deep assign). |
| 971 | * Any current data are disposed of. |
| 972 | * |
| 973 | * @param[in] trg Set to fill. |
| 974 | * @param[in] src Source set to copy into \p trg. |
| 975 | */ |
| 976 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 977 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 978 | { |
| 979 | if (!trg || !src) { |
| 980 | return; |
| 981 | } |
| 982 | |
| 983 | if (trg->type == LYXP_SET_NODE_SET) { |
| 984 | free(trg->val.nodes); |
| 985 | } else if (trg->type == LYXP_SET_STRING) { |
| 986 | free(trg->val.str); |
| 987 | } |
| 988 | set_init(trg, src); |
| 989 | |
| 990 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 991 | trg->type = LYXP_SET_SCNODE_SET; |
| 992 | trg->used = src->used; |
| 993 | trg->size = src->used; |
| 994 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 995 | if (trg->size) { |
| 996 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 997 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 998 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 999 | } else { |
| 1000 | trg->val.scnodes = NULL; |
| 1001 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1002 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1003 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1004 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1005 | set_fill_number(trg, src->val.num); |
| 1006 | } else if (src->type == LYXP_SET_STRING) { |
| 1007 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 1008 | } else { |
| 1009 | if (trg->type == LYXP_SET_NODE_SET) { |
| 1010 | free(trg->val.nodes); |
| 1011 | } else if (trg->type == LYXP_SET_STRING) { |
| 1012 | free(trg->val.str); |
| 1013 | } |
| 1014 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1015 | assert(src->type == LYXP_SET_NODE_SET); |
| 1016 | |
| 1017 | trg->type = LYXP_SET_NODE_SET; |
| 1018 | trg->used = src->used; |
| 1019 | trg->size = src->used; |
| 1020 | trg->ctx_pos = src->ctx_pos; |
| 1021 | trg->ctx_size = src->ctx_size; |
| 1022 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1023 | if (trg->size) { |
| 1024 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 1025 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1026 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 1027 | } else { |
| 1028 | trg->val.nodes = NULL; |
| 1029 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1030 | if (src->ht) { |
| 1031 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1032 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1033 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * @brief Clear context of all schema nodes. |
| 1040 | * |
| 1041 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1042 | * @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] | 1043 | */ |
| 1044 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1045 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1046 | { |
| 1047 | uint32_t i; |
| 1048 | |
| 1049 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1050 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1051 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1052 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 1053 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | /** |
| 1059 | * @brief Remove a node from a set. Removing last node changes |
| 1060 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1061 | * |
| 1062 | * @param[in] set Set to use. |
| 1063 | * @param[in] idx Index from @p set of the node to be removed. |
| 1064 | */ |
| 1065 | static void |
| 1066 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1067 | { |
| 1068 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1069 | assert(idx < set->used); |
| 1070 | |
| 1071 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1072 | |
| 1073 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1074 | if (idx < set->used) { |
| 1075 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1076 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1077 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1082 | * @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] | 1083 | * |
| 1084 | * @param[in] set Set to use. |
| 1085 | * @param[in] idx Index from @p set of the node to be removed. |
| 1086 | */ |
| 1087 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1088 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1089 | { |
| 1090 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1091 | assert(idx < set->used); |
| 1092 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1093 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1094 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1095 | } |
| 1096 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1100 | * @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] | 1101 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1102 | * |
| 1103 | * @param[in] set Set to consolidate. |
| 1104 | */ |
| 1105 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1106 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1107 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1108 | uint32_t i, orig_used, end = 0; |
| 1109 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1110 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1111 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1112 | |
| 1113 | orig_used = set->used; |
| 1114 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1115 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1116 | start = -1; |
| 1117 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1118 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1119 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1120 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1121 | end = i; |
| 1122 | ++i; |
| 1123 | break; |
| 1124 | } |
| 1125 | |
| 1126 | ++i; |
| 1127 | if (i == orig_used) { |
| 1128 | end = i; |
| 1129 | } |
| 1130 | } while (i < orig_used); |
| 1131 | |
| 1132 | if (start > -1) { |
| 1133 | /* move the whole chunk of valid nodes together */ |
| 1134 | if (set->used != (unsigned)start) { |
| 1135 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1136 | } |
| 1137 | set->used += end - start; |
| 1138 | } |
| 1139 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1143 | * @brief Check for duplicates in a node set. |
| 1144 | * |
| 1145 | * @param[in] set Set to check. |
| 1146 | * @param[in] node Node to look for in @p set. |
| 1147 | * @param[in] node_type Type of @p node. |
| 1148 | * @param[in] skip_idx Index from @p set to skip. |
| 1149 | * @return LY_ERR |
| 1150 | */ |
| 1151 | static LY_ERR |
| 1152 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1153 | { |
| 1154 | uint32_t i; |
| 1155 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1156 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1157 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1158 | } |
| 1159 | |
| 1160 | for (i = 0; i < set->used; ++i) { |
| 1161 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1162 | continue; |
| 1163 | } |
| 1164 | |
| 1165 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1166 | return LY_EEXIST; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | return LY_SUCCESS; |
| 1171 | } |
| 1172 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1173 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1174 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1175 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1176 | { |
| 1177 | uint32_t i; |
| 1178 | |
| 1179 | for (i = 0; i < set->used; ++i) { |
| 1180 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1181 | continue; |
| 1182 | } |
| 1183 | |
| 1184 | 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] | 1185 | if (index_p) { |
| 1186 | *index_p = i; |
| 1187 | } |
| 1188 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1192 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1195 | void |
| 1196 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1197 | { |
| 1198 | uint32_t orig_used, i, j; |
| 1199 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1200 | 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] | 1201 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1202 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1203 | return; |
| 1204 | } |
| 1205 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1206 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1207 | /* release hidden allocated data (lyxp_set.size) */ |
| 1208 | lyxp_set_free_content(set1); |
| 1209 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1210 | memcpy(set1, set2, sizeof *set1); |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | if (set1->used + set2->used > set1->size) { |
| 1215 | set1->size = set1->used + set2->used; |
| 1216 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1217 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1218 | } |
| 1219 | |
| 1220 | orig_used = set1->used; |
| 1221 | |
| 1222 | for (i = 0; i < set2->used; ++i) { |
| 1223 | for (j = 0; j < orig_used; ++j) { |
| 1224 | /* detect duplicities */ |
| 1225 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1230 | if (j < orig_used) { |
| 1231 | /* node is there, but update its status if needed */ |
| 1232 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1233 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1234 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1235 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1236 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1237 | } |
| 1238 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1239 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1240 | ++set1->used; |
| 1241 | } |
| 1242 | } |
| 1243 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1244 | lyxp_set_free_content(set2); |
| 1245 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | /** |
| 1249 | * @brief Insert a node into a set. Context position aware. |
| 1250 | * |
| 1251 | * @param[in] set Set to use. |
| 1252 | * @param[in] node Node to insert to @p set. |
| 1253 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1254 | * @param[in] node_type Node type of @p node. |
| 1255 | * @param[in] idx Index in @p set to insert into. |
| 1256 | */ |
| 1257 | static void |
| 1258 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1259 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1260 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1261 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1262 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1263 | /* first item */ |
| 1264 | if (idx) { |
| 1265 | /* no real harm done, but it is a bug */ |
| 1266 | LOGINT(set->ctx); |
| 1267 | idx = 0; |
| 1268 | } |
| 1269 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1270 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1271 | set->type = LYXP_SET_NODE_SET; |
| 1272 | set->used = 0; |
| 1273 | set->size = LYXP_SET_SIZE_START; |
| 1274 | set->ctx_pos = 1; |
| 1275 | set->ctx_size = 1; |
| 1276 | set->ht = NULL; |
| 1277 | } else { |
| 1278 | /* not an empty set */ |
| 1279 | if (set->used == set->size) { |
| 1280 | |
| 1281 | /* set is full */ |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1282 | 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] | 1283 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1284 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | if (idx > set->used) { |
| 1288 | LOGINT(set->ctx); |
| 1289 | idx = set->used; |
| 1290 | } |
| 1291 | |
| 1292 | /* make space for the new node */ |
| 1293 | if (idx < set->used) { |
| 1294 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | /* finally assign the value */ |
| 1299 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1300 | set->val.nodes[idx].type = node_type; |
| 1301 | set->val.nodes[idx].pos = pos; |
| 1302 | ++set->used; |
| 1303 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1304 | /* add into hash table */ |
| 1305 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1308 | LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1309 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
| 1310 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1311 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1312 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1313 | |
| 1314 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1315 | |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1316 | if (!set->size) { |
| 1317 | /* first item */ |
| 1318 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
| 1319 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
| 1320 | set->type = LYXP_SET_SCNODE_SET; |
| 1321 | set->used = 0; |
| 1322 | set->size = LYXP_SET_SIZE_START; |
| 1323 | set->ctx_pos = 1; |
| 1324 | set->ctx_size = 1; |
| 1325 | set->ht = NULL; |
| 1326 | } |
| 1327 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1328 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1329 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1330 | } else { |
| 1331 | if (set->used == set->size) { |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1332 | 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] | 1333 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1334 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1337 | index = set->used; |
| 1338 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1339 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1340 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1341 | ++set->used; |
| 1342 | } |
| 1343 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1344 | if (index_p) { |
| 1345 | *index_p = index; |
| 1346 | } |
| 1347 | |
| 1348 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1352 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1353 | * |
| 1354 | * @param[in] set Set to modify. |
| 1355 | * @return New context value. |
| 1356 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1357 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1358 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1359 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1360 | uint32_t i; |
| 1361 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1362 | |
| 1363 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1364 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1365 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1366 | retry: |
| 1367 | for (i = 0; i < set->used; ++i) { |
| 1368 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1369 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1370 | goto retry; |
| 1371 | } |
| 1372 | } |
| 1373 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1374 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1375 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | return ret_ctx; |
| 1380 | } |
| 1381 | |
| 1382 | /** |
| 1383 | * @brief Get unique @p node position in the data. |
| 1384 | * |
| 1385 | * @param[in] node Node to find. |
| 1386 | * @param[in] node_type Node type of @p node. |
| 1387 | * @param[in] root Root node. |
| 1388 | * @param[in] root_type Type of the XPath @p root node. |
| 1389 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1390 | * be used to increase efficiency and start the DFS from this node. |
| 1391 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1392 | * @return Node position. |
| 1393 | */ |
| 1394 | static uint32_t |
| 1395 | 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] | 1396 | 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] | 1397 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1398 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1399 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1400 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1401 | |
| 1402 | assert(prev && prev_pos && !root->prev->next); |
| 1403 | |
| 1404 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | if (*prev) { |
| 1409 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1410 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1411 | 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] | 1412 | goto dfs_search; |
| 1413 | } |
| 1414 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1415 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1416 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1417 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1418 | LYD_TREE_DFS_continue = 0; |
| 1419 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1420 | if (*prev && !elem) { |
| 1421 | /* resume previous DFS */ |
| 1422 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1423 | LYD_TREE_DFS_continue = 0; |
| 1424 | } |
| 1425 | |
Michal Vasko | 482a682 | 2022-05-06 08:56:12 +0200 | [diff] [blame] | 1426 | 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] | 1427 | /* skip */ |
| 1428 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1429 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1430 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1431 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1432 | break; |
| 1433 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1434 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1435 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1436 | |
| 1437 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1441 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1442 | break; |
| 1443 | } |
| 1444 | } |
| 1445 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1446 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1447 | if (!(*prev)) { |
| 1448 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1449 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1450 | return 0; |
| 1451 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1452 | /* start the search again from the beginning */ |
| 1453 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1454 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1455 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1456 | pos = 1; |
| 1457 | goto dfs_search; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /* remember the last found node for next time */ |
| 1462 | *prev = node; |
| 1463 | *prev_pos = pos; |
| 1464 | |
| 1465 | return pos; |
| 1466 | } |
| 1467 | |
| 1468 | /** |
| 1469 | * @brief Assign (fill) missing node positions. |
| 1470 | * |
| 1471 | * @param[in] set Set to fill positions in. |
| 1472 | * @param[in] root Context root node. |
| 1473 | * @param[in] root_type Context root type. |
| 1474 | * @return LY_ERR |
| 1475 | */ |
| 1476 | static LY_ERR |
| 1477 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1478 | { |
| 1479 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1480 | uint32_t i, tmp_pos = 0; |
| 1481 | |
| 1482 | for (i = 0; i < set->used; ++i) { |
| 1483 | if (!set->val.nodes[i].pos) { |
| 1484 | tmp_node = NULL; |
| 1485 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1486 | case LYXP_NODE_META: |
| 1487 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1488 | if (!tmp_node) { |
| 1489 | LOGINT_RET(root->schema->module->ctx); |
| 1490 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1491 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1492 | case LYXP_NODE_ELEM: |
| 1493 | case LYXP_NODE_TEXT: |
| 1494 | if (!tmp_node) { |
| 1495 | tmp_node = set->val.nodes[i].node; |
| 1496 | } |
| 1497 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1498 | break; |
| 1499 | default: |
| 1500 | /* all roots have position 0 */ |
| 1501 | break; |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | return LY_SUCCESS; |
| 1507 | } |
| 1508 | |
| 1509 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1510 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1511 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1512 | * @param[in] meta Metadata to use. |
| 1513 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1514 | */ |
| 1515 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1516 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1517 | { |
| 1518 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1519 | struct lyd_meta *meta2; |
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 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1522 | ++pos; |
| 1523 | } |
| 1524 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1525 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1526 | return pos; |
| 1527 | } |
| 1528 | |
| 1529 | /** |
| 1530 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1531 | * |
| 1532 | * @param[in] item1 1st node. |
| 1533 | * @param[in] item2 2nd node. |
| 1534 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1535 | */ |
| 1536 | static int |
| 1537 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1538 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1539 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1540 | |
| 1541 | if (item1->pos < item2->pos) { |
| 1542 | return -1; |
| 1543 | } |
| 1544 | |
| 1545 | if (item1->pos > item2->pos) { |
| 1546 | return 1; |
| 1547 | } |
| 1548 | |
| 1549 | /* node positions are equal, the fun case */ |
| 1550 | |
| 1551 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1552 | /* special case since text nodes are actually saved as their parents */ |
| 1553 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1554 | if (item1->type == LYXP_NODE_ELEM) { |
| 1555 | assert(item2->type == LYXP_NODE_TEXT); |
| 1556 | return -1; |
| 1557 | } else { |
| 1558 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1559 | return 1; |
| 1560 | } |
| 1561 | } |
| 1562 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1563 | /* we need meta positions now */ |
| 1564 | if (item1->type == LYXP_NODE_META) { |
| 1565 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1566 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1567 | if (item2->type == LYXP_NODE_META) { |
| 1568 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1571 | /* 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] | 1572 | /* check for duplicates */ |
| 1573 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1574 | 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] | 1575 | return 0; |
| 1576 | } |
| 1577 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1578 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1579 | /* elem is always first, 2nd node is after it */ |
| 1580 | if (item1->type == LYXP_NODE_ELEM) { |
| 1581 | assert(item2->type != LYXP_NODE_ELEM); |
| 1582 | return -1; |
| 1583 | } |
| 1584 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1585 | /* 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] | 1586 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1587 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1588 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1589 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1590 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1591 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1592 | return 1; |
| 1593 | } |
| 1594 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1595 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1596 | /* 2nd is after 1st */ |
| 1597 | return -1; |
| 1598 | } |
| 1599 | |
| 1600 | /** |
| 1601 | * @brief Set cast for comparisons. |
| 1602 | * |
| 1603 | * @param[in] trg Target set to cast source into. |
| 1604 | * @param[in] src Source set. |
| 1605 | * @param[in] type Target set type. |
| 1606 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1607 | * @return LY_ERR |
| 1608 | */ |
| 1609 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1610 | set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1611 | { |
| 1612 | assert(src->type == LYXP_SET_NODE_SET); |
| 1613 | |
| 1614 | set_init(trg, src); |
| 1615 | |
| 1616 | /* insert node into target set */ |
| 1617 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1618 | |
| 1619 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1620 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1621 | } |
| 1622 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1623 | /** |
| 1624 | * @brief Set content canonization for comparisons. |
| 1625 | * |
| 1626 | * @param[in] trg Target set to put the canononized source into. |
| 1627 | * @param[in] src Source set. |
| 1628 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1629 | * @return LY_ERR |
| 1630 | */ |
| 1631 | static LY_ERR |
| 1632 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1633 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1634 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1635 | struct lyd_value val; |
| 1636 | struct ly_err_item *err = NULL; |
| 1637 | char *str, *ptr; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1638 | LY_ERR rc; |
| 1639 | |
| 1640 | /* is there anything to canonize even? */ |
| 1641 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1642 | /* do we have a type to use for canonization? */ |
| 1643 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1644 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1645 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1646 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1647 | } |
| 1648 | } |
| 1649 | if (!type) { |
| 1650 | goto fill; |
| 1651 | } |
| 1652 | |
| 1653 | if (src->type == LYXP_SET_NUMBER) { |
| 1654 | /* canonize number */ |
| 1655 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1656 | LOGMEM(src->ctx); |
| 1657 | return LY_EMEM; |
| 1658 | } |
| 1659 | } else { |
| 1660 | /* canonize string */ |
| 1661 | str = strdup(src->val.str); |
| 1662 | } |
| 1663 | |
| 1664 | /* ignore errors, the value may not satisfy schema constraints */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 1665 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1666 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1667 | ly_err_free(err); |
| 1668 | if (rc) { |
| 1669 | /* invalid value */ |
Radek Iša | 4846022 | 2021-03-02 15:18:32 +0100 | [diff] [blame] | 1670 | /* function store automaticaly dealloc value when fail */ |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1671 | goto fill; |
| 1672 | } |
| 1673 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1674 | /* use the canonized value */ |
| 1675 | set_init(trg, src); |
| 1676 | trg->type = src->type; |
| 1677 | if (src->type == LYXP_SET_NUMBER) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1678 | trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1679 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1680 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1681 | trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL)); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1682 | } |
| 1683 | type->plugin->free(src->ctx, &val); |
| 1684 | return LY_SUCCESS; |
| 1685 | |
| 1686 | fill: |
| 1687 | /* no canonization needed/possible */ |
| 1688 | set_fill_set(trg, src); |
| 1689 | return LY_SUCCESS; |
| 1690 | } |
| 1691 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1692 | /** |
| 1693 | * @brief Bubble sort @p set into XPath document order. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1694 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1695 | * |
| 1696 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1697 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1698 | */ |
| 1699 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1700 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1701 | { |
| 1702 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1703 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1704 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1705 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1706 | struct lyxp_set_node item; |
| 1707 | struct lyxp_set_hash_node hnode; |
| 1708 | uint64_t hash; |
| 1709 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1710 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1711 | return 0; |
| 1712 | } |
| 1713 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1714 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1715 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1716 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1717 | |
| 1718 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1719 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1720 | return -1; |
| 1721 | } |
| 1722 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1723 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1724 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1725 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1726 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1727 | |
| 1728 | for (i = 0; i < set->used; ++i) { |
| 1729 | inverted = 0; |
| 1730 | change = 0; |
| 1731 | |
| 1732 | for (j = 1; j < set->used - i; ++j) { |
| 1733 | /* compare node positions */ |
| 1734 | if (inverted) { |
| 1735 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1736 | } else { |
| 1737 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1738 | } |
| 1739 | |
| 1740 | /* swap if needed */ |
| 1741 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1742 | change = 1; |
| 1743 | |
| 1744 | item = set->val.nodes[j - 1]; |
| 1745 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1746 | set->val.nodes[j] = item; |
| 1747 | } else { |
| 1748 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1749 | inverted = !inverted; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | ++ret; |
| 1754 | |
| 1755 | if (!change) { |
| 1756 | break; |
| 1757 | } |
| 1758 | } |
| 1759 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1760 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1761 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1762 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1763 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1764 | |
| 1765 | /* check node hashes */ |
| 1766 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1767 | assert(set->ht); |
| 1768 | for (i = 0; i < set->used; ++i) { |
| 1769 | hnode.node = set->val.nodes[i].node; |
| 1770 | hnode.type = set->val.nodes[i].type; |
| 1771 | |
| 1772 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1773 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1774 | hash = dict_hash_multi(hash, NULL, 0); |
| 1775 | |
| 1776 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | return ret - 1; |
| 1781 | } |
| 1782 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1783 | /** |
| 1784 | * @brief Merge 2 sorted sets into one. |
| 1785 | * |
| 1786 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1787 | * @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] | 1788 | * @return LY_ERR |
| 1789 | */ |
| 1790 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1791 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1792 | { |
| 1793 | uint32_t i, j, k, count, dup_count; |
| 1794 | int cmp; |
| 1795 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1796 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1797 | 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] | 1798 | return LY_EINVAL; |
| 1799 | } |
| 1800 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1801 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1802 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1803 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1804 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1805 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1806 | return LY_SUCCESS; |
| 1807 | } |
| 1808 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1809 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0143b68 | 2021-12-13 17:13:09 +0100 | [diff] [blame] | 1810 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1811 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1812 | |
| 1813 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1814 | 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] | 1815 | return LY_EINT; |
| 1816 | } |
| 1817 | |
| 1818 | #ifndef NDEBUG |
| 1819 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1820 | print_set_debug(trg); |
| 1821 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1822 | print_set_debug(src); |
| 1823 | #endif |
| 1824 | |
| 1825 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1826 | * will likely be wasted on them, too bad) */ |
| 1827 | if (trg->size - trg->used < src->used) { |
| 1828 | trg->size = trg->used + src->used; |
| 1829 | |
| 1830 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1831 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1832 | } |
| 1833 | |
| 1834 | i = 0; |
| 1835 | j = 0; |
| 1836 | count = 0; |
| 1837 | dup_count = 0; |
| 1838 | do { |
| 1839 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1840 | if (!cmp) { |
| 1841 | if (!count) { |
| 1842 | /* duplicate, just skip it */ |
| 1843 | ++i; |
| 1844 | ++j; |
| 1845 | } else { |
| 1846 | /* we are copying something already, so let's copy the duplicate too, |
| 1847 | * we are hoping that afterwards there are some more nodes to |
| 1848 | * copy and this way we can copy them all together */ |
| 1849 | ++count; |
| 1850 | ++dup_count; |
| 1851 | ++i; |
| 1852 | ++j; |
| 1853 | } |
| 1854 | } else if (cmp < 0) { |
| 1855 | /* inserting src node into trg, just remember it for now */ |
| 1856 | ++count; |
| 1857 | ++i; |
| 1858 | |
| 1859 | /* insert the hash now */ |
| 1860 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1861 | } else if (count) { |
| 1862 | copy_nodes: |
| 1863 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1864 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1865 | &trg->val.nodes[j], |
| 1866 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1867 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1868 | |
| 1869 | trg->used += count - dup_count; |
| 1870 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1871 | j += count - dup_count; |
| 1872 | |
| 1873 | count = 0; |
| 1874 | dup_count = 0; |
| 1875 | } else { |
| 1876 | ++j; |
| 1877 | } |
| 1878 | } while ((i < src->used) && (j < trg->used)); |
| 1879 | |
| 1880 | if ((i < src->used) || count) { |
| 1881 | /* insert all the hashes first */ |
| 1882 | for (k = i; k < src->used; ++k) { |
| 1883 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1884 | } |
| 1885 | |
| 1886 | /* loop ended, but we need to copy something at trg end */ |
| 1887 | count += src->used - i; |
| 1888 | i = src->used; |
| 1889 | goto copy_nodes; |
| 1890 | } |
| 1891 | |
| 1892 | /* we are inserting hashes before the actual node insert, which causes |
| 1893 | * situations when there were initially not enough items for a hash table, |
| 1894 | * but even after some were inserted, hash table was not created (during |
| 1895 | * insertion the number of items is not updated yet) */ |
| 1896 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1897 | set_insert_node_hash(trg, NULL, 0); |
| 1898 | } |
| 1899 | |
| 1900 | #ifndef NDEBUG |
| 1901 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1902 | print_set_debug(trg); |
| 1903 | #endif |
| 1904 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1905 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1906 | return LY_SUCCESS; |
| 1907 | } |
| 1908 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1909 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1910 | lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1911 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1912 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1913 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1914 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1915 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1916 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1917 | } |
| 1918 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1919 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1920 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1921 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]), |
| 1922 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1923 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1924 | return LY_ENOT; |
| 1925 | } |
| 1926 | |
| 1927 | return LY_SUCCESS; |
| 1928 | } |
| 1929 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1930 | LY_ERR |
| 1931 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1932 | { |
| 1933 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1934 | |
| 1935 | /* skip the token */ |
| 1936 | ++(*tok_idx); |
| 1937 | |
| 1938 | return LY_SUCCESS; |
| 1939 | } |
| 1940 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1941 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1942 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1943 | exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1944 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1945 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1946 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1947 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1948 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1949 | } |
| 1950 | return LY_EINCOMPLETE; |
| 1951 | } |
| 1952 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1953 | 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] | 1954 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1955 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1956 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1957 | } |
| 1958 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | return LY_SUCCESS; |
| 1962 | } |
| 1963 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 1964 | LY_ERR |
| 1965 | lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1, |
| 1966 | enum lyxp_token want_tok2) |
| 1967 | { |
| 1968 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 1969 | |
| 1970 | /* skip the token */ |
| 1971 | ++(*tok_idx); |
| 1972 | |
| 1973 | return LY_SUCCESS; |
| 1974 | } |
| 1975 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | /** |
| 1977 | * @brief Stack operation push on the repeat array. |
| 1978 | * |
| 1979 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1980 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1981 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1982 | */ |
| 1983 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1984 | exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | { |
| 1986 | uint16_t i; |
| 1987 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1989 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1990 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1991 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1992 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1993 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1994 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1995 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1996 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1997 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1998 | } |
| 1999 | } |
| 2000 | |
| 2001 | /** |
| 2002 | * @brief Reparse Predicate. Logs directly on error. |
| 2003 | * |
| 2004 | * [7] Predicate ::= '[' Expr ']' |
| 2005 | * |
| 2006 | * @param[in] ctx Context for logging. |
| 2007 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2008 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2009 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2010 | * @return LY_ERR |
| 2011 | */ |
| 2012 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2013 | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2014 | { |
| 2015 | LY_ERR rc; |
| 2016 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2017 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2018 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2019 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2020 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2021 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2022 | LY_CHECK_RET(rc); |
| 2023 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2024 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2025 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2026 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2027 | |
| 2028 | return LY_SUCCESS; |
| 2029 | } |
| 2030 | |
| 2031 | /** |
| 2032 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 2033 | * |
| 2034 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 2035 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 2036 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 2037 | * |
| 2038 | * @param[in] ctx Context for logging. |
| 2039 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2040 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2041 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2042 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 2043 | */ |
| 2044 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2045 | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2046 | { |
| 2047 | LY_ERR rc; |
| 2048 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2049 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2050 | LY_CHECK_RET(rc); |
| 2051 | |
| 2052 | goto step; |
| 2053 | do { |
| 2054 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2055 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2056 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2057 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2058 | LY_CHECK_RET(rc); |
| 2059 | step: |
| 2060 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2061 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2062 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2063 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2064 | break; |
| 2065 | |
| 2066 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2067 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2068 | break; |
| 2069 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2070 | case LYXP_TOKEN_AXISNAME: |
| 2071 | ++(*tok_idx); |
| 2072 | |
| 2073 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON); |
| 2074 | LY_CHECK_RET(rc); |
| 2075 | |
| 2076 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2077 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2078 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2079 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2080 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2081 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2082 | 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] | 2083 | 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] | 2084 | return LY_EVALID; |
| 2085 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2086 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) { |
| 2087 | goto reparse_nodetype; |
| 2088 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2089 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2090 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2091 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2092 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2093 | |
| 2094 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2095 | reparse_nodetype: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2096 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2097 | |
| 2098 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2099 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2100 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | |
| 2103 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2104 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2107 | |
| 2108 | reparse_predicate: |
| 2109 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2111 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2112 | LY_CHECK_RET(rc); |
| 2113 | } |
| 2114 | break; |
| 2115 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2116 | 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] | 2117 | return LY_EVALID; |
| 2118 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | } 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] | 2120 | |
| 2121 | return LY_SUCCESS; |
| 2122 | } |
| 2123 | |
| 2124 | /** |
| 2125 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2126 | * |
| 2127 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2128 | * |
| 2129 | * @param[in] ctx Context for logging. |
| 2130 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2131 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2132 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2133 | * @return LY_ERR |
| 2134 | */ |
| 2135 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2136 | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | { |
| 2138 | LY_ERR rc; |
| 2139 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2140 | 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] | 2141 | |
| 2142 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2143 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2144 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2145 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2147 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2148 | return LY_SUCCESS; |
| 2149 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2150 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2151 | case LYXP_TOKEN_DOT: |
| 2152 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2153 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2154 | case LYXP_TOKEN_AT: |
| 2155 | case LYXP_TOKEN_NAMETEST: |
| 2156 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2157 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2159 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2160 | default: |
| 2161 | break; |
| 2162 | } |
| 2163 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2164 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2165 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2166 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2168 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2169 | LY_CHECK_RET(rc); |
| 2170 | } |
| 2171 | |
| 2172 | return LY_SUCCESS; |
| 2173 | } |
| 2174 | |
| 2175 | /** |
| 2176 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2177 | * |
| 2178 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2179 | * |
| 2180 | * @param[in] ctx Context for logging. |
| 2181 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2182 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2183 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2184 | * @return LY_ERR |
| 2185 | */ |
| 2186 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2187 | reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2188 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2189 | int8_t min_arg_count = -1; |
| 2190 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2191 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2192 | LY_ERR rc; |
| 2193 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2194 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2195 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | func_tok_idx = *tok_idx; |
| 2197 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2198 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | min_arg_count = 1; |
| 2201 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2203 | min_arg_count = 1; |
| 2204 | max_arg_count = 1; |
| 2205 | } |
| 2206 | break; |
| 2207 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2208 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2209 | min_arg_count = 1; |
| 2210 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | min_arg_count = 0; |
| 2213 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2215 | min_arg_count = 0; |
| 2216 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | min_arg_count = 0; |
| 2219 | max_arg_count = 0; |
| 2220 | } |
| 2221 | break; |
| 2222 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | min_arg_count = 1; |
| 2225 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | min_arg_count = 0; |
| 2228 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | min_arg_count = 1; |
| 2231 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | min_arg_count = 1; |
| 2234 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2235 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2236 | min_arg_count = 1; |
| 2237 | max_arg_count = 1; |
| 2238 | } |
| 2239 | break; |
| 2240 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2242 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2243 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2244 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2245 | min_arg_count = 0; |
| 2246 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2248 | min_arg_count = 0; |
| 2249 | max_arg_count = 1; |
| 2250 | } |
| 2251 | break; |
| 2252 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2253 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2254 | min_arg_count = 1; |
| 2255 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | min_arg_count = 1; |
| 2258 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | min_arg_count = 0; |
| 2261 | max_arg_count = 0; |
| 2262 | } |
| 2263 | break; |
| 2264 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2265 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2266 | min_arg_count = 2; |
| 2267 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2268 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | min_arg_count = 0; |
| 2270 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | } 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] | 2272 | min_arg_count = 2; |
| 2273 | max_arg_count = 2; |
| 2274 | } |
| 2275 | break; |
| 2276 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2277 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2278 | min_arg_count = 2; |
| 2279 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2280 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2281 | min_arg_count = 3; |
| 2282 | max_arg_count = 3; |
| 2283 | } |
| 2284 | break; |
| 2285 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2286 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2287 | min_arg_count = 0; |
| 2288 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2289 | } 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] | 2290 | min_arg_count = 1; |
| 2291 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2292 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2293 | min_arg_count = 2; |
| 2294 | max_arg_count = 2; |
| 2295 | } |
| 2296 | break; |
| 2297 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2298 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2299 | min_arg_count = 2; |
| 2300 | max_arg_count = 2; |
| 2301 | } |
| 2302 | break; |
| 2303 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2304 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2305 | min_arg_count = 2; |
| 2306 | max_arg_count = 2; |
| 2307 | } |
| 2308 | break; |
| 2309 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2310 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2311 | min_arg_count = 0; |
| 2312 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2313 | } 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] | 2314 | min_arg_count = 0; |
| 2315 | max_arg_count = 1; |
| 2316 | } |
| 2317 | break; |
| 2318 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2320 | min_arg_count = 0; |
| 2321 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2322 | } 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] | 2323 | min_arg_count = 2; |
| 2324 | max_arg_count = 2; |
| 2325 | } |
| 2326 | break; |
| 2327 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | min_arg_count = 2; |
| 2330 | max_arg_count = 2; |
| 2331 | } |
| 2332 | break; |
| 2333 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2334 | 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] | 2335 | min_arg_count = 2; |
| 2336 | max_arg_count = 2; |
| 2337 | } |
| 2338 | break; |
| 2339 | } |
| 2340 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2341 | 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] | 2342 | return LY_EINVAL; |
| 2343 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2344 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2345 | |
| 2346 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2347 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2348 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2349 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2350 | |
| 2351 | /* ( Expr ( ',' Expr )* )? */ |
| 2352 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2353 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2354 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2355 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2356 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2357 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2358 | LY_CHECK_RET(rc); |
| 2359 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2360 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2361 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2362 | |
| 2363 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2364 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2365 | LY_CHECK_RET(rc); |
| 2366 | } |
| 2367 | |
| 2368 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2369 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2370 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2371 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2372 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2373 | 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] | 2374 | 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] | 2375 | return LY_EVALID; |
| 2376 | } |
| 2377 | |
| 2378 | return LY_SUCCESS; |
| 2379 | } |
| 2380 | |
| 2381 | /** |
| 2382 | * @brief Reparse PathExpr. Logs directly on error. |
| 2383 | * |
| 2384 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2385 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2386 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2387 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2388 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2389 | * |
| 2390 | * @param[in] ctx Context for logging. |
| 2391 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2392 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2393 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2394 | * @return LY_ERR |
| 2395 | */ |
| 2396 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2397 | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2398 | { |
| 2399 | LY_ERR rc; |
| 2400 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2401 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2402 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2403 | } |
| 2404 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2405 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2406 | case LYXP_TOKEN_PAR1: |
| 2407 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2408 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2409 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2410 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2411 | LY_CHECK_RET(rc); |
| 2412 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2413 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2414 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2415 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2416 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | case LYXP_TOKEN_DOT: |
| 2418 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2419 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2420 | case LYXP_TOKEN_AT: |
| 2421 | case LYXP_TOKEN_NAMETEST: |
| 2422 | case LYXP_TOKEN_NODETYPE: |
| 2423 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2424 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2425 | LY_CHECK_RET(rc); |
| 2426 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2427 | case LYXP_TOKEN_VARREF: |
| 2428 | /* VariableReference */ |
| 2429 | ++(*tok_idx); |
| 2430 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2431 | case LYXP_TOKEN_FUNCNAME: |
| 2432 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2433 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2434 | LY_CHECK_RET(rc); |
| 2435 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2436 | case LYXP_TOKEN_OPER_PATH: |
| 2437 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2438 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2439 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2440 | LY_CHECK_RET(rc); |
| 2441 | break; |
| 2442 | case LYXP_TOKEN_LITERAL: |
| 2443 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2444 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2445 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2446 | case LYXP_TOKEN_NUMBER: |
| 2447 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2448 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2449 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2450 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2451 | 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] | 2452 | return LY_EVALID; |
| 2453 | } |
| 2454 | |
| 2455 | return LY_SUCCESS; |
| 2456 | |
| 2457 | predicate: |
| 2458 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2459 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2460 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2461 | LY_CHECK_RET(rc); |
| 2462 | } |
| 2463 | |
| 2464 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2465 | 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] | 2466 | |
| 2467 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2468 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2469 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2470 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2471 | LY_CHECK_RET(rc); |
| 2472 | } |
| 2473 | |
| 2474 | return LY_SUCCESS; |
| 2475 | } |
| 2476 | |
| 2477 | /** |
| 2478 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2479 | * |
| 2480 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2481 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2482 | * |
| 2483 | * @param[in] ctx Context for logging. |
| 2484 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2485 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2486 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2487 | * @return LY_ERR |
| 2488 | */ |
| 2489 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2490 | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2491 | { |
| 2492 | uint16_t prev_exp; |
| 2493 | LY_ERR rc; |
| 2494 | |
| 2495 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2496 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2497 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2498 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2499 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2504 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2505 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | LY_CHECK_RET(rc); |
| 2507 | |
| 2508 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2510 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2512 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2513 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2514 | LY_CHECK_RET(rc); |
| 2515 | } |
| 2516 | |
| 2517 | return LY_SUCCESS; |
| 2518 | } |
| 2519 | |
| 2520 | /** |
| 2521 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2522 | * |
| 2523 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2524 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2525 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2526 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2527 | * | MultiplicativeExpr '*' UnaryExpr |
| 2528 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2529 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2530 | * |
| 2531 | * @param[in] ctx Context for logging. |
| 2532 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2533 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2534 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2535 | * @return LY_ERR |
| 2536 | */ |
| 2537 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2538 | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2539 | { |
| 2540 | uint16_t prev_add_exp, prev_mul_exp; |
| 2541 | LY_ERR rc; |
| 2542 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2543 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2544 | goto reparse_multiplicative_expr; |
| 2545 | |
| 2546 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2547 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2548 | ((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] | 2549 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2550 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2551 | |
| 2552 | reparse_multiplicative_expr: |
| 2553 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2554 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2555 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2556 | LY_CHECK_RET(rc); |
| 2557 | |
| 2558 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2559 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2560 | ((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] | 2561 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2562 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2563 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2564 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2565 | LY_CHECK_RET(rc); |
| 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | return LY_SUCCESS; |
| 2570 | } |
| 2571 | |
| 2572 | /** |
| 2573 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2574 | * |
| 2575 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2576 | * | EqualityExpr '!=' RelationalExpr |
| 2577 | * [14] RelationalExpr ::= AdditiveExpr |
| 2578 | * | RelationalExpr '<' AdditiveExpr |
| 2579 | * | RelationalExpr '>' AdditiveExpr |
| 2580 | * | RelationalExpr '<=' AdditiveExpr |
| 2581 | * | RelationalExpr '>=' AdditiveExpr |
| 2582 | * |
| 2583 | * @param[in] ctx Context for logging. |
| 2584 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2585 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2586 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2587 | * @return LY_ERR |
| 2588 | */ |
| 2589 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2590 | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2591 | { |
| 2592 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2593 | LY_ERR rc; |
| 2594 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2595 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2596 | goto reparse_additive_expr; |
| 2597 | |
| 2598 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2599 | 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] | 2600 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2601 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2602 | |
| 2603 | reparse_additive_expr: |
| 2604 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2605 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2606 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2607 | LY_CHECK_RET(rc); |
| 2608 | |
| 2609 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2610 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2611 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2612 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2613 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2614 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2615 | LY_CHECK_RET(rc); |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | return LY_SUCCESS; |
| 2620 | } |
| 2621 | |
| 2622 | /** |
| 2623 | * @brief Reparse OrExpr. Logs directly on error. |
| 2624 | * |
| 2625 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2626 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2627 | * |
| 2628 | * @param[in] ctx Context for logging. |
| 2629 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2630 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2631 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2632 | * @return LY_ERR |
| 2633 | */ |
| 2634 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2635 | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2636 | { |
| 2637 | uint16_t prev_or_exp, prev_and_exp; |
| 2638 | LY_ERR rc; |
| 2639 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2640 | ++depth; |
| 2641 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2642 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2643 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2644 | goto reparse_equality_expr; |
| 2645 | |
| 2646 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2647 | 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] | 2648 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2649 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2650 | |
| 2651 | reparse_equality_expr: |
| 2652 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2653 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2654 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2655 | LY_CHECK_RET(rc); |
| 2656 | |
| 2657 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2658 | 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] | 2659 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2660 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2661 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2662 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2663 | LY_CHECK_RET(rc); |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | return LY_SUCCESS; |
| 2668 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2669 | |
| 2670 | /** |
| 2671 | * @brief Parse NCName. |
| 2672 | * |
| 2673 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2674 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2675 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2676 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2677 | parse_ncname(const char *ncname) |
| 2678 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2679 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2680 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2681 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2682 | |
| 2683 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2684 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2685 | return len; |
| 2686 | } |
| 2687 | |
| 2688 | do { |
| 2689 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2690 | if (!*ncname) { |
| 2691 | break; |
| 2692 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2693 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2694 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2695 | |
| 2696 | return len; |
| 2697 | } |
| 2698 | |
| 2699 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2700 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2701 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2702 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2703 | * @param[in] exp Expression to use. |
| 2704 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2705 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2706 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2707 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2708 | */ |
| 2709 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2710 | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2711 | { |
| 2712 | uint32_t prev; |
| 2713 | |
| 2714 | if (exp->used == exp->size) { |
| 2715 | prev = exp->size; |
| 2716 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2717 | if (prev > exp->size) { |
| 2718 | LOGINT(ctx); |
| 2719 | return LY_EINT; |
| 2720 | } |
| 2721 | |
| 2722 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2723 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2724 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2725 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2726 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2727 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2728 | } |
| 2729 | |
| 2730 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2731 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2732 | exp->tok_len[exp->used] = tok_len; |
| 2733 | ++exp->used; |
| 2734 | return LY_SUCCESS; |
| 2735 | } |
| 2736 | |
| 2737 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2738 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2739 | { |
| 2740 | uint16_t i; |
| 2741 | |
| 2742 | if (!expr) { |
| 2743 | return; |
| 2744 | } |
| 2745 | |
| 2746 | lydict_remove(ctx, expr->expr); |
| 2747 | free(expr->tokens); |
| 2748 | free(expr->tok_pos); |
| 2749 | free(expr->tok_len); |
| 2750 | if (expr->repeat) { |
| 2751 | for (i = 0; i < expr->used; ++i) { |
| 2752 | free(expr->repeat[i]); |
| 2753 | } |
| 2754 | } |
| 2755 | free(expr->repeat); |
| 2756 | free(expr); |
| 2757 | } |
| 2758 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2759 | /** |
| 2760 | * @brief Parse Axis name. |
| 2761 | * |
| 2762 | * @param[in] str String to parse. |
| 2763 | * @param[in] str_len Length of @p str. |
| 2764 | * @return LY_SUCCESS if an axis. |
| 2765 | * @return LY_ENOT otherwise. |
| 2766 | */ |
| 2767 | static LY_ERR |
| 2768 | expr_parse_axis(const char *str, size_t str_len) |
| 2769 | { |
| 2770 | switch (str_len) { |
| 2771 | case 4: |
| 2772 | if (!strncmp("self", str, str_len)) { |
| 2773 | return LY_SUCCESS; |
| 2774 | } |
| 2775 | break; |
| 2776 | case 5: |
| 2777 | if (!strncmp("child", str, str_len)) { |
| 2778 | return LY_SUCCESS; |
| 2779 | } |
| 2780 | break; |
| 2781 | case 6: |
| 2782 | if (!strncmp("parent", str, str_len)) { |
| 2783 | return LY_SUCCESS; |
| 2784 | } |
| 2785 | break; |
| 2786 | case 8: |
| 2787 | if (!strncmp("ancestor", str, str_len)) { |
| 2788 | return LY_SUCCESS; |
| 2789 | } |
| 2790 | break; |
| 2791 | case 9: |
| 2792 | if (!strncmp("attribute", str, str_len)) { |
| 2793 | return LY_SUCCESS; |
| 2794 | } else if (!strncmp("following", str, str_len)) { |
| 2795 | return LY_SUCCESS; |
| 2796 | } else if (!strncmp("namespace", str, str_len)) { |
| 2797 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2798 | return LY_ENOT; |
| 2799 | } else if (!strncmp("preceding", str, str_len)) { |
| 2800 | return LY_SUCCESS; |
| 2801 | } |
| 2802 | break; |
| 2803 | case 10: |
| 2804 | if (!strncmp("descendant", str, str_len)) { |
| 2805 | return LY_SUCCESS; |
| 2806 | } |
| 2807 | break; |
| 2808 | case 16: |
| 2809 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2810 | return LY_SUCCESS; |
| 2811 | } |
| 2812 | break; |
| 2813 | case 17: |
| 2814 | if (!strncmp("following-sibling", str, str_len)) { |
| 2815 | return LY_SUCCESS; |
| 2816 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2817 | return LY_SUCCESS; |
| 2818 | } |
| 2819 | break; |
| 2820 | case 18: |
| 2821 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2822 | return LY_SUCCESS; |
| 2823 | } |
| 2824 | break; |
| 2825 | } |
| 2826 | |
| 2827 | return LY_ENOT; |
| 2828 | } |
| 2829 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2830 | LY_ERR |
| 2831 | 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] | 2832 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2833 | LY_ERR ret = LY_SUCCESS; |
| 2834 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2835 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2836 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2837 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2838 | uint16_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2839 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2840 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2841 | assert(expr_p); |
| 2842 | |
| 2843 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2844 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2845 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2849 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2850 | } |
| 2851 | if (expr_len > UINT16_MAX) { |
Michal Vasko | 623ac7b | 2021-04-09 12:44:23 +0200 | [diff] [blame] | 2852 | LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2853 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2857 | expr = calloc(1, sizeof *expr); |
| 2858 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2859 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2860 | expr->used = 0; |
| 2861 | expr->size = LYXP_EXPR_SIZE_START; |
| 2862 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2863 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2864 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2865 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2866 | 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] | 2867 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2868 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2869 | 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] | 2870 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2871 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2872 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2873 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2874 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2875 | ++parsed; |
| 2876 | } |
| 2877 | |
| 2878 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2879 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2880 | |
| 2881 | /* '(' */ |
| 2882 | tok_len = 1; |
| 2883 | tok_type = LYXP_TOKEN_PAR1; |
| 2884 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2885 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2886 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2887 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2888 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2889 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2890 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2891 | /* it is NodeType after all */ |
| 2892 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2893 | |
| 2894 | prev_ntype_check = 0; |
| 2895 | prev_func_check = 0; |
| 2896 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2897 | /* it is FunctionName after all */ |
| 2898 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2899 | |
| 2900 | prev_ntype_check = 0; |
| 2901 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2902 | } |
| 2903 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2904 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2905 | |
| 2906 | /* ')' */ |
| 2907 | tok_len = 1; |
| 2908 | tok_type = LYXP_TOKEN_PAR2; |
| 2909 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2910 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2911 | |
| 2912 | /* '[' */ |
| 2913 | tok_len = 1; |
| 2914 | tok_type = LYXP_TOKEN_BRACK1; |
| 2915 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2916 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2917 | |
| 2918 | /* ']' */ |
| 2919 | tok_len = 1; |
| 2920 | tok_type = LYXP_TOKEN_BRACK2; |
| 2921 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2922 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2923 | |
| 2924 | /* '..' */ |
| 2925 | tok_len = 2; |
| 2926 | tok_type = LYXP_TOKEN_DDOT; |
| 2927 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2928 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2929 | |
| 2930 | /* '.' */ |
| 2931 | tok_len = 1; |
| 2932 | tok_type = LYXP_TOKEN_DOT; |
| 2933 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2934 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2935 | |
| 2936 | /* '@' */ |
| 2937 | tok_len = 1; |
| 2938 | tok_type = LYXP_TOKEN_AT; |
| 2939 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2940 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2941 | |
| 2942 | /* ',' */ |
| 2943 | tok_len = 1; |
| 2944 | tok_type = LYXP_TOKEN_COMMA; |
| 2945 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2946 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2947 | |
| 2948 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2949 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2950 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2951 | 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] | 2952 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2953 | ++tok_len; |
| 2954 | tok_type = LYXP_TOKEN_LITERAL; |
| 2955 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2956 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2957 | |
| 2958 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2959 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2960 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2961 | 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] | 2962 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2963 | ++tok_len; |
| 2964 | tok_type = LYXP_TOKEN_LITERAL; |
| 2965 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2966 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2967 | |
| 2968 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2969 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2970 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2971 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2972 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2973 | } |
| 2974 | tok_type = LYXP_TOKEN_NUMBER; |
| 2975 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2976 | } else if (expr_str[parsed] == '$') { |
| 2977 | |
| 2978 | /* VariableReference */ |
| 2979 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2980 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2981 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 2982 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 2983 | tok_len = ncname_len; |
| 2984 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 2985 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 2986 | error); |
| 2987 | tok_type = LYXP_TOKEN_VARREF; |
| 2988 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2989 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2990 | |
| 2991 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2992 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2993 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2994 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2995 | } else { |
| 2996 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2997 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2998 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2999 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3000 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3001 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3002 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3003 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3004 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 3005 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3006 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3007 | |
| 3008 | /* Operator '<=', '>=' */ |
| 3009 | tok_len = 2; |
| 3010 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3011 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3012 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3013 | |
| 3014 | /* Operator '|' */ |
| 3015 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3016 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3017 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3018 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3019 | |
| 3020 | /* Operator '+', '-' */ |
| 3021 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3022 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3023 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3024 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3025 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3026 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3027 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3028 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3029 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3030 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3031 | |
| 3032 | /* Operator '<', '>' */ |
| 3033 | tok_len = 1; |
| 3034 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3035 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3036 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3037 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3038 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3039 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3040 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3041 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3042 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3043 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3044 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3045 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3046 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3047 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3048 | |
| 3049 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3050 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3051 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3052 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3053 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3054 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3055 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3056 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3057 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3058 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3059 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3060 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3061 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3062 | } 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] | 3063 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3064 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3065 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3066 | } else if (prev_ntype_check || prev_func_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3067 | 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] | 3068 | 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] | 3069 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3070 | goto error; |
| 3071 | } else { |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3072 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3073 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3074 | goto error; |
| 3075 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3076 | } else { |
| 3077 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3078 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3079 | if (expr_str[parsed] == '*') { |
| 3080 | ncname_len = 1; |
| 3081 | } else { |
| 3082 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3083 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3084 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3085 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3086 | tok_len = ncname_len; |
| 3087 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3088 | has_axis = 0; |
| 3089 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3090 | /* axis */ |
| 3091 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
| 3092 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error); |
| 3093 | tok_type = LYXP_TOKEN_AXISNAME; |
| 3094 | |
| 3095 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3096 | parsed += tok_len; |
| 3097 | |
| 3098 | /* '::' */ |
| 3099 | tok_len = 2; |
| 3100 | tok_type = LYXP_TOKEN_DCOLON; |
| 3101 | |
| 3102 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3103 | parsed += tok_len; |
| 3104 | |
| 3105 | if (expr_str[parsed] == '*') { |
| 3106 | ncname_len = 1; |
| 3107 | } else { |
| 3108 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3109 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 3110 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 3111 | } |
| 3112 | tok_len = ncname_len; |
| 3113 | |
| 3114 | has_axis = 1; |
| 3115 | } |
| 3116 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3117 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3118 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3119 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3120 | ++tok_len; |
| 3121 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3122 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 3123 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 3124 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3125 | tok_len += ncname_len; |
| 3126 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3127 | /* remove old flags to prevent ambiguities */ |
| 3128 | prev_ntype_check = 0; |
| 3129 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3130 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3131 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3132 | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 3133 | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
| 3134 | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3135 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3140 | 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] | 3141 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3142 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3143 | ++parsed; |
| 3144 | } |
| 3145 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3146 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3147 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3148 | if (reparse) { |
| 3149 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3150 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 3151 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3152 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3153 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 3154 | 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] | 3155 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3156 | 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] | 3157 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3158 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3159 | goto error; |
| 3160 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3161 | } |
| 3162 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3163 | print_expr_struct_debug(expr); |
| 3164 | *expr_p = expr; |
| 3165 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3166 | |
| 3167 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3168 | lyxp_expr_free(ctx, expr); |
| 3169 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3170 | } |
| 3171 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3172 | LY_ERR |
| 3173 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3174 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3175 | LY_ERR ret = LY_SUCCESS; |
| 3176 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3177 | uint32_t i, j; |
| 3178 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3179 | if (!exp) { |
| 3180 | goto cleanup; |
| 3181 | } |
| 3182 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3183 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3184 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3185 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3186 | if (exp->used) { |
| 3187 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 3188 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3189 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3190 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3191 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 3192 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3193 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3194 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3195 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 3196 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3197 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3198 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3199 | if (exp->repeat) { |
| 3200 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 3201 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3202 | for (i = 0; i < exp->used; ++i) { |
| 3203 | if (!exp->repeat[i]) { |
| 3204 | dup->repeat[i] = NULL; |
| 3205 | } else { |
| 3206 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3207 | /* the ending 0 as well */ |
| 3208 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3209 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3210 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
| 3211 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3212 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 3213 | dup->repeat[i][j - 1] = 0; |
| 3214 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3215 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | dup->used = exp->used; |
| 3220 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3221 | LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3222 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3223 | cleanup: |
| 3224 | if (ret) { |
| 3225 | lyxp_expr_free(ctx, dup); |
| 3226 | } else { |
| 3227 | *dup_p = dup; |
| 3228 | } |
| 3229 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3230 | } |
| 3231 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3232 | /** |
| 3233 | * @brief Get the last-added schema node that is currently in the context. |
| 3234 | * |
| 3235 | * @param[in] set Set to search in. |
| 3236 | * @return Last-added schema context node, NULL if no node is in context. |
| 3237 | */ |
| 3238 | static struct lysc_node * |
| 3239 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3240 | { |
| 3241 | uint32_t i; |
| 3242 | |
| 3243 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3244 | return NULL; |
| 3245 | } |
| 3246 | |
| 3247 | i = set->used; |
| 3248 | do { |
| 3249 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3250 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3251 | /* if there are more, simply return the first found (last added) */ |
| 3252 | return set->val.scnodes[i].scnode; |
| 3253 | } |
| 3254 | } while (i); |
| 3255 | |
| 3256 | return NULL; |
| 3257 | } |
| 3258 | |
| 3259 | /** |
| 3260 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3261 | * |
| 3262 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3263 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3264 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3265 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3266 | warn_is_numeric_type(struct lysc_type *type) |
| 3267 | { |
| 3268 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3269 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3270 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3271 | |
| 3272 | switch (type->basetype) { |
| 3273 | case LY_TYPE_DEC64: |
| 3274 | case LY_TYPE_INT8: |
| 3275 | case LY_TYPE_UINT8: |
| 3276 | case LY_TYPE_INT16: |
| 3277 | case LY_TYPE_UINT16: |
| 3278 | case LY_TYPE_INT32: |
| 3279 | case LY_TYPE_UINT32: |
| 3280 | case LY_TYPE_INT64: |
| 3281 | case LY_TYPE_UINT64: |
| 3282 | return 1; |
| 3283 | case LY_TYPE_UNION: |
| 3284 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3285 | LY_ARRAY_FOR(uni->types, u) { |
| 3286 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3287 | if (ret) { |
| 3288 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3289 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3290 | } |
| 3291 | } |
| 3292 | /* did not find any suitable type */ |
| 3293 | return 0; |
| 3294 | case LY_TYPE_LEAFREF: |
| 3295 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3296 | default: |
| 3297 | return 0; |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | /** |
| 3302 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3303 | * |
| 3304 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3305 | * @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] | 3306 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3307 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3308 | warn_is_string_type(struct lysc_type *type) |
| 3309 | { |
| 3310 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3311 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3312 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3313 | |
| 3314 | switch (type->basetype) { |
| 3315 | case LY_TYPE_BITS: |
| 3316 | case LY_TYPE_ENUM: |
| 3317 | case LY_TYPE_IDENT: |
| 3318 | case LY_TYPE_INST: |
| 3319 | case LY_TYPE_STRING: |
| 3320 | return 1; |
| 3321 | case LY_TYPE_UNION: |
| 3322 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3323 | LY_ARRAY_FOR(uni->types, u) { |
| 3324 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3325 | if (ret) { |
| 3326 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3327 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3328 | } |
| 3329 | } |
| 3330 | /* did not find any suitable type */ |
| 3331 | return 0; |
| 3332 | case LY_TYPE_LEAFREF: |
| 3333 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3334 | default: |
| 3335 | return 0; |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | /** |
| 3340 | * @brief Test whether a type is one specific type. |
| 3341 | * |
| 3342 | * @param[in] type Type to test. |
| 3343 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3344 | * @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] | 3345 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3346 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3347 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3348 | { |
| 3349 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3350 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3351 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3352 | |
| 3353 | if (type->basetype == base) { |
| 3354 | return 1; |
| 3355 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3356 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3357 | LY_ARRAY_FOR(uni->types, u) { |
| 3358 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3359 | if (ret) { |
| 3360 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3361 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | /* did not find any suitable type */ |
| 3365 | return 0; |
| 3366 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3367 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3368 | } |
| 3369 | |
| 3370 | return 0; |
| 3371 | } |
| 3372 | |
| 3373 | /** |
| 3374 | * @brief Get next type of a (union) type. |
| 3375 | * |
| 3376 | * @param[in] type Base type. |
| 3377 | * @param[in] prev_type Previously returned type. |
| 3378 | * @return Next type or NULL. |
| 3379 | */ |
| 3380 | static struct lysc_type * |
| 3381 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3382 | { |
| 3383 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3384 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3385 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3386 | |
| 3387 | switch (type->basetype) { |
| 3388 | case LY_TYPE_UNION: |
| 3389 | uni = (struct lysc_type_union *)type; |
| 3390 | if (!prev_type) { |
| 3391 | return uni->types[0]; |
| 3392 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3393 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3394 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3395 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3396 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3397 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3398 | found = 1; |
| 3399 | } |
| 3400 | } |
| 3401 | return NULL; |
| 3402 | default: |
| 3403 | if (prev_type) { |
| 3404 | assert(type == prev_type); |
| 3405 | return NULL; |
| 3406 | } else { |
| 3407 | return type; |
| 3408 | } |
| 3409 | } |
| 3410 | } |
| 3411 | |
| 3412 | /** |
| 3413 | * @brief Test whether 2 types have a common type. |
| 3414 | * |
| 3415 | * @param[in] type1 First type. |
| 3416 | * @param[in] type2 Second type. |
| 3417 | * @return 1 if they do, 0 otherwise. |
| 3418 | */ |
| 3419 | static int |
| 3420 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3421 | { |
| 3422 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3423 | |
| 3424 | t1 = NULL; |
| 3425 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3426 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3427 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3428 | } else { |
| 3429 | rt1 = t1; |
| 3430 | } |
| 3431 | |
| 3432 | t2 = NULL; |
| 3433 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3434 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3435 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3436 | } else { |
| 3437 | rt2 = t2; |
| 3438 | } |
| 3439 | |
| 3440 | if (rt2->basetype == rt1->basetype) { |
| 3441 | /* match found */ |
| 3442 | return 1; |
| 3443 | } |
| 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
| 3450 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3451 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3452 | * |
| 3453 | * @param[in] ctx Context for logging. |
| 3454 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3455 | * @param[in] subexpr Subexpression start. |
| 3456 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3457 | * @param[in] cur_scnode Expression context node. |
| 3458 | */ |
| 3459 | static void |
| 3460 | warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len, |
| 3461 | const struct lysc_node *cur_scnode) |
| 3462 | { |
| 3463 | char *path; |
| 3464 | |
| 3465 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 3466 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".", |
| 3467 | tok_pos, subexpr_len, subexpr, path); |
| 3468 | free(path); |
| 3469 | } |
| 3470 | |
| 3471 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3472 | * @brief Check both operands of comparison operators. |
| 3473 | * |
| 3474 | * @param[in] ctx Context for errors. |
| 3475 | * @param[in] set1 First operand set. |
| 3476 | * @param[in] set2 Second operand set. |
| 3477 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3478 | * @param[in] expr Start of the expression to print with the warning. |
| 3479 | * @param[in] tok_pos Token position. |
| 3480 | */ |
| 3481 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3482 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, |
| 3483 | uint16_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3484 | { |
| 3485 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3486 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3487 | |
| 3488 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3489 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3490 | |
| 3491 | if (!node1 && !node2) { |
| 3492 | /* no node-sets involved, nothing to do */ |
| 3493 | return; |
| 3494 | } |
| 3495 | |
| 3496 | if (node1) { |
| 3497 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3498 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3499 | warning = 1; |
| 3500 | leaves = 0; |
| 3501 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3502 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3503 | warning = 1; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | if (node2) { |
| 3508 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3509 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3510 | warning = 1; |
| 3511 | leaves = 0; |
| 3512 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3513 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3514 | warning = 1; |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3519 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3520 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3521 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3522 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3523 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3524 | warning = 1; |
| 3525 | } |
| 3526 | } |
| 3527 | |
| 3528 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3529 | 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] | 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | /** |
| 3534 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3535 | * |
| 3536 | * @param[in] exp Parsed XPath expression. |
| 3537 | * @param[in] set Set with the leaf/leaf-list. |
| 3538 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3539 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3540 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3541 | */ |
| 3542 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3543 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3544 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3545 | { |
| 3546 | struct lysc_node *scnode; |
| 3547 | struct lysc_type *type; |
| 3548 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3549 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3550 | LY_ERR rc; |
| 3551 | struct ly_err_item *err = NULL; |
| 3552 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3553 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3554 | ((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] | 3555 | /* check that the node can have the specified value */ |
| 3556 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3557 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3558 | } else { |
| 3559 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3560 | } |
| 3561 | if (!value) { |
| 3562 | LOGMEM(set->ctx); |
| 3563 | return; |
| 3564 | } |
| 3565 | |
| 3566 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3567 | 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] | 3568 | " 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] | 3569 | 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] | 3570 | (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] | 3571 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3572 | } |
| 3573 | |
| 3574 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3575 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3576 | 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] | 3577 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3578 | if (rc == LY_EINCOMPLETE) { |
| 3579 | rc = LY_SUCCESS; |
| 3580 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3581 | |
| 3582 | if (err) { |
| 3583 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3584 | ly_err_free(err); |
| 3585 | } else if (rc != LY_SUCCESS) { |
| 3586 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3587 | } |
| 3588 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3589 | 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] | 3590 | (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] | 3591 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3592 | } else { |
| 3593 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3594 | } |
| 3595 | } |
| 3596 | free(value); |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | /* |
| 3601 | * XPath functions |
| 3602 | */ |
| 3603 | |
| 3604 | /** |
| 3605 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3606 | * depending on whether the first node bit value from the second argument is set. |
| 3607 | * |
| 3608 | * @param[in] args Array of arguments. |
| 3609 | * @param[in] arg_count Count of elements in @p args. |
| 3610 | * @param[in,out] set Context and result set at the same time. |
| 3611 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3612 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3613 | */ |
| 3614 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3615 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3616 | { |
| 3617 | struct lyd_node_term *leaf; |
| 3618 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3619 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3620 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3621 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3622 | |
| 3623 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3624 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3625 | 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] | 3626 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3627 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3628 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3629 | sleaf->name); |
| 3630 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3631 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3632 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3636 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3637 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3638 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3639 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3640 | 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] | 3641 | } |
| 3642 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3643 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3644 | return rc; |
| 3645 | } |
| 3646 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3647 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3648 | 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] | 3649 | return LY_EVALID; |
| 3650 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3651 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3652 | LY_CHECK_RET(rc); |
| 3653 | |
| 3654 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3655 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3656 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3657 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3658 | LYD_VALUE_GET(&leaf->value, bits); |
| 3659 | LY_ARRAY_FOR(bits->items, u) { |
| 3660 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3661 | set_fill_boolean(set, 1); |
| 3662 | break; |
| 3663 | } |
| 3664 | } |
| 3665 | } |
| 3666 | } |
| 3667 | |
| 3668 | return LY_SUCCESS; |
| 3669 | } |
| 3670 | |
| 3671 | /** |
| 3672 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3673 | * with the argument converted to boolean. |
| 3674 | * |
| 3675 | * @param[in] args Array of arguments. |
| 3676 | * @param[in] arg_count Count of elements in @p args. |
| 3677 | * @param[in,out] set Context and result set at the same time. |
| 3678 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3679 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3680 | */ |
| 3681 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3682 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3683 | { |
| 3684 | LY_ERR rc; |
| 3685 | |
| 3686 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3687 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3688 | return LY_SUCCESS; |
| 3689 | } |
| 3690 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3691 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3692 | LY_CHECK_RET(rc); |
| 3693 | set_fill_set(set, args[0]); |
| 3694 | |
| 3695 | return LY_SUCCESS; |
| 3696 | } |
| 3697 | |
| 3698 | /** |
| 3699 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3700 | * with the first argument rounded up to the nearest integer. |
| 3701 | * |
| 3702 | * @param[in] args Array of arguments. |
| 3703 | * @param[in] arg_count Count of elements in @p args. |
| 3704 | * @param[in,out] set Context and result set at the same time. |
| 3705 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3706 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3707 | */ |
| 3708 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3709 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3710 | { |
| 3711 | struct lysc_node_leaf *sleaf; |
| 3712 | LY_ERR rc = LY_SUCCESS; |
| 3713 | |
| 3714 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3715 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3716 | 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] | 3717 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3718 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3719 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3720 | sleaf->name); |
| 3721 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3722 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3723 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3724 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3725 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3726 | return rc; |
| 3727 | } |
| 3728 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3729 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3730 | LY_CHECK_RET(rc); |
| 3731 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3732 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3733 | } else { |
| 3734 | set_fill_number(set, args[0]->val.num); |
| 3735 | } |
| 3736 | |
| 3737 | return LY_SUCCESS; |
| 3738 | } |
| 3739 | |
| 3740 | /** |
| 3741 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3742 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3743 | * |
| 3744 | * @param[in] args Array of arguments. |
| 3745 | * @param[in] arg_count Count of elements in @p args. |
| 3746 | * @param[in,out] set Context and result set at the same time. |
| 3747 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3748 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3749 | */ |
| 3750 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3751 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3752 | { |
| 3753 | uint16_t i; |
| 3754 | char *str = NULL; |
| 3755 | size_t used = 1; |
| 3756 | LY_ERR rc = LY_SUCCESS; |
| 3757 | struct lysc_node_leaf *sleaf; |
| 3758 | |
| 3759 | if (options & LYXP_SCNODE_ALL) { |
| 3760 | for (i = 0; i < arg_count; ++i) { |
| 3761 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3762 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3763 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3764 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3765 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3766 | 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] | 3767 | } |
| 3768 | } |
| 3769 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3770 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3771 | return rc; |
| 3772 | } |
| 3773 | |
| 3774 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3775 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3776 | if (rc != LY_SUCCESS) { |
| 3777 | free(str); |
| 3778 | return rc; |
| 3779 | } |
| 3780 | |
| 3781 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3782 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3783 | strcpy(str + used - 1, args[i]->val.str); |
| 3784 | used += strlen(args[i]->val.str); |
| 3785 | } |
| 3786 | |
| 3787 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3788 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3789 | set->type = LYXP_SET_STRING; |
| 3790 | set->val.str = str; |
| 3791 | |
| 3792 | return LY_SUCCESS; |
| 3793 | } |
| 3794 | |
| 3795 | /** |
| 3796 | * @brief Execute the XPath contains(string, string) function. |
| 3797 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3798 | * be found in the first or not. |
| 3799 | * |
| 3800 | * @param[in] args Array of arguments. |
| 3801 | * @param[in] arg_count Count of elements in @p args. |
| 3802 | * @param[in,out] set Context and result set at the same time. |
| 3803 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3804 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3805 | */ |
| 3806 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3807 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3808 | { |
| 3809 | struct lysc_node_leaf *sleaf; |
| 3810 | LY_ERR rc = LY_SUCCESS; |
| 3811 | |
| 3812 | if (options & LYXP_SCNODE_ALL) { |
| 3813 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3814 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3815 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3816 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3817 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3818 | 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] | 3819 | } |
| 3820 | } |
| 3821 | |
| 3822 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3823 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3824 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3825 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3826 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3827 | 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] | 3828 | } |
| 3829 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3830 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3831 | return rc; |
| 3832 | } |
| 3833 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3834 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3835 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3836 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3837 | LY_CHECK_RET(rc); |
| 3838 | |
| 3839 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3840 | set_fill_boolean(set, 1); |
| 3841 | } else { |
| 3842 | set_fill_boolean(set, 0); |
| 3843 | } |
| 3844 | |
| 3845 | return LY_SUCCESS; |
| 3846 | } |
| 3847 | |
| 3848 | /** |
| 3849 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3850 | * with the size of the node-set from the argument. |
| 3851 | * |
| 3852 | * @param[in] args Array of arguments. |
| 3853 | * @param[in] arg_count Count of elements in @p args. |
| 3854 | * @param[in,out] set Context and result set at the same time. |
| 3855 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3856 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3857 | */ |
| 3858 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3859 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3860 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3861 | LY_ERR rc = LY_SUCCESS; |
| 3862 | |
| 3863 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3864 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3865 | 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] | 3866 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3867 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3868 | return rc; |
| 3869 | } |
| 3870 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3871 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3872 | 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] | 3873 | return LY_EVALID; |
| 3874 | } |
| 3875 | |
| 3876 | set_fill_number(set, args[0]->used); |
| 3877 | return LY_SUCCESS; |
| 3878 | } |
| 3879 | |
| 3880 | /** |
| 3881 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3882 | * with the context with the intial node. |
| 3883 | * |
| 3884 | * @param[in] args Array of arguments. |
| 3885 | * @param[in] arg_count Count of elements in @p args. |
| 3886 | * @param[in,out] set Context and result set at the same time. |
| 3887 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3888 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3889 | */ |
| 3890 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3891 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3892 | { |
| 3893 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3894 | 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] | 3895 | return LY_EVALID; |
| 3896 | } |
| 3897 | |
| 3898 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3899 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3900 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3901 | if (set->cur_scnode) { |
| 3902 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL)); |
| 3903 | } else { |
| 3904 | /* root node */ |
| 3905 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL)); |
| 3906 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3907 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3908 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3909 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3910 | if (set->cur_node) { |
| 3911 | /* position is filled later */ |
| 3912 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3913 | } else { |
| 3914 | /* root node */ |
| 3915 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3916 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | return LY_SUCCESS; |
| 3920 | } |
| 3921 | |
| 3922 | /** |
| 3923 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3924 | * leafref or instance-identifier target node(s). |
| 3925 | * |
| 3926 | * @param[in] args Array of arguments. |
| 3927 | * @param[in] arg_count Count of elements in @p args. |
| 3928 | * @param[in,out] set Context and result set at the same time. |
| 3929 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3930 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3931 | */ |
| 3932 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3933 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3934 | { |
| 3935 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3936 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3937 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3938 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3939 | struct ly_path *p; |
| 3940 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3941 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3942 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3943 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3944 | |
| 3945 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3946 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3947 | 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] | 3948 | } 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] | 3949 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3950 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3951 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3952 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3953 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3954 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3955 | __func__, sleaf->name); |
| 3956 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3957 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3958 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 3959 | 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] | 3960 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 3961 | 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] | 3962 | |
| 3963 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3964 | 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] | 3965 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3966 | if (!r) { |
| 3967 | /* get the target node */ |
| 3968 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 3969 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3970 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3971 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL)); |
| 3972 | } /* 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] | 3973 | } |
| 3974 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3975 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3976 | } |
| 3977 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3978 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3979 | 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] | 3980 | return LY_EVALID; |
| 3981 | } |
| 3982 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3983 | lyxp_set_free_content(set); |
| 3984 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3985 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3986 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3987 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3988 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3989 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 3990 | 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] | 3991 | &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3992 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3993 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3994 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3995 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3996 | } else { |
| 3997 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3998 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3999 | 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] | 4000 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4001 | return LY_EVALID; |
| 4002 | } |
| 4003 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4004 | |
| 4005 | /* insert it */ |
| 4006 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | return LY_SUCCESS; |
| 4011 | } |
| 4012 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4013 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4014 | 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] | 4015 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 4016 | uint32_t i; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4017 | LY_ARRAY_COUNT_TYPE u; |
| 4018 | struct lyd_node_term *leaf; |
| 4019 | struct lysc_node_leaf *sleaf; |
| 4020 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4021 | struct lyd_value *val; |
| 4022 | const struct lys_module *mod; |
| 4023 | const char *id_name; |
| 4024 | uint16_t id_len; |
| 4025 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4026 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4027 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4028 | |
| 4029 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4030 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4031 | 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] | 4032 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4033 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4034 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4035 | sleaf->name); |
| 4036 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4037 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4038 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4042 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4043 | 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] | 4044 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4045 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4046 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4047 | } |
| 4048 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4049 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4050 | return rc; |
| 4051 | } |
| 4052 | |
| 4053 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4054 | 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] | 4055 | return LY_EVALID; |
| 4056 | } |
| 4057 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4058 | LY_CHECK_RET(rc); |
| 4059 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4060 | /* parse the identity */ |
| 4061 | id_name = args[1]->val.str; |
| 4062 | id_len = strlen(id_name); |
| 4063 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4064 | LY_CHECK_RET(rc); |
| 4065 | if (!mod) { |
| 4066 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4067 | return LY_EVALID; |
| 4068 | } |
| 4069 | |
| 4070 | /* find the identity */ |
| 4071 | found = 0; |
| 4072 | LY_ARRAY_FOR(mod->identities, u) { |
| 4073 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4074 | /* we have match */ |
| 4075 | found = 1; |
| 4076 | break; |
| 4077 | } |
| 4078 | } |
| 4079 | if (!found) { |
| 4080 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4081 | return LY_EVALID; |
| 4082 | } |
| 4083 | id = &mod->identities[u]; |
| 4084 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4085 | set_fill_boolean(set, 0); |
| 4086 | found = 0; |
| 4087 | for (i = 0; i < args[0]->used; ++i) { |
| 4088 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4089 | continue; |
| 4090 | } |
| 4091 | |
| 4092 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4093 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4094 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4095 | val = &leaf->value; |
| 4096 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 4097 | /* uninteresting */ |
| 4098 | continue; |
| 4099 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4100 | } else { |
| 4101 | meta = args[0]->val.meta[i].meta; |
| 4102 | val = &meta->value; |
| 4103 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4104 | /* uninteresting */ |
| 4105 | continue; |
| 4106 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4107 | } |
| 4108 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4109 | /* check the identity itself */ |
| 4110 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4111 | set_fill_boolean(set, 1); |
| 4112 | found = 1; |
| 4113 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4114 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4115 | set_fill_boolean(set, 1); |
| 4116 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4117 | } |
| 4118 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4119 | if (found) { |
| 4120 | break; |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | return LY_SUCCESS; |
| 4125 | } |
| 4126 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4127 | /** |
| 4128 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4129 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4130 | * argument identity. |
| 4131 | * |
| 4132 | * @param[in] args Array of arguments. |
| 4133 | * @param[in] arg_count Count of elements in @p args. |
| 4134 | * @param[in,out] set Context and result set at the same time. |
| 4135 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4136 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4137 | */ |
| 4138 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4139 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4140 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4141 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | /** |
| 4145 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4146 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4147 | * the second argument identity. |
| 4148 | * |
| 4149 | * @param[in] args Array of arguments. |
| 4150 | * @param[in] arg_count Count of elements in @p args. |
| 4151 | * @param[in,out] set Context and result set at the same time. |
| 4152 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4153 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4154 | */ |
| 4155 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4156 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4157 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4158 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | /** |
| 4162 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4163 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4164 | * |
| 4165 | * @param[in] args Array of arguments. |
| 4166 | * @param[in] arg_count Count of elements in @p args. |
| 4167 | * @param[in,out] set Context and result set at the same time. |
| 4168 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4169 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4170 | */ |
| 4171 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4172 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4173 | { |
| 4174 | struct lyd_node_term *leaf; |
| 4175 | struct lysc_node_leaf *sleaf; |
| 4176 | LY_ERR rc = LY_SUCCESS; |
| 4177 | |
| 4178 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4179 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4180 | 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] | 4181 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4182 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4183 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4184 | sleaf->name); |
| 4185 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4186 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4187 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4188 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4189 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4190 | return rc; |
| 4191 | } |
| 4192 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4193 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4194 | 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] | 4195 | return LY_EVALID; |
| 4196 | } |
| 4197 | |
| 4198 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4199 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4200 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4201 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4202 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4203 | set_fill_number(set, leaf->value.enum_item->value); |
| 4204 | } |
| 4205 | } |
| 4206 | |
| 4207 | return LY_SUCCESS; |
| 4208 | } |
| 4209 | |
| 4210 | /** |
| 4211 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4212 | * with false value. |
| 4213 | * |
| 4214 | * @param[in] args Array of arguments. |
| 4215 | * @param[in] arg_count Count of elements in @p args. |
| 4216 | * @param[in,out] set Context and result set at the same time. |
| 4217 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4218 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4219 | */ |
| 4220 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4221 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | { |
| 4223 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4224 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4225 | return LY_SUCCESS; |
| 4226 | } |
| 4227 | |
| 4228 | set_fill_boolean(set, 0); |
| 4229 | return LY_SUCCESS; |
| 4230 | } |
| 4231 | |
| 4232 | /** |
| 4233 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4234 | * with the first argument floored (truncated). |
| 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 |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4243 | xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4244 | { |
| 4245 | LY_ERR rc; |
| 4246 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4247 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4248 | LY_CHECK_RET(rc); |
| 4249 | if (isfinite(args[0]->val.num)) { |
| 4250 | set_fill_number(set, (long long)args[0]->val.num); |
| 4251 | } |
| 4252 | |
| 4253 | return LY_SUCCESS; |
| 4254 | } |
| 4255 | |
| 4256 | /** |
| 4257 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4258 | * whether the language of the text matches the one from the argument. |
| 4259 | * |
| 4260 | * @param[in] args Array of arguments. |
| 4261 | * @param[in] arg_count Count of elements in @p args. |
| 4262 | * @param[in,out] set Context and result set at the same time. |
| 4263 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4264 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4265 | */ |
| 4266 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4267 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4268 | { |
| 4269 | const struct lyd_node *node; |
| 4270 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4271 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4272 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4273 | LY_ERR rc = LY_SUCCESS; |
| 4274 | |
| 4275 | if (options & LYXP_SCNODE_ALL) { |
| 4276 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4277 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4278 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4279 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4280 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4281 | 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] | 4282 | } |
| 4283 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4284 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4285 | return rc; |
| 4286 | } |
| 4287 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4288 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4289 | LY_CHECK_RET(rc); |
| 4290 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4291 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4292 | 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] | 4293 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4294 | } else if (!set->used) { |
| 4295 | set_fill_boolean(set, 0); |
| 4296 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | switch (set->val.nodes[0].type) { |
| 4300 | case LYXP_NODE_ELEM: |
| 4301 | case LYXP_NODE_TEXT: |
| 4302 | node = set->val.nodes[0].node; |
| 4303 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4304 | case LYXP_NODE_META: |
| 4305 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4306 | break; |
| 4307 | default: |
| 4308 | /* nothing to do with roots */ |
| 4309 | set_fill_boolean(set, 0); |
| 4310 | return LY_SUCCESS; |
| 4311 | } |
| 4312 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4313 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4314 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4315 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4316 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4317 | 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] | 4318 | break; |
| 4319 | } |
| 4320 | } |
| 4321 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4322 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4323 | break; |
| 4324 | } |
| 4325 | } |
| 4326 | |
| 4327 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4328 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4329 | set_fill_boolean(set, 0); |
| 4330 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4331 | uint64_t i; |
| 4332 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4333 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4334 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4335 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4336 | set_fill_boolean(set, 0); |
| 4337 | break; |
| 4338 | } |
| 4339 | } |
| 4340 | if (!args[0]->val.str[i]) { |
| 4341 | if (!val[i] || (val[i] == '-')) { |
| 4342 | set_fill_boolean(set, 1); |
| 4343 | } else { |
| 4344 | set_fill_boolean(set, 0); |
| 4345 | } |
| 4346 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | return LY_SUCCESS; |
| 4350 | } |
| 4351 | |
| 4352 | /** |
| 4353 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4354 | * with the context size. |
| 4355 | * |
| 4356 | * @param[in] args Array of arguments. |
| 4357 | * @param[in] arg_count Count of elements in @p args. |
| 4358 | * @param[in,out] set Context and result set at the same time. |
| 4359 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4360 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4361 | */ |
| 4362 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4363 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4364 | { |
| 4365 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4366 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4367 | return LY_SUCCESS; |
| 4368 | } |
| 4369 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4370 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4371 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4372 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4373 | } else if (!set->used) { |
| 4374 | set_fill_number(set, 0); |
| 4375 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | set_fill_number(set, set->ctx_size); |
| 4379 | return LY_SUCCESS; |
| 4380 | } |
| 4381 | |
| 4382 | /** |
| 4383 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4384 | * with the node name without namespace from the argument or the context. |
| 4385 | * |
| 4386 | * @param[in] args Array of arguments. |
| 4387 | * @param[in] arg_count Count of elements in @p args. |
| 4388 | * @param[in,out] set Context and result set at the same time. |
| 4389 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4390 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4391 | */ |
| 4392 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4393 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4394 | { |
| 4395 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4396 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4397 | /* suppress unused variable warning */ |
| 4398 | (void)options; |
| 4399 | |
| 4400 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4401 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4402 | return LY_SUCCESS; |
| 4403 | } |
| 4404 | |
| 4405 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4406 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4407 | 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] | 4408 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4409 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4410 | } else if (!args[0]->used) { |
| 4411 | set_fill_string(set, "", 0); |
| 4412 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4416 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4417 | |
| 4418 | item = &args[0]->val.nodes[0]; |
| 4419 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4420 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4421 | 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] | 4422 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4423 | } else if (!set->used) { |
| 4424 | set_fill_string(set, "", 0); |
| 4425 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4429 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4430 | |
| 4431 | item = &set->val.nodes[0]; |
| 4432 | } |
| 4433 | |
| 4434 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4435 | case LYXP_NODE_NONE: |
| 4436 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4437 | case LYXP_NODE_ROOT: |
| 4438 | case LYXP_NODE_ROOT_CONFIG: |
| 4439 | case LYXP_NODE_TEXT: |
| 4440 | set_fill_string(set, "", 0); |
| 4441 | break; |
| 4442 | case LYXP_NODE_ELEM: |
| 4443 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4444 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4445 | case LYXP_NODE_META: |
| 4446 | 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] | 4447 | break; |
| 4448 | } |
| 4449 | |
| 4450 | return LY_SUCCESS; |
| 4451 | } |
| 4452 | |
| 4453 | /** |
| 4454 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4455 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4456 | * |
| 4457 | * @param[in] args Array of arguments. |
| 4458 | * @param[in] arg_count Count of elements in @p args. |
| 4459 | * @param[in,out] set Context and result set at the same time. |
| 4460 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4461 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4462 | */ |
| 4463 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4464 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4465 | { |
| 4466 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4467 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4468 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4469 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4470 | |
| 4471 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4472 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4473 | return LY_SUCCESS; |
| 4474 | } |
| 4475 | |
| 4476 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4477 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4478 | 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] | 4479 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4480 | } else if (!args[0]->used) { |
| 4481 | set_fill_string(set, "", 0); |
| 4482 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4486 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4487 | |
| 4488 | item = &args[0]->val.nodes[0]; |
| 4489 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4490 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4491 | 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] | 4492 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4493 | } else if (!set->used) { |
| 4494 | set_fill_string(set, "", 0); |
| 4495 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4499 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4500 | |
| 4501 | item = &set->val.nodes[0]; |
| 4502 | } |
| 4503 | |
| 4504 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4505 | case LYXP_NODE_NONE: |
| 4506 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4507 | case LYXP_NODE_ROOT: |
| 4508 | case LYXP_NODE_ROOT_CONFIG: |
| 4509 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4510 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4511 | break; |
| 4512 | case LYXP_NODE_ELEM: |
| 4513 | mod = item->node->schema->module; |
| 4514 | name = item->node->schema->name; |
| 4515 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4516 | case LYXP_NODE_META: |
| 4517 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4518 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4519 | break; |
| 4520 | } |
| 4521 | |
| 4522 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4523 | int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4524 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4525 | set_fill_string(set, str, strlen(str)); |
| 4526 | free(str); |
| 4527 | } else { |
| 4528 | set_fill_string(set, "", 0); |
| 4529 | } |
| 4530 | |
| 4531 | return LY_SUCCESS; |
| 4532 | } |
| 4533 | |
| 4534 | /** |
| 4535 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4536 | * with the namespace of the node from the argument or the context. |
| 4537 | * |
| 4538 | * @param[in] args Array of arguments. |
| 4539 | * @param[in] arg_count Count of elements in @p args. |
| 4540 | * @param[in,out] set Context and result set at the same time. |
| 4541 | * @param[in] options XPath options. |
| 4542 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4543 | */ |
| 4544 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4545 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4546 | { |
| 4547 | struct lyxp_set_node *item; |
| 4548 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4549 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4550 | /* suppress unused variable warning */ |
| 4551 | (void)options; |
| 4552 | |
| 4553 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4554 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4555 | return LY_SUCCESS; |
| 4556 | } |
| 4557 | |
| 4558 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4559 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4560 | 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] | 4561 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4562 | return LY_EVALID; |
| 4563 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4564 | set_fill_string(set, "", 0); |
| 4565 | return LY_SUCCESS; |
| 4566 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4567 | |
| 4568 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4569 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4570 | |
| 4571 | item = &args[0]->val.nodes[0]; |
| 4572 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4573 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4574 | 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] | 4575 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4576 | } else if (!set->used) { |
| 4577 | set_fill_string(set, "", 0); |
| 4578 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4582 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4583 | |
| 4584 | item = &set->val.nodes[0]; |
| 4585 | } |
| 4586 | |
| 4587 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4588 | case LYXP_NODE_NONE: |
| 4589 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4590 | case LYXP_NODE_ROOT: |
| 4591 | case LYXP_NODE_ROOT_CONFIG: |
| 4592 | case LYXP_NODE_TEXT: |
| 4593 | set_fill_string(set, "", 0); |
| 4594 | break; |
| 4595 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4596 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4597 | if (item->type == LYXP_NODE_ELEM) { |
| 4598 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4599 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4600 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4601 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4605 | break; |
| 4606 | } |
| 4607 | |
| 4608 | return LY_SUCCESS; |
| 4609 | } |
| 4610 | |
| 4611 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4612 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4613 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4614 | * from the argument or the context. |
| 4615 | * |
| 4616 | * @param[in] args Array of arguments. |
| 4617 | * @param[in] arg_count Count of elements in @p args. |
| 4618 | * @param[in,out] set Context and result set at the same time. |
| 4619 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4620 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4621 | */ |
| 4622 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4623 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4624 | { |
| 4625 | uint16_t i, new_used; |
| 4626 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4627 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4628 | struct lysc_node_leaf *sleaf; |
| 4629 | LY_ERR rc = LY_SUCCESS; |
| 4630 | |
| 4631 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4632 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4633 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4634 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4635 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4636 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4637 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4638 | 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] | 4639 | } |
| 4640 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4641 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4642 | return rc; |
| 4643 | } |
| 4644 | |
| 4645 | if (arg_count) { |
| 4646 | set_fill_set(set, args[0]); |
| 4647 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4648 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4649 | LY_CHECK_RET(rc); |
| 4650 | |
| 4651 | /* is there any normalization necessary? */ |
| 4652 | for (i = 0; set->val.str[i]; ++i) { |
| 4653 | if (is_xmlws(set->val.str[i])) { |
| 4654 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4655 | have_spaces = 1; |
| 4656 | break; |
| 4657 | } |
| 4658 | space_before = 1; |
| 4659 | } else { |
| 4660 | space_before = 0; |
| 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | /* yep, there is */ |
| 4665 | if (have_spaces) { |
| 4666 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4667 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4668 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4669 | new_used = 0; |
| 4670 | |
| 4671 | space_before = 0; |
| 4672 | for (i = 0; set->val.str[i]; ++i) { |
| 4673 | if (is_xmlws(set->val.str[i])) { |
| 4674 | if ((i == 0) || space_before) { |
| 4675 | space_before = 1; |
| 4676 | continue; |
| 4677 | } else { |
| 4678 | space_before = 1; |
| 4679 | } |
| 4680 | } else { |
| 4681 | space_before = 0; |
| 4682 | } |
| 4683 | |
| 4684 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4685 | ++new_used; |
| 4686 | } |
| 4687 | |
| 4688 | /* at worst there is one trailing space now */ |
| 4689 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4690 | --new_used; |
| 4691 | } |
| 4692 | |
| 4693 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4694 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4695 | new[new_used] = '\0'; |
| 4696 | |
| 4697 | free(set->val.str); |
| 4698 | set->val.str = new; |
| 4699 | } |
| 4700 | |
| 4701 | return LY_SUCCESS; |
| 4702 | } |
| 4703 | |
| 4704 | /** |
| 4705 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4706 | * with the argument converted to boolean and logically inverted. |
| 4707 | * |
| 4708 | * @param[in] args Array of arguments. |
| 4709 | * @param[in] arg_count Count of elements in @p args. |
| 4710 | * @param[in,out] set Context and result set at the same time. |
| 4711 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4712 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4713 | */ |
| 4714 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4715 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4716 | { |
| 4717 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4718 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4719 | return LY_SUCCESS; |
| 4720 | } |
| 4721 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4722 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4723 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4724 | set_fill_boolean(set, 0); |
| 4725 | } else { |
| 4726 | set_fill_boolean(set, 1); |
| 4727 | } |
| 4728 | |
| 4729 | return LY_SUCCESS; |
| 4730 | } |
| 4731 | |
| 4732 | /** |
| 4733 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4734 | * with the number representation of either the argument or the context. |
| 4735 | * |
| 4736 | * @param[in] args Array of arguments. |
| 4737 | * @param[in] arg_count Count of elements in @p args. |
| 4738 | * @param[in,out] set Context and result set at the same time. |
| 4739 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4740 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4741 | */ |
| 4742 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4743 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4744 | { |
| 4745 | LY_ERR rc; |
| 4746 | |
| 4747 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4748 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4749 | return LY_SUCCESS; |
| 4750 | } |
| 4751 | |
| 4752 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4753 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4754 | LY_CHECK_RET(rc); |
| 4755 | set_fill_set(set, args[0]); |
| 4756 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4757 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4758 | LY_CHECK_RET(rc); |
| 4759 | } |
| 4760 | |
| 4761 | return LY_SUCCESS; |
| 4762 | } |
| 4763 | |
| 4764 | /** |
| 4765 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4766 | * with the context position. |
| 4767 | * |
| 4768 | * @param[in] args Array of arguments. |
| 4769 | * @param[in] arg_count Count of elements in @p args. |
| 4770 | * @param[in,out] set Context and result set at the same time. |
| 4771 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4772 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4773 | */ |
| 4774 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4775 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4776 | { |
| 4777 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4778 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4779 | return LY_SUCCESS; |
| 4780 | } |
| 4781 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4782 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4783 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4784 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4785 | } else if (!set->used) { |
| 4786 | set_fill_number(set, 0); |
| 4787 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | set_fill_number(set, set->ctx_pos); |
| 4791 | |
| 4792 | /* UNUSED in 'Release' build type */ |
| 4793 | (void)options; |
| 4794 | return LY_SUCCESS; |
| 4795 | } |
| 4796 | |
| 4797 | /** |
| 4798 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4799 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4800 | * YANG 1.1 RFC section 10.2.1. |
| 4801 | * |
| 4802 | * @param[in] args Array of arguments. |
| 4803 | * @param[in] arg_count Count of elements in @p args. |
| 4804 | * @param[in,out] set Context and result set at the same time. |
| 4805 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4806 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4807 | */ |
| 4808 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4809 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4810 | { |
| 4811 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4812 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4813 | LY_ERR rc = LY_SUCCESS; |
| 4814 | struct ly_err_item *err; |
| 4815 | |
| 4816 | if (options & LYXP_SCNODE_ALL) { |
| 4817 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4818 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4819 | 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] | 4820 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4821 | 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] | 4822 | } |
| 4823 | } |
| 4824 | |
| 4825 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4826 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4827 | 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] | 4828 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4829 | 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] | 4830 | } |
| 4831 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4832 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4833 | return rc; |
| 4834 | } |
| 4835 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4836 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4837 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4838 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4839 | LY_CHECK_RET(rc); |
| 4840 | |
| 4841 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4842 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4843 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4844 | 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] | 4845 | if (set->cur_node) { |
| 4846 | LOG_LOCBACK(0, 1, 0, 0); |
| 4847 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4848 | if (rc != LY_SUCCESS) { |
| 4849 | LY_ARRAY_FREE(patterns); |
| 4850 | return rc; |
| 4851 | } |
| 4852 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4853 | 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] | 4854 | pcre2_code_free((*pattern)->code); |
| 4855 | free(*pattern); |
| 4856 | LY_ARRAY_FREE(patterns); |
| 4857 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4858 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4859 | ly_err_free(err); |
| 4860 | return rc; |
| 4861 | } |
| 4862 | |
| 4863 | if (rc == LY_EVALID) { |
| 4864 | ly_err_free(err); |
| 4865 | set_fill_boolean(set, 0); |
| 4866 | } else { |
| 4867 | set_fill_boolean(set, 1); |
| 4868 | } |
| 4869 | |
| 4870 | return LY_SUCCESS; |
| 4871 | } |
| 4872 | |
| 4873 | /** |
| 4874 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4875 | * with the rounded first argument. For details refer to |
| 4876 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4877 | * |
| 4878 | * @param[in] args Array of arguments. |
| 4879 | * @param[in] arg_count Count of elements in @p args. |
| 4880 | * @param[in,out] set Context and result set at the same time. |
| 4881 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4882 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4883 | */ |
| 4884 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4885 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4886 | { |
| 4887 | struct lysc_node_leaf *sleaf; |
| 4888 | LY_ERR rc = LY_SUCCESS; |
| 4889 | |
| 4890 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4891 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4892 | 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] | 4893 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4894 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4895 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4896 | sleaf->name); |
| 4897 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4898 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4899 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4900 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4901 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4902 | return rc; |
| 4903 | } |
| 4904 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4905 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4906 | LY_CHECK_RET(rc); |
| 4907 | |
| 4908 | /* cover only the cases where floor can't be used */ |
| 4909 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4910 | set_fill_number(set, -0.0f); |
| 4911 | } else { |
| 4912 | args[0]->val.num += 0.5; |
| 4913 | rc = xpath_floor(args, 1, args[0], options); |
| 4914 | LY_CHECK_RET(rc); |
| 4915 | set_fill_number(set, args[0]->val.num); |
| 4916 | } |
| 4917 | |
| 4918 | return LY_SUCCESS; |
| 4919 | } |
| 4920 | |
| 4921 | /** |
| 4922 | * @brief Execute the XPath starts-with(string, string) function. |
| 4923 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4924 | * the prefix of the first or not. |
| 4925 | * |
| 4926 | * @param[in] args Array of arguments. |
| 4927 | * @param[in] arg_count Count of elements in @p args. |
| 4928 | * @param[in,out] set Context and result set at the same time. |
| 4929 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4930 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4931 | */ |
| 4932 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4933 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4934 | { |
| 4935 | struct lysc_node_leaf *sleaf; |
| 4936 | LY_ERR rc = LY_SUCCESS; |
| 4937 | |
| 4938 | if (options & LYXP_SCNODE_ALL) { |
| 4939 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4940 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4941 | 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] | 4942 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4943 | 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] | 4944 | } |
| 4945 | } |
| 4946 | |
| 4947 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4948 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4949 | 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] | 4950 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4951 | 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] | 4952 | } |
| 4953 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4954 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4955 | return rc; |
| 4956 | } |
| 4957 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4958 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4959 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4960 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4961 | LY_CHECK_RET(rc); |
| 4962 | |
| 4963 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4964 | set_fill_boolean(set, 0); |
| 4965 | } else { |
| 4966 | set_fill_boolean(set, 1); |
| 4967 | } |
| 4968 | |
| 4969 | return LY_SUCCESS; |
| 4970 | } |
| 4971 | |
| 4972 | /** |
| 4973 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4974 | * with the string representation of either the argument or the context. |
| 4975 | * |
| 4976 | * @param[in] args Array of arguments. |
| 4977 | * @param[in] arg_count Count of elements in @p args. |
| 4978 | * @param[in,out] set Context and result set at the same time. |
| 4979 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4980 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4981 | */ |
| 4982 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4983 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4984 | { |
| 4985 | LY_ERR rc; |
| 4986 | |
| 4987 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4988 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4989 | return LY_SUCCESS; |
| 4990 | } |
| 4991 | |
| 4992 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4993 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4994 | LY_CHECK_RET(rc); |
| 4995 | set_fill_set(set, args[0]); |
| 4996 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4997 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4998 | LY_CHECK_RET(rc); |
| 4999 | } |
| 5000 | |
| 5001 | return LY_SUCCESS; |
| 5002 | } |
| 5003 | |
| 5004 | /** |
| 5005 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5006 | * with the length of the string in either the argument or the context. |
| 5007 | * |
| 5008 | * @param[in] args Array of arguments. |
| 5009 | * @param[in] arg_count Count of elements in @p args. |
| 5010 | * @param[in,out] set Context and result set at the same time. |
| 5011 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5012 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5013 | */ |
| 5014 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5015 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5016 | { |
| 5017 | struct lysc_node_leaf *sleaf; |
| 5018 | LY_ERR rc = LY_SUCCESS; |
| 5019 | |
| 5020 | if (options & LYXP_SCNODE_ALL) { |
| 5021 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5022 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5023 | 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] | 5024 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5025 | 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] | 5026 | } |
| 5027 | } |
| 5028 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5029 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5030 | 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] | 5031 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5032 | 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] | 5033 | } |
| 5034 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5035 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5036 | return rc; |
| 5037 | } |
| 5038 | |
| 5039 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5040 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5041 | LY_CHECK_RET(rc); |
| 5042 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5043 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5044 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5045 | LY_CHECK_RET(rc); |
| 5046 | set_fill_number(set, strlen(set->val.str)); |
| 5047 | } |
| 5048 | |
| 5049 | return LY_SUCCESS; |
| 5050 | } |
| 5051 | |
| 5052 | /** |
| 5053 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5054 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5055 | * on the second argument index ending on the third argument index, |
| 5056 | * indexed from 1. For exact definition refer to |
| 5057 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5058 | * |
| 5059 | * @param[in] args Array of arguments. |
| 5060 | * @param[in] arg_count Count of elements in @p args. |
| 5061 | * @param[in,out] set Context and result set at the same time. |
| 5062 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5063 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5064 | */ |
| 5065 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5066 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5067 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5068 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5069 | uint16_t str_start, str_len, pos; |
| 5070 | struct lysc_node_leaf *sleaf; |
| 5071 | LY_ERR rc = LY_SUCCESS; |
| 5072 | |
| 5073 | if (options & LYXP_SCNODE_ALL) { |
| 5074 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5075 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5076 | 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] | 5077 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5078 | 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] | 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5083 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5084 | 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] | 5085 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5086 | 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] | 5087 | } |
| 5088 | } |
| 5089 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5090 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5091 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5092 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5093 | 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] | 5094 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5095 | 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] | 5096 | } |
| 5097 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5098 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5099 | return rc; |
| 5100 | } |
| 5101 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5102 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5103 | LY_CHECK_RET(rc); |
| 5104 | |
| 5105 | /* start */ |
| 5106 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5107 | return -1; |
| 5108 | } |
| 5109 | if (isfinite(args[1]->val.num)) { |
| 5110 | start = args[1]->val.num - 1; |
| 5111 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5112 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5113 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5114 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | /* len */ |
| 5118 | if (arg_count == 3) { |
| 5119 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5120 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5121 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5122 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5123 | } else if (isfinite(args[2]->val.num)) { |
| 5124 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5125 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5126 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5127 | } |
| 5128 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5129 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | /* find matching character positions */ |
| 5133 | str_start = 0; |
| 5134 | str_len = 0; |
| 5135 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5136 | if (pos < start) { |
| 5137 | ++str_start; |
| 5138 | } else if (pos < start + len) { |
| 5139 | ++str_len; |
| 5140 | } else { |
| 5141 | break; |
| 5142 | } |
| 5143 | } |
| 5144 | |
| 5145 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5146 | return LY_SUCCESS; |
| 5147 | } |
| 5148 | |
| 5149 | /** |
| 5150 | * @brief Execute the XPath substring-after(string, string) function. |
| 5151 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5152 | * of the second argument in the first or an empty string. |
| 5153 | * |
| 5154 | * @param[in] args Array of arguments. |
| 5155 | * @param[in] arg_count Count of elements in @p args. |
| 5156 | * @param[in,out] set Context and result set at the same time. |
| 5157 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5158 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5159 | */ |
| 5160 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5161 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5162 | { |
| 5163 | char *ptr; |
| 5164 | struct lysc_node_leaf *sleaf; |
| 5165 | LY_ERR rc = LY_SUCCESS; |
| 5166 | |
| 5167 | if (options & LYXP_SCNODE_ALL) { |
| 5168 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5169 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5170 | 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] | 5171 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5172 | 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] | 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5177 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5178 | 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] | 5179 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5180 | 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] | 5181 | } |
| 5182 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5183 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5184 | return rc; |
| 5185 | } |
| 5186 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5187 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5188 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5189 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5190 | LY_CHECK_RET(rc); |
| 5191 | |
| 5192 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5193 | if (ptr) { |
| 5194 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5195 | } else { |
| 5196 | set_fill_string(set, "", 0); |
| 5197 | } |
| 5198 | |
| 5199 | return LY_SUCCESS; |
| 5200 | } |
| 5201 | |
| 5202 | /** |
| 5203 | * @brief Execute the XPath substring-before(string, string) function. |
| 5204 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5205 | * of the second argument in the first or an empty string. |
| 5206 | * |
| 5207 | * @param[in] args Array of arguments. |
| 5208 | * @param[in] arg_count Count of elements in @p args. |
| 5209 | * @param[in,out] set Context and result set at the same time. |
| 5210 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5211 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5212 | */ |
| 5213 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5214 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5215 | { |
| 5216 | char *ptr; |
| 5217 | struct lysc_node_leaf *sleaf; |
| 5218 | LY_ERR rc = LY_SUCCESS; |
| 5219 | |
| 5220 | if (options & LYXP_SCNODE_ALL) { |
| 5221 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5222 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5223 | 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] | 5224 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5225 | 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] | 5226 | } |
| 5227 | } |
| 5228 | |
| 5229 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5230 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5231 | 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] | 5232 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5233 | 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] | 5234 | } |
| 5235 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5236 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5237 | return rc; |
| 5238 | } |
| 5239 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5240 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5241 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5242 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5243 | LY_CHECK_RET(rc); |
| 5244 | |
| 5245 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5246 | if (ptr) { |
| 5247 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5248 | } else { |
| 5249 | set_fill_string(set, "", 0); |
| 5250 | } |
| 5251 | |
| 5252 | return LY_SUCCESS; |
| 5253 | } |
| 5254 | |
| 5255 | /** |
| 5256 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5257 | * with the sum of all the nodes in the context. |
| 5258 | * |
| 5259 | * @param[in] args Array of arguments. |
| 5260 | * @param[in] arg_count Count of elements in @p args. |
| 5261 | * @param[in,out] set Context and result set at the same time. |
| 5262 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5263 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5264 | */ |
| 5265 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5266 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5267 | { |
| 5268 | long double num; |
| 5269 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5270 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5271 | struct lyxp_set set_item; |
| 5272 | struct lysc_node_leaf *sleaf; |
| 5273 | LY_ERR rc = LY_SUCCESS; |
| 5274 | |
| 5275 | if (options & LYXP_SCNODE_ALL) { |
| 5276 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5277 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5278 | 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] | 5279 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5280 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5281 | 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] | 5282 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5283 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5284 | 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] | 5285 | } |
| 5286 | } |
| 5287 | } |
| 5288 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5289 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5290 | return rc; |
| 5291 | } |
| 5292 | |
| 5293 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5294 | |
| 5295 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5296 | 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] | 5297 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5298 | } else if (!args[0]->used) { |
| 5299 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5300 | } |
| 5301 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5302 | set_init(&set_item, set); |
| 5303 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5304 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5305 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5306 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5307 | |
| 5308 | set_item.used = 1; |
| 5309 | set_item.size = 1; |
| 5310 | |
| 5311 | for (i = 0; i < args[0]->used; ++i) { |
| 5312 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5313 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5314 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5315 | LY_CHECK_RET(rc); |
| 5316 | num = cast_string_to_number(str); |
| 5317 | free(str); |
| 5318 | set->val.num += num; |
| 5319 | } |
| 5320 | |
| 5321 | free(set_item.val.nodes); |
| 5322 | |
| 5323 | return LY_SUCCESS; |
| 5324 | } |
| 5325 | |
| 5326 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | * @brief Execute the XPath translate(string, string, string) function. |
| 5328 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5329 | * from the second argument replaced by those on the corresponding |
| 5330 | * positions in the third argument. |
| 5331 | * |
| 5332 | * @param[in] args Array of arguments. |
| 5333 | * @param[in] arg_count Count of elements in @p args. |
| 5334 | * @param[in,out] set Context and result set at the same time. |
| 5335 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5336 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5337 | */ |
| 5338 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5339 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5340 | { |
| 5341 | uint16_t i, j, new_used; |
| 5342 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5343 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5344 | struct lysc_node_leaf *sleaf; |
| 5345 | LY_ERR rc = LY_SUCCESS; |
| 5346 | |
| 5347 | if (options & LYXP_SCNODE_ALL) { |
| 5348 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5349 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5350 | 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] | 5351 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5352 | 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] | 5353 | } |
| 5354 | } |
| 5355 | |
| 5356 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5357 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5358 | 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] | 5359 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5360 | 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] | 5361 | } |
| 5362 | } |
| 5363 | |
| 5364 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5365 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5366 | 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] | 5367 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5368 | 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] | 5369 | } |
| 5370 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5371 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5372 | return rc; |
| 5373 | } |
| 5374 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5375 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5376 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5377 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5378 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5379 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5380 | LY_CHECK_RET(rc); |
| 5381 | |
| 5382 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5383 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5384 | new_used = 0; |
| 5385 | |
| 5386 | have_removed = 0; |
| 5387 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5388 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5389 | |
| 5390 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5391 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5392 | /* removing this char */ |
| 5393 | if (j >= strlen(args[2]->val.str)) { |
| 5394 | have_removed = 1; |
| 5395 | found = 1; |
| 5396 | break; |
| 5397 | } |
| 5398 | /* replacing this char */ |
| 5399 | new[new_used] = args[2]->val.str[j]; |
| 5400 | ++new_used; |
| 5401 | found = 1; |
| 5402 | break; |
| 5403 | } |
| 5404 | } |
| 5405 | |
| 5406 | /* copying this char */ |
| 5407 | if (!found) { |
| 5408 | new[new_used] = args[0]->val.str[i]; |
| 5409 | ++new_used; |
| 5410 | } |
| 5411 | } |
| 5412 | |
| 5413 | if (have_removed) { |
| 5414 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5415 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5416 | } |
| 5417 | new[new_used] = '\0'; |
| 5418 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5419 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5420 | set->type = LYXP_SET_STRING; |
| 5421 | set->val.str = new; |
| 5422 | |
| 5423 | return LY_SUCCESS; |
| 5424 | } |
| 5425 | |
| 5426 | /** |
| 5427 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5428 | * with true value. |
| 5429 | * |
| 5430 | * @param[in] args Array of arguments. |
| 5431 | * @param[in] arg_count Count of elements in @p args. |
| 5432 | * @param[in,out] set Context and result set at the same time. |
| 5433 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5434 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5435 | */ |
| 5436 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5437 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5438 | { |
| 5439 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5440 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5441 | return LY_SUCCESS; |
| 5442 | } |
| 5443 | |
| 5444 | set_fill_boolean(set, 1); |
| 5445 | return LY_SUCCESS; |
| 5446 | } |
| 5447 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5448 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5449 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5450 | * with only nodes from the context. |
| 5451 | * |
| 5452 | * @param[in,out] set Context and result set at the same time. |
| 5453 | * @param[in] axis Axis to search on. |
| 5454 | * @param[in] options XPath options. |
| 5455 | * @return LY_ERR |
| 5456 | */ |
| 5457 | static LY_ERR |
| 5458 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5459 | { |
| 5460 | if (options & LYXP_SCNODE_ALL) { |
| 5461 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5462 | } |
| 5463 | |
| 5464 | if (set->type != LYXP_SET_NODE_SET) { |
| 5465 | lyxp_set_free_content(set); |
| 5466 | return LY_SUCCESS; |
| 5467 | } |
| 5468 | |
| 5469 | /* just like moving to a node with no restrictions */ |
| 5470 | return moveto_node(set, NULL, NULL, axis, options); |
| 5471 | } |
| 5472 | |
| 5473 | /** |
| 5474 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5475 | * with the text content of the nodes in the context. |
| 5476 | * |
| 5477 | * @param[in,out] set Context and result set at the same time. |
| 5478 | * @param[in] axis Axis to search on. |
| 5479 | * @param[in] options XPath options. |
| 5480 | * @return LY_ERR |
| 5481 | */ |
| 5482 | static LY_ERR |
| 5483 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5484 | { |
| 5485 | uint32_t i; |
| 5486 | |
| 5487 | if (options & LYXP_SCNODE_ALL) { |
| 5488 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5489 | return LY_SUCCESS; |
| 5490 | } |
| 5491 | |
| 5492 | if (set->type != LYXP_SET_NODE_SET) { |
| 5493 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5494 | return LY_EVALID; |
| 5495 | } |
| 5496 | |
| 5497 | if (axis != LYXP_AXIS_CHILD) { |
| 5498 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5499 | lyxp_set_free_content(set); |
| 5500 | return LY_SUCCESS; |
| 5501 | } |
| 5502 | |
| 5503 | for (i = 0; i < set->used; ++i) { |
| 5504 | switch (set->val.nodes[i].type) { |
| 5505 | case LYXP_NODE_NONE: |
| 5506 | LOGINT_RET(set->ctx); |
| 5507 | case LYXP_NODE_ELEM: |
| 5508 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5509 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5510 | break; |
| 5511 | } |
| 5512 | /* fall through */ |
| 5513 | case LYXP_NODE_ROOT: |
| 5514 | case LYXP_NODE_ROOT_CONFIG: |
| 5515 | case LYXP_NODE_TEXT: |
| 5516 | case LYXP_NODE_META: |
| 5517 | set_remove_node_none(set, i); |
| 5518 | break; |
| 5519 | } |
| 5520 | } |
| 5521 | set_remove_nodes_none(set); |
| 5522 | |
| 5523 | return LY_SUCCESS; |
| 5524 | } |
| 5525 | |
| 5526 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5527 | * @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] | 5528 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5529 | * XPath @p set is expected to be a (sc)node set! |
| 5530 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5531 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5532 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5533 | * @param[in] set Set with general XPath context. |
| 5534 | * @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] | 5535 | * @param[out] moveto_mod Expected module of a matching node. |
| 5536 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5537 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5538 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5539 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5540 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5541 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5542 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5543 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5544 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5545 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5546 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5547 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5548 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5549 | /* specific module */ |
| 5550 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5551 | 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] | 5552 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5553 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5554 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5555 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5556 | return LY_EVALID; |
| 5557 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5558 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5559 | *qname += pref_len + 1; |
| 5560 | *qname_len -= pref_len + 1; |
| 5561 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5562 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5563 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5564 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5565 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5566 | case LY_VALUE_SCHEMA: |
| 5567 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5568 | /* current module */ |
| 5569 | mod = set->cur_mod; |
| 5570 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5571 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5572 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5573 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5574 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5575 | /* inherit parent (context node) module */ |
| 5576 | if (ctx_scnode) { |
| 5577 | mod = ctx_scnode->module; |
| 5578 | } else { |
| 5579 | mod = NULL; |
| 5580 | } |
| 5581 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5582 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5583 | /* all nodes need to be prefixed */ |
| 5584 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5585 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5586 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5587 | } |
| 5588 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5589 | *moveto_mod = mod; |
| 5590 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5591 | } |
| 5592 | |
| 5593 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5594 | * @brief Move context @p set to the root. Handles absolute path. |
| 5595 | * Result is LYXP_SET_NODE_SET. |
| 5596 | * |
| 5597 | * @param[in,out] set Set to use. |
| 5598 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5599 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5600 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5601 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5602 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5603 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5604 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5605 | |
| 5606 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5607 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5608 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5609 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5610 | set->type = LYXP_SET_NODE_SET; |
| 5611 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5612 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5613 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5614 | |
| 5615 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | /** |
| 5619 | * @brief Check @p node as a part of NameTest processing. |
| 5620 | * |
| 5621 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5622 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5623 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5624 | * @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] | 5625 | * @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] | 5626 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5627 | * @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] | 5628 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5629 | */ |
| 5630 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5631 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5632 | 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] | 5633 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5634 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5635 | assert(node_type == set->root_type); |
| 5636 | |
| 5637 | if (node_name || moveto_mod) { |
| 5638 | /* root will not match a specific node */ |
| 5639 | return LY_ENOT; |
| 5640 | } |
| 5641 | return LY_SUCCESS; |
| 5642 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5643 | /* other types will not match */ |
| 5644 | return LY_ENOT; |
| 5645 | } |
| 5646 | |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5647 | if (!node->schema) { |
| 5648 | /* opaque node never matches */ |
| 5649 | return LY_ENOT; |
| 5650 | } |
| 5651 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5652 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5653 | if (moveto_mod) { |
| 5654 | if (!(node->flags & LYD_EXT) && (node->schema->module != moveto_mod)) { |
| 5655 | return LY_ENOT; |
| 5656 | } else if ((node->flags & LYD_EXT) && strcmp(node->schema->module->name, moveto_mod->name)) { |
| 5657 | return LY_ENOT; |
| 5658 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5659 | } |
| 5660 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5661 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5662 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5663 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5664 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5665 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5666 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5670 | if (node_name) { |
| 5671 | if (!(node->flags & LYD_EXT) && (node->schema->name != node_name)) { |
| 5672 | return LY_ENOT; |
| 5673 | } else if ((node->flags & LYD_EXT) && strcmp(node->schema->name, node_name)) { |
| 5674 | return LY_ENOT; |
| 5675 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5676 | } |
| 5677 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5678 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5679 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5680 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5681 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5682 | |
| 5683 | /* match */ |
| 5684 | return LY_SUCCESS; |
| 5685 | } |
| 5686 | |
| 5687 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5688 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5689 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5690 | * @param[in] iter Last returned node. |
| 5691 | * @param[in] stop Node to stop the search on and not return. |
| 5692 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5693 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5694 | static const struct lyd_node * |
| 5695 | 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] | 5696 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5697 | const struct lyd_node *next = NULL; |
| 5698 | |
| 5699 | /* 1) child */ |
| 5700 | next = lyd_child(iter); |
| 5701 | if (!next) { |
| 5702 | if (iter == stop) { |
| 5703 | /* reached stop, no more descendants */ |
| 5704 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5705 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5706 | /* 2) child next sibling */ |
| 5707 | next = iter->next; |
| 5708 | } |
| 5709 | while (!next) { |
| 5710 | iter = lyd_parent(iter); |
| 5711 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5712 | return NULL; |
| 5713 | } |
| 5714 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5715 | } |
| 5716 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5717 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5721 | * @brief Get the next node in a backward DFS. |
| 5722 | * |
| 5723 | * @param[in] iter Last returned node. |
| 5724 | * @param[in] stop Node to stop the search on and not return. |
| 5725 | * @return Next node, NULL if there are no more. |
| 5726 | */ |
| 5727 | static const struct lyd_node * |
| 5728 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5729 | { |
| 5730 | const struct lyd_node *next = NULL; |
| 5731 | |
| 5732 | /* 1) previous sibling innermost last child */ |
| 5733 | next = iter->prev->next ? iter->prev : NULL; |
| 5734 | while (next && lyd_child(next)) { |
| 5735 | next = lyd_child(next); |
| 5736 | next = next->prev; |
| 5737 | } |
| 5738 | |
| 5739 | if (!next) { |
| 5740 | /* 2) parent */ |
| 5741 | iter = lyd_parent(iter); |
| 5742 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5743 | return NULL; |
| 5744 | } |
| 5745 | next = iter; |
| 5746 | } |
| 5747 | |
| 5748 | return next; |
| 5749 | } |
| 5750 | |
| 5751 | /** |
| 5752 | * @brief Get the first node on an axis for a context node. |
| 5753 | * |
| 5754 | * @param[in,out] iter NULL, updated to the next node. |
| 5755 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5756 | * @param[in] node Context node. |
| 5757 | * @param[in] node_type Type of @p node. |
| 5758 | * @param[in] axis Axis to use. |
| 5759 | * @param[in] set XPath set with the general context. |
| 5760 | * @return LY_SUCCESS on success. |
| 5761 | * @return LY_ENOTFOUND if no next node found. |
| 5762 | */ |
| 5763 | static LY_ERR |
| 5764 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5765 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5766 | { |
| 5767 | const struct lyd_node *next = NULL; |
| 5768 | enum lyxp_node_type next_type = 0; |
| 5769 | |
| 5770 | assert(!*iter); |
| 5771 | assert(!*iter_type); |
| 5772 | |
| 5773 | switch (axis) { |
| 5774 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5775 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5776 | case LYXP_AXIS_SELF: |
| 5777 | /* return the context node */ |
| 5778 | next = node; |
| 5779 | next_type = node_type; |
| 5780 | break; |
| 5781 | |
| 5782 | case LYXP_AXIS_ANCESTOR: |
| 5783 | case LYXP_AXIS_PARENT: |
| 5784 | if (node_type == LYXP_NODE_ELEM) { |
| 5785 | next = lyd_parent(node); |
| 5786 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5787 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5788 | next = node; |
| 5789 | next_type = LYXP_NODE_ELEM; |
| 5790 | } else if (node_type == LYXP_NODE_META) { |
| 5791 | next = ((struct lyd_meta *)node)->parent; |
| 5792 | next_type = LYXP_NODE_ELEM; |
| 5793 | } /* else root does not have a parent */ |
| 5794 | break; |
| 5795 | |
| 5796 | case LYXP_AXIS_CHILD: |
| 5797 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5798 | assert(!node); |
| 5799 | |
| 5800 | /* search in all the trees */ |
| 5801 | next = set->tree; |
| 5802 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5803 | } else { |
| 5804 | /* search in children */ |
| 5805 | next = lyd_child(node); |
| 5806 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5807 | } |
| 5808 | break; |
| 5809 | |
| 5810 | case LYXP_AXIS_DESCENDANT: |
| 5811 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5812 | /* top-level nodes */ |
| 5813 | next = set->tree; |
| 5814 | next_type = LYXP_NODE_ELEM; |
| 5815 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5816 | /* start from the context node */ |
| 5817 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5818 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5819 | } /* else no children */ |
| 5820 | break; |
| 5821 | |
| 5822 | case LYXP_AXIS_FOLLOWING: |
| 5823 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5824 | if (node_type == LYXP_NODE_ELEM) { |
| 5825 | /* first next sibling */ |
| 5826 | next = node->next; |
| 5827 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5828 | } /* else no sibling */ |
| 5829 | break; |
| 5830 | |
| 5831 | case LYXP_AXIS_PRECEDING: |
| 5832 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5833 | /* skip ancestors */ |
| 5834 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5835 | assert(next); |
| 5836 | next_type = LYXP_NODE_ELEM; |
| 5837 | } /* else no sibling */ |
| 5838 | break; |
| 5839 | |
| 5840 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5841 | if (node_type == LYXP_NODE_ELEM) { |
| 5842 | /* first previous sibling */ |
| 5843 | next = node->prev->next ? node->prev : NULL; |
| 5844 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5845 | } /* else no sibling */ |
| 5846 | break; |
| 5847 | |
| 5848 | case LYXP_AXIS_ATTRIBUTE: |
| 5849 | /* handled specially */ |
| 5850 | assert(0); |
| 5851 | LOGINT(set->ctx); |
| 5852 | break; |
| 5853 | } |
| 5854 | |
| 5855 | *iter = next; |
| 5856 | *iter_type = next_type; |
| 5857 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5858 | } |
| 5859 | |
| 5860 | /** |
| 5861 | * @brief Iterate over all nodes on an axis for a context node. |
| 5862 | * |
| 5863 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5864 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5865 | * @param[in] node Context node. |
| 5866 | * @param[in] node_type Type of @p node. |
| 5867 | * @param[in] axis Axis to use. |
| 5868 | * @param[in] set XPath set with the general context. |
| 5869 | * @return LY_SUCCESS on success. |
| 5870 | * @return LY_ENOTFOUND if no next node found. |
| 5871 | */ |
| 5872 | static LY_ERR |
| 5873 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5874 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5875 | { |
| 5876 | const struct lyd_node *next = NULL; |
| 5877 | enum lyxp_node_type next_type = 0; |
| 5878 | |
| 5879 | if (!*iter_type) { |
| 5880 | /* first returned node */ |
| 5881 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5882 | } |
| 5883 | |
| 5884 | switch (axis) { |
| 5885 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5886 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5887 | /* fake first ancestor, we returned self before */ |
| 5888 | *iter = NULL; |
| 5889 | *iter_type = 0; |
| 5890 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 5891 | } /* else continue ancestor */ |
| 5892 | |
| 5893 | /* fallthrough */ |
| 5894 | case LYXP_AXIS_ANCESTOR: |
| 5895 | if (*iter_type == LYXP_NODE_ELEM) { |
| 5896 | /* iter parent */ |
| 5897 | next = lyd_parent(*iter); |
| 5898 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5899 | } /* else root, no ancestors */ |
| 5900 | break; |
| 5901 | |
| 5902 | case LYXP_AXIS_CHILD: |
| 5903 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5904 | |
| 5905 | /* next sibling (child) */ |
| 5906 | next = (*iter)->next; |
| 5907 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5908 | break; |
| 5909 | |
| 5910 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5911 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5912 | /* fake first descendant, we returned self before */ |
| 5913 | *iter = NULL; |
| 5914 | *iter_type = 0; |
| 5915 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 5916 | } /* else continue descendant */ |
| 5917 | |
| 5918 | /* fallthrough */ |
| 5919 | case LYXP_AXIS_DESCENDANT: |
| 5920 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5921 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 5922 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5923 | break; |
| 5924 | |
| 5925 | case LYXP_AXIS_FOLLOWING: |
| 5926 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5927 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 5928 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5929 | break; |
| 5930 | |
| 5931 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5932 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5933 | |
| 5934 | /* next sibling */ |
| 5935 | next = (*iter)->next; |
| 5936 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5937 | break; |
| 5938 | |
| 5939 | case LYXP_AXIS_PARENT: |
| 5940 | case LYXP_AXIS_SELF: |
| 5941 | /* parent/self was returned before */ |
| 5942 | break; |
| 5943 | |
| 5944 | case LYXP_AXIS_PRECEDING: |
| 5945 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5946 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 5947 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5948 | break; |
| 5949 | |
| 5950 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5951 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5952 | |
| 5953 | /* previous sibling */ |
| 5954 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 5955 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5956 | break; |
| 5957 | |
| 5958 | case LYXP_AXIS_ATTRIBUTE: |
| 5959 | /* handled specially */ |
| 5960 | assert(0); |
| 5961 | LOGINT(set->ctx); |
| 5962 | break; |
| 5963 | } |
| 5964 | |
| 5965 | *iter = next; |
| 5966 | *iter_type = next_type; |
| 5967 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5968 | } |
| 5969 | |
| 5970 | /** |
| 5971 | * @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] | 5972 | * |
| 5973 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5974 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5975 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5976 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5977 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5978 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5979 | */ |
| 5980 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5981 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 5982 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5983 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5984 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5985 | const struct lyd_node *iter; |
| 5986 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5987 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5988 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5989 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5990 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5991 | return LY_SUCCESS; |
| 5992 | } |
| 5993 | |
| 5994 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5995 | 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] | 5996 | return LY_EVALID; |
| 5997 | } |
| 5998 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5999 | /* init result set */ |
| 6000 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6001 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6002 | for (i = 0; i < set->used; ++i) { |
| 6003 | /* iterate over all the nodes on the axis of the node */ |
| 6004 | iter = NULL; |
| 6005 | iter_type = 0; |
| 6006 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6007 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6008 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6009 | rc = r; |
| 6010 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6011 | } else if (r) { |
| 6012 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6013 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6014 | |
| 6015 | /* check for duplicates if they are possible */ |
| 6016 | switch (axis) { |
| 6017 | case LYXP_AXIS_ANCESTOR: |
| 6018 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6019 | case LYXP_AXIS_DESCENDANT: |
| 6020 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6021 | case LYXP_AXIS_FOLLOWING: |
| 6022 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6023 | case LYXP_AXIS_PARENT: |
| 6024 | case LYXP_AXIS_PRECEDING: |
| 6025 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6026 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6027 | continue; |
| 6028 | } |
| 6029 | break; |
| 6030 | case LYXP_AXIS_CHILD: |
| 6031 | case LYXP_AXIS_SELF: |
| 6032 | break; |
| 6033 | case LYXP_AXIS_ATTRIBUTE: |
| 6034 | /* handled specially */ |
| 6035 | assert(0); |
| 6036 | LOGINT(set->ctx); |
| 6037 | break; |
| 6038 | } |
| 6039 | |
| 6040 | /* matching node */ |
| 6041 | set_insert_node(&result, iter, 0, iter_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6042 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6043 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6044 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6045 | /* move result to the set */ |
| 6046 | lyxp_set_free_content(set); |
| 6047 | *set = result; |
| 6048 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6049 | |
| 6050 | /* sort the final set */ |
| 6051 | set_sort(set); |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6052 | |
| 6053 | cleanup: |
| 6054 | lyxp_set_free_content(&result); |
| 6055 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6056 | } |
| 6057 | |
| 6058 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6059 | * @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] | 6060 | * |
| 6061 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6062 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6063 | * @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] | 6064 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6065 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6066 | */ |
| 6067 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6068 | 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] | 6069 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6070 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6071 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6072 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6073 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6074 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6075 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6076 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6077 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6078 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6079 | /* init result set */ |
| 6080 | set_init(&result, set); |
| 6081 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6082 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6083 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6084 | } |
| 6085 | |
| 6086 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6087 | 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] | 6088 | ret = LY_EVALID; |
| 6089 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6090 | } |
| 6091 | |
| 6092 | /* context check for all the nodes since we have the schema node */ |
| 6093 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6094 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6095 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6096 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6097 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6098 | lyxp_set_free_content(set); |
| 6099 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6100 | } |
| 6101 | |
| 6102 | /* create specific data instance if needed */ |
| 6103 | if (scnode->nodetype == LYS_LIST) { |
| 6104 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 6105 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6106 | 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] | 6107 | } |
| 6108 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6109 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6110 | siblings = NULL; |
| 6111 | |
| 6112 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6113 | assert(!set->val.nodes[i].node); |
| 6114 | |
| 6115 | /* search in all the trees */ |
| 6116 | siblings = set->tree; |
| 6117 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6118 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6119 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6120 | } |
| 6121 | |
| 6122 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6123 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6124 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6125 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6126 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6127 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6128 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6129 | |
| 6130 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6131 | 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] | 6132 | ret = LY_EINCOMPLETE; |
| 6133 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6134 | } |
| 6135 | |
| 6136 | if (sub) { |
| 6137 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6138 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6139 | } |
| 6140 | } |
| 6141 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6142 | /* move result to the set */ |
| 6143 | lyxp_set_free_content(set); |
| 6144 | *set = result; |
| 6145 | result.type = LYXP_SET_NUMBER; |
| 6146 | assert(!set_sort(set)); |
| 6147 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6148 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6149 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6150 | lyd_free_tree(inst); |
| 6151 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6155 | * @brief Check @p node as a part of schema NameTest processing. |
| 6156 | * |
| 6157 | * @param[in] node Schema node to check. |
| 6158 | * @param[in] ctx_scnode Context node. |
| 6159 | * @param[in] set Set to read general context from. |
| 6160 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6161 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6162 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6163 | */ |
| 6164 | static LY_ERR |
| 6165 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6166 | const char *node_name, const struct lys_module *moveto_mod) |
| 6167 | { |
| 6168 | if (!moveto_mod && node_name) { |
| 6169 | switch (set->format) { |
| 6170 | case LY_VALUE_SCHEMA: |
| 6171 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6172 | /* use current module */ |
| 6173 | moveto_mod = set->cur_mod; |
| 6174 | break; |
| 6175 | case LY_VALUE_JSON: |
| 6176 | case LY_VALUE_LYB: |
| 6177 | case LY_VALUE_STR_NS: |
| 6178 | /* inherit module of the context node, if any */ |
| 6179 | if (ctx_scnode) { |
| 6180 | moveto_mod = ctx_scnode->module; |
| 6181 | } |
| 6182 | break; |
| 6183 | case LY_VALUE_CANON: |
| 6184 | case LY_VALUE_XML: |
| 6185 | /* not defined */ |
| 6186 | LOGINT(set->ctx); |
| 6187 | return LY_EINVAL; |
| 6188 | } |
| 6189 | } |
| 6190 | |
| 6191 | if (!node) { |
| 6192 | /* root will not match a specific node */ |
| 6193 | if (node_name || moveto_mod) { |
| 6194 | return LY_ENOT; |
| 6195 | } |
| 6196 | return LY_SUCCESS; |
| 6197 | } |
| 6198 | |
| 6199 | /* module check */ |
| 6200 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6201 | return LY_ENOT; |
| 6202 | } |
| 6203 | |
| 6204 | /* context check */ |
| 6205 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6206 | return LY_EINVAL; |
| 6207 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6208 | return LY_EINVAL; |
| 6209 | } |
| 6210 | |
| 6211 | /* name check */ |
| 6212 | if (node_name && (node->name != node_name)) { |
| 6213 | return LY_ENOT; |
| 6214 | } |
| 6215 | |
| 6216 | /* match */ |
| 6217 | return LY_SUCCESS; |
| 6218 | } |
| 6219 | |
| 6220 | /** |
| 6221 | * @brief Get the next node in a forward schema node DFS. |
| 6222 | * |
| 6223 | * @param[in] iter Last returned node. |
| 6224 | * @param[in] stop Node to stop the search on and not return. |
| 6225 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6226 | * @return Next node, NULL if there are no more. |
| 6227 | */ |
| 6228 | static const struct lysc_node * |
| 6229 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6230 | { |
| 6231 | const struct lysc_node *next = NULL; |
| 6232 | |
| 6233 | next = lysc_node_child(iter); |
| 6234 | if (!next) { |
| 6235 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6236 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6237 | /* we are done, no next element to process */ |
| 6238 | return NULL; |
| 6239 | } |
| 6240 | |
| 6241 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6242 | } |
| 6243 | while (!next && iter) { |
| 6244 | /* parent is already processed, go to its sibling */ |
| 6245 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6246 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6247 | /* we are done, no next element to process */ |
| 6248 | return NULL; |
| 6249 | } |
| 6250 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6251 | } |
| 6252 | |
| 6253 | return next; |
| 6254 | } |
| 6255 | |
| 6256 | /** |
| 6257 | * @brief Consider schema node based on its in_ctx enum value. |
| 6258 | * |
| 6259 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6260 | * @param[in] axis Axis to use. |
| 6261 | * @return LY_SUCCESS on success. |
| 6262 | * @return LY_ENOT if the node should not be returned. |
| 6263 | */ |
| 6264 | static LY_ERR |
| 6265 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6266 | { |
| 6267 | switch (axis) { |
| 6268 | case LYXP_AXIS_SELF: |
| 6269 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6270 | /* additionally put the start node into context */ |
| 6271 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6272 | return LY_SUCCESS; |
| 6273 | } |
| 6274 | break; |
| 6275 | case LYXP_AXIS_PARENT: |
| 6276 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6277 | case LYXP_AXIS_ANCESTOR: |
| 6278 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6279 | case LYXP_AXIS_DESCENDANT: |
| 6280 | case LYXP_AXIS_FOLLOWING: |
| 6281 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6282 | case LYXP_AXIS_PRECEDING: |
| 6283 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6284 | case LYXP_AXIS_CHILD: |
| 6285 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6286 | /* remember that context node was used */ |
| 6287 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6288 | return LY_SUCCESS; |
| 6289 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6290 | /* traversed */ |
| 6291 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6292 | return LY_SUCCESS; |
| 6293 | } |
| 6294 | break; |
| 6295 | case LYXP_AXIS_ATTRIBUTE: |
| 6296 | /* unreachable */ |
| 6297 | assert(0); |
| 6298 | LOGINT(NULL); |
| 6299 | break; |
| 6300 | } |
| 6301 | |
| 6302 | return LY_ENOT; |
| 6303 | } |
| 6304 | |
| 6305 | /** |
| 6306 | * @brief Get previous sibling for a schema node. |
| 6307 | * |
| 6308 | * @param[in] scnode Schema node. |
| 6309 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6310 | * @return Previous sibling, NULL if none. |
| 6311 | */ |
| 6312 | static const struct lysc_node * |
| 6313 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6314 | { |
| 6315 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6316 | |
| 6317 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6318 | if (next == scnode) { |
| 6319 | break; |
| 6320 | } |
| 6321 | |
| 6322 | prev = next; |
| 6323 | } |
| 6324 | |
| 6325 | return prev; |
| 6326 | } |
| 6327 | |
| 6328 | /** |
| 6329 | * @brief Get the first schema node on an axis for a context node. |
| 6330 | * |
| 6331 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6332 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6333 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6334 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6335 | * @param[in] scnode Context node. |
| 6336 | * @param[in] node_type Type of @p scnode. |
| 6337 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6338 | * @param[in] axis Axis to use. |
| 6339 | * @param[in] set XPath set with the general context. |
| 6340 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6341 | * @return LY_SUCCESS on success. |
| 6342 | * @return LY_ENOTFOUND if no next node found. |
| 6343 | */ |
| 6344 | static LY_ERR |
| 6345 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6346 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6347 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6348 | { |
| 6349 | const struct lysc_node *next = NULL; |
| 6350 | enum lyxp_node_type next_type = 0; |
| 6351 | |
| 6352 | assert(!*iter); |
| 6353 | assert(!*iter_type); |
| 6354 | |
| 6355 | *iter_mod = NULL; |
| 6356 | *iter_mod_idx = 0; |
| 6357 | |
| 6358 | switch (axis) { |
| 6359 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6360 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6361 | case LYXP_AXIS_SELF: |
| 6362 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6363 | /* just return the node */ |
| 6364 | next = scnode; |
| 6365 | next_type = node_type; |
| 6366 | } |
| 6367 | break; |
| 6368 | |
| 6369 | case LYXP_AXIS_ANCESTOR: |
| 6370 | case LYXP_AXIS_PARENT: |
| 6371 | if (node_type == LYXP_NODE_ELEM) { |
| 6372 | next = lysc_data_parent(scnode); |
| 6373 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6374 | } /* else no parent */ |
| 6375 | break; |
| 6376 | |
| 6377 | case LYXP_AXIS_DESCENDANT: |
| 6378 | case LYXP_AXIS_CHILD: |
| 6379 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6380 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6381 | * it can be in a top-level augment */ |
| 6382 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6383 | /* module may not be implemented or not compiled yet */ |
| 6384 | if (!(*iter_mod)->compiled) { |
| 6385 | continue; |
| 6386 | } |
| 6387 | |
| 6388 | /* get next node */ |
| 6389 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6390 | next_type = LYXP_NODE_ELEM; |
| 6391 | break; |
| 6392 | } |
| 6393 | } |
| 6394 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6395 | /* get next node */ |
| 6396 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6397 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6398 | } |
| 6399 | break; |
| 6400 | |
| 6401 | case LYXP_AXIS_FOLLOWING: |
| 6402 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6403 | if (node_type == LYXP_NODE_ELEM) { |
| 6404 | /* first next sibling */ |
| 6405 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6406 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6407 | } /* else no sibling */ |
| 6408 | break; |
| 6409 | |
| 6410 | case LYXP_AXIS_PRECEDING: |
| 6411 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6412 | if (node_type == LYXP_NODE_ELEM) { |
| 6413 | /* first parent sibling */ |
| 6414 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6415 | if (next == scnode) { |
| 6416 | /* no preceding sibling */ |
| 6417 | next = NULL; |
| 6418 | } |
| 6419 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6420 | } /* else no sibling */ |
| 6421 | break; |
| 6422 | |
| 6423 | case LYXP_AXIS_ATTRIBUTE: |
| 6424 | /* unreachable */ |
| 6425 | assert(0); |
| 6426 | LOGINT(set->ctx); |
| 6427 | break; |
| 6428 | } |
| 6429 | |
| 6430 | *iter = next; |
| 6431 | *iter_type = next_type; |
| 6432 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6433 | } |
| 6434 | |
| 6435 | /** |
| 6436 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6437 | * |
| 6438 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6439 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6440 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6441 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6442 | * @param[in] scnode Context node. |
| 6443 | * @param[in] node_type Type of @p scnode. |
| 6444 | * @param[in] axis Axis to use. |
| 6445 | * @param[in] set XPath set with the general context. |
| 6446 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6447 | * @return LY_SUCCESS on success. |
| 6448 | * @return LY_ENOTFOUND if no next node found. |
| 6449 | */ |
| 6450 | static LY_ERR |
| 6451 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6452 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6453 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6454 | { |
| 6455 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6456 | enum lyxp_node_type next_type = 0; |
| 6457 | |
| 6458 | if (!*iter_type) { |
| 6459 | /* first returned node */ |
| 6460 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6461 | getnext_opts); |
| 6462 | } |
| 6463 | |
| 6464 | switch (axis) { |
| 6465 | case LYXP_AXIS_PARENT: |
| 6466 | case LYXP_AXIS_SELF: |
| 6467 | /* parent/self was returned before */ |
| 6468 | break; |
| 6469 | |
| 6470 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6471 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6472 | /* fake first ancestor, we returned self before */ |
| 6473 | *iter = NULL; |
| 6474 | *iter_type = 0; |
| 6475 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6476 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
| 6477 | } /* else continue ancestor */ |
| 6478 | |
| 6479 | /* fallthrough */ |
| 6480 | case LYXP_AXIS_ANCESTOR: |
| 6481 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6482 | next = lysc_data_parent(*iter); |
| 6483 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6484 | } /* else no ancestor */ |
| 6485 | break; |
| 6486 | |
| 6487 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6488 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6489 | /* fake first descendant, we returned self before */ |
| 6490 | *iter = NULL; |
| 6491 | *iter_type = 0; |
| 6492 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6493 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6494 | } /* else DFS until context node */ |
| 6495 | dfs_stop = scnode; |
| 6496 | |
| 6497 | /* fallthrough */ |
| 6498 | case LYXP_AXIS_DESCENDANT: |
| 6499 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6500 | /* DFS until the context node */ |
| 6501 | dfs_stop = scnode; |
| 6502 | } |
| 6503 | |
| 6504 | /* fallthrough */ |
| 6505 | case LYXP_AXIS_PRECEDING: |
| 6506 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6507 | /* DFS until the previous sibling */ |
| 6508 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6509 | assert(dfs_stop); |
| 6510 | |
| 6511 | if (*iter == dfs_stop) { |
| 6512 | /* we are done */ |
| 6513 | break; |
| 6514 | } |
| 6515 | } |
| 6516 | |
| 6517 | /* fallthrough */ |
| 6518 | case LYXP_AXIS_FOLLOWING: |
| 6519 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6520 | /* DFS through the whole module */ |
| 6521 | dfs_stop = NULL; |
| 6522 | } |
| 6523 | |
| 6524 | /* nested nodes */ |
| 6525 | assert(*iter); |
| 6526 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6527 | if (next) { |
| 6528 | next_type = LYXP_NODE_ELEM; |
| 6529 | break; |
| 6530 | } /* else get next top-level node just like a child */ |
| 6531 | |
| 6532 | /* fallthrough */ |
| 6533 | case LYXP_AXIS_CHILD: |
| 6534 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6535 | if (!*iter_mod) { |
| 6536 | /* nodes from a single module */ |
| 6537 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6538 | next_type = LYXP_NODE_ELEM; |
| 6539 | break; |
| 6540 | } |
| 6541 | |
| 6542 | assert(scnode); |
| 6543 | if (!lysc_data_parent(scnode)) { |
| 6544 | /* iterating over top-level nodes, find next */ |
| 6545 | while (lysc_data_parent(*iter)) { |
| 6546 | *iter = lysc_data_parent(*iter); |
| 6547 | } |
| 6548 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6549 | next_type = LYXP_NODE_ELEM; |
| 6550 | break; |
| 6551 | } |
| 6552 | } |
| 6553 | } |
| 6554 | |
| 6555 | while (*iter_mod) { |
| 6556 | /* module top-level nodes */ |
| 6557 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6558 | next_type = LYXP_NODE_ELEM; |
| 6559 | break; |
| 6560 | } |
| 6561 | |
| 6562 | /* get next module */ |
| 6563 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6564 | /* module may not be implemented or not compiled yet */ |
| 6565 | if ((*iter_mod)->compiled) { |
| 6566 | break; |
| 6567 | } |
| 6568 | } |
| 6569 | |
| 6570 | /* new module, start over */ |
| 6571 | *iter = NULL; |
| 6572 | } |
| 6573 | break; |
| 6574 | |
| 6575 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6576 | assert(*iter); |
| 6577 | |
| 6578 | /* next parent sibling until scnode */ |
| 6579 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6580 | if (next == scnode) { |
| 6581 | /* no previous sibling */ |
| 6582 | next = NULL; |
| 6583 | } |
| 6584 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6585 | break; |
| 6586 | |
| 6587 | case LYXP_AXIS_ATTRIBUTE: |
| 6588 | /* unreachable */ |
| 6589 | assert(0); |
| 6590 | LOGINT(set->ctx); |
| 6591 | break; |
| 6592 | } |
| 6593 | |
| 6594 | *iter = next; |
| 6595 | *iter_type = next_type; |
| 6596 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6597 | } |
| 6598 | |
| 6599 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6600 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6601 | * |
| 6602 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6603 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6604 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6605 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6606 | * @param[in] options XPath options. |
| 6607 | * @return LY_ERR |
| 6608 | */ |
| 6609 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6610 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6611 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6612 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6613 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6614 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6615 | const struct lys_module *mod; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6616 | const struct lysc_node *iter; |
| 6617 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6618 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6619 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6620 | return LY_SUCCESS; |
| 6621 | } |
| 6622 | |
| 6623 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6624 | 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] | 6625 | return LY_EVALID; |
| 6626 | } |
| 6627 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6628 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6629 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6630 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6631 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6632 | } |
| 6633 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6634 | orig_used = set->used; |
| 6635 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6636 | /* update in_ctx first */ |
| 6637 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6638 | /* not usable, skip */ |
| 6639 | continue; |
| 6640 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6641 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6642 | iter = NULL; |
| 6643 | iter_type = 0; |
| 6644 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6645 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6646 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6647 | continue; |
| 6648 | } |
| 6649 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6650 | /* insert */ |
| 6651 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, &idx)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6652 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6653 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6654 | if ((idx < orig_used) && (idx > i)) { |
| 6655 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6656 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6657 | } |
| 6658 | } |
| 6659 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6660 | |
| 6661 | /* correct temporary in_ctx values */ |
| 6662 | if (temp_ctx) { |
| 6663 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6664 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6665 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6666 | } |
| 6667 | } |
| 6668 | } |
| 6669 | |
| 6670 | return LY_SUCCESS; |
| 6671 | } |
| 6672 | |
| 6673 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6674 | * @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] | 6675 | * Context position aware. |
| 6676 | * |
| 6677 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6678 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6679 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6680 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6681 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6682 | */ |
| 6683 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6684 | 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] | 6685 | { |
| 6686 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6687 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6688 | struct lyxp_set ret_set; |
| 6689 | LY_ERR rc; |
| 6690 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6691 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6692 | return LY_SUCCESS; |
| 6693 | } |
| 6694 | |
| 6695 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6696 | 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] | 6697 | return LY_EVALID; |
| 6698 | } |
| 6699 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6700 | /* 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] | 6701 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6702 | LY_CHECK_RET(rc); |
| 6703 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6704 | /* 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] | 6705 | set_init(&ret_set, set); |
| 6706 | for (i = 0; i < set->used; ++i) { |
| 6707 | |
| 6708 | /* TREE DFS */ |
| 6709 | start = set->val.nodes[i].node; |
| 6710 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6711 | 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] | 6712 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6713 | /* add matching node into result set */ |
| 6714 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6715 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6716 | /* the node is a duplicate, we'll process it later in the set */ |
| 6717 | goto skip_children; |
| 6718 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6719 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6720 | return rc; |
| 6721 | } else if (rc == LY_EINVAL) { |
| 6722 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6723 | } |
| 6724 | |
| 6725 | /* TREE DFS NEXT ELEM */ |
| 6726 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6727 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6728 | if (!next) { |
| 6729 | skip_children: |
| 6730 | /* no children, so try siblings, but only if it's not the start, |
| 6731 | * that is considered to be the root and it's siblings are not traversed */ |
| 6732 | if (elem != start) { |
| 6733 | next = elem->next; |
| 6734 | } else { |
| 6735 | break; |
| 6736 | } |
| 6737 | } |
| 6738 | while (!next) { |
| 6739 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6740 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6741 | /* we are done, no next element to process */ |
| 6742 | break; |
| 6743 | } |
| 6744 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6745 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6746 | next = elem->next; |
| 6747 | } |
| 6748 | } |
| 6749 | } |
| 6750 | |
| 6751 | /* make the temporary set the current one */ |
| 6752 | ret_set.ctx_pos = set->ctx_pos; |
| 6753 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6754 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6755 | memcpy(set, &ret_set, sizeof *set); |
| 6756 | |
| 6757 | return LY_SUCCESS; |
| 6758 | } |
| 6759 | |
| 6760 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6761 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6762 | * Result is LYXP_SET_NODE_SET. |
| 6763 | * |
| 6764 | * @param[in] set Set to use. |
| 6765 | * @param[in] start Start node whose subtree to add. |
| 6766 | * @param[in] start_idx Index of @p start in @p set. |
| 6767 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6768 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6769 | * @param[in] options XPath options. |
| 6770 | * @return LY_ERR value. |
| 6771 | */ |
| 6772 | static LY_ERR |
| 6773 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6774 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6775 | { |
| 6776 | const struct lysc_node *next, *elem; |
| 6777 | uint32_t idx; |
| 6778 | LY_ERR rc; |
| 6779 | |
| 6780 | /* TREE DFS */ |
| 6781 | for (elem = next = start; elem; elem = next) { |
| 6782 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6783 | /* schema-only nodes, skip root */ |
| 6784 | goto next_iter; |
| 6785 | } |
| 6786 | |
| 6787 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6788 | if (!rc) { |
| 6789 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6790 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6791 | if (idx > start_idx) { |
| 6792 | /* we will process it later in the set */ |
| 6793 | goto skip_children; |
| 6794 | } |
| 6795 | } else { |
| 6796 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL)); |
| 6797 | } |
| 6798 | } else if (rc == LY_EINVAL) { |
| 6799 | goto skip_children; |
| 6800 | } |
| 6801 | |
| 6802 | next_iter: |
| 6803 | /* TREE DFS NEXT ELEM */ |
| 6804 | /* select element for the next run - children first */ |
| 6805 | next = lysc_node_child(elem); |
| 6806 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6807 | next = next->next; |
| 6808 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6809 | next = next->next; |
| 6810 | } |
| 6811 | if (!next) { |
| 6812 | skip_children: |
| 6813 | /* no children, so try siblings, but only if it's not the start, |
| 6814 | * that is considered to be the root and it's siblings are not traversed */ |
| 6815 | if (elem != start) { |
| 6816 | next = elem->next; |
| 6817 | } else { |
| 6818 | break; |
| 6819 | } |
| 6820 | } |
| 6821 | while (!next) { |
| 6822 | /* no siblings, go back through the parents */ |
| 6823 | if (elem->parent == start) { |
| 6824 | /* we are done, no next element to process */ |
| 6825 | break; |
| 6826 | } |
| 6827 | /* parent is already processed, go to its sibling */ |
| 6828 | elem = elem->parent; |
| 6829 | next = elem->next; |
| 6830 | } |
| 6831 | } |
| 6832 | |
| 6833 | return LY_SUCCESS; |
| 6834 | } |
| 6835 | |
| 6836 | /** |
| 6837 | * @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] | 6838 | * |
| 6839 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6840 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6841 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6842 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6843 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6844 | */ |
| 6845 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6846 | 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] | 6847 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6848 | uint32_t i, orig_used, mod_idx; |
| 6849 | const struct lys_module *mod; |
| 6850 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6851 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6852 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6853 | return LY_SUCCESS; |
| 6854 | } |
| 6855 | |
| 6856 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6857 | 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] | 6858 | return LY_EVALID; |
| 6859 | } |
| 6860 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6861 | orig_used = set->used; |
| 6862 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6863 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6864 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6865 | continue; |
| 6866 | } |
| 6867 | |
| 6868 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6869 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6870 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6871 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6872 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6873 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6874 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6875 | /* traverse all top-level nodes in all the modules */ |
| 6876 | mod_idx = 0; |
| 6877 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6878 | /* module may not be implemented or not compiled yet */ |
| 6879 | if (!mod->compiled) { |
| 6880 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6881 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6882 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6883 | root = NULL; |
| 6884 | /* no getnext opts needed */ |
| 6885 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 6886 | 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] | 6887 | } |
| 6888 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6889 | |
| 6890 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6891 | /* add all the descendants recursively */ |
| 6892 | 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] | 6893 | } |
| 6894 | } |
| 6895 | |
| 6896 | return LY_SUCCESS; |
| 6897 | } |
| 6898 | |
| 6899 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6900 | * @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] | 6901 | * Indirectly context position aware. |
| 6902 | * |
| 6903 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6904 | * @param[in] mod Matching metadata module, NULL for any. |
| 6905 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6906 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6907 | * @return LY_ERR |
| 6908 | */ |
| 6909 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6910 | 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] | 6911 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6912 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6913 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6914 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6915 | return LY_SUCCESS; |
| 6916 | } |
| 6917 | |
| 6918 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6919 | 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] | 6920 | return LY_EVALID; |
| 6921 | } |
| 6922 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6923 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6924 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6925 | |
| 6926 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 6927 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6928 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6929 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6930 | |
| 6931 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6932 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6933 | continue; |
| 6934 | } |
| 6935 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6936 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6937 | /* match */ |
| 6938 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6939 | set->val.meta[i].meta = sub; |
| 6940 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6941 | /* pos does not change */ |
| 6942 | replaced = 1; |
| 6943 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6944 | 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] | 6945 | } |
| 6946 | ++i; |
| 6947 | } |
| 6948 | } |
| 6949 | } |
| 6950 | |
| 6951 | if (!replaced) { |
| 6952 | /* no match */ |
| 6953 | set_remove_node(set, i); |
| 6954 | } |
| 6955 | } |
| 6956 | |
| 6957 | return LY_SUCCESS; |
| 6958 | } |
| 6959 | |
| 6960 | /** |
| 6961 | * @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] | 6962 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6963 | * |
| 6964 | * @param[in,out] set1 Set to use for the result. |
| 6965 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6966 | * @return LY_ERR |
| 6967 | */ |
| 6968 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6969 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6970 | { |
| 6971 | LY_ERR rc; |
| 6972 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6973 | 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] | 6974 | 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] | 6975 | return LY_EVALID; |
| 6976 | } |
| 6977 | |
| 6978 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6979 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6980 | return LY_SUCCESS; |
| 6981 | } |
| 6982 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6983 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 6984 | /* release hidden allocated data (lyxp_set.size) */ |
| 6985 | lyxp_set_free_content(set1); |
| 6986 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6987 | memcpy(set1, set2, sizeof *set1); |
| 6988 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6989 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6990 | return LY_SUCCESS; |
| 6991 | } |
| 6992 | |
| 6993 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6994 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6995 | |
| 6996 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6997 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6998 | LY_CHECK_RET(rc); |
| 6999 | |
| 7000 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7001 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7002 | |
| 7003 | return LY_SUCCESS; |
| 7004 | } |
| 7005 | |
| 7006 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7007 | * @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] | 7008 | * Context position aware. |
| 7009 | * |
| 7010 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7011 | * @param[in] mod Matching metadata module, NULL for any. |
| 7012 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7013 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7014 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7015 | */ |
| 7016 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7017 | 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] | 7018 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7019 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7020 | struct lyxp_set *set_all_desc = NULL; |
| 7021 | LY_ERR rc; |
| 7022 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7023 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7024 | return LY_SUCCESS; |
| 7025 | } |
| 7026 | |
| 7027 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7028 | 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] | 7029 | return LY_EVALID; |
| 7030 | } |
| 7031 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7032 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7033 | * but it likely won't be used much, so it's a waste of time */ |
| 7034 | /* copy the context */ |
| 7035 | set_all_desc = set_copy(set); |
| 7036 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7037 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7038 | if (rc != LY_SUCCESS) { |
| 7039 | lyxp_set_free(set_all_desc); |
| 7040 | return rc; |
| 7041 | } |
| 7042 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7043 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7044 | if (rc != LY_SUCCESS) { |
| 7045 | lyxp_set_free(set_all_desc); |
| 7046 | return rc; |
| 7047 | } |
| 7048 | lyxp_set_free(set_all_desc); |
| 7049 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7050 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7051 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7052 | |
| 7053 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7054 | * we have all attributes qualified in lyd tree */ |
| 7055 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7056 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7057 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7058 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7059 | continue; |
| 7060 | } |
| 7061 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7062 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7063 | /* match */ |
| 7064 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7065 | set->val.meta[i].meta = sub; |
| 7066 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7067 | /* pos does not change */ |
| 7068 | replaced = 1; |
| 7069 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7070 | 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] | 7071 | } |
| 7072 | ++i; |
| 7073 | } |
| 7074 | } |
| 7075 | } |
| 7076 | |
| 7077 | if (!replaced) { |
| 7078 | /* no match */ |
| 7079 | set_remove_node(set, i); |
| 7080 | } |
| 7081 | } |
| 7082 | |
| 7083 | return LY_SUCCESS; |
| 7084 | } |
| 7085 | |
| 7086 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7087 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 7088 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7089 | * |
| 7090 | * @param[in,out] set1 Set to use for the result. |
| 7091 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7092 | * @param[in] op Comparison operator to process. |
| 7093 | * @param[in] options XPath options. |
| 7094 | * @return LY_ERR |
| 7095 | */ |
| 7096 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7097 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7098 | { |
| 7099 | /* |
| 7100 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7101 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7102 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7103 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7104 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7105 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7106 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7107 | * |
| 7108 | * '=' or '!=' |
| 7109 | * BOOLEAN + BOOLEAN |
| 7110 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7111 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7112 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7113 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7114 | * NUMBER + NUMBER |
| 7115 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7116 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7117 | * STRING + STRING |
| 7118 | * |
| 7119 | * '<=', '<', '>=', '>' |
| 7120 | * NUMBER + NUMBER |
| 7121 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7122 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7123 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7124 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7125 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7126 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7127 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7128 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7129 | */ |
| 7130 | struct lyxp_set iter1, iter2; |
| 7131 | int result; |
| 7132 | int64_t i; |
| 7133 | LY_ERR rc; |
| 7134 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7135 | memset(&iter1, 0, sizeof iter1); |
| 7136 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7137 | |
| 7138 | /* iterative evaluation with node-sets */ |
| 7139 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7140 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7141 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7142 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7143 | switch (set2->type) { |
| 7144 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7145 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7146 | break; |
| 7147 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7148 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7149 | break; |
| 7150 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7151 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7152 | break; |
| 7153 | } |
| 7154 | LY_CHECK_RET(rc); |
| 7155 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7156 | /* canonize set2 */ |
| 7157 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 7158 | |
| 7159 | /* compare recursively */ |
| 7160 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 7161 | lyxp_set_free_content(&iter2); |
| 7162 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7163 | |
| 7164 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7165 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7166 | set_fill_boolean(set1, 1); |
| 7167 | return LY_SUCCESS; |
| 7168 | } |
| 7169 | } |
| 7170 | } else { |
| 7171 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7172 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7173 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7174 | case LYXP_SET_NUMBER: |
| 7175 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 7176 | break; |
| 7177 | case LYXP_SET_BOOLEAN: |
| 7178 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 7179 | break; |
| 7180 | default: |
| 7181 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 7182 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7183 | } |
| 7184 | LY_CHECK_RET(rc); |
| 7185 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7186 | /* canonize set1 */ |
| 7187 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter1, set1, &set2->val.nodes[i]), lyxp_set_free_content(&iter2), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7188 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7189 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7190 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7191 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 7192 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7193 | |
| 7194 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7195 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7196 | set_fill_boolean(set1, 1); |
| 7197 | return LY_SUCCESS; |
| 7198 | } |
| 7199 | } |
| 7200 | } |
| 7201 | |
| 7202 | /* false for all nodes */ |
| 7203 | set_fill_boolean(set1, 0); |
| 7204 | return LY_SUCCESS; |
| 7205 | } |
| 7206 | |
| 7207 | /* first convert properly */ |
| 7208 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7209 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7210 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7211 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7212 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7213 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7214 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7215 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7216 | LY_CHECK_RET(rc); |
| 7217 | } /* else we have 2 strings */ |
| 7218 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7219 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7220 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7221 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7222 | LY_CHECK_RET(rc); |
| 7223 | } |
| 7224 | |
| 7225 | assert(set1->type == set2->type); |
| 7226 | |
| 7227 | /* compute result */ |
| 7228 | if (op[0] == '=') { |
| 7229 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7230 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7231 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 7232 | result = (set1->val.num == set2->val.num); |
| 7233 | } else { |
| 7234 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 7235 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7236 | } |
| 7237 | } else if (op[0] == '!') { |
| 7238 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7239 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7240 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 7241 | result = (set1->val.num != set2->val.num); |
| 7242 | } else { |
| 7243 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | c205843 | 2020-11-06 17:26:21 +0100 | [diff] [blame] | 7244 | result = strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7245 | } |
| 7246 | } else { |
| 7247 | assert(set1->type == LYXP_SET_NUMBER); |
| 7248 | if (op[0] == '<') { |
| 7249 | if (op[1] == '=') { |
| 7250 | result = (set1->val.num <= set2->val.num); |
| 7251 | } else { |
| 7252 | result = (set1->val.num < set2->val.num); |
| 7253 | } |
| 7254 | } else { |
| 7255 | if (op[1] == '=') { |
| 7256 | result = (set1->val.num >= set2->val.num); |
| 7257 | } else { |
| 7258 | result = (set1->val.num > set2->val.num); |
| 7259 | } |
| 7260 | } |
| 7261 | } |
| 7262 | |
| 7263 | /* assign result */ |
| 7264 | if (result) { |
| 7265 | set_fill_boolean(set1, 1); |
| 7266 | } else { |
| 7267 | set_fill_boolean(set1, 0); |
| 7268 | } |
| 7269 | |
| 7270 | return LY_SUCCESS; |
| 7271 | } |
| 7272 | |
| 7273 | /** |
| 7274 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7275 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7276 | * |
| 7277 | * @param[in,out] set1 Set to use for the result. |
| 7278 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7279 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7280 | * @return LY_ERR |
| 7281 | */ |
| 7282 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7283 | 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] | 7284 | { |
| 7285 | LY_ERR rc; |
| 7286 | |
| 7287 | /* unary '-' */ |
| 7288 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7289 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7290 | LY_CHECK_RET(rc); |
| 7291 | set1->val.num *= -1; |
| 7292 | lyxp_set_free(set2); |
| 7293 | return LY_SUCCESS; |
| 7294 | } |
| 7295 | |
| 7296 | assert(set1 && set2); |
| 7297 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7298 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7299 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7300 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7301 | LY_CHECK_RET(rc); |
| 7302 | |
| 7303 | switch (op[0]) { |
| 7304 | /* '+' */ |
| 7305 | case '+': |
| 7306 | set1->val.num += set2->val.num; |
| 7307 | break; |
| 7308 | |
| 7309 | /* '-' */ |
| 7310 | case '-': |
| 7311 | set1->val.num -= set2->val.num; |
| 7312 | break; |
| 7313 | |
| 7314 | /* '*' */ |
| 7315 | case '*': |
| 7316 | set1->val.num *= set2->val.num; |
| 7317 | break; |
| 7318 | |
| 7319 | /* 'div' */ |
| 7320 | case 'd': |
| 7321 | set1->val.num /= set2->val.num; |
| 7322 | break; |
| 7323 | |
| 7324 | /* 'mod' */ |
| 7325 | case 'm': |
| 7326 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7327 | break; |
| 7328 | |
| 7329 | default: |
| 7330 | LOGINT_RET(set1->ctx); |
| 7331 | } |
| 7332 | |
| 7333 | return LY_SUCCESS; |
| 7334 | } |
| 7335 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7336 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7337 | * @brief Evaluate Predicate. Logs directly on error. |
| 7338 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7339 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7340 | * |
| 7341 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7342 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7343 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7344 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7345 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7346 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7347 | */ |
| 7348 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7349 | eval_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, enum lyxp_axis axis) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | { |
| 7351 | LY_ERR rc; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 7352 | uint16_t orig_exp; |
| 7353 | uint32_t i, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7354 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7355 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7356 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7357 | |
| 7358 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7359 | 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] | 7360 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7361 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7362 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7363 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7364 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7365 | 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] | 7366 | LY_CHECK_RET(rc); |
| 7367 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7368 | /* 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] | 7369 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | |
| 7371 | /* empty set, nothing to evaluate */ |
| 7372 | if (!set->used) { |
| 7373 | goto only_parse; |
| 7374 | } |
| 7375 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7376 | /* decide forward or reverse axis */ |
| 7377 | switch (axis) { |
| 7378 | case LYXP_AXIS_ANCESTOR: |
| 7379 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7380 | case LYXP_AXIS_PRECEDING: |
| 7381 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7382 | reverse_axis = 1; |
| 7383 | break; |
| 7384 | case LYXP_AXIS_DESCENDANT: |
| 7385 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7386 | case LYXP_AXIS_FOLLOWING: |
| 7387 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7388 | case LYXP_AXIS_PARENT: |
| 7389 | case LYXP_AXIS_CHILD: |
| 7390 | case LYXP_AXIS_SELF: |
| 7391 | case LYXP_AXIS_ATTRIBUTE: |
| 7392 | reverse_axis = 0; |
| 7393 | break; |
| 7394 | } |
| 7395 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7396 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7397 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7398 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7399 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7400 | set_init(&set2, set); |
| 7401 | 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] | 7402 | |
| 7403 | /* remember the node context position for position() and context size for last() */ |
| 7404 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7405 | |
| 7406 | set2.ctx_pos = orig_pos; |
| 7407 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7408 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7409 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7410 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7411 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7412 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7413 | return rc; |
| 7414 | } |
| 7415 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7416 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7417 | if (set2.type == LYXP_SET_NUMBER) { |
| 7418 | if ((long long)set2.val.num == orig_pos) { |
| 7419 | set2.val.num = 1; |
| 7420 | } else { |
| 7421 | set2.val.num = 0; |
| 7422 | } |
| 7423 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7424 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7425 | |
| 7426 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7427 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7428 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7429 | } |
| 7430 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7431 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7432 | |
| 7433 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7434 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7435 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7436 | /* there is a currently-valid node */ |
| 7437 | break; |
| 7438 | } |
| 7439 | } |
| 7440 | /* empty set, nothing to evaluate */ |
| 7441 | if (i == set->used) { |
| 7442 | goto only_parse; |
| 7443 | } |
| 7444 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7445 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7446 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7447 | /* set special in_ctx to all the valid snodes */ |
| 7448 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7449 | |
| 7450 | /* use the valid snodes one-by-one */ |
| 7451 | for (i = 0; i < set->used; ++i) { |
| 7452 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7453 | continue; |
| 7454 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7455 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7458 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7459 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7460 | LY_CHECK_RET(rc); |
| 7461 | |
| 7462 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7463 | } |
| 7464 | |
| 7465 | /* restore the state as it was before the predicate */ |
| 7466 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7467 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7468 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7469 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7470 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | } |
| 7472 | } |
| 7473 | |
| 7474 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7475 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7476 | set_fill_set(&set2, set); |
| 7477 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7478 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7479 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7480 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7481 | return rc; |
| 7482 | } |
| 7483 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7484 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7485 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7486 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7487 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7488 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7489 | } |
| 7490 | |
| 7491 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7492 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7493 | 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] | 7494 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7495 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7496 | |
| 7497 | return LY_SUCCESS; |
| 7498 | } |
| 7499 | |
| 7500 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7501 | * @brief Evaluate Literal. Logs directly on error. |
| 7502 | * |
| 7503 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7504 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7505 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7506 | */ |
| 7507 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7508 | eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7509 | { |
| 7510 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7511 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7512 | set_fill_string(set, "", 0); |
| 7513 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7514 | 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] | 7515 | } |
| 7516 | } |
| 7517 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7518 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7519 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7520 | } |
| 7521 | |
| 7522 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7523 | * @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] | 7524 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7525 | * @param[in] exp Full parsed XPath expression. |
| 7526 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7527 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 7528 | * @param[in] cur_mod Current module for the expression. |
| 7529 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7530 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7531 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7532 | * @param[out] predicates Parsed predicates. |
| 7533 | * @param[out] pred_type Type of @p predicates. |
| 7534 | * @return LY_SUCCESS on success, |
| 7535 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7536 | */ |
| 7537 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7538 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7539 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_VALUE_FORMAT format, void *prefix_data, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7540 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7541 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7542 | LY_ERR ret = LY_SUCCESS; |
| 7543 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7544 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7545 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7546 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7547 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7548 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7549 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7550 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7551 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7552 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7553 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7554 | return LY_EINVAL; |
| 7555 | } |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 7556 | for (key_count = 0, key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7557 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7558 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7559 | /* learn where the predicates end */ |
| 7560 | e_idx = *tok_idx; |
| 7561 | while (key_count) { |
| 7562 | /* '[' */ |
| 7563 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7564 | return LY_EINVAL; |
| 7565 | } |
| 7566 | ++e_idx; |
| 7567 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7568 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
| 7569 | /* definitely not a key */ |
| 7570 | return LY_EINVAL; |
| 7571 | } |
| 7572 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7573 | /* ']' */ |
| 7574 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7575 | ++e_idx; |
| 7576 | } |
| 7577 | ++e_idx; |
| 7578 | |
| 7579 | /* another presumably key predicate parsed */ |
| 7580 | --key_count; |
| 7581 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7582 | } else { |
| 7583 | /* learn just where this single predicate ends */ |
| 7584 | e_idx = *tok_idx; |
| 7585 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7586 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7587 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7588 | return LY_EINVAL; |
| 7589 | } |
| 7590 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7591 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7592 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
| 7593 | /* definitely not the value */ |
| 7594 | return LY_EINVAL; |
| 7595 | } |
| 7596 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7597 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7598 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7599 | ++e_idx; |
| 7600 | } |
| 7601 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7602 | } |
| 7603 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7604 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 7605 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 7606 | |
| 7607 | /* turn logging off */ |
| 7608 | prev_lo = ly_log_options(0); |
| 7609 | |
| 7610 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7611 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 7612 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7613 | |
| 7614 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7615 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 7616 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7617 | LY_CHECK_GOTO(ret, cleanup); |
| 7618 | |
| 7619 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7620 | *tok_idx = e_idx; |
| 7621 | |
| 7622 | cleanup: |
| 7623 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7624 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7625 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7626 | } |
| 7627 | |
| 7628 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7629 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7630 | * data node(s) could be found using a single hash-based search. |
| 7631 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7632 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7633 | * @param[in] node Next context node to check. |
| 7634 | * @param[in] name Expected node name. |
| 7635 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7636 | * @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] | 7637 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7638 | * @param[in] format Prefix format. |
| 7639 | * @param[in,out] found Previously found node, is updated. |
| 7640 | * @return LY_SUCCESS on success, |
| 7641 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7642 | */ |
| 7643 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7644 | eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7645 | uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7646 | const struct lysc_node **found) |
| 7647 | { |
| 7648 | const struct lysc_node *scnode; |
| 7649 | const struct lys_module *mod; |
| 7650 | uint32_t idx = 0; |
| 7651 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7652 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7653 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7654 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7655 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7656 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7657 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7658 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7659 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7660 | if (!mod->implemented) { |
| 7661 | continue; |
| 7662 | } |
| 7663 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7664 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7665 | if (scnode) { |
| 7666 | /* we have found a match */ |
| 7667 | break; |
| 7668 | } |
| 7669 | } |
| 7670 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7671 | if (!scnode) { |
| 7672 | /* all modules searched */ |
| 7673 | idx = 0; |
| 7674 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7675 | } else { |
| 7676 | /* search in top-level */ |
| 7677 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7678 | } |
| 7679 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7680 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7681 | /* we must adjust the module to inherit the one from the context node */ |
| 7682 | moveto_mod = node->schema->module; |
| 7683 | } |
| 7684 | |
| 7685 | /* search in children, do not repeat the same search */ |
| 7686 | 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] | 7687 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7688 | |
| 7689 | /* additional context check */ |
| 7690 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7691 | scnode = NULL; |
| 7692 | } |
| 7693 | |
| 7694 | if (scnode) { |
| 7695 | if (*found) { |
| 7696 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7697 | * (more hash-based searches would be required, not supported) */ |
| 7698 | return LY_ENOT; |
| 7699 | } else { |
| 7700 | /* remember the found schema node and continue to make sure it can be used */ |
| 7701 | *found = scnode; |
| 7702 | } |
| 7703 | } |
| 7704 | |
| 7705 | if (idx) { |
| 7706 | /* continue searching all the following models */ |
| 7707 | goto continue_search; |
| 7708 | } |
| 7709 | |
| 7710 | return LY_SUCCESS; |
| 7711 | } |
| 7712 | |
| 7713 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7714 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7715 | * |
| 7716 | * @param[in] set XPath set. |
| 7717 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7718 | * @param[in] ncname XPath NCName being evaluated. |
| 7719 | * @param[in] ncname_len Length of @p ncname. |
| 7720 | * @param[in] expr Whole XPath expression. |
| 7721 | * @param[in] options XPath options. |
| 7722 | */ |
| 7723 | static void |
| 7724 | eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname, |
| 7725 | uint16_t ncname_len, const char *expr, uint32_t options) |
| 7726 | { |
| 7727 | const char *format; |
| 7728 | char *path = NULL, *ppath = NULL; |
| 7729 | |
| 7730 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7731 | if (scparent) { |
| 7732 | /* generate path for the parent */ |
| 7733 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7734 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7735 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7736 | ppath = strdup("<root>"); |
| 7737 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7738 | ppath = strdup("<config-root>"); |
| 7739 | } |
| 7740 | } |
| 7741 | if (ppath) { |
| 7742 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7743 | if (options & LYXP_SCNODE_ERROR) { |
| 7744 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7745 | } else { |
| 7746 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7747 | } |
| 7748 | } else { |
| 7749 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7750 | if (options & LYXP_SCNODE_ERROR) { |
| 7751 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7752 | } else { |
| 7753 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7754 | } |
| 7755 | } |
| 7756 | free(path); |
| 7757 | free(ppath); |
| 7758 | } |
| 7759 | |
| 7760 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7761 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7762 | * |
| 7763 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7764 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7765 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7766 | * |
| 7767 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7768 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7769 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7770 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7771 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7772 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7773 | * @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] | 7774 | */ |
| 7775 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7776 | eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7777 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7778 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7779 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7780 | const char *ncname, *ncname_dict = NULL; |
| 7781 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7782 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7783 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7784 | struct ly_path_predicate *predicates = NULL; |
| 7785 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7786 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7787 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7788 | 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] | 7789 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7790 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7791 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7792 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7793 | goto moveto; |
| 7794 | } |
| 7795 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7796 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7797 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7798 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7799 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 7800 | /* all nodes will match */ |
| 7801 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7802 | } |
| 7803 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7804 | /* parse (and skip) module name */ |
| 7805 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7806 | LY_CHECK_GOTO(rc, cleanup); |
| 7807 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7808 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 7809 | /* all nodes from the module will match */ |
| 7810 | goto moveto; |
| 7811 | } |
| 7812 | |
| 7813 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 7814 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7815 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7816 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7817 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 7818 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7819 | /* check failed */ |
| 7820 | scnode = NULL; |
| 7821 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7822 | } |
| 7823 | } |
| 7824 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7825 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7826 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7827 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7828 | set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7829 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7830 | scnode = NULL; |
| 7831 | } |
| 7832 | } |
| 7833 | } |
| 7834 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7835 | if (!scnode) { |
| 7836 | /* 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] | 7837 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7838 | 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] | 7839 | } |
| 7840 | |
| 7841 | moveto: |
| 7842 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7843 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7844 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7845 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7846 | } else { |
| 7847 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7848 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7849 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7850 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7851 | } |
| 7852 | LY_CHECK_GOTO(rc, cleanup); |
| 7853 | } |
| 7854 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7855 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7856 | int64_t i; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7857 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7858 | |
| 7859 | /* remember parent if there is only one, to print in the warning */ |
| 7860 | for (i = 0; i < set->used; ++i) { |
| 7861 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 7862 | if (!scparent) { |
| 7863 | /* remember the context node */ |
| 7864 | scparent = &set->val.scnodes[i]; |
| 7865 | } else { |
| 7866 | /* several context nodes, no reasonable error possible */ |
| 7867 | scparent = NULL; |
| 7868 | break; |
| 7869 | } |
| 7870 | } |
| 7871 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7872 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7873 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 7874 | /* efficient evaluation that does not add all the descendants into the set */ |
| 7875 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7876 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7877 | if (all_desc) { |
| 7878 | /* "//" == "/descendant-or-self::node()/" */ |
| 7879 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 7880 | LY_CHECK_GOTO(rc, cleanup); |
| 7881 | } |
| 7882 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7883 | } |
| 7884 | LY_CHECK_GOTO(rc, cleanup); |
| 7885 | |
| 7886 | for (i = set->used - 1; i > -1; --i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7887 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7888 | break; |
| 7889 | } |
| 7890 | } |
| 7891 | if (i == -1) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7892 | /* generate message */ |
| 7893 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 7894 | |
| 7895 | if (options & LYXP_SCNODE_ERROR) { |
| 7896 | /* error */ |
| 7897 | rc = LY_EVALID; |
| 7898 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7899 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7900 | |
| 7901 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 7902 | rc = LY_ENOT; |
| 7903 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7904 | } |
| 7905 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7906 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 7907 | /* efficient evaluation */ |
| 7908 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 7909 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 7910 | /* we can find the child nodes using hashes */ |
| 7911 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7912 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7913 | if (all_desc) { |
| 7914 | /* "//" == "/descendant-or-self::node()/" */ |
| 7915 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 7916 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7917 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7918 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7919 | } |
| 7920 | LY_CHECK_GOTO(rc, cleanup); |
| 7921 | } |
| 7922 | } |
| 7923 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7924 | if (scnode_skip_pred) { |
| 7925 | /* skip predicates */ |
| 7926 | options |= LYXP_SKIP_EXPR; |
| 7927 | } |
| 7928 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7929 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7930 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7931 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7932 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7933 | } |
| 7934 | |
| 7935 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7936 | if (scnode_skip_pred) { |
| 7937 | /* restore options */ |
| 7938 | options &= ~LYXP_SKIP_EXPR; |
| 7939 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7940 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7941 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 7942 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7943 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7944 | return rc; |
| 7945 | } |
| 7946 | |
| 7947 | /** |
| 7948 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7949 | * |
| 7950 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7951 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7952 | * [8] NodeType ::= 'text' | 'node' |
| 7953 | * |
| 7954 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7955 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7956 | * @param[in] axis Axis to search on. |
| 7957 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7958 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7959 | * @param[in] options XPath options. |
| 7960 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7961 | */ |
| 7962 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7963 | eval_node_type_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7964 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7965 | { |
| 7966 | LY_ERR rc; |
| 7967 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7968 | (void)all_desc; |
| 7969 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7970 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7971 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7972 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 7973 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7974 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7975 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 7976 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7977 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7978 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7979 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7980 | 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] | 7981 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7982 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7983 | |
| 7984 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7985 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7986 | 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] | 7987 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7988 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7989 | |
| 7990 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7991 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7992 | 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] | 7993 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7994 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7995 | |
| 7996 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7997 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7998 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7999 | LY_CHECK_RET(rc); |
| 8000 | } |
| 8001 | |
| 8002 | return LY_SUCCESS; |
| 8003 | } |
| 8004 | |
| 8005 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8006 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8007 | * |
| 8008 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8009 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8010 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8011 | * |
| 8012 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8013 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8014 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8015 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8016 | * @param[in] options XPath options. |
| 8017 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8018 | */ |
| 8019 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8020 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 8021 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8022 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8023 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8024 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8025 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8026 | |
| 8027 | goto step; |
| 8028 | do { |
| 8029 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8030 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8031 | all_desc = 0; |
| 8032 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8033 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8034 | all_desc = 1; |
| 8035 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8036 | 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] | 8037 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8038 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8039 | |
| 8040 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8041 | /* AxisSpecifier */ |
| 8042 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8043 | 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] | 8044 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8045 | 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] | 8046 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8047 | ++(*tok_idx); |
| 8048 | |
| 8049 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8050 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8051 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8052 | ++(*tok_idx); |
| 8053 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8054 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8055 | |
| 8056 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8057 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8058 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8059 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8060 | /* default */ |
| 8061 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8062 | } |
| 8063 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8064 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8065 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8066 | case LYXP_TOKEN_DOT: |
| 8067 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8068 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8069 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8070 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8071 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8072 | rc = LY_EVALID; |
| 8073 | goto cleanup; |
| 8074 | } |
| 8075 | |
| 8076 | if (all_desc) { |
| 8077 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8078 | LY_CHECK_GOTO(rc, cleanup); |
| 8079 | } |
| 8080 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8081 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8082 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8083 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8084 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8085 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8086 | break; |
| 8087 | |
| 8088 | case LYXP_TOKEN_DDOT: |
| 8089 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8090 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8091 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8092 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8093 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8094 | rc = LY_EVALID; |
| 8095 | goto cleanup; |
| 8096 | } |
| 8097 | |
| 8098 | if (all_desc) { |
| 8099 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8100 | LY_CHECK_GOTO(rc, cleanup); |
| 8101 | } |
| 8102 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8103 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8104 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8105 | 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] | 8106 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8107 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8108 | break; |
| 8109 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8110 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8111 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8112 | 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] | 8113 | if (rc == LY_ENOT) { |
| 8114 | assert(options & LYXP_SCNODE_ALL); |
| 8115 | /* skip the rest of this path */ |
| 8116 | rc = LY_SUCCESS; |
| 8117 | scnode_skip_path = 1; |
| 8118 | options |= LYXP_SKIP_EXPR; |
| 8119 | } |
| 8120 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8121 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8122 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8123 | case LYXP_TOKEN_NODETYPE: |
| 8124 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8125 | 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] | 8126 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8127 | break; |
| 8128 | |
| 8129 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8130 | LOGINT(set->ctx); |
| 8131 | rc = LY_EINT; |
| 8132 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8133 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8134 | } 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] | 8135 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8136 | cleanup: |
| 8137 | if (scnode_skip_path) { |
| 8138 | options &= ~LYXP_SKIP_EXPR; |
| 8139 | } |
| 8140 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8141 | } |
| 8142 | |
| 8143 | /** |
| 8144 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8145 | * |
| 8146 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8147 | * |
| 8148 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8149 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8150 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8151 | * @param[in] options XPath options. |
| 8152 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8153 | */ |
| 8154 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8155 | eval_absolute_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8156 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8157 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8158 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8159 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8160 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8161 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8162 | } |
| 8163 | |
| 8164 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8165 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8166 | /* evaluate '/' - deferred */ |
| 8167 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8168 | 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] | 8169 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8170 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8171 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8172 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8173 | return LY_SUCCESS; |
| 8174 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8175 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8176 | case LYXP_TOKEN_DOT: |
| 8177 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8178 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8179 | case LYXP_TOKEN_AT: |
| 8180 | case LYXP_TOKEN_NAMETEST: |
| 8181 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8182 | 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] | 8183 | break; |
| 8184 | default: |
| 8185 | break; |
| 8186 | } |
| 8187 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8188 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8189 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8190 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8191 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8192 | 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] | 8193 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8194 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8195 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8196 | 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] | 8197 | } |
| 8198 | |
| 8199 | return LY_SUCCESS; |
| 8200 | } |
| 8201 | |
| 8202 | /** |
| 8203 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8204 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8205 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8206 | * |
| 8207 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8208 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8209 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8210 | * @param[in] options XPath options. |
| 8211 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8212 | */ |
| 8213 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8214 | eval_function_call(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8215 | { |
| 8216 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8217 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8218 | LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL; |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 8219 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8220 | struct lyxp_set **args = NULL, **args_aux; |
| 8221 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8222 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8223 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8224 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8225 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8226 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8227 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8228 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8229 | xpath_func = &xpath_sum; |
| 8230 | } |
| 8231 | break; |
| 8232 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8233 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8234 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8235 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8236 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8237 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8238 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8239 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8240 | xpath_func = &xpath_true; |
| 8241 | } |
| 8242 | break; |
| 8243 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8244 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8245 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8246 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8247 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8248 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8249 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8250 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8251 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8252 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8253 | xpath_func = &xpath_deref; |
| 8254 | } |
| 8255 | break; |
| 8256 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8257 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8258 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8259 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8260 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8261 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8262 | xpath_func = &xpath_string; |
| 8263 | } |
| 8264 | break; |
| 8265 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8266 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8267 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8268 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8269 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8270 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8271 | xpath_func = &xpath_current; |
| 8272 | } |
| 8273 | break; |
| 8274 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8275 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8276 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8277 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8278 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8279 | } 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] | 8280 | xpath_func = &xpath_re_match; |
| 8281 | } |
| 8282 | break; |
| 8283 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8284 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8285 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8286 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8287 | xpath_func = &xpath_translate; |
| 8288 | } |
| 8289 | break; |
| 8290 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8291 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8292 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8293 | } 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] | 8294 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8295 | } 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] | 8296 | xpath_func = &xpath_bit_is_set; |
| 8297 | } |
| 8298 | break; |
| 8299 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8300 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8301 | xpath_func = &xpath_starts_with; |
| 8302 | } |
| 8303 | break; |
| 8304 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8305 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8306 | xpath_func = &xpath_derived_from; |
| 8307 | } |
| 8308 | break; |
| 8309 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8310 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8311 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8312 | } 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] | 8313 | xpath_func = &xpath_string_length; |
| 8314 | } |
| 8315 | break; |
| 8316 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8317 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8318 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8319 | } 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] | 8320 | xpath_func = &xpath_substring_after; |
| 8321 | } |
| 8322 | break; |
| 8323 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8324 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8325 | xpath_func = &xpath_substring_before; |
| 8326 | } |
| 8327 | break; |
| 8328 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8329 | 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] | 8330 | xpath_func = &xpath_derived_from_or_self; |
| 8331 | } |
| 8332 | break; |
| 8333 | } |
| 8334 | |
| 8335 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8336 | 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] | 8337 | return LY_EVALID; |
| 8338 | } |
| 8339 | } |
| 8340 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8341 | 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] | 8342 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8343 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8344 | |
| 8345 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8346 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8347 | 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] | 8348 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8349 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8350 | |
| 8351 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8352 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8353 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8354 | args = malloc(sizeof *args); |
| 8355 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8356 | arg_count = 1; |
| 8357 | args[0] = set_copy(set); |
| 8358 | if (!args[0]) { |
| 8359 | rc = LY_EMEM; |
| 8360 | goto cleanup; |
| 8361 | } |
| 8362 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8363 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8364 | LY_CHECK_GOTO(rc, cleanup); |
| 8365 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8366 | 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] | 8367 | LY_CHECK_GOTO(rc, cleanup); |
| 8368 | } |
| 8369 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8370 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8371 | 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] | 8372 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8373 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8374 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8375 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8376 | ++arg_count; |
| 8377 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8378 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8379 | args = args_aux; |
| 8380 | args[arg_count - 1] = set_copy(set); |
| 8381 | if (!args[arg_count - 1]) { |
| 8382 | rc = LY_EMEM; |
| 8383 | goto cleanup; |
| 8384 | } |
| 8385 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8386 | 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] | 8387 | LY_CHECK_GOTO(rc, cleanup); |
| 8388 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8389 | 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] | 8390 | LY_CHECK_GOTO(rc, cleanup); |
| 8391 | } |
| 8392 | } |
| 8393 | |
| 8394 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8395 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8396 | 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] | 8397 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8398 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8399 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8400 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8401 | /* evaluate function */ |
| 8402 | rc = xpath_func(args, arg_count, set, options); |
| 8403 | |
| 8404 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8405 | /* merge all nodes from arg evaluations */ |
| 8406 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8407 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8408 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8409 | } |
| 8410 | } |
| 8411 | } else { |
| 8412 | rc = LY_SUCCESS; |
| 8413 | } |
| 8414 | |
| 8415 | cleanup: |
| 8416 | for (i = 0; i < arg_count; ++i) { |
| 8417 | lyxp_set_free(args[i]); |
| 8418 | } |
| 8419 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8420 | return rc; |
| 8421 | } |
| 8422 | |
| 8423 | /** |
| 8424 | * @brief Evaluate Number. Logs directly on error. |
| 8425 | * |
| 8426 | * @param[in] ctx Context for errors. |
| 8427 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8428 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8429 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8430 | * @return LY_ERR |
| 8431 | */ |
| 8432 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8433 | eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8434 | { |
| 8435 | long double num; |
| 8436 | char *endptr; |
| 8437 | |
| 8438 | if (set) { |
| 8439 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8440 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8441 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8442 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8443 | 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] | 8444 | 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] | 8445 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8446 | } 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] | 8447 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8448 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8449 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8450 | return LY_EVALID; |
| 8451 | } |
| 8452 | |
| 8453 | set_fill_number(set, num); |
| 8454 | } |
| 8455 | |
| 8456 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8457 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8458 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8459 | return LY_SUCCESS; |
| 8460 | } |
| 8461 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8462 | LY_ERR |
| 8463 | lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var) |
| 8464 | { |
| 8465 | LY_ERR ret = LY_ENOTFOUND; |
| 8466 | LY_ARRAY_COUNT_TYPE u; |
| 8467 | |
| 8468 | assert(vars && name); |
| 8469 | |
| 8470 | name_len = name_len ? name_len : strlen(name); |
| 8471 | |
| 8472 | LY_ARRAY_FOR(vars, u) { |
| 8473 | if (!strncmp(vars[u].name, name, name_len)) { |
| 8474 | ret = LY_SUCCESS; |
| 8475 | break; |
| 8476 | } |
| 8477 | } |
| 8478 | |
| 8479 | if (var && !ret) { |
| 8480 | *var = &vars[u]; |
| 8481 | } |
| 8482 | |
| 8483 | return ret; |
| 8484 | } |
| 8485 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8486 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8487 | * @brief Evaluate VariableReference. |
| 8488 | * |
| 8489 | * @param[in] exp Parsed XPath expression. |
| 8490 | * @param[in] tok_idx Position in the expression @p exp. |
| 8491 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8492 | * @param[in,out] set Context and result set. |
| 8493 | * @param[in] options XPath options. |
| 8494 | * @return LY_ERR value. |
| 8495 | */ |
| 8496 | static LY_ERR |
| 8497 | eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
| 8498 | { |
| 8499 | LY_ERR ret; |
| 8500 | const char *name; |
| 8501 | struct lyxp_var *var; |
| 8502 | const struct lyxp_var *vars; |
| 8503 | struct lyxp_expr *tokens = NULL; |
| 8504 | uint16_t token_index; |
| 8505 | |
| 8506 | vars = set->vars; |
| 8507 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8508 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8509 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
| 8510 | ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var); |
| 8511 | LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret, |
| 8512 | "XPath variable \"%s\" not defined.", name), ret); |
| 8513 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8514 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8515 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8516 | LY_CHECK_GOTO(ret, cleanup); |
| 8517 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8518 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8519 | token_index = 0; |
| 8520 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8521 | LY_CHECK_GOTO(ret, cleanup); |
| 8522 | |
| 8523 | cleanup: |
| 8524 | lyxp_expr_free(set->ctx, tokens); |
| 8525 | |
| 8526 | return ret; |
| 8527 | } |
| 8528 | |
| 8529 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8530 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8531 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8532 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8534 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8535 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8536 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8537 | * |
| 8538 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8539 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8540 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8541 | * @param[in] options XPath options. |
| 8542 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8543 | */ |
| 8544 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8545 | eval_path_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8546 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8547 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8548 | LY_ERR rc; |
| 8549 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8550 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8551 | case LYXP_TOKEN_PAR1: |
| 8552 | /* '(' Expr ')' */ |
| 8553 | |
| 8554 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8555 | 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] | 8556 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8557 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8558 | |
| 8559 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8560 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8561 | LY_CHECK_RET(rc); |
| 8562 | |
| 8563 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8564 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8565 | 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] | 8566 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8567 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8568 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8569 | goto predicate; |
| 8570 | |
| 8571 | case LYXP_TOKEN_DOT: |
| 8572 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8573 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8574 | case LYXP_TOKEN_AT: |
| 8575 | case LYXP_TOKEN_NAMETEST: |
| 8576 | case LYXP_TOKEN_NODETYPE: |
| 8577 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8578 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8579 | LY_CHECK_RET(rc); |
| 8580 | break; |
| 8581 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8582 | case LYXP_TOKEN_VARREF: |
| 8583 | /* VariableReference */ |
| 8584 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8585 | LY_CHECK_RET(rc); |
| 8586 | ++(*tok_idx); |
| 8587 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8588 | goto predicate; |
| 8589 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8590 | case LYXP_TOKEN_FUNCNAME: |
| 8591 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8592 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8593 | LY_CHECK_RET(rc); |
| 8594 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8595 | goto predicate; |
| 8596 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8597 | case LYXP_TOKEN_OPER_PATH: |
| 8598 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8599 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8600 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8601 | LY_CHECK_RET(rc); |
| 8602 | break; |
| 8603 | |
| 8604 | case LYXP_TOKEN_LITERAL: |
| 8605 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8606 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8607 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8608 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8609 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8610 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8611 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8612 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8613 | } |
| 8614 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8615 | goto predicate; |
| 8616 | |
| 8617 | case LYXP_TOKEN_NUMBER: |
| 8618 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8619 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8620 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8621 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8622 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8623 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8624 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8625 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8626 | } |
| 8627 | LY_CHECK_RET(rc); |
| 8628 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8629 | goto predicate; |
| 8630 | |
| 8631 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8632 | 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] | 8633 | return LY_EVALID; |
| 8634 | } |
| 8635 | |
| 8636 | return LY_SUCCESS; |
| 8637 | |
| 8638 | predicate: |
| 8639 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8640 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8641 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8642 | LY_CHECK_RET(rc); |
| 8643 | } |
| 8644 | |
| 8645 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8646 | 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] | 8647 | |
| 8648 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8649 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8650 | all_desc = 0; |
| 8651 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8652 | all_desc = 1; |
| 8653 | } |
| 8654 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8655 | 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] | 8656 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8657 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8658 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8659 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8660 | LY_CHECK_RET(rc); |
| 8661 | } |
| 8662 | |
| 8663 | return LY_SUCCESS; |
| 8664 | } |
| 8665 | |
| 8666 | /** |
| 8667 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8668 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8669 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8670 | * |
| 8671 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8672 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8673 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8674 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8675 | * @param[in] options XPath options. |
| 8676 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8677 | */ |
| 8678 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8679 | eval_union_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8680 | { |
| 8681 | LY_ERR rc = LY_SUCCESS; |
| 8682 | struct lyxp_set orig_set, set2; |
| 8683 | uint16_t i; |
| 8684 | |
| 8685 | assert(repeat); |
| 8686 | |
| 8687 | set_init(&orig_set, set); |
| 8688 | set_init(&set2, set); |
| 8689 | |
| 8690 | set_fill_set(&orig_set, set); |
| 8691 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8692 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8693 | LY_CHECK_GOTO(rc, cleanup); |
| 8694 | |
| 8695 | /* ('|' PathExpr)* */ |
| 8696 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8697 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8698 | 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] | 8699 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8700 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8701 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8702 | if (options & LYXP_SKIP_EXPR) { |
| 8703 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8704 | LY_CHECK_GOTO(rc, cleanup); |
| 8705 | continue; |
| 8706 | } |
| 8707 | |
| 8708 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8709 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8710 | LY_CHECK_GOTO(rc, cleanup); |
| 8711 | |
| 8712 | /* eval */ |
| 8713 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8714 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8715 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8716 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8717 | LY_CHECK_GOTO(rc, cleanup); |
| 8718 | } |
| 8719 | } |
| 8720 | |
| 8721 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8722 | lyxp_set_free_content(&orig_set); |
| 8723 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8724 | return rc; |
| 8725 | } |
| 8726 | |
| 8727 | /** |
| 8728 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8729 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8730 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8731 | * |
| 8732 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8733 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8734 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8735 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8736 | * @param[in] options XPath options. |
| 8737 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8738 | */ |
| 8739 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8740 | eval_unary_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8741 | { |
| 8742 | LY_ERR rc; |
| 8743 | uint16_t this_op, i; |
| 8744 | |
| 8745 | assert(repeat); |
| 8746 | |
| 8747 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8748 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8749 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8750 | 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] | 8751 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8752 | 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] | 8753 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8754 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8755 | } |
| 8756 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8757 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8758 | LY_CHECK_RET(rc); |
| 8759 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8760 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8761 | if (options & LYXP_SCNODE_ALL) { |
| 8762 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 8763 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8764 | 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] | 8765 | LY_CHECK_RET(rc); |
| 8766 | } |
| 8767 | } |
| 8768 | |
| 8769 | return LY_SUCCESS; |
| 8770 | } |
| 8771 | |
| 8772 | /** |
| 8773 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 8774 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8775 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8776 | * | MultiplicativeExpr '*' UnaryExpr |
| 8777 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8778 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8779 | * |
| 8780 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8781 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8782 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8783 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8784 | * @param[in] options XPath options. |
| 8785 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8786 | */ |
| 8787 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8788 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 8789 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8790 | { |
| 8791 | LY_ERR rc; |
| 8792 | uint16_t this_op; |
| 8793 | struct lyxp_set orig_set, set2; |
| 8794 | uint16_t i; |
| 8795 | |
| 8796 | assert(repeat); |
| 8797 | |
| 8798 | set_init(&orig_set, set); |
| 8799 | set_init(&set2, set); |
| 8800 | |
| 8801 | set_fill_set(&orig_set, set); |
| 8802 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8803 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8804 | LY_CHECK_GOTO(rc, cleanup); |
| 8805 | |
| 8806 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 8807 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8808 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8809 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8810 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8811 | 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] | 8812 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8813 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8814 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8815 | if (options & LYXP_SKIP_EXPR) { |
| 8816 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8817 | LY_CHECK_GOTO(rc, cleanup); |
| 8818 | continue; |
| 8819 | } |
| 8820 | |
| 8821 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8822 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8823 | LY_CHECK_GOTO(rc, cleanup); |
| 8824 | |
| 8825 | /* eval */ |
| 8826 | if (options & LYXP_SCNODE_ALL) { |
| 8827 | 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] | 8828 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8829 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8830 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8831 | 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] | 8832 | LY_CHECK_GOTO(rc, cleanup); |
| 8833 | } |
| 8834 | } |
| 8835 | |
| 8836 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8837 | lyxp_set_free_content(&orig_set); |
| 8838 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8839 | return rc; |
| 8840 | } |
| 8841 | |
| 8842 | /** |
| 8843 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 8844 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8845 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8846 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8847 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8848 | * |
| 8849 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8850 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8851 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8852 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8853 | * @param[in] options XPath options. |
| 8854 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8855 | */ |
| 8856 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8857 | eval_additive_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8858 | { |
| 8859 | LY_ERR rc; |
| 8860 | uint16_t this_op; |
| 8861 | struct lyxp_set orig_set, set2; |
| 8862 | uint16_t i; |
| 8863 | |
| 8864 | assert(repeat); |
| 8865 | |
| 8866 | set_init(&orig_set, set); |
| 8867 | set_init(&set2, set); |
| 8868 | |
| 8869 | set_fill_set(&orig_set, set); |
| 8870 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8871 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8872 | LY_CHECK_GOTO(rc, cleanup); |
| 8873 | |
| 8874 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8875 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8876 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8877 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8878 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8879 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8880 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8881 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8882 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8883 | if (options & LYXP_SKIP_EXPR) { |
| 8884 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8885 | LY_CHECK_GOTO(rc, cleanup); |
| 8886 | continue; |
| 8887 | } |
| 8888 | |
| 8889 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8890 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8891 | LY_CHECK_GOTO(rc, cleanup); |
| 8892 | |
| 8893 | /* eval */ |
| 8894 | if (options & LYXP_SCNODE_ALL) { |
| 8895 | 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] | 8896 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8897 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8898 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8899 | 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] | 8900 | LY_CHECK_GOTO(rc, cleanup); |
| 8901 | } |
| 8902 | } |
| 8903 | |
| 8904 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8905 | lyxp_set_free_content(&orig_set); |
| 8906 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8907 | return rc; |
| 8908 | } |
| 8909 | |
| 8910 | /** |
| 8911 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8912 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8913 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8914 | * | RelationalExpr '<' AdditiveExpr |
| 8915 | * | RelationalExpr '>' AdditiveExpr |
| 8916 | * | RelationalExpr '<=' AdditiveExpr |
| 8917 | * | RelationalExpr '>=' AdditiveExpr |
| 8918 | * |
| 8919 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8920 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8921 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8922 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8923 | * @param[in] options XPath options. |
| 8924 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8925 | */ |
| 8926 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8927 | eval_relational_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8928 | { |
| 8929 | LY_ERR rc; |
| 8930 | uint16_t this_op; |
| 8931 | struct lyxp_set orig_set, set2; |
| 8932 | uint16_t i; |
| 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_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8942 | LY_CHECK_GOTO(rc, cleanup); |
| 8943 | |
| 8944 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8945 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8946 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8947 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8948 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8949 | 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] | 8950 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8951 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8952 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8953 | if (options & LYXP_SKIP_EXPR) { |
| 8954 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8955 | LY_CHECK_GOTO(rc, cleanup); |
| 8956 | continue; |
| 8957 | } |
| 8958 | |
| 8959 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8960 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8961 | LY_CHECK_GOTO(rc, cleanup); |
| 8962 | |
| 8963 | /* eval */ |
| 8964 | if (options & LYXP_SCNODE_ALL) { |
| 8965 | 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] | 8966 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8967 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8968 | } else { |
| 8969 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8970 | LY_CHECK_GOTO(rc, cleanup); |
| 8971 | } |
| 8972 | } |
| 8973 | |
| 8974 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8975 | lyxp_set_free_content(&orig_set); |
| 8976 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8977 | return rc; |
| 8978 | } |
| 8979 | |
| 8980 | /** |
| 8981 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8982 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8983 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8984 | * | EqualityExpr '!=' RelationalExpr |
| 8985 | * |
| 8986 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8987 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8988 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8989 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8990 | * @param[in] options XPath options. |
| 8991 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8992 | */ |
| 8993 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8994 | eval_equality_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8995 | { |
| 8996 | LY_ERR rc; |
| 8997 | uint16_t this_op; |
| 8998 | struct lyxp_set orig_set, set2; |
| 8999 | uint16_t i; |
| 9000 | |
| 9001 | assert(repeat); |
| 9002 | |
| 9003 | set_init(&orig_set, set); |
| 9004 | set_init(&set2, set); |
| 9005 | |
| 9006 | set_fill_set(&orig_set, set); |
| 9007 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9008 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9009 | LY_CHECK_GOTO(rc, cleanup); |
| 9010 | |
| 9011 | /* ('=' / '!=' RelationalExpr)* */ |
| 9012 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9013 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9014 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9015 | 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] | 9016 | 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] | 9017 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9018 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9019 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9020 | if (options & LYXP_SKIP_EXPR) { |
| 9021 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9022 | LY_CHECK_GOTO(rc, cleanup); |
| 9023 | continue; |
| 9024 | } |
| 9025 | |
| 9026 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9027 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9028 | LY_CHECK_GOTO(rc, cleanup); |
| 9029 | |
| 9030 | /* eval */ |
| 9031 | if (options & LYXP_SCNODE_ALL) { |
| 9032 | 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] | 9033 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9034 | 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] | 9035 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9036 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9037 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9038 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 9039 | LY_CHECK_GOTO(rc, cleanup); |
| 9040 | } |
| 9041 | } |
| 9042 | |
| 9043 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9044 | lyxp_set_free_content(&orig_set); |
| 9045 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9046 | return rc; |
| 9047 | } |
| 9048 | |
| 9049 | /** |
| 9050 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9051 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9052 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9053 | * |
| 9054 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9055 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9056 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9057 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9058 | * @param[in] options XPath options. |
| 9059 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9060 | */ |
| 9061 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9062 | eval_and_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9063 | { |
| 9064 | LY_ERR rc; |
| 9065 | struct lyxp_set orig_set, set2; |
| 9066 | uint16_t i; |
| 9067 | |
| 9068 | assert(repeat); |
| 9069 | |
| 9070 | set_init(&orig_set, set); |
| 9071 | set_init(&set2, set); |
| 9072 | |
| 9073 | set_fill_set(&orig_set, set); |
| 9074 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9075 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9076 | LY_CHECK_GOTO(rc, cleanup); |
| 9077 | |
| 9078 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9079 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9080 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9081 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9082 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9083 | } |
| 9084 | |
| 9085 | /* ('and' EqualityExpr)* */ |
| 9086 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9087 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9088 | 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] | 9089 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9090 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9091 | |
| 9092 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9093 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9094 | 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] | 9095 | LY_CHECK_GOTO(rc, cleanup); |
| 9096 | continue; |
| 9097 | } |
| 9098 | |
| 9099 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9100 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9101 | LY_CHECK_GOTO(rc, cleanup); |
| 9102 | |
| 9103 | /* eval - just get boolean value actually */ |
| 9104 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9105 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9106 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9107 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9108 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9109 | set_fill_set(set, &set2); |
| 9110 | } |
| 9111 | } |
| 9112 | |
| 9113 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9114 | lyxp_set_free_content(&orig_set); |
| 9115 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9116 | return rc; |
| 9117 | } |
| 9118 | |
| 9119 | /** |
| 9120 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9121 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9122 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9123 | * |
| 9124 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9125 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9126 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9127 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9128 | * @param[in] options XPath options. |
| 9129 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9130 | */ |
| 9131 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9132 | eval_or_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9133 | { |
| 9134 | LY_ERR rc; |
| 9135 | struct lyxp_set orig_set, set2; |
| 9136 | uint16_t i; |
| 9137 | |
| 9138 | assert(repeat); |
| 9139 | |
| 9140 | set_init(&orig_set, set); |
| 9141 | set_init(&set2, set); |
| 9142 | |
| 9143 | set_fill_set(&orig_set, set); |
| 9144 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9145 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9146 | LY_CHECK_GOTO(rc, cleanup); |
| 9147 | |
| 9148 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9149 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9150 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9151 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9152 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9153 | } |
| 9154 | |
| 9155 | /* ('or' AndExpr)* */ |
| 9156 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9157 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9158 | 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] | 9159 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9160 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9161 | |
| 9162 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9163 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9164 | 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] | 9165 | LY_CHECK_GOTO(rc, cleanup); |
| 9166 | continue; |
| 9167 | } |
| 9168 | |
| 9169 | set_fill_set(&set2, &orig_set); |
| 9170 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9171 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9172 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9173 | LY_CHECK_GOTO(rc, cleanup); |
| 9174 | |
| 9175 | /* eval - just get boolean value actually */ |
| 9176 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9177 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9178 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9179 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9180 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9181 | set_fill_set(set, &set2); |
| 9182 | } |
| 9183 | } |
| 9184 | |
| 9185 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9186 | lyxp_set_free_content(&orig_set); |
| 9187 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9188 | return rc; |
| 9189 | } |
| 9190 | |
| 9191 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9192 | * @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] | 9193 | * |
| 9194 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9195 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9196 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9197 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9198 | * @param[in] options XPath options. |
| 9199 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9200 | */ |
| 9201 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9202 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 9203 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9204 | { |
| 9205 | uint16_t i, count; |
| 9206 | enum lyxp_expr_type next_etype; |
| 9207 | LY_ERR rc; |
| 9208 | |
| 9209 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9210 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9211 | next_etype = LYXP_EXPR_NONE; |
| 9212 | } else { |
| 9213 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9214 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9215 | |
| 9216 | /* select one-priority lower because etype expression called us */ |
| 9217 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9218 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9219 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9220 | 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] | 9221 | } else { |
| 9222 | next_etype = LYXP_EXPR_NONE; |
| 9223 | } |
| 9224 | } |
| 9225 | |
| 9226 | /* decide what expression are we parsing based on the repeat */ |
| 9227 | switch (next_etype) { |
| 9228 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9229 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9230 | break; |
| 9231 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9232 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9233 | break; |
| 9234 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9235 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9236 | break; |
| 9237 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9238 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9239 | break; |
| 9240 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9241 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9242 | break; |
| 9243 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9244 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9245 | break; |
| 9246 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9247 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9248 | break; |
| 9249 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9250 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9251 | break; |
| 9252 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9253 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9254 | break; |
| 9255 | default: |
| 9256 | LOGINT_RET(set->ctx); |
| 9257 | } |
| 9258 | |
| 9259 | return rc; |
| 9260 | } |
| 9261 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9262 | /** |
| 9263 | * @brief Get root type. |
| 9264 | * |
| 9265 | * @param[in] ctx_node Context node. |
| 9266 | * @param[in] ctx_scnode Schema context node. |
| 9267 | * @param[in] options XPath options. |
| 9268 | * @return Root type. |
| 9269 | */ |
| 9270 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9271 | 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] | 9272 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9273 | const struct lysc_node *op; |
| 9274 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9275 | /* explicit */ |
| 9276 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9277 | return LYXP_NODE_ROOT; |
| 9278 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9279 | return LYXP_NODE_ROOT_CONFIG; |
| 9280 | } |
| 9281 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9282 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9283 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9284 | 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] | 9285 | |
| 9286 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9287 | /* general root that can access everything */ |
| 9288 | return LYXP_NODE_ROOT; |
| 9289 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9290 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9291 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9292 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9293 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9294 | } |
| 9295 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9296 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9297 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9298 | 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] | 9299 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9300 | if (op || !(options & LYXP_SCHEMA)) { |
| 9301 | /* general root that can access everything */ |
| 9302 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9303 | } 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] | 9304 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9305 | return LYXP_NODE_ROOT_CONFIG; |
| 9306 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9307 | return LYXP_NODE_ROOT; |
| 9308 | } |
| 9309 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9310 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9311 | lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9312 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree, |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9313 | const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9314 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9315 | uint16_t tok_idx = 0; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9316 | const struct lysc_node *snode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9317 | LY_ERR rc; |
| 9318 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9319 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9320 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9321 | 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] | 9322 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9323 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9324 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9325 | if (tree) { |
| 9326 | /* adjust the pointer to be the first top-level sibling */ |
| 9327 | while (tree->parent) { |
| 9328 | tree = lyd_parent(tree); |
| 9329 | } |
| 9330 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9331 | |
| 9332 | for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {} |
| 9333 | if (snode) { |
| 9334 | /* unable to evaluate absolute paths */ |
| 9335 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9336 | LYD_NAME(tree)); |
| 9337 | return LY_EINVAL; |
| 9338 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9339 | } |
| 9340 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9341 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9342 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9343 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9344 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9345 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9346 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9347 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9348 | set->cur_node = ctx_node; |
| 9349 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9350 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9351 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9352 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9353 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9354 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9355 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9356 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9357 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9358 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9359 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9360 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9361 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9362 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9363 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9364 | } |
| 9365 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9366 | if (set->cur_node) { |
| 9367 | LOG_LOCBACK(0, 1, 0, 0); |
| 9368 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9369 | return rc; |
| 9370 | } |
| 9371 | |
| 9372 | #if 0 |
| 9373 | |
| 9374 | /* full xml printing of set elements, not used currently */ |
| 9375 | |
| 9376 | void |
| 9377 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9378 | { |
| 9379 | uint32_t i; |
| 9380 | char *str_num; |
| 9381 | struct lyout out; |
| 9382 | |
| 9383 | memset(&out, 0, sizeof out); |
| 9384 | |
| 9385 | out.type = LYOUT_STREAM; |
| 9386 | out.method.f = f; |
| 9387 | |
| 9388 | switch (set->type) { |
| 9389 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9390 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9391 | break; |
| 9392 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9393 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9394 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9395 | break; |
| 9396 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9397 | ly_print_(&out, "String XPath set:\n"); |
| 9398 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9399 | break; |
| 9400 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9401 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9402 | |
| 9403 | if (isnan(set->value.num)) { |
| 9404 | str_num = strdup("NaN"); |
| 9405 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9406 | str_num = strdup("0"); |
| 9407 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9408 | str_num = strdup("Infinity"); |
| 9409 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9410 | str_num = strdup("-Infinity"); |
| 9411 | } else if ((long long)set->value.num == set->value.num) { |
| 9412 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9413 | str_num = NULL; |
| 9414 | } |
| 9415 | } else { |
| 9416 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9417 | str_num = NULL; |
| 9418 | } |
| 9419 | } |
| 9420 | if (!str_num) { |
| 9421 | LOGMEM; |
| 9422 | return; |
| 9423 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9424 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9425 | free(str_num); |
| 9426 | break; |
| 9427 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9428 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9429 | |
| 9430 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9431 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9432 | switch (set->node_type[i]) { |
| 9433 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9434 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9435 | break; |
| 9436 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9437 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9438 | break; |
| 9439 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9440 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9441 | break; |
| 9442 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9443 | 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] | 9444 | break; |
| 9445 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9446 | 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] | 9447 | break; |
| 9448 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9449 | 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] | 9450 | break; |
| 9451 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9452 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9453 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9454 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9455 | break; |
| 9456 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9457 | 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] | 9458 | break; |
| 9459 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9460 | 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] | 9461 | break; |
| 9462 | } |
| 9463 | } |
| 9464 | break; |
| 9465 | } |
| 9466 | } |
| 9467 | |
| 9468 | #endif |
| 9469 | |
| 9470 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9471 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9472 | { |
| 9473 | long double num; |
| 9474 | char *str; |
| 9475 | LY_ERR rc; |
| 9476 | |
| 9477 | if (!set || (set->type == target)) { |
| 9478 | return LY_SUCCESS; |
| 9479 | } |
| 9480 | |
| 9481 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9482 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9483 | |
| 9484 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9485 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9486 | return LY_EINVAL; |
| 9487 | } |
| 9488 | |
| 9489 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9490 | 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] | 9491 | switch (set->type) { |
| 9492 | case LYXP_SET_NUMBER: |
| 9493 | if (isnan(set->val.num)) { |
| 9494 | set->val.str = strdup("NaN"); |
| 9495 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9496 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9497 | set->val.str = strdup("0"); |
| 9498 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9499 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9500 | set->val.str = strdup("Infinity"); |
| 9501 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9502 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9503 | set->val.str = strdup("-Infinity"); |
| 9504 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9505 | } else if ((long long)set->val.num == set->val.num) { |
| 9506 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9507 | LOGMEM_RET(set->ctx); |
| 9508 | } |
| 9509 | set->val.str = str; |
| 9510 | } else { |
| 9511 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9512 | LOGMEM_RET(set->ctx); |
| 9513 | } |
| 9514 | set->val.str = str; |
| 9515 | } |
| 9516 | break; |
| 9517 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9518 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9519 | set->val.str = strdup("true"); |
| 9520 | } else { |
| 9521 | set->val.str = strdup("false"); |
| 9522 | } |
| 9523 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9524 | break; |
| 9525 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9526 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9527 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9528 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9529 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9530 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9531 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9532 | set->val.str = str; |
| 9533 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9534 | default: |
| 9535 | LOGINT_RET(set->ctx); |
| 9536 | } |
| 9537 | set->type = LYXP_SET_STRING; |
| 9538 | } |
| 9539 | |
| 9540 | /* to NUMBER */ |
| 9541 | if (target == LYXP_SET_NUMBER) { |
| 9542 | switch (set->type) { |
| 9543 | case LYXP_SET_STRING: |
| 9544 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9545 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9546 | set->val.num = num; |
| 9547 | break; |
| 9548 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9549 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9550 | set->val.num = 1; |
| 9551 | } else { |
| 9552 | set->val.num = 0; |
| 9553 | } |
| 9554 | break; |
| 9555 | default: |
| 9556 | LOGINT_RET(set->ctx); |
| 9557 | } |
| 9558 | set->type = LYXP_SET_NUMBER; |
| 9559 | } |
| 9560 | |
| 9561 | /* to BOOLEAN */ |
| 9562 | if (target == LYXP_SET_BOOLEAN) { |
| 9563 | switch (set->type) { |
| 9564 | case LYXP_SET_NUMBER: |
| 9565 | 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] | 9566 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9567 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9568 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9569 | } |
| 9570 | break; |
| 9571 | case LYXP_SET_STRING: |
| 9572 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9573 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9574 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9575 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9576 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9577 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9578 | } |
| 9579 | break; |
| 9580 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9581 | if (set->used) { |
| 9582 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9583 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9584 | } else { |
| 9585 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9586 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9587 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9588 | break; |
| 9589 | default: |
| 9590 | LOGINT_RET(set->ctx); |
| 9591 | } |
| 9592 | set->type = LYXP_SET_BOOLEAN; |
| 9593 | } |
| 9594 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9595 | return LY_SUCCESS; |
| 9596 | } |
| 9597 | |
| 9598 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9599 | lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9600 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set, |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9601 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9602 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9603 | LY_ERR ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9604 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9605 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9606 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9607 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9608 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9609 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9610 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9611 | |
| 9612 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9613 | memset(set, 0, sizeof *set); |
| 9614 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9615 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 9616 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL)); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 9617 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9618 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9619 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9620 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9621 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9622 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9623 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9624 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9625 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9626 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9627 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9628 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9629 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9630 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9631 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9632 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9633 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9634 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9635 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9636 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9637 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9638 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9639 | { |
| 9640 | if (!path) { |
| 9641 | return NULL; |
| 9642 | } |
| 9643 | |
| 9644 | return path->expr; |
| 9645 | } |