Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file xpath.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG XPath evaluation functions |
| 5 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 17 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 19 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include <errno.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 25 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 26 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 28 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 32 | #include "out.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 33 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 34 | #include "path.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 35 | #include "plugins_exts/metadata.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 36 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 37 | #include "printer_data.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 38 | #include "schema_compile_node.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 39 | #include "tree.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 40 | #include "tree_data.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 42 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 43 | #include "tree_schema_internal.h" |
| 44 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 45 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 46 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth); |
| 47 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 48 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 49 | static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set, |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 50 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 51 | static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, |
| 52 | const struct lyd_node *node, enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 53 | static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 54 | enum lyxp_axis axis, uint32_t options); |
| 55 | static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, |
| 56 | enum lyxp_axis axis, uint32_t options); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 57 | static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 58 | |
aPiecek | 96dc1e3 | 2021-10-08 15:45:59 +0200 | [diff] [blame] | 59 | /* Functions are divided into the following basic classes: |
| 60 | * |
| 61 | * (re)parse functions: |
| 62 | * Parse functions parse the expression into |
| 63 | * tokens (syntactic analysis). |
| 64 | * Reparse functions perform semantic analysis |
| 65 | * (do not save the result, just a check) of |
| 66 | * the expression and fill repeat indices. |
| 67 | * |
| 68 | * warn functions: |
| 69 | * Warn functions check specific reasonable conditions for schema XPath |
| 70 | * and print a warning if they are not satisfied. |
| 71 | * |
| 72 | * moveto functions: |
| 73 | * They and only they actually change the context (set). |
| 74 | * |
| 75 | * eval functions: |
| 76 | * They execute a parsed XPath expression on some data subtree. |
| 77 | */ |
| 78 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 79 | /** |
| 80 | * @brief Print the type of an XPath \p set. |
| 81 | * |
| 82 | * @param[in] set Set to use. |
| 83 | * @return Set type string. |
| 84 | */ |
| 85 | static const char * |
| 86 | print_set_type(struct lyxp_set *set) |
| 87 | { |
| 88 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 89 | case LYXP_SET_NODE_SET: |
| 90 | return "node set"; |
| 91 | case LYXP_SET_SCNODE_SET: |
| 92 | return "schema node set"; |
| 93 | case LYXP_SET_BOOLEAN: |
| 94 | return "boolean"; |
| 95 | case LYXP_SET_NUMBER: |
| 96 | return "number"; |
| 97 | case LYXP_SET_STRING: |
| 98 | return "string"; |
| 99 | } |
| 100 | |
| 101 | return NULL; |
| 102 | } |
| 103 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 104 | const char * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 105 | lyxp_token2str(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 106 | { |
| 107 | switch (tok) { |
| 108 | case LYXP_TOKEN_PAR1: |
| 109 | return "("; |
| 110 | case LYXP_TOKEN_PAR2: |
| 111 | return ")"; |
| 112 | case LYXP_TOKEN_BRACK1: |
| 113 | return "["; |
| 114 | case LYXP_TOKEN_BRACK2: |
| 115 | return "]"; |
| 116 | case LYXP_TOKEN_DOT: |
| 117 | return "."; |
| 118 | case LYXP_TOKEN_DDOT: |
| 119 | return ".."; |
| 120 | case LYXP_TOKEN_AT: |
| 121 | return "@"; |
| 122 | case LYXP_TOKEN_COMMA: |
| 123 | return ","; |
Michal Vasko | e0a8ce7 | 2023-04-04 08:30:44 +0200 | [diff] [blame] | 124 | case LYXP_TOKEN_DCOLON: |
| 125 | return "::"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 126 | case LYXP_TOKEN_NAMETEST: |
| 127 | return "NameTest"; |
| 128 | case LYXP_TOKEN_NODETYPE: |
| 129 | return "NodeType"; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 130 | case LYXP_TOKEN_VARREF: |
| 131 | return "VariableReference"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 132 | case LYXP_TOKEN_FUNCNAME: |
| 133 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 134 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 135 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 136 | case LYXP_TOKEN_OPER_EQUAL: |
| 137 | return "Operator(Equal)"; |
| 138 | case LYXP_TOKEN_OPER_NEQUAL: |
| 139 | return "Operator(Non-equal)"; |
| 140 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 141 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 142 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 143 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 144 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 145 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 146 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 147 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 148 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 149 | return "Operator(Recursive Path)"; |
Michal Vasko | e0a8ce7 | 2023-04-04 08:30:44 +0200 | [diff] [blame] | 150 | case LYXP_TOKEN_AXISNAME: |
| 151 | return "AxisName"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 152 | case LYXP_TOKEN_LITERAL: |
| 153 | return "Literal"; |
| 154 | case LYXP_TOKEN_NUMBER: |
| 155 | return "Number"; |
| 156 | default: |
| 157 | LOGINT(NULL); |
| 158 | return ""; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 163 | * @brief Transform string into an axis. |
| 164 | * |
| 165 | * @param[in] str String to transform. |
| 166 | * @param[in] str_len Length of @p str. |
| 167 | * @return Transformed axis. |
| 168 | */ |
| 169 | static enum lyxp_axis |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 170 | str2axis(const char *str, uint32_t str_len) |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 171 | { |
| 172 | switch (str_len) { |
| 173 | case 4: |
| 174 | assert(!strncmp("self", str, str_len)); |
| 175 | return LYXP_AXIS_SELF; |
| 176 | case 5: |
| 177 | assert(!strncmp("child", str, str_len)); |
| 178 | return LYXP_AXIS_CHILD; |
| 179 | case 6: |
| 180 | assert(!strncmp("parent", str, str_len)); |
| 181 | return LYXP_AXIS_PARENT; |
| 182 | case 8: |
| 183 | assert(!strncmp("ancestor", str, str_len)); |
| 184 | return LYXP_AXIS_ANCESTOR; |
| 185 | case 9: |
| 186 | if (str[0] == 'a') { |
| 187 | assert(!strncmp("attribute", str, str_len)); |
| 188 | return LYXP_AXIS_ATTRIBUTE; |
| 189 | } else if (str[0] == 'f') { |
| 190 | assert(!strncmp("following", str, str_len)); |
| 191 | return LYXP_AXIS_FOLLOWING; |
| 192 | } else { |
| 193 | assert(!strncmp("preceding", str, str_len)); |
| 194 | return LYXP_AXIS_PRECEDING; |
| 195 | } |
| 196 | break; |
| 197 | case 10: |
| 198 | assert(!strncmp("descendant", str, str_len)); |
| 199 | return LYXP_AXIS_DESCENDANT; |
| 200 | case 16: |
| 201 | assert(!strncmp("ancestor-or-self", str, str_len)); |
| 202 | return LYXP_AXIS_ANCESTOR_OR_SELF; |
| 203 | case 17: |
| 204 | if (str[0] == 'f') { |
| 205 | assert(!strncmp("following-sibling", str, str_len)); |
| 206 | return LYXP_AXIS_FOLLOWING_SIBLING; |
| 207 | } else { |
| 208 | assert(!strncmp("preceding-sibling", str, str_len)); |
| 209 | return LYXP_AXIS_PRECEDING_SIBLING; |
| 210 | } |
| 211 | break; |
| 212 | case 18: |
| 213 | assert(!strncmp("descendant-or-self", str, str_len)); |
| 214 | return LYXP_AXIS_DESCENDANT_OR_SELF; |
| 215 | } |
| 216 | |
| 217 | LOGINT(NULL); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /** |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame^] | 222 | * @brief Append a string to a dynamic string variable. |
| 223 | * |
| 224 | * @param[in,out] str String to use. |
| 225 | * @param[in,out] size String size. |
| 226 | * @param[in,out] used String used size excluding terminating zero. |
| 227 | * @param[in] format Message format. |
| 228 | * @param[in] ... Message format arguments. |
| 229 | */ |
| 230 | static void |
| 231 | print_expr_str(char **str, size_t *size, size_t *used, const char *format, ...) |
| 232 | { |
| 233 | int p; |
| 234 | va_list ap; |
| 235 | |
| 236 | va_start(ap, format); |
| 237 | |
| 238 | /* try to append the string */ |
| 239 | p = vsnprintf(*str + *used, *size - *used, format, ap); |
| 240 | |
| 241 | if ((unsigned)p >= *size - *used) { |
| 242 | /* realloc */ |
| 243 | *str = ly_realloc(*str, *size + p + 1); |
| 244 | *size += p + 1; |
| 245 | |
| 246 | /* restart ap */ |
| 247 | va_end(ap); |
| 248 | va_start(ap, format); |
| 249 | |
| 250 | /* print */ |
| 251 | p = vsnprintf(*str + *used, *size - *used, format, ap); |
| 252 | } |
| 253 | |
| 254 | *used += p; |
| 255 | va_end(ap); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @brief Print the whole expression @p exp to debug output. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 260 | * |
| 261 | * @param[in] exp Expression to use. |
| 262 | */ |
| 263 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 264 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 265 | { |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame^] | 266 | char *buf = NULL; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 267 | uint32_t i, j; |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame^] | 268 | size_t size = 0, used = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 269 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 270 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 271 | return; |
| 272 | } |
| 273 | |
| 274 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 275 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame^] | 276 | print_expr_str(&buf, &size, &used, "\ttoken %s, in expression \"%.*s\"", |
| 277 | lyxp_token2str(exp->tokens[i]), exp->tok_len[i], &exp->expr[exp->tok_pos[i]]); |
| 278 | |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 279 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame^] | 280 | print_expr_str(&buf, &size, &used, " (repeat %d", exp->repeat[i][0]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 281 | for (j = 1; exp->repeat[i][j]; ++j) { |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame^] | 282 | print_expr_str(&buf, &size, &used, ", %d", exp->repeat[i][j]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 283 | } |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame^] | 284 | print_expr_str(&buf, &size, &used, ")"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 285 | } |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame^] | 286 | LOGDBG(LY_LDGXPATH, buf); |
| 287 | used = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 288 | } |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame^] | 289 | |
| 290 | free(buf); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | #ifndef NDEBUG |
| 294 | |
| 295 | /** |
| 296 | * @brief Print XPath set content to debug output. |
| 297 | * |
| 298 | * @param[in] set Set to print. |
| 299 | */ |
| 300 | static void |
| 301 | print_set_debug(struct lyxp_set *set) |
| 302 | { |
| 303 | uint32_t i; |
| 304 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 305 | struct lyxp_set_node *item; |
| 306 | struct lyxp_set_scnode *sitem; |
| 307 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 308 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | |
| 312 | switch (set->type) { |
| 313 | case LYXP_SET_NODE_SET: |
| 314 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 315 | for (i = 0; i < set->used; ++i) { |
| 316 | item = &set->val.nodes[i]; |
| 317 | |
| 318 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 319 | case LYXP_NODE_NONE: |
| 320 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 321 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 322 | case LYXP_NODE_ROOT: |
| 323 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 324 | break; |
| 325 | case LYXP_NODE_ROOT_CONFIG: |
| 326 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 327 | break; |
| 328 | case LYXP_NODE_ELEM: |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 329 | if (item->node->schema && (item->node->schema->nodetype == LYS_LIST) && |
| 330 | (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 331 | 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] | 332 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 333 | } else if ((!item->node->schema && !lyd_child(item->node)) || (item->node->schema->nodetype == LYS_LEAFLIST)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 334 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 335 | LYD_NAME(item->node), lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 336 | } else { |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 337 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, LYD_NAME(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 338 | } |
| 339 | break; |
| 340 | case LYXP_NODE_TEXT: |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 341 | if (item->node->schema && (item->node->schema->nodetype & LYS_ANYDATA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 342 | 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] | 343 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 344 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 345 | 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] | 346 | } |
| 347 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 348 | case LYXP_NODE_META: |
| 349 | 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] | 350 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | } |
| 354 | break; |
| 355 | |
| 356 | case LYXP_SET_SCNODE_SET: |
| 357 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 358 | for (i = 0; i < set->used; ++i) { |
| 359 | sitem = &set->val.scnodes[i]; |
| 360 | |
| 361 | switch (sitem->type) { |
| 362 | case LYXP_NODE_ROOT: |
| 363 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 364 | break; |
| 365 | case LYXP_NODE_ROOT_CONFIG: |
| 366 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 367 | break; |
| 368 | case LYXP_NODE_ELEM: |
| 369 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 370 | break; |
| 371 | default: |
| 372 | LOGINT(NULL); |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | break; |
| 377 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 378 | case LYXP_SET_BOOLEAN: |
| 379 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 380 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 381 | break; |
| 382 | |
| 383 | case LYXP_SET_STRING: |
| 384 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 385 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 386 | break; |
| 387 | |
| 388 | case LYXP_SET_NUMBER: |
| 389 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 390 | |
| 391 | if (isnan(set->val.num)) { |
| 392 | str = strdup("NaN"); |
| 393 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 394 | str = strdup("0"); |
| 395 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 396 | str = strdup("Infinity"); |
| 397 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 398 | str = strdup("-Infinity"); |
| 399 | } else if ((long long)set->val.num == set->val.num) { |
| 400 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 401 | str = NULL; |
| 402 | } |
| 403 | } else { |
| 404 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 405 | str = NULL; |
| 406 | } |
| 407 | } |
| 408 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 409 | |
| 410 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 411 | free(str); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | #endif |
| 416 | |
| 417 | /** |
| 418 | * @brief Realloc the string \p str. |
| 419 | * |
| 420 | * @param[in] ctx libyang context for logging. |
| 421 | * @param[in] needed How much free space is required. |
| 422 | * @param[in,out] str Pointer to the string to use. |
| 423 | * @param[in,out] used Used bytes in \p str. |
| 424 | * @param[in,out] size Allocated bytes in \p str. |
| 425 | * @return LY_ERR |
| 426 | */ |
| 427 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 428 | cast_string_realloc(const struct ly_ctx *ctx, uint64_t needed, char **str, uint32_t *used, uint32_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 429 | { |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 430 | if (*size - (unsigned)*used < needed) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 431 | do { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 432 | if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 433 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 434 | return LY_EINVAL; |
| 435 | } |
| 436 | *size += LYXP_STRING_CAST_SIZE_STEP; |
Michal Vasko | 2291ab9 | 2022-08-08 08:53:12 +0200 | [diff] [blame] | 437 | } while (*size - (unsigned)*used < needed); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 438 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 439 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 440 | } |
| 441 | |
| 442 | return LY_SUCCESS; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * @brief Cast nodes recursively to one string @p str. |
| 447 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 448 | * @param[in] node Node to cast, NULL if root. |
| 449 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 450 | * @param[in] indent Current indent. |
| 451 | * @param[in,out] str Resulting string. |
| 452 | * @param[in,out] used Used bytes in @p str. |
| 453 | * @param[in,out] size Allocated bytes in @p str. |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 454 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 455 | */ |
| 456 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 457 | cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used, |
| 458 | uint32_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 459 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 460 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 461 | const char *value_str; |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 462 | uint16_t nodetype; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 463 | const struct lyd_node *child; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 464 | enum lyxp_node_type child_type; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 465 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 466 | struct lyd_node_any *any; |
| 467 | LY_ERR rc; |
| 468 | |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 469 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && node->schema && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 470 | return LY_SUCCESS; |
| 471 | } |
| 472 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 473 | if (!node) { |
| 474 | /* fake container */ |
| 475 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 476 | strcpy(*str + (*used - 1), "\n"); |
| 477 | ++(*used); |
| 478 | |
| 479 | ++indent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 480 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 481 | /* print all the top-level nodes */ |
| 482 | child = NULL; |
| 483 | child_type = 0; |
| 484 | while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) { |
| 485 | LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 486 | } |
| 487 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 488 | /* end fake container */ |
| 489 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 490 | strcpy(*str + (*used - 1), "\n"); |
| 491 | ++(*used); |
| 492 | |
| 493 | --indent; |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 494 | } else { |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 495 | if (node->schema) { |
| 496 | nodetype = node->schema->nodetype; |
| 497 | } else if (lyd_child(node)) { |
| 498 | nodetype = LYS_CONTAINER; |
| 499 | } else { |
| 500 | nodetype = LYS_LEAF; |
| 501 | } |
| 502 | |
| 503 | switch (nodetype) { |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 504 | case LYS_CONTAINER: |
| 505 | case LYS_LIST: |
| 506 | case LYS_RPC: |
| 507 | case LYS_NOTIF: |
| 508 | LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size)); |
| 509 | strcpy(*str + (*used - 1), "\n"); |
| 510 | ++(*used); |
| 511 | |
| 512 | for (child = lyd_child(node); child; child = child->next) { |
| 513 | LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size)); |
| 514 | } |
| 515 | |
| 516 | break; |
| 517 | |
| 518 | case LYS_LEAF: |
| 519 | case LYS_LEAFLIST: |
| 520 | value_str = lyd_get_value(node); |
| 521 | |
| 522 | /* print indent */ |
| 523 | LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size)); |
| 524 | memset(*str + (*used - 1), ' ', indent * 2); |
| 525 | *used += indent * 2; |
| 526 | |
| 527 | /* print value */ |
| 528 | if (*used == 1) { |
| 529 | sprintf(*str + (*used - 1), "%s", value_str); |
| 530 | *used += strlen(value_str); |
| 531 | } else { |
| 532 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 533 | *used += strlen(value_str) + 1; |
| 534 | } |
| 535 | |
| 536 | break; |
| 537 | |
| 538 | case LYS_ANYXML: |
| 539 | case LYS_ANYDATA: |
| 540 | any = (struct lyd_node_any *)node; |
| 541 | if (!(void *)any->value.tree) { |
| 542 | /* no content */ |
| 543 | buf = strdup(""); |
| 544 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 545 | } else { |
| 546 | struct ly_out *out; |
| 547 | |
| 548 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 549 | /* try to parse it into a data tree */ |
| 550 | if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB, |
| 551 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
| 552 | /* successfully parsed */ |
| 553 | free(any->value.mem); |
| 554 | any->value.tree = tree; |
| 555 | any->value_type = LYD_ANYDATA_DATATREE; |
| 556 | } |
| 557 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
| 558 | } |
| 559 | |
| 560 | switch (any->value_type) { |
| 561 | case LYD_ANYDATA_STRING: |
| 562 | case LYD_ANYDATA_XML: |
| 563 | case LYD_ANYDATA_JSON: |
| 564 | buf = strdup(any->value.json); |
| 565 | LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM); |
| 566 | break; |
| 567 | case LYD_ANYDATA_DATATREE: |
| 568 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
| 569 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
| 570 | ly_out_free(out, NULL, 0); |
| 571 | LY_CHECK_RET(rc < 0, -rc); |
| 572 | break; |
| 573 | case LYD_ANYDATA_LYB: |
| 574 | LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 575 | return LY_EINVAL; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | line = strtok_r(buf, "\n", &ptr); |
| 580 | do { |
| 581 | rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size); |
| 582 | if (rc != LY_SUCCESS) { |
| 583 | free(buf); |
| 584 | return rc; |
| 585 | } |
| 586 | memset(*str + (*used - 1), ' ', indent * 2); |
| 587 | *used += indent * 2; |
| 588 | |
| 589 | strcpy(*str + (*used - 1), line); |
| 590 | *used += strlen(line); |
| 591 | |
| 592 | strcpy(*str + (*used - 1), "\n"); |
| 593 | *used += 1; |
| 594 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 595 | |
| 596 | free(buf); |
| 597 | break; |
| 598 | |
| 599 | default: |
| 600 | LOGINT_RET(set->ctx); |
| 601 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | return LY_SUCCESS; |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * @brief Cast an element into a string. |
| 609 | * |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 610 | * @param[in] node Node to cast, NULL if root. |
| 611 | * @param[in] set XPath set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 612 | * @param[out] str Element cast to dynamically-allocated string. |
| 613 | * @return LY_ERR |
| 614 | */ |
| 615 | static LY_ERR |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 616 | cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 617 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 618 | uint32_t used, size; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 619 | LY_ERR rc; |
| 620 | |
| 621 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 622 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 623 | (*str)[0] = '\0'; |
| 624 | used = 1; |
| 625 | size = LYXP_STRING_CAST_SIZE_START; |
| 626 | |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 627 | rc = cast_string_recursive(node, set, 0, str, &used, &size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 628 | if (rc != LY_SUCCESS) { |
| 629 | free(*str); |
| 630 | return rc; |
| 631 | } |
| 632 | |
| 633 | if (size > used) { |
| 634 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 635 | LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 636 | } |
| 637 | return LY_SUCCESS; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 642 | * Context position aware. |
| 643 | * |
| 644 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 645 | * @param[out] str Cast dynamically-allocated string. |
| 646 | * @return LY_ERR |
| 647 | */ |
| 648 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 649 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 650 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 651 | if (!set->used) { |
| 652 | *str = strdup(""); |
| 653 | if (!*str) { |
| 654 | LOGMEM_RET(set->ctx); |
| 655 | } |
| 656 | return LY_SUCCESS; |
| 657 | } |
| 658 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 659 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 660 | case LYXP_NODE_NONE: |
| 661 | /* invalid */ |
| 662 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 663 | case LYXP_NODE_ROOT: |
| 664 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 665 | case LYXP_NODE_ELEM: |
| 666 | case LYXP_NODE_TEXT: |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 667 | return cast_string_elem(set->val.nodes[0].node, set, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 668 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 669 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 670 | if (!*str) { |
| 671 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 672 | } |
| 673 | return LY_SUCCESS; |
| 674 | } |
| 675 | |
| 676 | LOGINT_RET(set->ctx); |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * @brief Cast a string into an XPath number. |
| 681 | * |
| 682 | * @param[in] str String to use. |
| 683 | * @return Cast number. |
| 684 | */ |
| 685 | static long double |
| 686 | cast_string_to_number(const char *str) |
| 687 | { |
| 688 | long double num; |
| 689 | char *ptr; |
| 690 | |
| 691 | errno = 0; |
| 692 | num = strtold(str, &ptr); |
| 693 | if (errno || *ptr) { |
| 694 | num = NAN; |
| 695 | } |
| 696 | return num; |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * @brief Callback for checking value equality. |
| 701 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 702 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 703 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 704 | * @param[in] val1_p First value. |
| 705 | * @param[in] val2_p Second value. |
| 706 | * @param[in] mod Whether hash table is being modified. |
| 707 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 708 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 709 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 710 | static ly_bool |
| 711 | 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] | 712 | { |
| 713 | struct lyxp_set_hash_node *val1, *val2; |
| 714 | |
| 715 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 716 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 717 | |
| 718 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 719 | return 1; |
| 720 | } |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * @brief Insert node and its hash into set. |
| 727 | * |
| 728 | * @param[in] set et to insert to. |
| 729 | * @param[in] node Node with hash. |
| 730 | * @param[in] type Node type. |
| 731 | */ |
| 732 | static void |
| 733 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 734 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 735 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 736 | uint32_t i, hash; |
| 737 | struct lyxp_set_hash_node hnode; |
| 738 | |
| 739 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 740 | /* create hash table and add all the nodes */ |
| 741 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 742 | for (i = 0; i < set->used; ++i) { |
| 743 | hnode.node = set->val.nodes[i].node; |
| 744 | hnode.type = set->val.nodes[i].type; |
| 745 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 746 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 747 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 748 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 749 | |
| 750 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 751 | assert(!r); |
| 752 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 753 | |
Michal Vasko | 513b87d | 2023-08-04 14:03:40 +0200 | [diff] [blame] | 754 | if ((hnode.node == node) && (hnode.type == type)) { |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 755 | /* it was just added, do not add it twice */ |
Michal Vasko | 513b87d | 2023-08-04 14:03:40 +0200 | [diff] [blame] | 756 | return; |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
Michal Vasko | 513b87d | 2023-08-04 14:03:40 +0200 | [diff] [blame] | 761 | if (set->ht) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 762 | /* add the new node into hash table */ |
| 763 | hnode.node = node; |
| 764 | hnode.type = type; |
| 765 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 766 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 767 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 768 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 769 | |
| 770 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 771 | assert(!r); |
| 772 | (void)r; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * @brief Remove node and its hash from set. |
| 778 | * |
| 779 | * @param[in] set Set to remove from. |
| 780 | * @param[in] node Node to remove. |
| 781 | * @param[in] type Node type. |
| 782 | */ |
| 783 | static void |
| 784 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 785 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 786 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 787 | struct lyxp_set_hash_node hnode; |
| 788 | uint32_t hash; |
| 789 | |
| 790 | if (set->ht) { |
| 791 | hnode.node = node; |
| 792 | hnode.type = type; |
| 793 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 794 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 795 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 796 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 797 | |
| 798 | r = lyht_remove(set->ht, &hnode, hash); |
| 799 | assert(!r); |
| 800 | (void)r; |
| 801 | |
| 802 | if (!set->ht->used) { |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 803 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 804 | set->ht = NULL; |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * @brief Check whether node is in set based on its hash. |
| 811 | * |
| 812 | * @param[in] set Set to search in. |
| 813 | * @param[in] node Node to search for. |
| 814 | * @param[in] type Node type. |
| 815 | * @param[in] skip_idx Index in @p set to skip. |
| 816 | * @return LY_ERR |
| 817 | */ |
| 818 | static LY_ERR |
| 819 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 820 | { |
| 821 | struct lyxp_set_hash_node hnode, *match_p; |
| 822 | uint32_t hash; |
| 823 | |
| 824 | hnode.node = node; |
| 825 | hnode.type = type; |
| 826 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 827 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 828 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 829 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 830 | |
| 831 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 832 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 833 | /* we found it on the index that should be skipped, find another */ |
| 834 | hnode = *match_p; |
| 835 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 836 | /* none other found */ |
| 837 | return LY_SUCCESS; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | return LY_EEXIST; |
| 842 | } |
| 843 | |
| 844 | /* not found */ |
| 845 | return LY_SUCCESS; |
| 846 | } |
| 847 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 848 | void |
| 849 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 850 | { |
| 851 | if (!set) { |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | if (set->type == LYXP_SET_NODE_SET) { |
| 856 | free(set->val.nodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 857 | lyht_free(set->ht, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 858 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 859 | free(set->val.scnodes); |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 860 | lyht_free(set->ht, NULL); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 861 | } else { |
| 862 | if (set->type == LYXP_SET_STRING) { |
| 863 | free(set->val.str); |
| 864 | } |
| 865 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 866 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 867 | |
| 868 | set->val.nodes = NULL; |
| 869 | set->used = 0; |
| 870 | set->size = 0; |
| 871 | set->ht = NULL; |
| 872 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 873 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 874 | } |
| 875 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 876 | /** |
| 877 | * @brief Free dynamically-allocated set. |
| 878 | * |
| 879 | * @param[in] set Set to free. |
| 880 | */ |
| 881 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 882 | lyxp_set_free(struct lyxp_set *set) |
| 883 | { |
| 884 | if (!set) { |
| 885 | return; |
| 886 | } |
| 887 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 888 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 889 | free(set); |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * @brief Initialize set context. |
| 894 | * |
| 895 | * @param[in] new Set to initialize. |
| 896 | * @param[in] set Arbitrary initialized set. |
| 897 | */ |
| 898 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 899 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 900 | { |
| 901 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 902 | if (set) { |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 903 | new->non_child_axis = set->non_child_axis; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 904 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 905 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 906 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 907 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 908 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 909 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 910 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 911 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 912 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 913 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /** |
| 917 | * @brief Create a deep copy of a set. |
| 918 | * |
| 919 | * @param[in] set Set to copy. |
| 920 | * @return Copy of @p set. |
| 921 | */ |
| 922 | static struct lyxp_set * |
| 923 | set_copy(struct lyxp_set *set) |
| 924 | { |
| 925 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 926 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 927 | |
| 928 | if (!set) { |
| 929 | return NULL; |
| 930 | } |
| 931 | |
| 932 | ret = malloc(sizeof *ret); |
| 933 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 934 | set_init(ret, set); |
| 935 | |
| 936 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 937 | ret->type = set->type; |
| 938 | |
| 939 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 940 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 941 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 942 | uint32_t idx; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 943 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 944 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 945 | set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 946 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 947 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 948 | lyxp_set_free(ret); |
| 949 | return NULL; |
| 950 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 951 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 955 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 956 | if (set->used) { |
| 957 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 958 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 959 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 960 | } else { |
| 961 | ret->val.nodes = NULL; |
| 962 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 963 | |
| 964 | ret->used = ret->size = set->used; |
| 965 | ret->ctx_pos = set->ctx_pos; |
| 966 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 967 | if (set->ht) { |
| 968 | ret->ht = lyht_dup(set->ht); |
| 969 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 970 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 971 | memcpy(ret, set, sizeof *ret); |
| 972 | if (set->type == LYXP_SET_STRING) { |
| 973 | ret->val.str = strdup(set->val.str); |
| 974 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 975 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | return ret; |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 983 | * |
| 984 | * @param[in] set Set to fill. |
| 985 | * @param[in] string String to fill into \p set. |
| 986 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 987 | */ |
| 988 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 989 | set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 990 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 991 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 992 | |
| 993 | set->type = LYXP_SET_STRING; |
| 994 | if ((str_len == 0) && (string[0] != '\0')) { |
| 995 | string = ""; |
| 996 | } |
| 997 | set->val.str = strndup(string, str_len); |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 1002 | * |
| 1003 | * @param[in] set Set to fill. |
| 1004 | * @param[in] number Number to fill into \p set. |
| 1005 | */ |
| 1006 | static void |
| 1007 | set_fill_number(struct lyxp_set *set, long double number) |
| 1008 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1009 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1010 | |
| 1011 | set->type = LYXP_SET_NUMBER; |
| 1012 | set->val.num = number; |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 1017 | * |
| 1018 | * @param[in] set Set to fill. |
| 1019 | * @param[in] boolean Boolean to fill into \p set. |
| 1020 | */ |
| 1021 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1022 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1023 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1024 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1025 | |
| 1026 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1027 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * @brief Fill XPath set with the value from another set (deep assign). |
| 1032 | * Any current data are disposed of. |
| 1033 | * |
| 1034 | * @param[in] trg Set to fill. |
| 1035 | * @param[in] src Source set to copy into \p trg. |
| 1036 | */ |
| 1037 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1038 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1039 | { |
| 1040 | if (!trg || !src) { |
| 1041 | return; |
| 1042 | } |
| 1043 | |
Michal Vasko | f06d19d | 2023-08-04 14:04:08 +0200 | [diff] [blame] | 1044 | lyxp_set_free_content(trg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1045 | set_init(trg, src); |
| 1046 | |
| 1047 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 1048 | trg->type = LYXP_SET_SCNODE_SET; |
| 1049 | trg->used = src->used; |
| 1050 | trg->size = src->used; |
| 1051 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1052 | if (trg->size) { |
| 1053 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 1054 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1055 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 1056 | } else { |
| 1057 | trg->val.scnodes = NULL; |
| 1058 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1059 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1060 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1061 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1062 | set_fill_number(trg, src->val.num); |
| 1063 | } else if (src->type == LYXP_SET_STRING) { |
| 1064 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 1065 | } else { |
| 1066 | if (trg->type == LYXP_SET_NODE_SET) { |
| 1067 | free(trg->val.nodes); |
| 1068 | } else if (trg->type == LYXP_SET_STRING) { |
| 1069 | free(trg->val.str); |
| 1070 | } |
| 1071 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1072 | assert(src->type == LYXP_SET_NODE_SET); |
| 1073 | |
| 1074 | trg->type = LYXP_SET_NODE_SET; |
| 1075 | trg->used = src->used; |
| 1076 | trg->size = src->used; |
| 1077 | trg->ctx_pos = src->ctx_pos; |
| 1078 | trg->ctx_size = src->ctx_size; |
| 1079 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1080 | if (trg->size) { |
| 1081 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 1082 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1083 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 1084 | } else { |
| 1085 | trg->val.nodes = NULL; |
| 1086 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1087 | if (src->ht) { |
| 1088 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1089 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1090 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * @brief Clear context of all schema nodes. |
| 1097 | * |
| 1098 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1099 | * @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] | 1100 | */ |
| 1101 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1102 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1103 | { |
| 1104 | uint32_t i; |
| 1105 | |
| 1106 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1107 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1108 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1109 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 1110 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * @brief Remove a node from a set. Removing last node changes |
| 1117 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1118 | * |
| 1119 | * @param[in] set Set to use. |
| 1120 | * @param[in] idx Index from @p set of the node to be removed. |
| 1121 | */ |
| 1122 | static void |
| 1123 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1124 | { |
| 1125 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1126 | assert(idx < set->used); |
| 1127 | |
| 1128 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1129 | |
| 1130 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1131 | if (idx < set->used) { |
| 1132 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1133 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1134 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1139 | * @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] | 1140 | * |
| 1141 | * @param[in] set Set to use. |
| 1142 | * @param[in] idx Index from @p set of the node to be removed. |
| 1143 | */ |
| 1144 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1145 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1146 | { |
| 1147 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1148 | assert(idx < set->used); |
| 1149 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1150 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1151 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1152 | } |
| 1153 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1157 | * @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] | 1158 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1159 | * |
| 1160 | * @param[in] set Set to consolidate. |
| 1161 | */ |
| 1162 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1163 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1164 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1165 | uint32_t i, orig_used, end = 0; |
| 1166 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1167 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1168 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1169 | |
| 1170 | orig_used = set->used; |
| 1171 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1172 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1173 | start = -1; |
| 1174 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1175 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1176 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1177 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1178 | end = i; |
| 1179 | ++i; |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | ++i; |
| 1184 | if (i == orig_used) { |
| 1185 | end = i; |
| 1186 | } |
| 1187 | } while (i < orig_used); |
| 1188 | |
| 1189 | if (start > -1) { |
| 1190 | /* move the whole chunk of valid nodes together */ |
| 1191 | if (set->used != (unsigned)start) { |
| 1192 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1193 | } |
| 1194 | set->used += end - start; |
| 1195 | } |
| 1196 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1200 | * @brief Check for duplicates in a node set. |
| 1201 | * |
| 1202 | * @param[in] set Set to check. |
| 1203 | * @param[in] node Node to look for in @p set. |
| 1204 | * @param[in] node_type Type of @p node. |
| 1205 | * @param[in] skip_idx Index from @p set to skip. |
| 1206 | * @return LY_ERR |
| 1207 | */ |
| 1208 | static LY_ERR |
| 1209 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1210 | { |
| 1211 | uint32_t i; |
| 1212 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1213 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1214 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1215 | } |
| 1216 | |
| 1217 | for (i = 0; i < set->used; ++i) { |
| 1218 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1219 | continue; |
| 1220 | } |
| 1221 | |
| 1222 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1223 | return LY_EEXIST; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | return LY_SUCCESS; |
| 1228 | } |
| 1229 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1230 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1231 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1232 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1233 | { |
| 1234 | uint32_t i; |
| 1235 | |
| 1236 | for (i = 0; i < set->used; ++i) { |
| 1237 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1238 | continue; |
| 1239 | } |
| 1240 | |
| 1241 | 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] | 1242 | if (index_p) { |
| 1243 | *index_p = i; |
| 1244 | } |
| 1245 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1249 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1252 | void |
| 1253 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1254 | { |
| 1255 | uint32_t orig_used, i, j; |
| 1256 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1257 | 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] | 1258 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1259 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1260 | return; |
| 1261 | } |
| 1262 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1263 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1264 | /* release hidden allocated data (lyxp_set.size) */ |
| 1265 | lyxp_set_free_content(set1); |
| 1266 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1267 | memcpy(set1, set2, sizeof *set1); |
| 1268 | return; |
| 1269 | } |
| 1270 | |
| 1271 | if (set1->used + set2->used > set1->size) { |
| 1272 | set1->size = set1->used + set2->used; |
| 1273 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1274 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1275 | } |
| 1276 | |
| 1277 | orig_used = set1->used; |
| 1278 | |
| 1279 | for (i = 0; i < set2->used; ++i) { |
| 1280 | for (j = 0; j < orig_used; ++j) { |
| 1281 | /* detect duplicities */ |
| 1282 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1283 | break; |
| 1284 | } |
| 1285 | } |
| 1286 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1287 | if (j < orig_used) { |
| 1288 | /* node is there, but update its status if needed */ |
| 1289 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1290 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1291 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1292 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1293 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1294 | } |
| 1295 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1296 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1297 | ++set1->used; |
| 1298 | } |
| 1299 | } |
| 1300 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1301 | lyxp_set_free_content(set2); |
| 1302 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * @brief Insert a node into a set. Context position aware. |
| 1307 | * |
| 1308 | * @param[in] set Set to use. |
| 1309 | * @param[in] node Node to insert to @p set. |
| 1310 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1311 | * @param[in] node_type Node type of @p node. |
| 1312 | * @param[in] idx Index in @p set to insert into. |
| 1313 | */ |
| 1314 | static void |
| 1315 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1316 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1317 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1318 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1319 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1320 | /* first item */ |
| 1321 | if (idx) { |
| 1322 | /* no real harm done, but it is a bug */ |
| 1323 | LOGINT(set->ctx); |
| 1324 | idx = 0; |
| 1325 | } |
| 1326 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1327 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1328 | set->type = LYXP_SET_NODE_SET; |
| 1329 | set->used = 0; |
| 1330 | set->size = LYXP_SET_SIZE_START; |
| 1331 | set->ctx_pos = 1; |
| 1332 | set->ctx_size = 1; |
| 1333 | set->ht = NULL; |
| 1334 | } else { |
| 1335 | /* not an empty set */ |
| 1336 | if (set->used == set->size) { |
| 1337 | |
| 1338 | /* set is full */ |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1339 | 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] | 1340 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1341 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | if (idx > set->used) { |
| 1345 | LOGINT(set->ctx); |
| 1346 | idx = set->used; |
| 1347 | } |
| 1348 | |
| 1349 | /* make space for the new node */ |
| 1350 | if (idx < set->used) { |
| 1351 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | /* finally assign the value */ |
| 1356 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1357 | set->val.nodes[idx].type = node_type; |
| 1358 | set->val.nodes[idx].pos = pos; |
| 1359 | ++set->used; |
| 1360 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1361 | /* add into hash table */ |
| 1362 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1363 | } |
| 1364 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 1365 | LY_ERR |
| 1366 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1367 | enum lyxp_axis axis, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1368 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1369 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1370 | |
| 1371 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1372 | |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1373 | if (!set->size) { |
| 1374 | /* first item */ |
| 1375 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
| 1376 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
| 1377 | set->type = LYXP_SET_SCNODE_SET; |
| 1378 | set->used = 0; |
| 1379 | set->size = LYXP_SET_SIZE_START; |
| 1380 | set->ctx_pos = 1; |
| 1381 | set->ctx_size = 1; |
| 1382 | set->ht = NULL; |
| 1383 | } |
| 1384 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1385 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 1386 | /* BUG if axes differ, this new one is thrown away */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1387 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1388 | } else { |
| 1389 | if (set->used == set->size) { |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1390 | 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] | 1391 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1392 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1393 | } |
| 1394 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1395 | index = set->used; |
| 1396 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1397 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1398 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1399 | set->val.scnodes[index].axis = axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1400 | ++set->used; |
| 1401 | } |
| 1402 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1403 | if (index_p) { |
| 1404 | *index_p = index; |
| 1405 | } |
| 1406 | |
| 1407 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1411 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1412 | * |
| 1413 | * @param[in] set Set to modify. |
| 1414 | * @return New context value. |
| 1415 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1416 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1417 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1418 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1419 | uint32_t i; |
| 1420 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1421 | |
| 1422 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1423 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1424 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1425 | retry: |
| 1426 | for (i = 0; i < set->used; ++i) { |
| 1427 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1428 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1429 | goto retry; |
| 1430 | } |
| 1431 | } |
| 1432 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1433 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1434 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | return ret_ctx; |
| 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * @brief Get unique @p node position in the data. |
| 1443 | * |
| 1444 | * @param[in] node Node to find. |
| 1445 | * @param[in] node_type Node type of @p node. |
| 1446 | * @param[in] root Root node. |
| 1447 | * @param[in] root_type Type of the XPath @p root node. |
| 1448 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1449 | * be used to increase efficiency and start the DFS from this node. |
| 1450 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1451 | * @return Node position. |
| 1452 | */ |
| 1453 | static uint32_t |
| 1454 | 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] | 1455 | 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] | 1456 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1457 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1458 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1459 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1460 | |
| 1461 | assert(prev && prev_pos && !root->prev->next); |
| 1462 | |
| 1463 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | if (*prev) { |
| 1468 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1469 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1470 | 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] | 1471 | goto dfs_search; |
| 1472 | } |
| 1473 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1474 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1475 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1476 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1477 | LYD_TREE_DFS_continue = 0; |
| 1478 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1479 | if (*prev && !elem) { |
| 1480 | /* resume previous DFS */ |
| 1481 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1482 | LYD_TREE_DFS_continue = 0; |
| 1483 | } |
| 1484 | |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 1485 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1486 | /* skip */ |
| 1487 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1488 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1489 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1490 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1491 | break; |
| 1492 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1493 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1494 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1495 | |
| 1496 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1500 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1501 | break; |
| 1502 | } |
| 1503 | } |
| 1504 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1505 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1506 | if (!(*prev)) { |
| 1507 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1508 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1509 | return 0; |
| 1510 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1511 | /* start the search again from the beginning */ |
| 1512 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1513 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1514 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1515 | pos = 1; |
| 1516 | goto dfs_search; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | /* remember the last found node for next time */ |
| 1521 | *prev = node; |
| 1522 | *prev_pos = pos; |
| 1523 | |
| 1524 | return pos; |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * @brief Assign (fill) missing node positions. |
| 1529 | * |
| 1530 | * @param[in] set Set to fill positions in. |
| 1531 | * @param[in] root Context root node. |
| 1532 | * @param[in] root_type Context root type. |
| 1533 | * @return LY_ERR |
| 1534 | */ |
| 1535 | static LY_ERR |
| 1536 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1537 | { |
| 1538 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1539 | uint32_t i, tmp_pos = 0; |
| 1540 | |
| 1541 | for (i = 0; i < set->used; ++i) { |
| 1542 | if (!set->val.nodes[i].pos) { |
| 1543 | tmp_node = NULL; |
| 1544 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1545 | case LYXP_NODE_META: |
| 1546 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1547 | if (!tmp_node) { |
| 1548 | LOGINT_RET(root->schema->module->ctx); |
| 1549 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1550 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1551 | case LYXP_NODE_ELEM: |
| 1552 | case LYXP_NODE_TEXT: |
| 1553 | if (!tmp_node) { |
| 1554 | tmp_node = set->val.nodes[i].node; |
| 1555 | } |
| 1556 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1557 | break; |
| 1558 | default: |
| 1559 | /* all roots have position 0 */ |
| 1560 | break; |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | return LY_SUCCESS; |
| 1566 | } |
| 1567 | |
| 1568 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1569 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1570 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1571 | * @param[in] meta Metadata to use. |
| 1572 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1573 | */ |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1574 | static uint32_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1575 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1576 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1577 | uint32_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1578 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1579 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1580 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1581 | ++pos; |
| 1582 | } |
| 1583 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1584 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1585 | return pos; |
| 1586 | } |
| 1587 | |
| 1588 | /** |
| 1589 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1590 | * |
| 1591 | * @param[in] item1 1st node. |
| 1592 | * @param[in] item2 2nd node. |
| 1593 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1594 | */ |
| 1595 | static int |
| 1596 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1597 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1598 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1599 | |
| 1600 | if (item1->pos < item2->pos) { |
| 1601 | return -1; |
| 1602 | } |
| 1603 | |
| 1604 | if (item1->pos > item2->pos) { |
| 1605 | return 1; |
| 1606 | } |
| 1607 | |
| 1608 | /* node positions are equal, the fun case */ |
| 1609 | |
| 1610 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1611 | /* special case since text nodes are actually saved as their parents */ |
| 1612 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1613 | if (item1->type == LYXP_NODE_ELEM) { |
| 1614 | assert(item2->type == LYXP_NODE_TEXT); |
| 1615 | return -1; |
| 1616 | } else { |
| 1617 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1618 | return 1; |
| 1619 | } |
| 1620 | } |
| 1621 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1622 | /* we need meta positions now */ |
| 1623 | if (item1->type == LYXP_NODE_META) { |
| 1624 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1625 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1626 | if (item2->type == LYXP_NODE_META) { |
| 1627 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1628 | } |
| 1629 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1630 | /* 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] | 1631 | /* check for duplicates */ |
| 1632 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1633 | 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] | 1634 | return 0; |
| 1635 | } |
| 1636 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1637 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1638 | /* elem is always first, 2nd node is after it */ |
| 1639 | if (item1->type == LYXP_NODE_ELEM) { |
| 1640 | assert(item2->type != LYXP_NODE_ELEM); |
| 1641 | return -1; |
| 1642 | } |
| 1643 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1644 | /* 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] | 1645 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1646 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1647 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1648 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1649 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1650 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1651 | return 1; |
| 1652 | } |
| 1653 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1654 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1655 | /* 2nd is after 1st */ |
| 1656 | return -1; |
| 1657 | } |
| 1658 | |
| 1659 | /** |
| 1660 | * @brief Set cast for comparisons. |
| 1661 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1662 | * @param[in,out] trg Target set to cast source into. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1663 | * @param[in] src Source set. |
| 1664 | * @param[in] type Target set type. |
| 1665 | * @param[in] src_idx Source set node index. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1666 | * @return LY_SUCCESS on success. |
| 1667 | * @return LY_ERR value on error. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1668 | */ |
| 1669 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1670 | set_comp_cast(struct lyxp_set *trg, const struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1671 | { |
| 1672 | assert(src->type == LYXP_SET_NODE_SET); |
| 1673 | |
| 1674 | set_init(trg, src); |
| 1675 | |
| 1676 | /* insert node into target set */ |
| 1677 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1678 | |
| 1679 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1680 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1681 | } |
| 1682 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1683 | /** |
| 1684 | * @brief Set content canonization for comparisons. |
| 1685 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1686 | * @param[in,out] set Set to canonize. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1687 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1688 | * @return LY_SUCCESS on success. |
| 1689 | * @return LY_ERR value on error. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1690 | */ |
| 1691 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1692 | set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node) |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1693 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1694 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1695 | struct lyd_value val; |
| 1696 | struct ly_err_item *err = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1697 | LY_ERR rc; |
| 1698 | |
| 1699 | /* is there anything to canonize even? */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1700 | if (set->type == LYXP_SET_STRING) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1701 | /* do we have a type to use for canonization? */ |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 1702 | if ((xp_node->type == LYXP_NODE_ELEM) && xp_node->node->schema && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1703 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1704 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1705 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1706 | } |
| 1707 | } |
| 1708 | if (!type) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1709 | /* no canonization needed/possible */ |
| 1710 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1713 | /* check for built-in types without required canonization */ |
| 1714 | if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) { |
| 1715 | /* string */ |
| 1716 | return LY_SUCCESS; |
| 1717 | } |
| 1718 | if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) { |
| 1719 | /* boolean */ |
| 1720 | return LY_SUCCESS; |
| 1721 | } |
| 1722 | if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) { |
| 1723 | /* enumeration */ |
| 1724 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1725 | } |
| 1726 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1727 | /* print canonized string, ignore errors, the value may not satisfy schema constraints */ |
| 1728 | rc = type->plugin->store(set->ctx, type, set->val.str, strlen(set->val.str), 0, set->format, set->prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1729 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1730 | ly_err_free(err); |
| 1731 | if (rc) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1732 | /* invalid value, function store automaticaly dealloc value when fail */ |
| 1733 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1734 | } |
| 1735 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1736 | /* use the canonized string value */ |
| 1737 | free(set->val.str); |
| 1738 | set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val)); |
| 1739 | type->plugin->free(set->ctx, &val); |
| 1740 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1741 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1742 | return LY_SUCCESS; |
| 1743 | } |
| 1744 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1745 | /** |
| 1746 | * @brief Bubble sort @p set into XPath document order. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1747 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1748 | * |
| 1749 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1750 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1751 | */ |
| 1752 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1753 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1754 | { |
| 1755 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1756 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1757 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1758 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1759 | struct lyxp_set_node item; |
| 1760 | struct lyxp_set_hash_node hnode; |
| 1761 | uint64_t hash; |
| 1762 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1763 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1764 | return 0; |
| 1765 | } |
| 1766 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1767 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1768 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1769 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1770 | |
| 1771 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1772 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1773 | return -1; |
| 1774 | } |
| 1775 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1776 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1777 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1778 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1779 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1780 | |
| 1781 | for (i = 0; i < set->used; ++i) { |
| 1782 | inverted = 0; |
| 1783 | change = 0; |
| 1784 | |
| 1785 | for (j = 1; j < set->used - i; ++j) { |
| 1786 | /* compare node positions */ |
| 1787 | if (inverted) { |
| 1788 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1789 | } else { |
| 1790 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1791 | } |
| 1792 | |
| 1793 | /* swap if needed */ |
| 1794 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1795 | change = 1; |
| 1796 | |
| 1797 | item = set->val.nodes[j - 1]; |
| 1798 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1799 | set->val.nodes[j] = item; |
| 1800 | } else { |
| 1801 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1802 | inverted = !inverted; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | ++ret; |
| 1807 | |
| 1808 | if (!change) { |
| 1809 | break; |
| 1810 | } |
| 1811 | } |
| 1812 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1813 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1814 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1815 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1816 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1817 | |
| 1818 | /* check node hashes */ |
| 1819 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1820 | assert(set->ht); |
| 1821 | for (i = 0; i < set->used; ++i) { |
| 1822 | hnode.node = set->val.nodes[i].node; |
| 1823 | hnode.type = set->val.nodes[i].type; |
| 1824 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 1825 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1826 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1827 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1828 | |
| 1829 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | return ret - 1; |
| 1834 | } |
| 1835 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1836 | /** |
| 1837 | * @brief Merge 2 sorted sets into one. |
| 1838 | * |
| 1839 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1840 | * @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] | 1841 | * @return LY_ERR |
| 1842 | */ |
| 1843 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1844 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1845 | { |
| 1846 | uint32_t i, j, k, count, dup_count; |
| 1847 | int cmp; |
| 1848 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1849 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1850 | 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] | 1851 | return LY_EINVAL; |
| 1852 | } |
| 1853 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1854 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1855 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1856 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1857 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1858 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1859 | return LY_SUCCESS; |
| 1860 | } |
| 1861 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1862 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0143b68 | 2021-12-13 17:13:09 +0100 | [diff] [blame] | 1863 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1864 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1865 | |
| 1866 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1867 | 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] | 1868 | return LY_EINT; |
| 1869 | } |
| 1870 | |
| 1871 | #ifndef NDEBUG |
| 1872 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1873 | print_set_debug(trg); |
| 1874 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1875 | print_set_debug(src); |
| 1876 | #endif |
| 1877 | |
| 1878 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1879 | * will likely be wasted on them, too bad) */ |
| 1880 | if (trg->size - trg->used < src->used) { |
| 1881 | trg->size = trg->used + src->used; |
| 1882 | |
| 1883 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1884 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1885 | } |
| 1886 | |
| 1887 | i = 0; |
| 1888 | j = 0; |
| 1889 | count = 0; |
| 1890 | dup_count = 0; |
| 1891 | do { |
| 1892 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1893 | if (!cmp) { |
| 1894 | if (!count) { |
| 1895 | /* duplicate, just skip it */ |
| 1896 | ++i; |
| 1897 | ++j; |
| 1898 | } else { |
| 1899 | /* we are copying something already, so let's copy the duplicate too, |
| 1900 | * we are hoping that afterwards there are some more nodes to |
| 1901 | * copy and this way we can copy them all together */ |
| 1902 | ++count; |
| 1903 | ++dup_count; |
| 1904 | ++i; |
| 1905 | ++j; |
| 1906 | } |
| 1907 | } else if (cmp < 0) { |
| 1908 | /* inserting src node into trg, just remember it for now */ |
| 1909 | ++count; |
| 1910 | ++i; |
| 1911 | |
| 1912 | /* insert the hash now */ |
| 1913 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1914 | } else if (count) { |
| 1915 | copy_nodes: |
| 1916 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1917 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1918 | &trg->val.nodes[j], |
| 1919 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1920 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1921 | |
| 1922 | trg->used += count - dup_count; |
| 1923 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1924 | j += count - dup_count; |
| 1925 | |
| 1926 | count = 0; |
| 1927 | dup_count = 0; |
| 1928 | } else { |
| 1929 | ++j; |
| 1930 | } |
| 1931 | } while ((i < src->used) && (j < trg->used)); |
| 1932 | |
| 1933 | if ((i < src->used) || count) { |
| 1934 | /* insert all the hashes first */ |
| 1935 | for (k = i; k < src->used; ++k) { |
| 1936 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1937 | } |
| 1938 | |
| 1939 | /* loop ended, but we need to copy something at trg end */ |
| 1940 | count += src->used - i; |
| 1941 | i = src->used; |
| 1942 | goto copy_nodes; |
| 1943 | } |
| 1944 | |
| 1945 | /* we are inserting hashes before the actual node insert, which causes |
| 1946 | * situations when there were initially not enough items for a hash table, |
| 1947 | * but even after some were inserted, hash table was not created (during |
| 1948 | * insertion the number of items is not updated yet) */ |
| 1949 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1950 | set_insert_node_hash(trg, NULL, 0); |
| 1951 | } |
| 1952 | |
| 1953 | #ifndef NDEBUG |
| 1954 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1955 | print_set_debug(trg); |
| 1956 | #endif |
| 1957 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1958 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1959 | return LY_SUCCESS; |
| 1960 | } |
| 1961 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1962 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1963 | lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1964 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1965 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1966 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1967 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1968 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1969 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1970 | } |
| 1971 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1972 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1973 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1974 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]), |
| 1975 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1977 | return LY_ENOT; |
| 1978 | } |
| 1979 | |
| 1980 | return LY_SUCCESS; |
| 1981 | } |
| 1982 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1983 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1984 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1985 | { |
| 1986 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1987 | |
| 1988 | /* skip the token */ |
| 1989 | ++(*tok_idx); |
| 1990 | |
| 1991 | return LY_SUCCESS; |
| 1992 | } |
| 1993 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1994 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1995 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1996 | exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok1, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1997 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1998 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2000 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2001 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2002 | } |
| 2003 | return LY_EINCOMPLETE; |
| 2004 | } |
| 2005 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2006 | 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] | 2007 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2008 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 2009 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2010 | } |
| 2011 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | return LY_SUCCESS; |
| 2015 | } |
| 2016 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 2017 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2018 | lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok1, |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 2019 | enum lyxp_token want_tok2) |
| 2020 | { |
| 2021 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 2022 | |
| 2023 | /* skip the token */ |
| 2024 | ++(*tok_idx); |
| 2025 | |
| 2026 | return LY_SUCCESS; |
| 2027 | } |
| 2028 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2029 | /** |
| 2030 | * @brief Stack operation push on the repeat array. |
| 2031 | * |
| 2032 | * @param[in] exp Expression to use. |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2033 | * @param[in] tok_idx Position in the expresion @p exp. |
| 2034 | * @param[in] repeat_expr_type Repeated expression type, this value is pushed. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2035 | */ |
| 2036 | static void |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2037 | exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_expr_type repeat_expr_type) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2038 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2039 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2040 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2041 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2042 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2043 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 2044 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2045 | exp->repeat[tok_idx][i] = repeat_expr_type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2046 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2047 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2048 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 2049 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2050 | exp->repeat[tok_idx][0] = repeat_expr_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | /** |
| 2055 | * @brief Reparse Predicate. Logs directly on error. |
| 2056 | * |
| 2057 | * [7] Predicate ::= '[' Expr ']' |
| 2058 | * |
| 2059 | * @param[in] ctx Context for logging. |
| 2060 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2061 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2062 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | * @return LY_ERR |
| 2064 | */ |
| 2065 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2066 | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2067 | { |
| 2068 | LY_ERR rc; |
| 2069 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2070 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2072 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2073 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2074 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2075 | LY_CHECK_RET(rc); |
| 2076 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2079 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2080 | |
| 2081 | return LY_SUCCESS; |
| 2082 | } |
| 2083 | |
| 2084 | /** |
| 2085 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 2086 | * |
| 2087 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 2088 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 2089 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 2090 | * |
| 2091 | * @param[in] ctx Context for logging. |
| 2092 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2093 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2094 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2095 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 2096 | */ |
| 2097 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2098 | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | { |
| 2100 | LY_ERR rc; |
| 2101 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2102 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2103 | LY_CHECK_RET(rc); |
| 2104 | |
| 2105 | goto step; |
| 2106 | do { |
| 2107 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2108 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2109 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | LY_CHECK_RET(rc); |
| 2112 | step: |
| 2113 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2114 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2115 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | break; |
| 2118 | |
| 2119 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2120 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2121 | break; |
| 2122 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2123 | case LYXP_TOKEN_AXISNAME: |
| 2124 | ++(*tok_idx); |
| 2125 | |
| 2126 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON); |
| 2127 | LY_CHECK_RET(rc); |
| 2128 | |
| 2129 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2130 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2131 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2132 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2135 | 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] | 2136 | 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] | 2137 | return LY_EVALID; |
| 2138 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2139 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) { |
| 2140 | goto reparse_nodetype; |
| 2141 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2142 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2143 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2144 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2145 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | |
| 2147 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2148 | reparse_nodetype: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2149 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2150 | |
| 2151 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2152 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | |
| 2156 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2159 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2160 | |
| 2161 | reparse_predicate: |
| 2162 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2164 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2165 | LY_CHECK_RET(rc); |
| 2166 | } |
| 2167 | break; |
| 2168 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2169 | 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] | 2170 | return LY_EVALID; |
| 2171 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | } 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] | 2173 | |
| 2174 | return LY_SUCCESS; |
| 2175 | } |
| 2176 | |
| 2177 | /** |
| 2178 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2179 | * |
| 2180 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2181 | * |
| 2182 | * @param[in] ctx Context for logging. |
| 2183 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2184 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2185 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2186 | * @return LY_ERR |
| 2187 | */ |
| 2188 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2189 | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2190 | { |
| 2191 | LY_ERR rc; |
| 2192 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2193 | 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] | 2194 | |
| 2195 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2197 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2198 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2199 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2200 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2201 | return LY_SUCCESS; |
| 2202 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2203 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2204 | case LYXP_TOKEN_DOT: |
| 2205 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2206 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2207 | case LYXP_TOKEN_AT: |
| 2208 | case LYXP_TOKEN_NAMETEST: |
| 2209 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2210 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2211 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2212 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2213 | default: |
| 2214 | break; |
| 2215 | } |
| 2216 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2217 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2218 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2219 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2220 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2221 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2222 | LY_CHECK_RET(rc); |
| 2223 | } |
| 2224 | |
| 2225 | return LY_SUCCESS; |
| 2226 | } |
| 2227 | |
| 2228 | /** |
| 2229 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2230 | * |
| 2231 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2232 | * |
| 2233 | * @param[in] ctx Context for logging. |
| 2234 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2235 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2236 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2237 | * @return LY_ERR |
| 2238 | */ |
| 2239 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2240 | reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2241 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2242 | int8_t min_arg_count = -1; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2243 | uint32_t arg_count, max_arg_count = 0, func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2244 | LY_ERR rc; |
| 2245 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2246 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2247 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2248 | func_tok_idx = *tok_idx; |
| 2249 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2250 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2252 | min_arg_count = 1; |
| 2253 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2254 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | min_arg_count = 1; |
| 2256 | max_arg_count = 1; |
| 2257 | } |
| 2258 | break; |
| 2259 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2260 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2261 | min_arg_count = 1; |
| 2262 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2263 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2264 | min_arg_count = 0; |
| 2265 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2267 | min_arg_count = 0; |
| 2268 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2269 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2270 | min_arg_count = 0; |
| 2271 | max_arg_count = 0; |
| 2272 | } |
| 2273 | break; |
| 2274 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2275 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2276 | min_arg_count = 1; |
| 2277 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2278 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2279 | min_arg_count = 0; |
| 2280 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2281 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2282 | min_arg_count = 1; |
| 2283 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2284 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2285 | min_arg_count = 1; |
| 2286 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2287 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2288 | min_arg_count = 1; |
| 2289 | max_arg_count = 1; |
| 2290 | } |
| 2291 | break; |
| 2292 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2293 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2294 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2295 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2296 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2297 | min_arg_count = 0; |
| 2298 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2299 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2300 | min_arg_count = 0; |
| 2301 | max_arg_count = 1; |
| 2302 | } |
| 2303 | break; |
| 2304 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2305 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2306 | min_arg_count = 1; |
| 2307 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2308 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2309 | min_arg_count = 1; |
| 2310 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2311 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2312 | min_arg_count = 0; |
| 2313 | max_arg_count = 0; |
| 2314 | } |
| 2315 | break; |
| 2316 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2317 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2318 | min_arg_count = 2; |
| 2319 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2320 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2321 | min_arg_count = 0; |
| 2322 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2323 | } 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] | 2324 | min_arg_count = 2; |
| 2325 | max_arg_count = 2; |
| 2326 | } |
| 2327 | break; |
| 2328 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2329 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2330 | min_arg_count = 2; |
| 2331 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2332 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2333 | min_arg_count = 3; |
| 2334 | max_arg_count = 3; |
| 2335 | } |
| 2336 | break; |
| 2337 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2338 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2339 | min_arg_count = 0; |
| 2340 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2341 | } 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] | 2342 | min_arg_count = 1; |
| 2343 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2344 | } 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] | 2345 | min_arg_count = 2; |
| 2346 | max_arg_count = 2; |
| 2347 | } |
| 2348 | break; |
| 2349 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2350 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2351 | min_arg_count = 2; |
| 2352 | max_arg_count = 2; |
| 2353 | } |
| 2354 | break; |
| 2355 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2356 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2357 | min_arg_count = 2; |
| 2358 | max_arg_count = 2; |
| 2359 | } |
| 2360 | break; |
| 2361 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2362 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2363 | min_arg_count = 0; |
| 2364 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2365 | } 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] | 2366 | min_arg_count = 0; |
| 2367 | max_arg_count = 1; |
| 2368 | } |
| 2369 | break; |
| 2370 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2371 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2372 | min_arg_count = 0; |
| 2373 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2374 | } 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] | 2375 | min_arg_count = 2; |
| 2376 | max_arg_count = 2; |
| 2377 | } |
| 2378 | break; |
| 2379 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2380 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2381 | min_arg_count = 2; |
| 2382 | max_arg_count = 2; |
| 2383 | } |
| 2384 | break; |
| 2385 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2386 | 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] | 2387 | min_arg_count = 2; |
| 2388 | max_arg_count = 2; |
| 2389 | } |
| 2390 | break; |
| 2391 | } |
| 2392 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2393 | 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] | 2394 | return LY_EINVAL; |
| 2395 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2396 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2397 | |
| 2398 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2399 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2400 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2401 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2402 | |
| 2403 | /* ( Expr ( ',' Expr )* )? */ |
| 2404 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2405 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2406 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2407 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2408 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2409 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2410 | LY_CHECK_RET(rc); |
| 2411 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2412 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2413 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2414 | |
| 2415 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2416 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | LY_CHECK_RET(rc); |
| 2418 | } |
| 2419 | |
| 2420 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2421 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2422 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2423 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2424 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2425 | 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] | 2426 | 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] | 2427 | return LY_EVALID; |
| 2428 | } |
| 2429 | |
| 2430 | return LY_SUCCESS; |
| 2431 | } |
| 2432 | |
| 2433 | /** |
| 2434 | * @brief Reparse PathExpr. Logs directly on error. |
| 2435 | * |
| 2436 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2437 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2438 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2439 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2440 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2441 | * |
| 2442 | * @param[in] ctx Context for logging. |
| 2443 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2444 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2445 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2446 | * @return LY_ERR |
| 2447 | */ |
| 2448 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2449 | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2450 | { |
| 2451 | LY_ERR rc; |
| 2452 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2453 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2454 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2455 | } |
| 2456 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2457 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2458 | case LYXP_TOKEN_PAR1: |
| 2459 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2460 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2461 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2462 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2463 | LY_CHECK_RET(rc); |
| 2464 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2465 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2466 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2467 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2468 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2469 | case LYXP_TOKEN_DOT: |
| 2470 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2471 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2472 | case LYXP_TOKEN_AT: |
| 2473 | case LYXP_TOKEN_NAMETEST: |
| 2474 | case LYXP_TOKEN_NODETYPE: |
| 2475 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2476 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2477 | LY_CHECK_RET(rc); |
| 2478 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2479 | case LYXP_TOKEN_VARREF: |
| 2480 | /* VariableReference */ |
| 2481 | ++(*tok_idx); |
| 2482 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2483 | case LYXP_TOKEN_FUNCNAME: |
| 2484 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2485 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2486 | LY_CHECK_RET(rc); |
| 2487 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2488 | case LYXP_TOKEN_OPER_PATH: |
| 2489 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2490 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2491 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2492 | LY_CHECK_RET(rc); |
| 2493 | break; |
| 2494 | case LYXP_TOKEN_LITERAL: |
| 2495 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2496 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2497 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2498 | case LYXP_TOKEN_NUMBER: |
| 2499 | /* Number */ |
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 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2502 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2503 | 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] | 2504 | return LY_EVALID; |
| 2505 | } |
| 2506 | |
| 2507 | return LY_SUCCESS; |
| 2508 | |
| 2509 | predicate: |
| 2510 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2512 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2513 | LY_CHECK_RET(rc); |
| 2514 | } |
| 2515 | |
| 2516 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2517 | 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] | 2518 | |
| 2519 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2520 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2522 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2523 | LY_CHECK_RET(rc); |
| 2524 | } |
| 2525 | |
| 2526 | return LY_SUCCESS; |
| 2527 | } |
| 2528 | |
| 2529 | /** |
| 2530 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2531 | * |
| 2532 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2533 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2534 | * |
| 2535 | * @param[in] ctx Context for logging. |
| 2536 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2537 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2538 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2539 | * @return LY_ERR |
| 2540 | */ |
| 2541 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2542 | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2543 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2544 | uint32_t prev_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2545 | LY_ERR rc; |
| 2546 | |
| 2547 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2548 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2549 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2550 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2551 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2552 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2556 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2557 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2558 | LY_CHECK_RET(rc); |
| 2559 | |
| 2560 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2561 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2562 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2563 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2564 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2565 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2566 | LY_CHECK_RET(rc); |
| 2567 | } |
| 2568 | |
| 2569 | return LY_SUCCESS; |
| 2570 | } |
| 2571 | |
| 2572 | /** |
| 2573 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2574 | * |
| 2575 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2576 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2577 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2578 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2579 | * | MultiplicativeExpr '*' UnaryExpr |
| 2580 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2581 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 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 |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2590 | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2591 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2592 | uint32_t prev_add_exp, prev_mul_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2593 | LY_ERR rc; |
| 2594 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2595 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2596 | goto reparse_multiplicative_expr; |
| 2597 | |
| 2598 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2599 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2600 | ((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] | 2601 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2602 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2603 | |
| 2604 | reparse_multiplicative_expr: |
| 2605 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2606 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2607 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2608 | LY_CHECK_RET(rc); |
| 2609 | |
| 2610 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2611 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2612 | ((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] | 2613 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2614 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2615 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2616 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | LY_CHECK_RET(rc); |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | return LY_SUCCESS; |
| 2622 | } |
| 2623 | |
| 2624 | /** |
| 2625 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2626 | * |
| 2627 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2628 | * | EqualityExpr '!=' RelationalExpr |
| 2629 | * [14] RelationalExpr ::= AdditiveExpr |
| 2630 | * | RelationalExpr '<' AdditiveExpr |
| 2631 | * | RelationalExpr '>' AdditiveExpr |
| 2632 | * | RelationalExpr '<=' AdditiveExpr |
| 2633 | * | RelationalExpr '>=' AdditiveExpr |
| 2634 | * |
| 2635 | * @param[in] ctx Context for logging. |
| 2636 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2637 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2638 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2639 | * @return LY_ERR |
| 2640 | */ |
| 2641 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2642 | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2643 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2644 | uint32_t prev_eq_exp, prev_rel_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2645 | LY_ERR rc; |
| 2646 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2647 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2648 | goto reparse_additive_expr; |
| 2649 | |
| 2650 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2651 | 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] | 2652 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2653 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2654 | |
| 2655 | reparse_additive_expr: |
| 2656 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2657 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2658 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2659 | LY_CHECK_RET(rc); |
| 2660 | |
| 2661 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2662 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2663 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2664 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2665 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2666 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2667 | LY_CHECK_RET(rc); |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | return LY_SUCCESS; |
| 2672 | } |
| 2673 | |
| 2674 | /** |
| 2675 | * @brief Reparse OrExpr. Logs directly on error. |
| 2676 | * |
| 2677 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2678 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2679 | * |
| 2680 | * @param[in] ctx Context for logging. |
| 2681 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2682 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2683 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2684 | * @return LY_ERR |
| 2685 | */ |
| 2686 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2687 | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2688 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2689 | uint32_t prev_or_exp, prev_and_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2690 | LY_ERR rc; |
| 2691 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2692 | ++depth; |
| 2693 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2694 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2695 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2696 | goto reparse_equality_expr; |
| 2697 | |
| 2698 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2699 | 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] | 2700 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2701 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2702 | |
| 2703 | reparse_equality_expr: |
| 2704 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2705 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2706 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2707 | LY_CHECK_RET(rc); |
| 2708 | |
| 2709 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2710 | 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] | 2711 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2712 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2713 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2714 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2715 | LY_CHECK_RET(rc); |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | return LY_SUCCESS; |
| 2720 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2721 | |
| 2722 | /** |
| 2723 | * @brief Parse NCName. |
| 2724 | * |
| 2725 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2726 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2727 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2728 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2729 | parse_ncname(const char *ncname) |
| 2730 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2731 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2732 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2733 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2734 | |
| 2735 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2736 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2737 | return len; |
| 2738 | } |
| 2739 | |
| 2740 | do { |
| 2741 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2742 | if (!*ncname) { |
| 2743 | break; |
| 2744 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2745 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2746 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2747 | |
| 2748 | return len; |
| 2749 | } |
| 2750 | |
| 2751 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2752 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2753 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2754 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2755 | * @param[in] exp Expression to use. |
| 2756 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2757 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2758 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2759 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2760 | */ |
| 2761 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2762 | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint32_t tok_pos, uint32_t tok_len) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2763 | { |
| 2764 | uint32_t prev; |
| 2765 | |
| 2766 | if (exp->used == exp->size) { |
| 2767 | prev = exp->size; |
| 2768 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2769 | if (prev > exp->size) { |
| 2770 | LOGINT(ctx); |
| 2771 | return LY_EINT; |
| 2772 | } |
| 2773 | |
| 2774 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2775 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2776 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2777 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2778 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2779 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2780 | } |
| 2781 | |
| 2782 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2783 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2784 | exp->tok_len[exp->used] = tok_len; |
| 2785 | ++exp->used; |
| 2786 | return LY_SUCCESS; |
| 2787 | } |
| 2788 | |
| 2789 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2790 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2791 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2792 | uint32_t i; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2793 | |
| 2794 | if (!expr) { |
| 2795 | return; |
| 2796 | } |
| 2797 | |
| 2798 | lydict_remove(ctx, expr->expr); |
| 2799 | free(expr->tokens); |
| 2800 | free(expr->tok_pos); |
| 2801 | free(expr->tok_len); |
| 2802 | if (expr->repeat) { |
| 2803 | for (i = 0; i < expr->used; ++i) { |
| 2804 | free(expr->repeat[i]); |
| 2805 | } |
| 2806 | } |
| 2807 | free(expr->repeat); |
| 2808 | free(expr); |
| 2809 | } |
| 2810 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2811 | /** |
| 2812 | * @brief Parse Axis name. |
| 2813 | * |
| 2814 | * @param[in] str String to parse. |
| 2815 | * @param[in] str_len Length of @p str. |
| 2816 | * @return LY_SUCCESS if an axis. |
| 2817 | * @return LY_ENOT otherwise. |
| 2818 | */ |
| 2819 | static LY_ERR |
| 2820 | expr_parse_axis(const char *str, size_t str_len) |
| 2821 | { |
| 2822 | switch (str_len) { |
| 2823 | case 4: |
| 2824 | if (!strncmp("self", str, str_len)) { |
| 2825 | return LY_SUCCESS; |
| 2826 | } |
| 2827 | break; |
| 2828 | case 5: |
| 2829 | if (!strncmp("child", str, str_len)) { |
| 2830 | return LY_SUCCESS; |
| 2831 | } |
| 2832 | break; |
| 2833 | case 6: |
| 2834 | if (!strncmp("parent", str, str_len)) { |
| 2835 | return LY_SUCCESS; |
| 2836 | } |
| 2837 | break; |
| 2838 | case 8: |
| 2839 | if (!strncmp("ancestor", str, str_len)) { |
| 2840 | return LY_SUCCESS; |
| 2841 | } |
| 2842 | break; |
| 2843 | case 9: |
| 2844 | if (!strncmp("attribute", str, str_len)) { |
| 2845 | return LY_SUCCESS; |
| 2846 | } else if (!strncmp("following", str, str_len)) { |
| 2847 | return LY_SUCCESS; |
| 2848 | } else if (!strncmp("namespace", str, str_len)) { |
| 2849 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2850 | return LY_ENOT; |
| 2851 | } else if (!strncmp("preceding", str, str_len)) { |
| 2852 | return LY_SUCCESS; |
| 2853 | } |
| 2854 | break; |
| 2855 | case 10: |
| 2856 | if (!strncmp("descendant", str, str_len)) { |
| 2857 | return LY_SUCCESS; |
| 2858 | } |
| 2859 | break; |
| 2860 | case 16: |
| 2861 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2862 | return LY_SUCCESS; |
| 2863 | } |
| 2864 | break; |
| 2865 | case 17: |
| 2866 | if (!strncmp("following-sibling", str, str_len)) { |
| 2867 | return LY_SUCCESS; |
| 2868 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2869 | return LY_SUCCESS; |
| 2870 | } |
| 2871 | break; |
| 2872 | case 18: |
| 2873 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2874 | return LY_SUCCESS; |
| 2875 | } |
| 2876 | break; |
| 2877 | } |
| 2878 | |
| 2879 | return LY_ENOT; |
| 2880 | } |
| 2881 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2882 | LY_ERR |
| 2883 | 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] | 2884 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2885 | LY_ERR ret = LY_SUCCESS; |
| 2886 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2887 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2888 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2889 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2890 | uint32_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2891 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2892 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2893 | assert(expr_p); |
| 2894 | |
| 2895 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2896 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2897 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2901 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2902 | } |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2903 | if (expr_len > UINT32_MAX) { |
| 2904 | LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2905 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2909 | expr = calloc(1, sizeof *expr); |
| 2910 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2911 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2912 | expr->used = 0; |
| 2913 | expr->size = LYXP_EXPR_SIZE_START; |
| 2914 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2915 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2916 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2917 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2918 | 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] | 2919 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2920 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2921 | 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] | 2922 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2923 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2924 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2925 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2926 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2927 | ++parsed; |
| 2928 | } |
| 2929 | |
| 2930 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2931 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2932 | |
| 2933 | /* '(' */ |
| 2934 | tok_len = 1; |
| 2935 | tok_type = LYXP_TOKEN_PAR1; |
| 2936 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2937 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2938 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2939 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2940 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2941 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2942 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2943 | /* it is NodeType after all */ |
| 2944 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2945 | |
| 2946 | prev_ntype_check = 0; |
| 2947 | prev_func_check = 0; |
| 2948 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2949 | /* it is FunctionName after all */ |
| 2950 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2951 | |
| 2952 | prev_ntype_check = 0; |
| 2953 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2954 | } |
| 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 | /* ')' */ |
| 2959 | tok_len = 1; |
| 2960 | tok_type = LYXP_TOKEN_PAR2; |
| 2961 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2962 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2963 | |
| 2964 | /* '[' */ |
| 2965 | tok_len = 1; |
| 2966 | tok_type = LYXP_TOKEN_BRACK1; |
| 2967 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2968 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2969 | |
| 2970 | /* ']' */ |
| 2971 | tok_len = 1; |
| 2972 | tok_type = LYXP_TOKEN_BRACK2; |
| 2973 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2974 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2975 | |
| 2976 | /* '..' */ |
| 2977 | tok_len = 2; |
| 2978 | tok_type = LYXP_TOKEN_DDOT; |
| 2979 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2980 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2981 | |
| 2982 | /* '.' */ |
| 2983 | tok_len = 1; |
| 2984 | tok_type = LYXP_TOKEN_DOT; |
| 2985 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2986 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2987 | |
| 2988 | /* '@' */ |
| 2989 | tok_len = 1; |
| 2990 | tok_type = LYXP_TOKEN_AT; |
| 2991 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2992 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2993 | |
| 2994 | /* ',' */ |
| 2995 | tok_len = 1; |
| 2996 | tok_type = LYXP_TOKEN_COMMA; |
| 2997 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2998 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2999 | |
| 3000 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3001 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 3002 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3003 | 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] | 3004 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3005 | ++tok_len; |
| 3006 | tok_type = LYXP_TOKEN_LITERAL; |
| 3007 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3008 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3009 | |
| 3010 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3011 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 3012 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3013 | 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] | 3014 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3015 | ++tok_len; |
| 3016 | tok_type = LYXP_TOKEN_LITERAL; |
| 3017 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3018 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3019 | |
| 3020 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3021 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 3022 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3023 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3024 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3025 | } |
| 3026 | tok_type = LYXP_TOKEN_NUMBER; |
| 3027 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3028 | } else if (expr_str[parsed] == '$') { |
| 3029 | |
| 3030 | /* VariableReference */ |
| 3031 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3032 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3033 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3034 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3035 | tok_len = ncname_len; |
| 3036 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 3037 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 3038 | error); |
| 3039 | tok_type = LYXP_TOKEN_VARREF; |
| 3040 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3041 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3042 | |
| 3043 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3044 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3045 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3046 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3047 | } else { |
| 3048 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3049 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3050 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3051 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3052 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3053 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3054 | /* Operator '!=' */ |
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_NEQUAL; |
| 3057 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3058 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3059 | |
| 3060 | /* Operator '<=', '>=' */ |
| 3061 | tok_len = 2; |
| 3062 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3063 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3064 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3065 | |
| 3066 | /* Operator '|' */ |
| 3067 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3068 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3069 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3070 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3071 | |
| 3072 | /* Operator '+', '-' */ |
| 3073 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3074 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3075 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3076 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3077 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3078 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3079 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3080 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3081 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3082 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3083 | |
| 3084 | /* Operator '<', '>' */ |
| 3085 | tok_len = 1; |
| 3086 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3087 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3088 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3089 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3090 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3091 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3092 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3093 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3094 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3095 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3096 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3097 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3098 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3099 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3100 | |
| 3101 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3102 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3103 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3104 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3105 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3106 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3107 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3108 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3109 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3110 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3111 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3112 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3113 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3114 | } 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] | 3115 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3116 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3117 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3118 | } else if (prev_ntype_check || prev_func_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3119 | 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] | 3120 | 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] | 3121 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3122 | goto error; |
| 3123 | } else { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3124 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3125 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3126 | goto error; |
| 3127 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3128 | } else { |
| 3129 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3130 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3131 | if (expr_str[parsed] == '*') { |
| 3132 | ncname_len = 1; |
| 3133 | } else { |
| 3134 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3135 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3136 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3137 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3138 | tok_len = ncname_len; |
| 3139 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3140 | has_axis = 0; |
| 3141 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3142 | /* axis */ |
| 3143 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3144 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); ret = LY_EVALID, |
| 3145 | error); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3146 | tok_type = LYXP_TOKEN_AXISNAME; |
| 3147 | |
| 3148 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3149 | parsed += tok_len; |
| 3150 | |
| 3151 | /* '::' */ |
| 3152 | tok_len = 2; |
| 3153 | tok_type = LYXP_TOKEN_DCOLON; |
| 3154 | |
| 3155 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3156 | parsed += tok_len; |
| 3157 | |
| 3158 | if (expr_str[parsed] == '*') { |
| 3159 | ncname_len = 1; |
| 3160 | } else { |
| 3161 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3162 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3163 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3164 | } |
| 3165 | tok_len = ncname_len; |
| 3166 | |
| 3167 | has_axis = 1; |
| 3168 | } |
| 3169 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3170 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3171 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3172 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3173 | ++tok_len; |
| 3174 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3175 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 3176 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3177 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3178 | tok_len += ncname_len; |
| 3179 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3180 | /* remove old flags to prevent ambiguities */ |
| 3181 | prev_ntype_check = 0; |
| 3182 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3183 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3184 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3185 | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 3186 | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
| 3187 | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3188 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3193 | 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] | 3194 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3195 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3196 | ++parsed; |
| 3197 | } |
| 3198 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3199 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3200 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3201 | if (reparse) { |
| 3202 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3203 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 3204 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3205 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3206 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 3207 | 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] | 3208 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3209 | 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] | 3210 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3211 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3212 | goto error; |
| 3213 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3214 | } |
| 3215 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3216 | print_expr_struct_debug(expr); |
| 3217 | *expr_p = expr; |
| 3218 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3219 | |
| 3220 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3221 | lyxp_expr_free(ctx, expr); |
| 3222 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3223 | } |
| 3224 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3225 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3226 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t start_idx, uint32_t end_idx, |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3227 | struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3228 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3229 | LY_ERR ret = LY_SUCCESS; |
| 3230 | struct lyxp_expr *dup = NULL; |
Michal Vasko | d59039d | 2022-09-09 09:07:30 +0200 | [diff] [blame] | 3231 | uint32_t used = 0, i, j, expr_len; |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3232 | const char *expr_start; |
| 3233 | |
| 3234 | assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx))); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3235 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3236 | if (!exp) { |
| 3237 | goto cleanup; |
| 3238 | } |
| 3239 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3240 | if (!start_idx && !end_idx) { |
| 3241 | end_idx = exp->used - 1; |
| 3242 | } |
| 3243 | |
| 3244 | expr_start = exp->expr + exp->tok_pos[start_idx]; |
| 3245 | expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx]; |
| 3246 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3247 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3248 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3249 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3250 | if (exp->used) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3251 | used = (end_idx - start_idx) + 1; |
| 3252 | |
| 3253 | dup->tokens = malloc(used * sizeof *dup->tokens); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3254 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3255 | memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3256 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3257 | dup->tok_pos = malloc(used * sizeof *dup->tok_pos); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3258 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3259 | memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3260 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3261 | if (start_idx) { |
| 3262 | /* fix the indices in the expression */ |
| 3263 | for (i = 0; i < used; ++i) { |
| 3264 | dup->tok_pos[i] -= expr_start - exp->expr; |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | dup->tok_len = malloc(used * sizeof *dup->tok_len); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3269 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3270 | memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3271 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3272 | if (exp->repeat) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3273 | dup->repeat = malloc(used * sizeof *dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3274 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3275 | for (i = start_idx; i <= end_idx; ++i) { |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3276 | if (!exp->repeat[i]) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3277 | dup->repeat[i - start_idx] = NULL; |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3278 | } else { |
| 3279 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3280 | /* the ending 0 as well */ |
| 3281 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3282 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3283 | dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat); |
| 3284 | LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3285 | memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3286 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3287 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3288 | } |
| 3289 | } |
| 3290 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3291 | dup->used = used; |
| 3292 | dup->size = used; |
| 3293 | |
| 3294 | /* copy only subexpression */ |
| 3295 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3296 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3297 | cleanup: |
| 3298 | if (ret) { |
| 3299 | lyxp_expr_free(ctx, dup); |
| 3300 | } else { |
| 3301 | *dup_p = dup; |
| 3302 | } |
| 3303 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3304 | } |
| 3305 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3306 | /** |
| 3307 | * @brief Get the last-added schema node that is currently in the context. |
| 3308 | * |
| 3309 | * @param[in] set Set to search in. |
| 3310 | * @return Last-added schema context node, NULL if no node is in context. |
| 3311 | */ |
| 3312 | static struct lysc_node * |
| 3313 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3314 | { |
| 3315 | uint32_t i; |
| 3316 | |
| 3317 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3318 | return NULL; |
| 3319 | } |
| 3320 | |
| 3321 | i = set->used; |
| 3322 | do { |
| 3323 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3324 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3325 | /* if there are more, simply return the first found (last added) */ |
| 3326 | return set->val.scnodes[i].scnode; |
| 3327 | } |
| 3328 | } while (i); |
| 3329 | |
| 3330 | return NULL; |
| 3331 | } |
| 3332 | |
| 3333 | /** |
| 3334 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3335 | * |
| 3336 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3337 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3338 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3339 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3340 | warn_is_numeric_type(struct lysc_type *type) |
| 3341 | { |
| 3342 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3343 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3344 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3345 | |
| 3346 | switch (type->basetype) { |
| 3347 | case LY_TYPE_DEC64: |
| 3348 | case LY_TYPE_INT8: |
| 3349 | case LY_TYPE_UINT8: |
| 3350 | case LY_TYPE_INT16: |
| 3351 | case LY_TYPE_UINT16: |
| 3352 | case LY_TYPE_INT32: |
| 3353 | case LY_TYPE_UINT32: |
| 3354 | case LY_TYPE_INT64: |
| 3355 | case LY_TYPE_UINT64: |
| 3356 | return 1; |
| 3357 | case LY_TYPE_UNION: |
| 3358 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3359 | LY_ARRAY_FOR(uni->types, u) { |
| 3360 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3361 | if (ret) { |
| 3362 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3363 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3364 | } |
| 3365 | } |
| 3366 | /* did not find any suitable type */ |
| 3367 | return 0; |
| 3368 | case LY_TYPE_LEAFREF: |
| 3369 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3370 | default: |
| 3371 | return 0; |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | /** |
| 3376 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3377 | * |
| 3378 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3379 | * @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] | 3380 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3381 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3382 | warn_is_string_type(struct lysc_type *type) |
| 3383 | { |
| 3384 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3385 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3386 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3387 | |
| 3388 | switch (type->basetype) { |
| 3389 | case LY_TYPE_BITS: |
| 3390 | case LY_TYPE_ENUM: |
| 3391 | case LY_TYPE_IDENT: |
| 3392 | case LY_TYPE_INST: |
| 3393 | case LY_TYPE_STRING: |
| 3394 | return 1; |
| 3395 | case LY_TYPE_UNION: |
| 3396 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3397 | LY_ARRAY_FOR(uni->types, u) { |
| 3398 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3399 | if (ret) { |
| 3400 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3401 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3402 | } |
| 3403 | } |
| 3404 | /* did not find any suitable type */ |
| 3405 | return 0; |
| 3406 | case LY_TYPE_LEAFREF: |
| 3407 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3408 | default: |
| 3409 | return 0; |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | /** |
| 3414 | * @brief Test whether a type is one specific type. |
| 3415 | * |
| 3416 | * @param[in] type Type to test. |
| 3417 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3418 | * @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] | 3419 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3420 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3421 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3422 | { |
| 3423 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3424 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3425 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3426 | |
| 3427 | if (type->basetype == base) { |
| 3428 | return 1; |
| 3429 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3430 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3431 | LY_ARRAY_FOR(uni->types, u) { |
| 3432 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3433 | if (ret) { |
| 3434 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3435 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3436 | } |
| 3437 | } |
| 3438 | /* did not find any suitable type */ |
| 3439 | return 0; |
| 3440 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3441 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3442 | } |
| 3443 | |
| 3444 | return 0; |
| 3445 | } |
| 3446 | |
| 3447 | /** |
| 3448 | * @brief Get next type of a (union) type. |
| 3449 | * |
| 3450 | * @param[in] type Base type. |
| 3451 | * @param[in] prev_type Previously returned type. |
| 3452 | * @return Next type or NULL. |
| 3453 | */ |
| 3454 | static struct lysc_type * |
| 3455 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3456 | { |
| 3457 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3458 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3459 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3460 | |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3461 | if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3462 | uni = (struct lysc_type_union *)type; |
| 3463 | if (!prev_type) { |
| 3464 | return uni->types[0]; |
| 3465 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3466 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3467 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3468 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3469 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3470 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3471 | found = 1; |
| 3472 | } |
| 3473 | } |
| 3474 | return NULL; |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3475 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3476 | if (prev_type) { |
| 3477 | assert(type == prev_type); |
| 3478 | return NULL; |
| 3479 | } else { |
| 3480 | return type; |
| 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | /** |
| 3486 | * @brief Test whether 2 types have a common type. |
| 3487 | * |
| 3488 | * @param[in] type1 First type. |
| 3489 | * @param[in] type2 Second type. |
| 3490 | * @return 1 if they do, 0 otherwise. |
| 3491 | */ |
| 3492 | static int |
| 3493 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3494 | { |
| 3495 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3496 | |
| 3497 | t1 = NULL; |
| 3498 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3499 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3500 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3501 | } else { |
| 3502 | rt1 = t1; |
| 3503 | } |
| 3504 | |
| 3505 | t2 = NULL; |
| 3506 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3507 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3508 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3509 | } else { |
| 3510 | rt2 = t2; |
| 3511 | } |
| 3512 | |
| 3513 | if (rt2->basetype == rt1->basetype) { |
| 3514 | /* match found */ |
| 3515 | return 1; |
| 3516 | } |
| 3517 | } |
| 3518 | } |
| 3519 | |
| 3520 | return 0; |
| 3521 | } |
| 3522 | |
| 3523 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3524 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3525 | * |
| 3526 | * @param[in] ctx Context for logging. |
| 3527 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3528 | * @param[in] subexpr Subexpression start. |
| 3529 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3530 | * @param[in] cur_scnode Expression context node. |
| 3531 | */ |
| 3532 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3533 | warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len, |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3534 | const struct lysc_node *cur_scnode) |
| 3535 | { |
| 3536 | char *path; |
| 3537 | |
| 3538 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3539 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".", |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3540 | tok_pos, subexpr_len, subexpr, path); |
| 3541 | free(path); |
| 3542 | } |
| 3543 | |
| 3544 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3545 | * @brief Check both operands of comparison operators. |
| 3546 | * |
| 3547 | * @param[in] ctx Context for errors. |
| 3548 | * @param[in] set1 First operand set. |
| 3549 | * @param[in] set2 Second operand set. |
| 3550 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3551 | * @param[in] expr Start of the expression to print with the warning. |
| 3552 | * @param[in] tok_pos Token position. |
| 3553 | */ |
| 3554 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3555 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3556 | uint32_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3557 | { |
| 3558 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3559 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3560 | |
| 3561 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3562 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3563 | |
| 3564 | if (!node1 && !node2) { |
| 3565 | /* no node-sets involved, nothing to do */ |
| 3566 | return; |
| 3567 | } |
| 3568 | |
| 3569 | if (node1) { |
| 3570 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3571 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3572 | warning = 1; |
| 3573 | leaves = 0; |
| 3574 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3575 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3576 | warning = 1; |
| 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | if (node2) { |
| 3581 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3582 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3583 | warning = 1; |
| 3584 | leaves = 0; |
| 3585 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3586 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3587 | warning = 1; |
| 3588 | } |
| 3589 | } |
| 3590 | |
| 3591 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3592 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3593 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3594 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3595 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3596 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3597 | warning = 1; |
| 3598 | } |
| 3599 | } |
| 3600 | |
| 3601 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3602 | 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] | 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | /** |
| 3607 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3608 | * |
| 3609 | * @param[in] exp Parsed XPath expression. |
| 3610 | * @param[in] set Set with the leaf/leaf-list. |
| 3611 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3612 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3613 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3614 | */ |
| 3615 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3616 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp, |
| 3617 | uint32_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3618 | { |
| 3619 | struct lysc_node *scnode; |
| 3620 | struct lysc_type *type; |
| 3621 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3622 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3623 | LY_ERR rc; |
| 3624 | struct ly_err_item *err = NULL; |
| 3625 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3626 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3627 | ((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] | 3628 | /* check that the node can have the specified value */ |
| 3629 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3630 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3631 | } else { |
| 3632 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3633 | } |
| 3634 | if (!value) { |
| 3635 | LOGMEM(set->ctx); |
| 3636 | return; |
| 3637 | } |
| 3638 | |
| 3639 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3640 | 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] | 3641 | " 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] | 3642 | 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] | 3643 | (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] | 3644 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3648 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3649 | 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] | 3650 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3651 | if (rc == LY_EINCOMPLETE) { |
| 3652 | rc = LY_SUCCESS; |
| 3653 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3654 | |
| 3655 | if (err) { |
| 3656 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3657 | ly_err_free(err); |
| 3658 | } else if (rc != LY_SUCCESS) { |
| 3659 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3660 | } |
| 3661 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3662 | 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] | 3663 | (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] | 3664 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3665 | } else { |
| 3666 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3667 | } |
| 3668 | } |
| 3669 | free(value); |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | /* |
| 3674 | * XPath functions |
| 3675 | */ |
| 3676 | |
| 3677 | /** |
| 3678 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3679 | * depending on whether the first node bit value from the second argument is set. |
| 3680 | * |
| 3681 | * @param[in] args Array of arguments. |
| 3682 | * @param[in] arg_count Count of elements in @p args. |
| 3683 | * @param[in,out] set Context and result set at the same time. |
| 3684 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3685 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3686 | */ |
| 3687 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3688 | xpath_bit_is_set(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3689 | { |
| 3690 | struct lyd_node_term *leaf; |
| 3691 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3692 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3693 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3694 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3695 | |
| 3696 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3697 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3698 | 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] | 3699 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3700 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3701 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3702 | sleaf->name); |
| 3703 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3704 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3705 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3709 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3710 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3711 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3712 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3713 | 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] | 3714 | } |
| 3715 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3716 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3717 | return rc; |
| 3718 | } |
| 3719 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3720 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3721 | 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] | 3722 | return LY_EVALID; |
| 3723 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3724 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3725 | LY_CHECK_RET(rc); |
| 3726 | |
| 3727 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3728 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3729 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3730 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3731 | LYD_VALUE_GET(&leaf->value, bits); |
| 3732 | LY_ARRAY_FOR(bits->items, u) { |
| 3733 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3734 | set_fill_boolean(set, 1); |
| 3735 | break; |
| 3736 | } |
| 3737 | } |
| 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | return LY_SUCCESS; |
| 3742 | } |
| 3743 | |
| 3744 | /** |
| 3745 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3746 | * with the argument converted to boolean. |
| 3747 | * |
| 3748 | * @param[in] args Array of arguments. |
| 3749 | * @param[in] arg_count Count of elements in @p args. |
| 3750 | * @param[in,out] set Context and result set at the same time. |
| 3751 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3752 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3753 | */ |
| 3754 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3755 | xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3756 | { |
| 3757 | LY_ERR rc; |
| 3758 | |
| 3759 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3760 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3761 | return LY_SUCCESS; |
| 3762 | } |
| 3763 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3764 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3765 | LY_CHECK_RET(rc); |
| 3766 | set_fill_set(set, args[0]); |
| 3767 | |
| 3768 | return LY_SUCCESS; |
| 3769 | } |
| 3770 | |
| 3771 | /** |
| 3772 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3773 | * with the first argument rounded up to the nearest integer. |
| 3774 | * |
| 3775 | * @param[in] args Array of arguments. |
| 3776 | * @param[in] arg_count Count of elements in @p args. |
| 3777 | * @param[in,out] set Context and result set at the same time. |
| 3778 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3779 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3780 | */ |
| 3781 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3782 | xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3783 | { |
| 3784 | struct lysc_node_leaf *sleaf; |
| 3785 | LY_ERR rc = LY_SUCCESS; |
| 3786 | |
| 3787 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3788 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3789 | 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] | 3790 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3791 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3792 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3793 | sleaf->name); |
| 3794 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3795 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3796 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3797 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3798 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3799 | return rc; |
| 3800 | } |
| 3801 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3802 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3803 | LY_CHECK_RET(rc); |
| 3804 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3805 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3806 | } else { |
| 3807 | set_fill_number(set, args[0]->val.num); |
| 3808 | } |
| 3809 | |
| 3810 | return LY_SUCCESS; |
| 3811 | } |
| 3812 | |
| 3813 | /** |
| 3814 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3815 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3816 | * |
| 3817 | * @param[in] args Array of arguments. |
| 3818 | * @param[in] arg_count Count of elements in @p args. |
| 3819 | * @param[in,out] set Context and result set at the same time. |
| 3820 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3821 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3822 | */ |
| 3823 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3824 | xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3825 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3826 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3827 | char *str = NULL; |
| 3828 | size_t used = 1; |
| 3829 | LY_ERR rc = LY_SUCCESS; |
| 3830 | struct lysc_node_leaf *sleaf; |
| 3831 | |
| 3832 | if (options & LYXP_SCNODE_ALL) { |
| 3833 | for (i = 0; i < arg_count; ++i) { |
| 3834 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3835 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3836 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3837 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3838 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3839 | 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] | 3840 | } |
| 3841 | } |
| 3842 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3843 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3844 | return rc; |
| 3845 | } |
| 3846 | |
| 3847 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3848 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3849 | if (rc != LY_SUCCESS) { |
| 3850 | free(str); |
| 3851 | return rc; |
| 3852 | } |
| 3853 | |
| 3854 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3855 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3856 | strcpy(str + used - 1, args[i]->val.str); |
| 3857 | used += strlen(args[i]->val.str); |
| 3858 | } |
| 3859 | |
| 3860 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3861 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3862 | set->type = LYXP_SET_STRING; |
| 3863 | set->val.str = str; |
| 3864 | |
| 3865 | return LY_SUCCESS; |
| 3866 | } |
| 3867 | |
| 3868 | /** |
| 3869 | * @brief Execute the XPath contains(string, string) function. |
| 3870 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3871 | * be found in the first or not. |
| 3872 | * |
| 3873 | * @param[in] args Array of arguments. |
| 3874 | * @param[in] arg_count Count of elements in @p args. |
| 3875 | * @param[in,out] set Context and result set at the same time. |
| 3876 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3877 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3878 | */ |
| 3879 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3880 | xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3881 | { |
| 3882 | struct lysc_node_leaf *sleaf; |
| 3883 | LY_ERR rc = LY_SUCCESS; |
| 3884 | |
| 3885 | if (options & LYXP_SCNODE_ALL) { |
| 3886 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3887 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3888 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3889 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3890 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3891 | 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] | 3892 | } |
| 3893 | } |
| 3894 | |
| 3895 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3896 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3897 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3898 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3899 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3900 | 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] | 3901 | } |
| 3902 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3903 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3904 | return rc; |
| 3905 | } |
| 3906 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3907 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3908 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3909 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3910 | LY_CHECK_RET(rc); |
| 3911 | |
| 3912 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3913 | set_fill_boolean(set, 1); |
| 3914 | } else { |
| 3915 | set_fill_boolean(set, 0); |
| 3916 | } |
| 3917 | |
| 3918 | return LY_SUCCESS; |
| 3919 | } |
| 3920 | |
| 3921 | /** |
| 3922 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3923 | * with the size of the node-set from the argument. |
| 3924 | * |
| 3925 | * @param[in] args Array of arguments. |
| 3926 | * @param[in] arg_count Count of elements in @p args. |
| 3927 | * @param[in,out] set Context and result set at the same time. |
| 3928 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3929 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3930 | */ |
| 3931 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3932 | xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3933 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3934 | LY_ERR rc = LY_SUCCESS; |
| 3935 | |
| 3936 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3937 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3938 | 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] | 3939 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3940 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3941 | return rc; |
| 3942 | } |
| 3943 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3944 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3945 | 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] | 3946 | return LY_EVALID; |
| 3947 | } |
| 3948 | |
| 3949 | set_fill_number(set, args[0]->used); |
| 3950 | return LY_SUCCESS; |
| 3951 | } |
| 3952 | |
| 3953 | /** |
| 3954 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3955 | * with the context with the intial node. |
| 3956 | * |
| 3957 | * @param[in] args Array of arguments. |
| 3958 | * @param[in] arg_count Count of elements in @p args. |
| 3959 | * @param[in,out] set Context and result set at the same time. |
| 3960 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3961 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3962 | */ |
| 3963 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3964 | xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3965 | { |
| 3966 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3967 | 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] | 3968 | return LY_EVALID; |
| 3969 | } |
| 3970 | |
| 3971 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3972 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3973 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3974 | if (set->cur_scnode) { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 3975 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3976 | } else { |
| 3977 | /* root node */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 3978 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3979 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3980 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3981 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3982 | |
Michal Vasko | 296dfaf | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3983 | if (set->cur_node) { |
| 3984 | /* position is filled later */ |
| 3985 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3986 | } else { |
| 3987 | /* root node */ |
| 3988 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3989 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | return LY_SUCCESS; |
| 3993 | } |
| 3994 | |
| 3995 | /** |
| 3996 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3997 | * leafref or instance-identifier target node(s). |
| 3998 | * |
| 3999 | * @param[in] args Array of arguments. |
| 4000 | * @param[in] arg_count Count of elements in @p args. |
| 4001 | * @param[in,out] set Context and result set at the same time. |
| 4002 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4003 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4004 | */ |
| 4005 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4006 | xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4007 | { |
| 4008 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 4009 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4010 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 4011 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4012 | struct ly_path *p; |
| 4013 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4014 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 4015 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4016 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4017 | |
| 4018 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4019 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4020 | 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] | 4021 | } 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] | 4022 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4023 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4024 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 4025 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 4026 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4027 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 4028 | __func__, sleaf->name); |
| 4029 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4030 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4031 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 4032 | 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] | 4033 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 4034 | 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] | 4035 | |
| 4036 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4037 | 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] | 4038 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4039 | if (!r) { |
| 4040 | /* get the target node */ |
| 4041 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 4042 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4043 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 4044 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4045 | } /* 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] | 4046 | } |
| 4047 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4048 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4049 | } |
| 4050 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4051 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4052 | 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] | 4053 | return LY_EVALID; |
| 4054 | } |
| 4055 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4056 | lyxp_set_free_content(set); |
| 4057 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4058 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4059 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4060 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4061 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 4062 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4063 | 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] | 4064 | &node, &errmsg)) { |
Michal Vasko | 1a2b8ee | 2022-12-05 10:41:55 +0100 | [diff] [blame] | 4065 | LOGERR(set->ctx, LY_EVALID, "%s", errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4066 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4067 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4068 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4069 | } else { |
| 4070 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 4071 | if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4072 | 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] | 4073 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4074 | return LY_EVALID; |
| 4075 | } |
| 4076 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4077 | |
| 4078 | /* insert it */ |
| 4079 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | return LY_SUCCESS; |
| 4084 | } |
| 4085 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4086 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4087 | 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] | 4088 | { |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 4089 | uint32_t i, id_len; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4090 | LY_ARRAY_COUNT_TYPE u; |
| 4091 | struct lyd_node_term *leaf; |
| 4092 | struct lysc_node_leaf *sleaf; |
| 4093 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4094 | struct lyd_value *val; |
| 4095 | const struct lys_module *mod; |
| 4096 | const char *id_name; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4097 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4098 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4099 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4100 | |
| 4101 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4102 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4103 | 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] | 4104 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4105 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4106 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4107 | sleaf->name); |
| 4108 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4109 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4110 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4114 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4115 | 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] | 4116 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4117 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4118 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4119 | } |
| 4120 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4121 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4122 | return rc; |
| 4123 | } |
| 4124 | |
| 4125 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4126 | 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] | 4127 | return LY_EVALID; |
| 4128 | } |
| 4129 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4130 | LY_CHECK_RET(rc); |
| 4131 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4132 | /* parse the identity */ |
| 4133 | id_name = args[1]->val.str; |
| 4134 | id_len = strlen(id_name); |
| 4135 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4136 | LY_CHECK_RET(rc); |
| 4137 | if (!mod) { |
| 4138 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4139 | return LY_EVALID; |
| 4140 | } |
| 4141 | |
| 4142 | /* find the identity */ |
| 4143 | found = 0; |
| 4144 | LY_ARRAY_FOR(mod->identities, u) { |
| 4145 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4146 | /* we have match */ |
| 4147 | found = 1; |
| 4148 | break; |
| 4149 | } |
| 4150 | } |
| 4151 | if (!found) { |
| 4152 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4153 | return LY_EVALID; |
| 4154 | } |
| 4155 | id = &mod->identities[u]; |
| 4156 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4157 | set_fill_boolean(set, 0); |
| 4158 | found = 0; |
| 4159 | for (i = 0; i < args[0]->used; ++i) { |
| 4160 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4161 | continue; |
| 4162 | } |
| 4163 | |
| 4164 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4165 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4166 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4167 | val = &leaf->value; |
| 4168 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 4169 | /* uninteresting */ |
| 4170 | continue; |
| 4171 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4172 | } else { |
| 4173 | meta = args[0]->val.meta[i].meta; |
| 4174 | val = &meta->value; |
| 4175 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4176 | /* uninteresting */ |
| 4177 | continue; |
| 4178 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4181 | /* check the identity itself */ |
| 4182 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4183 | set_fill_boolean(set, 1); |
| 4184 | found = 1; |
| 4185 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4186 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4187 | set_fill_boolean(set, 1); |
| 4188 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4189 | } |
| 4190 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4191 | if (found) { |
| 4192 | break; |
| 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | return LY_SUCCESS; |
| 4197 | } |
| 4198 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4199 | /** |
| 4200 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4201 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4202 | * argument identity. |
| 4203 | * |
| 4204 | * @param[in] args Array of arguments. |
| 4205 | * @param[in] arg_count Count of elements in @p args. |
| 4206 | * @param[in,out] set Context and result set at the same time. |
| 4207 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4208 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4209 | */ |
| 4210 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4211 | xpath_derived_from(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4212 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4213 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | /** |
| 4217 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4218 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4219 | * the second argument identity. |
| 4220 | * |
| 4221 | * @param[in] args Array of arguments. |
| 4222 | * @param[in] arg_count Count of elements in @p args. |
| 4223 | * @param[in,out] set Context and result set at the same time. |
| 4224 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4225 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4226 | */ |
| 4227 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4228 | xpath_derived_from_or_self(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4229 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4230 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | /** |
| 4234 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4235 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4236 | * |
| 4237 | * @param[in] args Array of arguments. |
| 4238 | * @param[in] arg_count Count of elements in @p args. |
| 4239 | * @param[in,out] set Context and result set at the same time. |
| 4240 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4241 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4242 | */ |
| 4243 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4244 | xpath_enum_value(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4245 | { |
| 4246 | struct lyd_node_term *leaf; |
| 4247 | struct lysc_node_leaf *sleaf; |
| 4248 | LY_ERR rc = LY_SUCCESS; |
| 4249 | |
| 4250 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4251 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4252 | 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] | 4253 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4254 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4255 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4256 | sleaf->name); |
| 4257 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4258 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4259 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4260 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4261 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4262 | return rc; |
| 4263 | } |
| 4264 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4265 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4266 | 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] | 4267 | return LY_EVALID; |
| 4268 | } |
| 4269 | |
| 4270 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4271 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4272 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4273 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4274 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4275 | set_fill_number(set, leaf->value.enum_item->value); |
| 4276 | } |
| 4277 | } |
| 4278 | |
| 4279 | return LY_SUCCESS; |
| 4280 | } |
| 4281 | |
| 4282 | /** |
| 4283 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4284 | * with false value. |
| 4285 | * |
| 4286 | * @param[in] args Array of arguments. |
| 4287 | * @param[in] arg_count Count of elements in @p args. |
| 4288 | * @param[in,out] set Context and result set at the same time. |
| 4289 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4290 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4291 | */ |
| 4292 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4293 | xpath_false(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4294 | { |
| 4295 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4296 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4297 | return LY_SUCCESS; |
| 4298 | } |
| 4299 | |
| 4300 | set_fill_boolean(set, 0); |
| 4301 | return LY_SUCCESS; |
| 4302 | } |
| 4303 | |
| 4304 | /** |
| 4305 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4306 | * with the first argument floored (truncated). |
| 4307 | * |
| 4308 | * @param[in] args Array of arguments. |
| 4309 | * @param[in] arg_count Count of elements in @p args. |
| 4310 | * @param[in,out] set Context and result set at the same time. |
| 4311 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4312 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4313 | */ |
| 4314 | static LY_ERR |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4315 | xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4316 | { |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4317 | struct lysc_node_leaf *sleaf; |
| 4318 | LY_ERR rc = LY_SUCCESS; |
| 4319 | |
| 4320 | if (options & LYXP_SCNODE_ALL) { |
| 4321 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
| 4322 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 4323 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4324 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4325 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4326 | sleaf->name); |
| 4327 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4328 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4329 | } |
| 4330 | } |
| 4331 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 4332 | return rc; |
| 4333 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4334 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4335 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4336 | LY_CHECK_RET(rc); |
| 4337 | if (isfinite(args[0]->val.num)) { |
| 4338 | set_fill_number(set, (long long)args[0]->val.num); |
| 4339 | } |
| 4340 | |
| 4341 | return LY_SUCCESS; |
| 4342 | } |
| 4343 | |
| 4344 | /** |
| 4345 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4346 | * whether the language of the text matches the one from the argument. |
| 4347 | * |
| 4348 | * @param[in] args Array of arguments. |
| 4349 | * @param[in] arg_count Count of elements in @p args. |
| 4350 | * @param[in,out] set Context and result set at the same time. |
| 4351 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4352 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4353 | */ |
| 4354 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4355 | xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4356 | { |
| 4357 | const struct lyd_node *node; |
| 4358 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4359 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4360 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4361 | LY_ERR rc = LY_SUCCESS; |
| 4362 | |
| 4363 | if (options & LYXP_SCNODE_ALL) { |
| 4364 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4365 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4366 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4367 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4368 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4369 | 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] | 4370 | } |
| 4371 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4372 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4373 | return rc; |
| 4374 | } |
| 4375 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4376 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4377 | LY_CHECK_RET(rc); |
| 4378 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4379 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4380 | 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] | 4381 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4382 | } else if (!set->used) { |
| 4383 | set_fill_boolean(set, 0); |
| 4384 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | switch (set->val.nodes[0].type) { |
| 4388 | case LYXP_NODE_ELEM: |
| 4389 | case LYXP_NODE_TEXT: |
| 4390 | node = set->val.nodes[0].node; |
| 4391 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4392 | case LYXP_NODE_META: |
| 4393 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4394 | break; |
| 4395 | default: |
| 4396 | /* nothing to do with roots */ |
| 4397 | set_fill_boolean(set, 0); |
| 4398 | return LY_SUCCESS; |
| 4399 | } |
| 4400 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4401 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4402 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4403 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4404 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4405 | 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] | 4406 | break; |
| 4407 | } |
| 4408 | } |
| 4409 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4410 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4411 | break; |
| 4412 | } |
| 4413 | } |
| 4414 | |
| 4415 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4416 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4417 | set_fill_boolean(set, 0); |
| 4418 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4419 | uint64_t i; |
| 4420 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4421 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4422 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4423 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4424 | set_fill_boolean(set, 0); |
| 4425 | break; |
| 4426 | } |
| 4427 | } |
| 4428 | if (!args[0]->val.str[i]) { |
| 4429 | if (!val[i] || (val[i] == '-')) { |
| 4430 | set_fill_boolean(set, 1); |
| 4431 | } else { |
| 4432 | set_fill_boolean(set, 0); |
| 4433 | } |
| 4434 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | return LY_SUCCESS; |
| 4438 | } |
| 4439 | |
| 4440 | /** |
| 4441 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4442 | * with the context size. |
| 4443 | * |
| 4444 | * @param[in] args Array of arguments. |
| 4445 | * @param[in] arg_count Count of elements in @p args. |
| 4446 | * @param[in,out] set Context and result set at the same time. |
| 4447 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4448 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4449 | */ |
| 4450 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4451 | xpath_last(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4452 | { |
| 4453 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4454 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4455 | return LY_SUCCESS; |
| 4456 | } |
| 4457 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4458 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4459 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4460 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4461 | } else if (!set->used) { |
| 4462 | set_fill_number(set, 0); |
| 4463 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | set_fill_number(set, set->ctx_size); |
| 4467 | return LY_SUCCESS; |
| 4468 | } |
| 4469 | |
| 4470 | /** |
| 4471 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4472 | * with the node name without namespace from the argument or the context. |
| 4473 | * |
| 4474 | * @param[in] args Array of arguments. |
| 4475 | * @param[in] arg_count Count of elements in @p args. |
| 4476 | * @param[in,out] set Context and result set at the same time. |
| 4477 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4478 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4479 | */ |
| 4480 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4481 | xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4482 | { |
| 4483 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4484 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4485 | /* suppress unused variable warning */ |
| 4486 | (void)options; |
| 4487 | |
| 4488 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4489 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4490 | return LY_SUCCESS; |
| 4491 | } |
| 4492 | |
| 4493 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4494 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4495 | 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] | 4496 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4497 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4498 | } else if (!args[0]->used) { |
| 4499 | set_fill_string(set, "", 0); |
| 4500 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4504 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4505 | |
| 4506 | item = &args[0]->val.nodes[0]; |
| 4507 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4508 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4509 | 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] | 4510 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4511 | } else if (!set->used) { |
| 4512 | set_fill_string(set, "", 0); |
| 4513 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4517 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4518 | |
| 4519 | item = &set->val.nodes[0]; |
| 4520 | } |
| 4521 | |
| 4522 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4523 | case LYXP_NODE_NONE: |
| 4524 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4525 | case LYXP_NODE_ROOT: |
| 4526 | case LYXP_NODE_ROOT_CONFIG: |
| 4527 | case LYXP_NODE_TEXT: |
| 4528 | set_fill_string(set, "", 0); |
| 4529 | break; |
| 4530 | case LYXP_NODE_ELEM: |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 4531 | set_fill_string(set, LYD_NAME(item->node), strlen(LYD_NAME(item->node))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4532 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4533 | case LYXP_NODE_META: |
| 4534 | 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] | 4535 | break; |
| 4536 | } |
| 4537 | |
| 4538 | return LY_SUCCESS; |
| 4539 | } |
| 4540 | |
| 4541 | /** |
| 4542 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4543 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4544 | * |
| 4545 | * @param[in] args Array of arguments. |
| 4546 | * @param[in] arg_count Count of elements in @p args. |
| 4547 | * @param[in,out] set Context and result set at the same time. |
| 4548 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4549 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4550 | */ |
| 4551 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4552 | xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4553 | { |
| 4554 | struct lyxp_set_node *item; |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 4555 | const struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4556 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4557 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4558 | |
| 4559 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4560 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4561 | return LY_SUCCESS; |
| 4562 | } |
| 4563 | |
| 4564 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4565 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4566 | 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] | 4567 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4568 | } else if (!args[0]->used) { |
| 4569 | set_fill_string(set, "", 0); |
| 4570 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4574 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4575 | |
| 4576 | item = &args[0]->val.nodes[0]; |
| 4577 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4578 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4579 | 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] | 4580 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4581 | } else if (!set->used) { |
| 4582 | set_fill_string(set, "", 0); |
| 4583 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4587 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4588 | |
| 4589 | item = &set->val.nodes[0]; |
| 4590 | } |
| 4591 | |
| 4592 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4593 | case LYXP_NODE_NONE: |
| 4594 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4595 | case LYXP_NODE_ROOT: |
| 4596 | case LYXP_NODE_ROOT_CONFIG: |
| 4597 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4598 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4599 | break; |
| 4600 | case LYXP_NODE_ELEM: |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 4601 | mod = lyd_node_module(item->node); |
| 4602 | name = LYD_NAME(item->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4603 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4604 | case LYXP_NODE_META: |
| 4605 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4606 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4607 | break; |
| 4608 | } |
| 4609 | |
| 4610 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4611 | int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 4612 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4613 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4614 | set_fill_string(set, str, strlen(str)); |
| 4615 | free(str); |
| 4616 | } else { |
| 4617 | set_fill_string(set, "", 0); |
| 4618 | } |
| 4619 | |
| 4620 | return LY_SUCCESS; |
| 4621 | } |
| 4622 | |
| 4623 | /** |
| 4624 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4625 | * with the namespace of the node from the argument or the context. |
| 4626 | * |
| 4627 | * @param[in] args Array of arguments. |
| 4628 | * @param[in] arg_count Count of elements in @p args. |
| 4629 | * @param[in,out] set Context and result set at the same time. |
| 4630 | * @param[in] options XPath options. |
| 4631 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4632 | */ |
| 4633 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4634 | xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4635 | { |
| 4636 | struct lyxp_set_node *item; |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 4637 | const struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4638 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4639 | /* suppress unused variable warning */ |
| 4640 | (void)options; |
| 4641 | |
| 4642 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4643 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4644 | return LY_SUCCESS; |
| 4645 | } |
| 4646 | |
| 4647 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4648 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4649 | 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] | 4650 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4651 | return LY_EVALID; |
| 4652 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4653 | set_fill_string(set, "", 0); |
| 4654 | return LY_SUCCESS; |
| 4655 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4656 | |
| 4657 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4658 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4659 | |
| 4660 | item = &args[0]->val.nodes[0]; |
| 4661 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4662 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4663 | 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] | 4664 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4665 | } else if (!set->used) { |
| 4666 | set_fill_string(set, "", 0); |
| 4667 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4671 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4672 | |
| 4673 | item = &set->val.nodes[0]; |
| 4674 | } |
| 4675 | |
| 4676 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4677 | case LYXP_NODE_NONE: |
| 4678 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4679 | case LYXP_NODE_ROOT: |
| 4680 | case LYXP_NODE_ROOT_CONFIG: |
| 4681 | case LYXP_NODE_TEXT: |
| 4682 | set_fill_string(set, "", 0); |
| 4683 | break; |
| 4684 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4685 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4686 | if (item->type == LYXP_NODE_ELEM) { |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 4687 | mod = lyd_node_module(item->node); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4688 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4689 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4690 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4694 | break; |
| 4695 | } |
| 4696 | |
| 4697 | return LY_SUCCESS; |
| 4698 | } |
| 4699 | |
| 4700 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4701 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4702 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4703 | * from the argument or the context. |
| 4704 | * |
| 4705 | * @param[in] args Array of arguments. |
| 4706 | * @param[in] arg_count Count of elements in @p args. |
| 4707 | * @param[in,out] set Context and result set at the same time. |
| 4708 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4709 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4710 | */ |
| 4711 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4712 | xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4713 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4714 | uint32_t i, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4715 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4716 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4717 | struct lysc_node_leaf *sleaf; |
| 4718 | LY_ERR rc = LY_SUCCESS; |
| 4719 | |
| 4720 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4721 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4722 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4723 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4724 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4725 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4726 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4727 | 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] | 4728 | } |
| 4729 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4730 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4731 | return rc; |
| 4732 | } |
| 4733 | |
| 4734 | if (arg_count) { |
| 4735 | set_fill_set(set, args[0]); |
| 4736 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4737 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4738 | LY_CHECK_RET(rc); |
| 4739 | |
| 4740 | /* is there any normalization necessary? */ |
| 4741 | for (i = 0; set->val.str[i]; ++i) { |
| 4742 | if (is_xmlws(set->val.str[i])) { |
| 4743 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4744 | have_spaces = 1; |
| 4745 | break; |
| 4746 | } |
| 4747 | space_before = 1; |
| 4748 | } else { |
| 4749 | space_before = 0; |
| 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | /* yep, there is */ |
| 4754 | if (have_spaces) { |
| 4755 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4756 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4757 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4758 | new_used = 0; |
| 4759 | |
| 4760 | space_before = 0; |
| 4761 | for (i = 0; set->val.str[i]; ++i) { |
| 4762 | if (is_xmlws(set->val.str[i])) { |
| 4763 | if ((i == 0) || space_before) { |
| 4764 | space_before = 1; |
| 4765 | continue; |
| 4766 | } else { |
| 4767 | space_before = 1; |
| 4768 | } |
| 4769 | } else { |
| 4770 | space_before = 0; |
| 4771 | } |
| 4772 | |
| 4773 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4774 | ++new_used; |
| 4775 | } |
| 4776 | |
| 4777 | /* at worst there is one trailing space now */ |
| 4778 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4779 | --new_used; |
| 4780 | } |
| 4781 | |
| 4782 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4783 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4784 | new[new_used] = '\0'; |
| 4785 | |
| 4786 | free(set->val.str); |
| 4787 | set->val.str = new; |
| 4788 | } |
| 4789 | |
| 4790 | return LY_SUCCESS; |
| 4791 | } |
| 4792 | |
| 4793 | /** |
| 4794 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4795 | * with the argument converted to boolean and logically inverted. |
| 4796 | * |
| 4797 | * @param[in] args Array of arguments. |
| 4798 | * @param[in] arg_count Count of elements in @p args. |
| 4799 | * @param[in,out] set Context and result set at the same time. |
| 4800 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4801 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4802 | */ |
| 4803 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4804 | xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4805 | { |
| 4806 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4807 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4808 | return LY_SUCCESS; |
| 4809 | } |
| 4810 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4811 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4812 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4813 | set_fill_boolean(set, 0); |
| 4814 | } else { |
| 4815 | set_fill_boolean(set, 1); |
| 4816 | } |
| 4817 | |
| 4818 | return LY_SUCCESS; |
| 4819 | } |
| 4820 | |
| 4821 | /** |
| 4822 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4823 | * with the number representation of either the argument or the context. |
| 4824 | * |
| 4825 | * @param[in] args Array of arguments. |
| 4826 | * @param[in] arg_count Count of elements in @p args. |
| 4827 | * @param[in,out] set Context and result set at the same time. |
| 4828 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4829 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4830 | */ |
| 4831 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4832 | xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4833 | { |
| 4834 | LY_ERR rc; |
| 4835 | |
| 4836 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4837 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4838 | return LY_SUCCESS; |
| 4839 | } |
| 4840 | |
| 4841 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4842 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4843 | LY_CHECK_RET(rc); |
| 4844 | set_fill_set(set, args[0]); |
| 4845 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4846 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4847 | LY_CHECK_RET(rc); |
| 4848 | } |
| 4849 | |
| 4850 | return LY_SUCCESS; |
| 4851 | } |
| 4852 | |
| 4853 | /** |
| 4854 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4855 | * with the context position. |
| 4856 | * |
| 4857 | * @param[in] args Array of arguments. |
| 4858 | * @param[in] arg_count Count of elements in @p args. |
| 4859 | * @param[in,out] set Context and result set at the same time. |
| 4860 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4861 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4862 | */ |
| 4863 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4864 | xpath_position(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4865 | { |
| 4866 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4867 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4868 | return LY_SUCCESS; |
| 4869 | } |
| 4870 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4871 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4872 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4873 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4874 | } else if (!set->used) { |
| 4875 | set_fill_number(set, 0); |
| 4876 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | set_fill_number(set, set->ctx_pos); |
| 4880 | |
| 4881 | /* UNUSED in 'Release' build type */ |
| 4882 | (void)options; |
| 4883 | return LY_SUCCESS; |
| 4884 | } |
| 4885 | |
| 4886 | /** |
| 4887 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4888 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4889 | * YANG 1.1 RFC section 10.2.1. |
| 4890 | * |
| 4891 | * @param[in] args Array of arguments. |
| 4892 | * @param[in] arg_count Count of elements in @p args. |
| 4893 | * @param[in,out] set Context and result set at the same time. |
| 4894 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4895 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4896 | */ |
| 4897 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4898 | xpath_re_match(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4899 | { |
| 4900 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4901 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4902 | LY_ERR rc = LY_SUCCESS; |
| 4903 | struct ly_err_item *err; |
| 4904 | |
| 4905 | if (options & LYXP_SCNODE_ALL) { |
| 4906 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4907 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4908 | 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] | 4909 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4910 | 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] | 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4915 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4916 | 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] | 4917 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4918 | 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] | 4919 | } |
| 4920 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4921 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4922 | return rc; |
| 4923 | } |
| 4924 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4925 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4926 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4927 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4928 | LY_CHECK_RET(rc); |
| 4929 | |
| 4930 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4931 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4932 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4933 | 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] | 4934 | if (set->cur_node) { |
| 4935 | LOG_LOCBACK(0, 1, 0, 0); |
| 4936 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4937 | if (rc != LY_SUCCESS) { |
| 4938 | LY_ARRAY_FREE(patterns); |
| 4939 | return rc; |
| 4940 | } |
| 4941 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4942 | 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] | 4943 | pcre2_code_free((*pattern)->code); |
| 4944 | free(*pattern); |
| 4945 | LY_ARRAY_FREE(patterns); |
| 4946 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4947 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4948 | ly_err_free(err); |
| 4949 | return rc; |
| 4950 | } |
| 4951 | |
| 4952 | if (rc == LY_EVALID) { |
| 4953 | ly_err_free(err); |
| 4954 | set_fill_boolean(set, 0); |
| 4955 | } else { |
| 4956 | set_fill_boolean(set, 1); |
| 4957 | } |
| 4958 | |
| 4959 | return LY_SUCCESS; |
| 4960 | } |
| 4961 | |
| 4962 | /** |
| 4963 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4964 | * with the rounded first argument. For details refer to |
| 4965 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4966 | * |
| 4967 | * @param[in] args Array of arguments. |
| 4968 | * @param[in] arg_count Count of elements in @p args. |
| 4969 | * @param[in,out] set Context and result set at the same time. |
| 4970 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4971 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4972 | */ |
| 4973 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4974 | xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4975 | { |
| 4976 | struct lysc_node_leaf *sleaf; |
| 4977 | LY_ERR rc = LY_SUCCESS; |
| 4978 | |
| 4979 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4980 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4981 | 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] | 4982 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4983 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4984 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4985 | sleaf->name); |
| 4986 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4987 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4988 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4989 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4990 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4991 | return rc; |
| 4992 | } |
| 4993 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4994 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4995 | LY_CHECK_RET(rc); |
| 4996 | |
| 4997 | /* cover only the cases where floor can't be used */ |
| 4998 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4999 | set_fill_number(set, -0.0f); |
| 5000 | } else { |
| 5001 | args[0]->val.num += 0.5; |
| 5002 | rc = xpath_floor(args, 1, args[0], options); |
| 5003 | LY_CHECK_RET(rc); |
| 5004 | set_fill_number(set, args[0]->val.num); |
| 5005 | } |
| 5006 | |
| 5007 | return LY_SUCCESS; |
| 5008 | } |
| 5009 | |
| 5010 | /** |
| 5011 | * @brief Execute the XPath starts-with(string, string) function. |
| 5012 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 5013 | * the prefix of the first or not. |
| 5014 | * |
| 5015 | * @param[in] args Array of arguments. |
| 5016 | * @param[in] arg_count Count of elements in @p args. |
| 5017 | * @param[in,out] set Context and result set at the same time. |
| 5018 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5019 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5020 | */ |
| 5021 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5022 | xpath_starts_with(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5023 | { |
| 5024 | struct lysc_node_leaf *sleaf; |
| 5025 | LY_ERR rc = LY_SUCCESS; |
| 5026 | |
| 5027 | if (options & LYXP_SCNODE_ALL) { |
| 5028 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5029 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5030 | 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] | 5031 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5032 | 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] | 5033 | } |
| 5034 | } |
| 5035 | |
| 5036 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5037 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5038 | 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] | 5039 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5040 | 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] | 5041 | } |
| 5042 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5043 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5044 | return rc; |
| 5045 | } |
| 5046 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5047 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5048 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5049 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5050 | LY_CHECK_RET(rc); |
| 5051 | |
| 5052 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 5053 | set_fill_boolean(set, 0); |
| 5054 | } else { |
| 5055 | set_fill_boolean(set, 1); |
| 5056 | } |
| 5057 | |
| 5058 | return LY_SUCCESS; |
| 5059 | } |
| 5060 | |
| 5061 | /** |
| 5062 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 5063 | * with the string representation of either the argument or the context. |
| 5064 | * |
| 5065 | * @param[in] args Array of arguments. |
| 5066 | * @param[in] arg_count Count of elements in @p args. |
| 5067 | * @param[in,out] set Context and result set at the same time. |
| 5068 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5069 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5070 | */ |
| 5071 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5072 | xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5073 | { |
| 5074 | LY_ERR rc; |
| 5075 | |
| 5076 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5077 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5078 | return LY_SUCCESS; |
| 5079 | } |
| 5080 | |
| 5081 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5082 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5083 | LY_CHECK_RET(rc); |
| 5084 | set_fill_set(set, args[0]); |
| 5085 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5086 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5087 | LY_CHECK_RET(rc); |
| 5088 | } |
| 5089 | |
| 5090 | return LY_SUCCESS; |
| 5091 | } |
| 5092 | |
| 5093 | /** |
| 5094 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5095 | * with the length of the string in either the argument or the context. |
| 5096 | * |
| 5097 | * @param[in] args Array of arguments. |
| 5098 | * @param[in] arg_count Count of elements in @p args. |
| 5099 | * @param[in,out] set Context and result set at the same time. |
| 5100 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5101 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5102 | */ |
| 5103 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5104 | xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5105 | { |
| 5106 | struct lysc_node_leaf *sleaf; |
| 5107 | LY_ERR rc = LY_SUCCESS; |
| 5108 | |
| 5109 | if (options & LYXP_SCNODE_ALL) { |
| 5110 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5111 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5112 | 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] | 5113 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5114 | 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] | 5115 | } |
| 5116 | } |
| 5117 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5118 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5119 | 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] | 5120 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5121 | 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] | 5122 | } |
| 5123 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5124 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5125 | return rc; |
| 5126 | } |
| 5127 | |
| 5128 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5129 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5130 | LY_CHECK_RET(rc); |
| 5131 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5132 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5133 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5134 | LY_CHECK_RET(rc); |
| 5135 | set_fill_number(set, strlen(set->val.str)); |
| 5136 | } |
| 5137 | |
| 5138 | return LY_SUCCESS; |
| 5139 | } |
| 5140 | |
| 5141 | /** |
| 5142 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5143 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5144 | * on the second argument index ending on the third argument index, |
| 5145 | * indexed from 1. For exact definition refer to |
| 5146 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5147 | * |
| 5148 | * @param[in] args Array of arguments. |
| 5149 | * @param[in] arg_count Count of elements in @p args. |
| 5150 | * @param[in,out] set Context and result set at the same time. |
| 5151 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5152 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5153 | */ |
| 5154 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5155 | xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5156 | { |
Michal Vasko | 6db996e | 2022-07-28 10:28:04 +0200 | [diff] [blame] | 5157 | int64_t start; |
| 5158 | int32_t len; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5159 | uint32_t str_start, str_len, pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5160 | struct lysc_node_leaf *sleaf; |
| 5161 | LY_ERR rc = LY_SUCCESS; |
| 5162 | |
| 5163 | if (options & LYXP_SCNODE_ALL) { |
| 5164 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5165 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5166 | 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] | 5167 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5168 | 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] | 5169 | } |
| 5170 | } |
| 5171 | |
| 5172 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5173 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5174 | 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] | 5175 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5176 | 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] | 5177 | } |
| 5178 | } |
| 5179 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5180 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5181 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5182 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5183 | 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] | 5184 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5185 | 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] | 5186 | } |
| 5187 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5188 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5189 | return rc; |
| 5190 | } |
| 5191 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5192 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5193 | LY_CHECK_RET(rc); |
| 5194 | |
| 5195 | /* start */ |
| 5196 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5197 | return -1; |
| 5198 | } |
| 5199 | if (isfinite(args[1]->val.num)) { |
| 5200 | start = args[1]->val.num - 1; |
| 5201 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5202 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5203 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5204 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5205 | } |
| 5206 | |
| 5207 | /* len */ |
| 5208 | if (arg_count == 3) { |
| 5209 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5210 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5211 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5212 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5213 | } else if (isfinite(args[2]->val.num)) { |
| 5214 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5215 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5216 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5217 | } |
| 5218 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5219 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | /* find matching character positions */ |
| 5223 | str_start = 0; |
| 5224 | str_len = 0; |
| 5225 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5226 | if (pos < start) { |
| 5227 | ++str_start; |
| 5228 | } else if (pos < start + len) { |
| 5229 | ++str_len; |
| 5230 | } else { |
| 5231 | break; |
| 5232 | } |
| 5233 | } |
| 5234 | |
| 5235 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5236 | return LY_SUCCESS; |
| 5237 | } |
| 5238 | |
| 5239 | /** |
| 5240 | * @brief Execute the XPath substring-after(string, string) function. |
| 5241 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5242 | * of the second argument in the first or an empty string. |
| 5243 | * |
| 5244 | * @param[in] args Array of arguments. |
| 5245 | * @param[in] arg_count Count of elements in @p args. |
| 5246 | * @param[in,out] set Context and result set at the same time. |
| 5247 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5248 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5249 | */ |
| 5250 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5251 | xpath_substring_after(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5252 | { |
| 5253 | char *ptr; |
| 5254 | struct lysc_node_leaf *sleaf; |
| 5255 | LY_ERR rc = LY_SUCCESS; |
| 5256 | |
| 5257 | if (options & LYXP_SCNODE_ALL) { |
| 5258 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5259 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5260 | 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] | 5261 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5262 | 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] | 5263 | } |
| 5264 | } |
| 5265 | |
| 5266 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5267 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5268 | 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] | 5269 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5270 | 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] | 5271 | } |
| 5272 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5273 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5274 | return rc; |
| 5275 | } |
| 5276 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5277 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5278 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5279 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5280 | LY_CHECK_RET(rc); |
| 5281 | |
| 5282 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5283 | if (ptr) { |
| 5284 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5285 | } else { |
| 5286 | set_fill_string(set, "", 0); |
| 5287 | } |
| 5288 | |
| 5289 | return LY_SUCCESS; |
| 5290 | } |
| 5291 | |
| 5292 | /** |
| 5293 | * @brief Execute the XPath substring-before(string, string) function. |
| 5294 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5295 | * of the second argument in the first or an empty string. |
| 5296 | * |
| 5297 | * @param[in] args Array of arguments. |
| 5298 | * @param[in] arg_count Count of elements in @p args. |
| 5299 | * @param[in,out] set Context and result set at the same time. |
| 5300 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5301 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5302 | */ |
| 5303 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5304 | xpath_substring_before(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5305 | { |
| 5306 | char *ptr; |
| 5307 | struct lysc_node_leaf *sleaf; |
| 5308 | LY_ERR rc = LY_SUCCESS; |
| 5309 | |
| 5310 | if (options & LYXP_SCNODE_ALL) { |
| 5311 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5312 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5313 | 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] | 5314 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5315 | 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] | 5316 | } |
| 5317 | } |
| 5318 | |
| 5319 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5320 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5321 | 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] | 5322 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5323 | 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] | 5324 | } |
| 5325 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5326 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | return rc; |
| 5328 | } |
| 5329 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5330 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5331 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5332 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5333 | LY_CHECK_RET(rc); |
| 5334 | |
| 5335 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5336 | if (ptr) { |
| 5337 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5338 | } else { |
| 5339 | set_fill_string(set, "", 0); |
| 5340 | } |
| 5341 | |
| 5342 | return LY_SUCCESS; |
| 5343 | } |
| 5344 | |
| 5345 | /** |
| 5346 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5347 | * with the sum of all the nodes in the context. |
| 5348 | * |
| 5349 | * @param[in] args Array of arguments. |
| 5350 | * @param[in] arg_count Count of elements in @p args. |
| 5351 | * @param[in,out] set Context and result set at the same time. |
| 5352 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5353 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5354 | */ |
| 5355 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5356 | xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5357 | { |
| 5358 | long double num; |
| 5359 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5360 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5361 | struct lyxp_set set_item; |
| 5362 | struct lysc_node_leaf *sleaf; |
| 5363 | LY_ERR rc = LY_SUCCESS; |
| 5364 | |
| 5365 | if (options & LYXP_SCNODE_ALL) { |
| 5366 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5367 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5368 | 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] | 5369 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5370 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5371 | 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] | 5372 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5373 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5374 | 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] | 5375 | } |
| 5376 | } |
| 5377 | } |
| 5378 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5379 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5380 | return rc; |
| 5381 | } |
| 5382 | |
| 5383 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5384 | |
| 5385 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5386 | 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] | 5387 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5388 | } else if (!args[0]->used) { |
| 5389 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5390 | } |
| 5391 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5392 | set_init(&set_item, set); |
| 5393 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5394 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5395 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5396 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5397 | |
| 5398 | set_item.used = 1; |
| 5399 | set_item.size = 1; |
| 5400 | |
| 5401 | for (i = 0; i < args[0]->used; ++i) { |
| 5402 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5403 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5404 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5405 | LY_CHECK_RET(rc); |
| 5406 | num = cast_string_to_number(str); |
| 5407 | free(str); |
| 5408 | set->val.num += num; |
| 5409 | } |
| 5410 | |
| 5411 | free(set_item.val.nodes); |
| 5412 | |
| 5413 | return LY_SUCCESS; |
| 5414 | } |
| 5415 | |
| 5416 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5417 | * @brief Execute the XPath translate(string, string, string) function. |
| 5418 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5419 | * from the second argument replaced by those on the corresponding |
| 5420 | * positions in the third argument. |
| 5421 | * |
| 5422 | * @param[in] args Array of arguments. |
| 5423 | * @param[in] arg_count Count of elements in @p args. |
| 5424 | * @param[in,out] set Context and result set at the same time. |
| 5425 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5426 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5427 | */ |
| 5428 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5429 | xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5430 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5431 | uint32_t i, j, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5432 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5433 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5434 | struct lysc_node_leaf *sleaf; |
| 5435 | LY_ERR rc = LY_SUCCESS; |
| 5436 | |
| 5437 | if (options & LYXP_SCNODE_ALL) { |
| 5438 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5439 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5440 | 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] | 5441 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5442 | 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] | 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5447 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5448 | 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] | 5449 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5450 | 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] | 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5455 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5456 | 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] | 5457 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5458 | 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] | 5459 | } |
| 5460 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5461 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5462 | return rc; |
| 5463 | } |
| 5464 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5465 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5466 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5467 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5468 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5469 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5470 | LY_CHECK_RET(rc); |
| 5471 | |
| 5472 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5473 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5474 | new_used = 0; |
| 5475 | |
| 5476 | have_removed = 0; |
| 5477 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5478 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5479 | |
| 5480 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5481 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5482 | /* removing this char */ |
| 5483 | if (j >= strlen(args[2]->val.str)) { |
| 5484 | have_removed = 1; |
| 5485 | found = 1; |
| 5486 | break; |
| 5487 | } |
| 5488 | /* replacing this char */ |
| 5489 | new[new_used] = args[2]->val.str[j]; |
| 5490 | ++new_used; |
| 5491 | found = 1; |
| 5492 | break; |
| 5493 | } |
| 5494 | } |
| 5495 | |
| 5496 | /* copying this char */ |
| 5497 | if (!found) { |
| 5498 | new[new_used] = args[0]->val.str[i]; |
| 5499 | ++new_used; |
| 5500 | } |
| 5501 | } |
| 5502 | |
| 5503 | if (have_removed) { |
| 5504 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5505 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5506 | } |
| 5507 | new[new_used] = '\0'; |
| 5508 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5509 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5510 | set->type = LYXP_SET_STRING; |
| 5511 | set->val.str = new; |
| 5512 | |
| 5513 | return LY_SUCCESS; |
| 5514 | } |
| 5515 | |
| 5516 | /** |
| 5517 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5518 | * with true value. |
| 5519 | * |
| 5520 | * @param[in] args Array of arguments. |
| 5521 | * @param[in] arg_count Count of elements in @p args. |
| 5522 | * @param[in,out] set Context and result set at the same time. |
| 5523 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5524 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5525 | */ |
| 5526 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5527 | xpath_true(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5528 | { |
| 5529 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5530 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5531 | return LY_SUCCESS; |
| 5532 | } |
| 5533 | |
| 5534 | set_fill_boolean(set, 1); |
| 5535 | return LY_SUCCESS; |
| 5536 | } |
| 5537 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5538 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5539 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5540 | * with only nodes from the context. |
| 5541 | * |
| 5542 | * @param[in,out] set Context and result set at the same time. |
| 5543 | * @param[in] axis Axis to search on. |
| 5544 | * @param[in] options XPath options. |
| 5545 | * @return LY_ERR |
| 5546 | */ |
| 5547 | static LY_ERR |
| 5548 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5549 | { |
| 5550 | if (options & LYXP_SCNODE_ALL) { |
| 5551 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5552 | } |
| 5553 | |
| 5554 | if (set->type != LYXP_SET_NODE_SET) { |
| 5555 | lyxp_set_free_content(set); |
| 5556 | return LY_SUCCESS; |
| 5557 | } |
| 5558 | |
| 5559 | /* just like moving to a node with no restrictions */ |
| 5560 | return moveto_node(set, NULL, NULL, axis, options); |
| 5561 | } |
| 5562 | |
| 5563 | /** |
| 5564 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5565 | * with the text content of the nodes in the context. |
| 5566 | * |
| 5567 | * @param[in,out] set Context and result set at the same time. |
| 5568 | * @param[in] axis Axis to search on. |
| 5569 | * @param[in] options XPath options. |
| 5570 | * @return LY_ERR |
| 5571 | */ |
| 5572 | static LY_ERR |
| 5573 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5574 | { |
| 5575 | uint32_t i; |
| 5576 | |
| 5577 | if (options & LYXP_SCNODE_ALL) { |
| 5578 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5579 | return LY_SUCCESS; |
| 5580 | } |
| 5581 | |
| 5582 | if (set->type != LYXP_SET_NODE_SET) { |
| 5583 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5584 | return LY_EVALID; |
| 5585 | } |
| 5586 | |
| 5587 | if (axis != LYXP_AXIS_CHILD) { |
| 5588 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5589 | lyxp_set_free_content(set); |
| 5590 | return LY_SUCCESS; |
| 5591 | } |
| 5592 | |
| 5593 | for (i = 0; i < set->used; ++i) { |
| 5594 | switch (set->val.nodes[i].type) { |
| 5595 | case LYXP_NODE_NONE: |
| 5596 | LOGINT_RET(set->ctx); |
| 5597 | case LYXP_NODE_ELEM: |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 5598 | if (!set->val.nodes[i].node->schema || (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5599 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5600 | break; |
| 5601 | } |
| 5602 | /* fall through */ |
| 5603 | case LYXP_NODE_ROOT: |
| 5604 | case LYXP_NODE_ROOT_CONFIG: |
| 5605 | case LYXP_NODE_TEXT: |
| 5606 | case LYXP_NODE_META: |
| 5607 | set_remove_node_none(set, i); |
| 5608 | break; |
| 5609 | } |
| 5610 | } |
| 5611 | set_remove_nodes_none(set); |
| 5612 | |
| 5613 | return LY_SUCCESS; |
| 5614 | } |
| 5615 | |
| 5616 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5617 | * @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] | 5618 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5619 | * XPath @p set is expected to be a (sc)node set! |
| 5620 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5621 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5622 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5623 | * @param[in] set Set with general XPath context. |
| 5624 | * @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] | 5625 | * @param[out] moveto_mod Expected module of a matching node. |
| 5626 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5627 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5628 | static LY_ERR |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 5629 | moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5630 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5631 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5632 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5633 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5634 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5635 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5636 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5637 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5638 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5639 | /* specific module */ |
| 5640 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5641 | 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] | 5642 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5643 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5644 | if (!mod || !mod->implemented) { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 5645 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, (int)pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5646 | return LY_EVALID; |
| 5647 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5648 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5649 | *qname += pref_len + 1; |
| 5650 | *qname_len -= pref_len + 1; |
| 5651 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5652 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5653 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5654 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5655 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5656 | case LY_VALUE_SCHEMA: |
| 5657 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5658 | /* current module */ |
| 5659 | mod = set->cur_mod; |
| 5660 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5661 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5662 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5663 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5664 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5665 | /* inherit parent (context node) module */ |
| 5666 | if (ctx_scnode) { |
| 5667 | mod = ctx_scnode->module; |
| 5668 | } else { |
| 5669 | mod = NULL; |
| 5670 | } |
| 5671 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5672 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5673 | /* all nodes need to be prefixed */ |
| 5674 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5675 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5676 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5677 | } |
| 5678 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5679 | *moveto_mod = mod; |
| 5680 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5681 | } |
| 5682 | |
| 5683 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5684 | * @brief Move context @p set to the root. Handles absolute path. |
| 5685 | * Result is LYXP_SET_NODE_SET. |
| 5686 | * |
| 5687 | * @param[in,out] set Set to use. |
| 5688 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5689 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5690 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5691 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5692 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5693 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5694 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5695 | |
| 5696 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5697 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 5698 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5699 | } else { |
Michal Vasko | ef71b39 | 2023-08-16 09:45:23 +0200 | [diff] [blame] | 5700 | lyxp_set_free_content(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5701 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 5702 | set->non_child_axis = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5703 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5704 | |
| 5705 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | /** |
| 5709 | * @brief Check @p node as a part of NameTest processing. |
| 5710 | * |
| 5711 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5712 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5713 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5714 | * @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] | 5715 | * @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] | 5716 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5717 | * @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] | 5718 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5719 | */ |
| 5720 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5721 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5722 | 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] | 5723 | { |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5724 | const struct lysc_node *schema; |
| 5725 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5726 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5727 | assert(node_type == set->root_type); |
| 5728 | |
| 5729 | if (node_name || moveto_mod) { |
| 5730 | /* root will not match a specific node */ |
| 5731 | return LY_ENOT; |
| 5732 | } |
| 5733 | return LY_SUCCESS; |
| 5734 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5735 | /* other types will not match */ |
| 5736 | return LY_ENOT; |
| 5737 | } |
| 5738 | |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5739 | /* get schema node even of an opaque node */ |
| 5740 | schema = lyd_node_schema(node); |
| 5741 | if (!schema) { |
| 5742 | /* unknown opaque node never matches */ |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5743 | return LY_ENOT; |
| 5744 | } |
| 5745 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5746 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5747 | if (moveto_mod) { |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5748 | if ((set->ctx == LYD_CTX(node)) && (schema->module != moveto_mod)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5749 | return LY_ENOT; |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5750 | } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->module->name, moveto_mod->name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5751 | return LY_ENOT; |
| 5752 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5753 | } |
| 5754 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5755 | /* context check */ |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5756 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5757 | return LY_EINVAL; |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5758 | } else if (set->context_op && (schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5759 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5763 | if (node_name) { |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5764 | if ((set->ctx == LYD_CTX(node)) && (schema->name != node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5765 | return LY_ENOT; |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5766 | } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->name, node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5767 | return LY_ENOT; |
| 5768 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5769 | } |
| 5770 | |
Michal Vasko | 6d65712 | 2022-10-19 14:38:35 +0200 | [diff] [blame] | 5771 | /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */ |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5772 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(schema) && !(node->flags & LYD_WHEN_TRUE) && |
Michal Vasko | 6d65712 | 2022-10-19 14:38:35 +0200 | [diff] [blame] | 5773 | (node != set->cur_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5774 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5775 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5776 | |
| 5777 | /* match */ |
| 5778 | return LY_SUCCESS; |
| 5779 | } |
| 5780 | |
| 5781 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5782 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5783 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5784 | * @param[in] iter Last returned node. |
| 5785 | * @param[in] stop Node to stop the search on and not return. |
| 5786 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5787 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5788 | static const struct lyd_node * |
| 5789 | 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] | 5790 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5791 | const struct lyd_node *next = NULL; |
| 5792 | |
| 5793 | /* 1) child */ |
| 5794 | next = lyd_child(iter); |
| 5795 | if (!next) { |
| 5796 | if (iter == stop) { |
| 5797 | /* reached stop, no more descendants */ |
| 5798 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5799 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5800 | /* 2) child next sibling */ |
| 5801 | next = iter->next; |
| 5802 | } |
| 5803 | while (!next) { |
| 5804 | iter = lyd_parent(iter); |
| 5805 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5806 | return NULL; |
| 5807 | } |
| 5808 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5809 | } |
| 5810 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5811 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5815 | * @brief Get the next node in a backward DFS. |
| 5816 | * |
| 5817 | * @param[in] iter Last returned node. |
| 5818 | * @param[in] stop Node to stop the search on and not return. |
| 5819 | * @return Next node, NULL if there are no more. |
| 5820 | */ |
| 5821 | static const struct lyd_node * |
| 5822 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5823 | { |
| 5824 | const struct lyd_node *next = NULL; |
| 5825 | |
| 5826 | /* 1) previous sibling innermost last child */ |
| 5827 | next = iter->prev->next ? iter->prev : NULL; |
| 5828 | while (next && lyd_child(next)) { |
| 5829 | next = lyd_child(next); |
| 5830 | next = next->prev; |
| 5831 | } |
| 5832 | |
| 5833 | if (!next) { |
| 5834 | /* 2) parent */ |
| 5835 | iter = lyd_parent(iter); |
| 5836 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5837 | return NULL; |
| 5838 | } |
| 5839 | next = iter; |
| 5840 | } |
| 5841 | |
| 5842 | return next; |
| 5843 | } |
| 5844 | |
| 5845 | /** |
| 5846 | * @brief Get the first node on an axis for a context node. |
| 5847 | * |
| 5848 | * @param[in,out] iter NULL, updated to the next node. |
| 5849 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5850 | * @param[in] node Context node. |
| 5851 | * @param[in] node_type Type of @p node. |
| 5852 | * @param[in] axis Axis to use. |
| 5853 | * @param[in] set XPath set with the general context. |
| 5854 | * @return LY_SUCCESS on success. |
| 5855 | * @return LY_ENOTFOUND if no next node found. |
| 5856 | */ |
| 5857 | static LY_ERR |
| 5858 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5859 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5860 | { |
| 5861 | const struct lyd_node *next = NULL; |
| 5862 | enum lyxp_node_type next_type = 0; |
| 5863 | |
| 5864 | assert(!*iter); |
| 5865 | assert(!*iter_type); |
| 5866 | |
| 5867 | switch (axis) { |
| 5868 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5869 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5870 | case LYXP_AXIS_SELF: |
| 5871 | /* return the context node */ |
| 5872 | next = node; |
| 5873 | next_type = node_type; |
| 5874 | break; |
| 5875 | |
| 5876 | case LYXP_AXIS_ANCESTOR: |
| 5877 | case LYXP_AXIS_PARENT: |
| 5878 | if (node_type == LYXP_NODE_ELEM) { |
| 5879 | next = lyd_parent(node); |
| 5880 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5881 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5882 | next = node; |
| 5883 | next_type = LYXP_NODE_ELEM; |
| 5884 | } else if (node_type == LYXP_NODE_META) { |
| 5885 | next = ((struct lyd_meta *)node)->parent; |
| 5886 | next_type = LYXP_NODE_ELEM; |
| 5887 | } /* else root does not have a parent */ |
| 5888 | break; |
| 5889 | |
| 5890 | case LYXP_AXIS_CHILD: |
| 5891 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5892 | assert(!node); |
| 5893 | |
| 5894 | /* search in all the trees */ |
| 5895 | next = set->tree; |
| 5896 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5897 | } else { |
| 5898 | /* search in children */ |
| 5899 | next = lyd_child(node); |
| 5900 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5901 | } |
| 5902 | break; |
| 5903 | |
| 5904 | case LYXP_AXIS_DESCENDANT: |
| 5905 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5906 | /* top-level nodes */ |
| 5907 | next = set->tree; |
| 5908 | next_type = LYXP_NODE_ELEM; |
| 5909 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5910 | /* start from the context node */ |
| 5911 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5912 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5913 | } /* else no children */ |
| 5914 | break; |
| 5915 | |
| 5916 | case LYXP_AXIS_FOLLOWING: |
| 5917 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5918 | if (node_type == LYXP_NODE_ELEM) { |
| 5919 | /* first next sibling */ |
| 5920 | next = node->next; |
| 5921 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5922 | } /* else no sibling */ |
| 5923 | break; |
| 5924 | |
| 5925 | case LYXP_AXIS_PRECEDING: |
| 5926 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5927 | /* skip ancestors */ |
| 5928 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5929 | assert(next); |
| 5930 | next_type = LYXP_NODE_ELEM; |
| 5931 | } /* else no sibling */ |
| 5932 | break; |
| 5933 | |
| 5934 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5935 | if (node_type == LYXP_NODE_ELEM) { |
| 5936 | /* first previous sibling */ |
| 5937 | next = node->prev->next ? node->prev : NULL; |
| 5938 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5939 | } /* else no sibling */ |
| 5940 | break; |
| 5941 | |
| 5942 | case LYXP_AXIS_ATTRIBUTE: |
| 5943 | /* handled specially */ |
| 5944 | assert(0); |
| 5945 | LOGINT(set->ctx); |
| 5946 | break; |
| 5947 | } |
| 5948 | |
| 5949 | *iter = next; |
| 5950 | *iter_type = next_type; |
| 5951 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5952 | } |
| 5953 | |
| 5954 | /** |
| 5955 | * @brief Iterate over all nodes on an axis for a context node. |
| 5956 | * |
| 5957 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5958 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5959 | * @param[in] node Context node. |
| 5960 | * @param[in] node_type Type of @p node. |
| 5961 | * @param[in] axis Axis to use. |
| 5962 | * @param[in] set XPath set with the general context. |
| 5963 | * @return LY_SUCCESS on success. |
| 5964 | * @return LY_ENOTFOUND if no next node found. |
| 5965 | */ |
| 5966 | static LY_ERR |
| 5967 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5968 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5969 | { |
| 5970 | const struct lyd_node *next = NULL; |
| 5971 | enum lyxp_node_type next_type = 0; |
| 5972 | |
| 5973 | if (!*iter_type) { |
| 5974 | /* first returned node */ |
| 5975 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5976 | } |
| 5977 | |
| 5978 | switch (axis) { |
| 5979 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5980 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5981 | /* fake first ancestor, we returned self before */ |
| 5982 | *iter = NULL; |
| 5983 | *iter_type = 0; |
| 5984 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 5985 | } /* else continue ancestor */ |
| 5986 | |
| 5987 | /* fallthrough */ |
| 5988 | case LYXP_AXIS_ANCESTOR: |
| 5989 | if (*iter_type == LYXP_NODE_ELEM) { |
| 5990 | /* iter parent */ |
| 5991 | next = lyd_parent(*iter); |
| 5992 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5993 | } /* else root, no ancestors */ |
| 5994 | break; |
| 5995 | |
| 5996 | case LYXP_AXIS_CHILD: |
| 5997 | assert(*iter_type == LYXP_NODE_ELEM); |
| 5998 | |
| 5999 | /* next sibling (child) */ |
| 6000 | next = (*iter)->next; |
| 6001 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6002 | break; |
| 6003 | |
| 6004 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6005 | if ((*iter == node) && (*iter_type == node_type)) { |
| 6006 | /* fake first descendant, we returned self before */ |
| 6007 | *iter = NULL; |
| 6008 | *iter_type = 0; |
| 6009 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 6010 | } /* else continue descendant */ |
| 6011 | |
| 6012 | /* fallthrough */ |
| 6013 | case LYXP_AXIS_DESCENDANT: |
| 6014 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6015 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 6016 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6017 | break; |
| 6018 | |
| 6019 | case LYXP_AXIS_FOLLOWING: |
| 6020 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6021 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 6022 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6023 | break; |
| 6024 | |
| 6025 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6026 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6027 | |
| 6028 | /* next sibling */ |
| 6029 | next = (*iter)->next; |
| 6030 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6031 | break; |
| 6032 | |
| 6033 | case LYXP_AXIS_PARENT: |
| 6034 | case LYXP_AXIS_SELF: |
| 6035 | /* parent/self was returned before */ |
| 6036 | break; |
| 6037 | |
| 6038 | case LYXP_AXIS_PRECEDING: |
| 6039 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6040 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 6041 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6042 | break; |
| 6043 | |
| 6044 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6045 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6046 | |
| 6047 | /* previous sibling */ |
| 6048 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 6049 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6050 | break; |
| 6051 | |
| 6052 | case LYXP_AXIS_ATTRIBUTE: |
| 6053 | /* handled specially */ |
| 6054 | assert(0); |
| 6055 | LOGINT(set->ctx); |
| 6056 | break; |
| 6057 | } |
| 6058 | |
| 6059 | *iter = next; |
| 6060 | *iter_type = next_type; |
| 6061 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6062 | } |
| 6063 | |
| 6064 | /** |
| 6065 | * @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] | 6066 | * |
| 6067 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6068 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6069 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6070 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6071 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6072 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6073 | */ |
| 6074 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6075 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6076 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6077 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6078 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6079 | const struct lyd_node *iter; |
| 6080 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6081 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6082 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6083 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6084 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6085 | return LY_SUCCESS; |
| 6086 | } |
| 6087 | |
| 6088 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6089 | 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] | 6090 | return LY_EVALID; |
| 6091 | } |
| 6092 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6093 | /* init result set */ |
| 6094 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6095 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6096 | for (i = 0; i < set->used; ++i) { |
| 6097 | /* iterate over all the nodes on the axis of the node */ |
| 6098 | iter = NULL; |
| 6099 | iter_type = 0; |
| 6100 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6101 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6102 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6103 | rc = r; |
| 6104 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6105 | } else if (r) { |
| 6106 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6107 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6108 | |
| 6109 | /* check for duplicates if they are possible */ |
| 6110 | switch (axis) { |
| 6111 | case LYXP_AXIS_ANCESTOR: |
| 6112 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6113 | case LYXP_AXIS_DESCENDANT: |
| 6114 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6115 | case LYXP_AXIS_FOLLOWING: |
| 6116 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6117 | case LYXP_AXIS_PARENT: |
| 6118 | case LYXP_AXIS_PRECEDING: |
| 6119 | case LYXP_AXIS_PRECEDING_SIBLING: |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6120 | result.non_child_axis = 1; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6121 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6122 | continue; |
| 6123 | } |
| 6124 | break; |
| 6125 | case LYXP_AXIS_CHILD: |
| 6126 | case LYXP_AXIS_SELF: |
| 6127 | break; |
| 6128 | case LYXP_AXIS_ATTRIBUTE: |
| 6129 | /* handled specially */ |
| 6130 | assert(0); |
| 6131 | LOGINT(set->ctx); |
| 6132 | break; |
| 6133 | } |
| 6134 | |
| 6135 | /* matching node */ |
| 6136 | set_insert_node(&result, iter, 0, iter_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6137 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6138 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6139 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6140 | /* move result to the set */ |
| 6141 | lyxp_set_free_content(set); |
| 6142 | *set = result; |
| 6143 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6144 | |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6145 | /* sort the final set if the document order could have been broken */ |
| 6146 | if (set->non_child_axis) { |
| 6147 | set_sort(set); |
| 6148 | } else { |
| 6149 | assert(!set_sort(set)); |
| 6150 | } |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6151 | |
| 6152 | cleanup: |
| 6153 | lyxp_set_free_content(&result); |
| 6154 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6155 | } |
| 6156 | |
| 6157 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6158 | * @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] | 6159 | * |
| 6160 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6161 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6162 | * @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] | 6163 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6164 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6165 | */ |
| 6166 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6167 | 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] | 6168 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6169 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6170 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6171 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6172 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6173 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6174 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6175 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6176 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6177 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6178 | /* init result set */ |
| 6179 | set_init(&result, set); |
| 6180 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6181 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6182 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6183 | } |
| 6184 | |
| 6185 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6186 | 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] | 6187 | ret = LY_EVALID; |
| 6188 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6189 | } |
| 6190 | |
| 6191 | /* context check for all the nodes since we have the schema node */ |
| 6192 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6193 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6194 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6195 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6196 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6197 | lyxp_set_free_content(set); |
| 6198 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6199 | } |
| 6200 | |
| 6201 | /* create specific data instance if needed */ |
| 6202 | if (scnode->nodetype == LYS_LIST) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 6203 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6204 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6205 | 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] | 6206 | } |
| 6207 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6208 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6209 | siblings = NULL; |
| 6210 | |
| 6211 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6212 | assert(!set->val.nodes[i].node); |
| 6213 | |
| 6214 | /* search in all the trees */ |
| 6215 | siblings = set->tree; |
| 6216 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6217 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6218 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6222 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6223 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6224 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6225 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6226 | } |
Michal Vasko | d8a5201 | 2023-08-15 11:38:10 +0200 | [diff] [blame] | 6227 | if (r == LY_ENOTFOUND) { |
| 6228 | /* may still be an opaque node */ |
| 6229 | r = lyd_find_sibling_opaq_next(siblings, scnode->name, &sub); |
| 6230 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6231 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6232 | |
| 6233 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6234 | 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] | 6235 | ret = LY_EINCOMPLETE; |
| 6236 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6237 | } |
| 6238 | |
| 6239 | if (sub) { |
| 6240 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6241 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6242 | } |
| 6243 | } |
| 6244 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6245 | /* move result to the set */ |
| 6246 | lyxp_set_free_content(set); |
| 6247 | *set = result; |
| 6248 | result.type = LYXP_SET_NUMBER; |
| 6249 | assert(!set_sort(set)); |
| 6250 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6251 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6252 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6253 | lyd_free_tree(inst); |
| 6254 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6255 | } |
| 6256 | |
| 6257 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6258 | * @brief Check @p node as a part of schema NameTest processing. |
| 6259 | * |
| 6260 | * @param[in] node Schema node to check. |
| 6261 | * @param[in] ctx_scnode Context node. |
| 6262 | * @param[in] set Set to read general context from. |
| 6263 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6264 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6265 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6266 | */ |
| 6267 | static LY_ERR |
| 6268 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6269 | const char *node_name, const struct lys_module *moveto_mod) |
| 6270 | { |
| 6271 | if (!moveto_mod && node_name) { |
| 6272 | switch (set->format) { |
| 6273 | case LY_VALUE_SCHEMA: |
| 6274 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6275 | /* use current module */ |
| 6276 | moveto_mod = set->cur_mod; |
| 6277 | break; |
| 6278 | case LY_VALUE_JSON: |
| 6279 | case LY_VALUE_LYB: |
| 6280 | case LY_VALUE_STR_NS: |
| 6281 | /* inherit module of the context node, if any */ |
| 6282 | if (ctx_scnode) { |
| 6283 | moveto_mod = ctx_scnode->module; |
| 6284 | } |
| 6285 | break; |
| 6286 | case LY_VALUE_CANON: |
| 6287 | case LY_VALUE_XML: |
| 6288 | /* not defined */ |
| 6289 | LOGINT(set->ctx); |
| 6290 | return LY_EINVAL; |
| 6291 | } |
| 6292 | } |
| 6293 | |
| 6294 | if (!node) { |
| 6295 | /* root will not match a specific node */ |
| 6296 | if (node_name || moveto_mod) { |
| 6297 | return LY_ENOT; |
| 6298 | } |
| 6299 | return LY_SUCCESS; |
| 6300 | } |
| 6301 | |
| 6302 | /* module check */ |
| 6303 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6304 | return LY_ENOT; |
| 6305 | } |
| 6306 | |
| 6307 | /* context check */ |
| 6308 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6309 | return LY_EINVAL; |
| 6310 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6311 | return LY_EINVAL; |
| 6312 | } |
| 6313 | |
| 6314 | /* name check */ |
| 6315 | if (node_name && (node->name != node_name)) { |
| 6316 | return LY_ENOT; |
| 6317 | } |
| 6318 | |
| 6319 | /* match */ |
| 6320 | return LY_SUCCESS; |
| 6321 | } |
| 6322 | |
| 6323 | /** |
| 6324 | * @brief Get the next node in a forward schema node DFS. |
| 6325 | * |
| 6326 | * @param[in] iter Last returned node. |
| 6327 | * @param[in] stop Node to stop the search on and not return. |
| 6328 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6329 | * @return Next node, NULL if there are no more. |
| 6330 | */ |
| 6331 | static const struct lysc_node * |
| 6332 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6333 | { |
| 6334 | const struct lysc_node *next = NULL; |
| 6335 | |
| 6336 | next = lysc_node_child(iter); |
| 6337 | if (!next) { |
| 6338 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6339 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6340 | /* we are done, no next element to process */ |
| 6341 | return NULL; |
| 6342 | } |
| 6343 | |
| 6344 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6345 | } |
| 6346 | while (!next && iter) { |
| 6347 | /* parent is already processed, go to its sibling */ |
| 6348 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6349 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6350 | /* we are done, no next element to process */ |
| 6351 | return NULL; |
| 6352 | } |
| 6353 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6354 | } |
| 6355 | |
| 6356 | return next; |
| 6357 | } |
| 6358 | |
| 6359 | /** |
| 6360 | * @brief Consider schema node based on its in_ctx enum value. |
| 6361 | * |
| 6362 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6363 | * @param[in] axis Axis to use. |
| 6364 | * @return LY_SUCCESS on success. |
| 6365 | * @return LY_ENOT if the node should not be returned. |
| 6366 | */ |
| 6367 | static LY_ERR |
| 6368 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6369 | { |
| 6370 | switch (axis) { |
| 6371 | case LYXP_AXIS_SELF: |
| 6372 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6373 | /* additionally put the start node into context */ |
| 6374 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6375 | return LY_SUCCESS; |
| 6376 | } |
| 6377 | break; |
| 6378 | case LYXP_AXIS_PARENT: |
| 6379 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6380 | case LYXP_AXIS_ANCESTOR: |
| 6381 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6382 | case LYXP_AXIS_DESCENDANT: |
| 6383 | case LYXP_AXIS_FOLLOWING: |
| 6384 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6385 | case LYXP_AXIS_PRECEDING: |
| 6386 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6387 | case LYXP_AXIS_CHILD: |
| 6388 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6389 | /* remember that context node was used */ |
| 6390 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6391 | return LY_SUCCESS; |
| 6392 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6393 | /* traversed */ |
| 6394 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6395 | return LY_SUCCESS; |
| 6396 | } |
| 6397 | break; |
| 6398 | case LYXP_AXIS_ATTRIBUTE: |
| 6399 | /* unreachable */ |
| 6400 | assert(0); |
| 6401 | LOGINT(NULL); |
| 6402 | break; |
| 6403 | } |
| 6404 | |
| 6405 | return LY_ENOT; |
| 6406 | } |
| 6407 | |
| 6408 | /** |
| 6409 | * @brief Get previous sibling for a schema node. |
| 6410 | * |
| 6411 | * @param[in] scnode Schema node. |
| 6412 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6413 | * @return Previous sibling, NULL if none. |
| 6414 | */ |
| 6415 | static const struct lysc_node * |
| 6416 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6417 | { |
| 6418 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6419 | |
| 6420 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6421 | if (next == scnode) { |
| 6422 | break; |
| 6423 | } |
| 6424 | |
| 6425 | prev = next; |
| 6426 | } |
| 6427 | |
| 6428 | return prev; |
| 6429 | } |
| 6430 | |
| 6431 | /** |
| 6432 | * @brief Get the first schema node on an axis for a context node. |
| 6433 | * |
| 6434 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6435 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6436 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6437 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6438 | * @param[in] scnode Context node. |
| 6439 | * @param[in] node_type Type of @p scnode. |
| 6440 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6441 | * @param[in] axis Axis to use. |
| 6442 | * @param[in] set XPath set with the general context. |
| 6443 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6444 | * @return LY_SUCCESS on success. |
| 6445 | * @return LY_ENOTFOUND if no next node found. |
| 6446 | */ |
| 6447 | static LY_ERR |
| 6448 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6449 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6450 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6451 | { |
| 6452 | const struct lysc_node *next = NULL; |
| 6453 | enum lyxp_node_type next_type = 0; |
| 6454 | |
| 6455 | assert(!*iter); |
| 6456 | assert(!*iter_type); |
| 6457 | |
| 6458 | *iter_mod = NULL; |
| 6459 | *iter_mod_idx = 0; |
| 6460 | |
| 6461 | switch (axis) { |
| 6462 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6463 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6464 | case LYXP_AXIS_SELF: |
| 6465 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6466 | /* just return the node */ |
| 6467 | next = scnode; |
| 6468 | next_type = node_type; |
| 6469 | } |
| 6470 | break; |
| 6471 | |
| 6472 | case LYXP_AXIS_ANCESTOR: |
| 6473 | case LYXP_AXIS_PARENT: |
| 6474 | if (node_type == LYXP_NODE_ELEM) { |
| 6475 | next = lysc_data_parent(scnode); |
| 6476 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6477 | } /* else no parent */ |
| 6478 | break; |
| 6479 | |
| 6480 | case LYXP_AXIS_DESCENDANT: |
| 6481 | case LYXP_AXIS_CHILD: |
| 6482 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6483 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6484 | * it can be in a top-level augment */ |
| 6485 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6486 | /* module may not be implemented or not compiled yet */ |
| 6487 | if (!(*iter_mod)->compiled) { |
| 6488 | continue; |
| 6489 | } |
| 6490 | |
| 6491 | /* get next node */ |
| 6492 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6493 | next_type = LYXP_NODE_ELEM; |
| 6494 | break; |
| 6495 | } |
| 6496 | } |
| 6497 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6498 | /* get next node */ |
| 6499 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6500 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6501 | } |
| 6502 | break; |
| 6503 | |
| 6504 | case LYXP_AXIS_FOLLOWING: |
| 6505 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6506 | if (node_type == LYXP_NODE_ELEM) { |
| 6507 | /* first next sibling */ |
| 6508 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6509 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6510 | } /* else no sibling */ |
| 6511 | break; |
| 6512 | |
| 6513 | case LYXP_AXIS_PRECEDING: |
| 6514 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6515 | if (node_type == LYXP_NODE_ELEM) { |
| 6516 | /* first parent sibling */ |
| 6517 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6518 | if (next == scnode) { |
| 6519 | /* no preceding sibling */ |
| 6520 | next = NULL; |
| 6521 | } |
| 6522 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6523 | } /* else no sibling */ |
| 6524 | break; |
| 6525 | |
| 6526 | case LYXP_AXIS_ATTRIBUTE: |
| 6527 | /* unreachable */ |
| 6528 | assert(0); |
| 6529 | LOGINT(set->ctx); |
| 6530 | break; |
| 6531 | } |
| 6532 | |
| 6533 | *iter = next; |
| 6534 | *iter_type = next_type; |
| 6535 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6536 | } |
| 6537 | |
| 6538 | /** |
| 6539 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6540 | * |
| 6541 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6542 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6543 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6544 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6545 | * @param[in] scnode Context node. |
| 6546 | * @param[in] node_type Type of @p scnode. |
| 6547 | * @param[in] axis Axis to use. |
| 6548 | * @param[in] set XPath set with the general context. |
| 6549 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6550 | * @return LY_SUCCESS on success. |
| 6551 | * @return LY_ENOTFOUND if no next node found. |
| 6552 | */ |
| 6553 | static LY_ERR |
| 6554 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6555 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6556 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6557 | { |
| 6558 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6559 | enum lyxp_node_type next_type = 0; |
| 6560 | |
| 6561 | if (!*iter_type) { |
| 6562 | /* first returned node */ |
| 6563 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6564 | getnext_opts); |
| 6565 | } |
| 6566 | |
| 6567 | switch (axis) { |
| 6568 | case LYXP_AXIS_PARENT: |
| 6569 | case LYXP_AXIS_SELF: |
| 6570 | /* parent/self was returned before */ |
| 6571 | break; |
| 6572 | |
| 6573 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6574 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6575 | /* fake first ancestor, we returned self before */ |
| 6576 | *iter = NULL; |
| 6577 | *iter_type = 0; |
| 6578 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6579 | LYXP_AXIS_ANCESTOR, set, getnext_opts); |
| 6580 | } /* else continue ancestor */ |
| 6581 | |
| 6582 | /* fallthrough */ |
| 6583 | case LYXP_AXIS_ANCESTOR: |
| 6584 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6585 | next = lysc_data_parent(*iter); |
| 6586 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6587 | } /* else no ancestor */ |
| 6588 | break; |
| 6589 | |
| 6590 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6591 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6592 | /* fake first descendant, we returned self before */ |
| 6593 | *iter = NULL; |
| 6594 | *iter_type = 0; |
| 6595 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6596 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6597 | } /* else DFS until context node */ |
| 6598 | dfs_stop = scnode; |
| 6599 | |
| 6600 | /* fallthrough */ |
| 6601 | case LYXP_AXIS_DESCENDANT: |
| 6602 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6603 | /* DFS until the context node */ |
| 6604 | dfs_stop = scnode; |
| 6605 | } |
| 6606 | |
| 6607 | /* fallthrough */ |
| 6608 | case LYXP_AXIS_PRECEDING: |
| 6609 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6610 | /* DFS until the previous sibling */ |
| 6611 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6612 | assert(dfs_stop); |
| 6613 | |
| 6614 | if (*iter == dfs_stop) { |
| 6615 | /* we are done */ |
| 6616 | break; |
| 6617 | } |
| 6618 | } |
| 6619 | |
| 6620 | /* fallthrough */ |
| 6621 | case LYXP_AXIS_FOLLOWING: |
| 6622 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6623 | /* DFS through the whole module */ |
| 6624 | dfs_stop = NULL; |
| 6625 | } |
| 6626 | |
| 6627 | /* nested nodes */ |
| 6628 | assert(*iter); |
| 6629 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6630 | if (next) { |
| 6631 | next_type = LYXP_NODE_ELEM; |
| 6632 | break; |
| 6633 | } /* else get next top-level node just like a child */ |
| 6634 | |
| 6635 | /* fallthrough */ |
| 6636 | case LYXP_AXIS_CHILD: |
| 6637 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6638 | if (!*iter_mod) { |
| 6639 | /* nodes from a single module */ |
| 6640 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6641 | next_type = LYXP_NODE_ELEM; |
| 6642 | break; |
| 6643 | } |
| 6644 | |
| 6645 | assert(scnode); |
Michal Vasko | a353cce | 2022-11-14 10:09:55 +0100 | [diff] [blame] | 6646 | if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6647 | /* iterating over top-level nodes, find next */ |
| 6648 | while (lysc_data_parent(*iter)) { |
| 6649 | *iter = lysc_data_parent(*iter); |
| 6650 | } |
| 6651 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6652 | next_type = LYXP_NODE_ELEM; |
| 6653 | break; |
| 6654 | } |
| 6655 | } |
| 6656 | } |
| 6657 | |
| 6658 | while (*iter_mod) { |
| 6659 | /* module top-level nodes */ |
| 6660 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6661 | next_type = LYXP_NODE_ELEM; |
| 6662 | break; |
| 6663 | } |
| 6664 | |
| 6665 | /* get next module */ |
| 6666 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6667 | /* module may not be implemented or not compiled yet */ |
| 6668 | if ((*iter_mod)->compiled) { |
| 6669 | break; |
| 6670 | } |
| 6671 | } |
| 6672 | |
| 6673 | /* new module, start over */ |
| 6674 | *iter = NULL; |
| 6675 | } |
| 6676 | break; |
| 6677 | |
| 6678 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6679 | assert(*iter); |
| 6680 | |
| 6681 | /* next parent sibling until scnode */ |
| 6682 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6683 | if (next == scnode) { |
| 6684 | /* no previous sibling */ |
| 6685 | next = NULL; |
| 6686 | } |
| 6687 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6688 | break; |
| 6689 | |
| 6690 | case LYXP_AXIS_ATTRIBUTE: |
| 6691 | /* unreachable */ |
| 6692 | assert(0); |
| 6693 | LOGINT(set->ctx); |
| 6694 | break; |
| 6695 | } |
| 6696 | |
| 6697 | *iter = next; |
| 6698 | *iter_type = next_type; |
| 6699 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6700 | } |
| 6701 | |
| 6702 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6703 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6704 | * |
| 6705 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6706 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6707 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6708 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6709 | * @param[in] options XPath options. |
| 6710 | * @return LY_ERR |
| 6711 | */ |
| 6712 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6713 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6714 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6715 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6716 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6717 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 6718 | const struct lys_module *mod = NULL; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6719 | const struct lysc_node *iter; |
| 6720 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6721 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6722 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6723 | return LY_SUCCESS; |
| 6724 | } |
| 6725 | |
| 6726 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6727 | 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] | 6728 | return LY_EVALID; |
| 6729 | } |
| 6730 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6731 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6732 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6733 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6734 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6735 | } |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 6736 | if (options & LYXP_SCNODE_SCHEMAMOUNT) { |
| 6737 | getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT; |
| 6738 | } |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6739 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6740 | orig_used = set->used; |
| 6741 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6742 | /* update in_ctx first */ |
| 6743 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6744 | /* not usable, skip */ |
| 6745 | continue; |
| 6746 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6747 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6748 | iter = NULL; |
| 6749 | iter_type = 0; |
| 6750 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6751 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6752 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6753 | continue; |
| 6754 | } |
| 6755 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6756 | /* insert */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6757 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, axis, &idx)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6758 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6759 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6760 | if ((idx < orig_used) && (idx > i)) { |
| 6761 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6762 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6763 | } |
| 6764 | } |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 6765 | |
| 6766 | if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) && |
| 6767 | (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode, |
| 6768 | moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) { |
| 6769 | /* there is a matching node from an extension, use it */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6770 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx)); |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 6771 | if ((idx < orig_used) && (idx > i)) { |
| 6772 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6773 | temp_ctx = 1; |
| 6774 | } |
| 6775 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6776 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6777 | |
| 6778 | /* correct temporary in_ctx values */ |
| 6779 | if (temp_ctx) { |
| 6780 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6781 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6782 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6783 | } |
| 6784 | } |
| 6785 | } |
| 6786 | |
| 6787 | return LY_SUCCESS; |
| 6788 | } |
| 6789 | |
| 6790 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6791 | * @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] | 6792 | * Context position aware. |
| 6793 | * |
| 6794 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6795 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6796 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6797 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6798 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6799 | */ |
| 6800 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6801 | 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] | 6802 | { |
| 6803 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6804 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6805 | struct lyxp_set ret_set; |
| 6806 | LY_ERR rc; |
| 6807 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6808 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6809 | return LY_SUCCESS; |
| 6810 | } |
| 6811 | |
| 6812 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6813 | 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] | 6814 | return LY_EVALID; |
| 6815 | } |
| 6816 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6817 | /* 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] | 6818 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6819 | LY_CHECK_RET(rc); |
| 6820 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6821 | /* 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] | 6822 | set_init(&ret_set, set); |
| 6823 | for (i = 0; i < set->used; ++i) { |
| 6824 | |
| 6825 | /* TREE DFS */ |
| 6826 | start = set->val.nodes[i].node; |
| 6827 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6828 | 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] | 6829 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6830 | /* add matching node into result set */ |
| 6831 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6832 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6833 | /* the node is a duplicate, we'll process it later in the set */ |
| 6834 | goto skip_children; |
| 6835 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6836 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6837 | return rc; |
| 6838 | } else if (rc == LY_EINVAL) { |
| 6839 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | /* TREE DFS NEXT ELEM */ |
| 6843 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6844 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6845 | if (!next) { |
| 6846 | skip_children: |
| 6847 | /* no children, so try siblings, but only if it's not the start, |
| 6848 | * that is considered to be the root and it's siblings are not traversed */ |
| 6849 | if (elem != start) { |
| 6850 | next = elem->next; |
| 6851 | } else { |
| 6852 | break; |
| 6853 | } |
| 6854 | } |
| 6855 | while (!next) { |
| 6856 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6857 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6858 | /* we are done, no next element to process */ |
| 6859 | break; |
| 6860 | } |
| 6861 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6862 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6863 | next = elem->next; |
| 6864 | } |
| 6865 | } |
| 6866 | } |
| 6867 | |
| 6868 | /* make the temporary set the current one */ |
| 6869 | ret_set.ctx_pos = set->ctx_pos; |
| 6870 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6871 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6872 | memcpy(set, &ret_set, sizeof *set); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6873 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6874 | |
| 6875 | return LY_SUCCESS; |
| 6876 | } |
| 6877 | |
| 6878 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6879 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6880 | * Result is LYXP_SET_NODE_SET. |
| 6881 | * |
| 6882 | * @param[in] set Set to use. |
| 6883 | * @param[in] start Start node whose subtree to add. |
| 6884 | * @param[in] start_idx Index of @p start in @p set. |
| 6885 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6886 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6887 | * @param[in] options XPath options. |
| 6888 | * @return LY_ERR value. |
| 6889 | */ |
| 6890 | static LY_ERR |
| 6891 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6892 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6893 | { |
| 6894 | const struct lysc_node *next, *elem; |
| 6895 | uint32_t idx; |
| 6896 | LY_ERR rc; |
| 6897 | |
| 6898 | /* TREE DFS */ |
| 6899 | for (elem = next = start; elem; elem = next) { |
| 6900 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6901 | /* schema-only nodes, skip root */ |
| 6902 | goto next_iter; |
| 6903 | } |
| 6904 | |
| 6905 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6906 | if (!rc) { |
| 6907 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6908 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6909 | if (idx > start_idx) { |
| 6910 | /* we will process it later in the set */ |
| 6911 | goto skip_children; |
| 6912 | } |
| 6913 | } else { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6914 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL)); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6915 | } |
| 6916 | } else if (rc == LY_EINVAL) { |
| 6917 | goto skip_children; |
| 6918 | } |
| 6919 | |
| 6920 | next_iter: |
| 6921 | /* TREE DFS NEXT ELEM */ |
| 6922 | /* select element for the next run - children first */ |
| 6923 | next = lysc_node_child(elem); |
| 6924 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6925 | next = next->next; |
| 6926 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6927 | next = next->next; |
| 6928 | } |
| 6929 | if (!next) { |
| 6930 | skip_children: |
| 6931 | /* no children, so try siblings, but only if it's not the start, |
| 6932 | * that is considered to be the root and it's siblings are not traversed */ |
| 6933 | if (elem != start) { |
| 6934 | next = elem->next; |
| 6935 | } else { |
| 6936 | break; |
| 6937 | } |
| 6938 | } |
| 6939 | while (!next) { |
| 6940 | /* no siblings, go back through the parents */ |
| 6941 | if (elem->parent == start) { |
| 6942 | /* we are done, no next element to process */ |
| 6943 | break; |
| 6944 | } |
| 6945 | /* parent is already processed, go to its sibling */ |
| 6946 | elem = elem->parent; |
| 6947 | next = elem->next; |
| 6948 | } |
| 6949 | } |
| 6950 | |
| 6951 | return LY_SUCCESS; |
| 6952 | } |
| 6953 | |
| 6954 | /** |
| 6955 | * @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] | 6956 | * |
| 6957 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6958 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6959 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6960 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6961 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6962 | */ |
| 6963 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6964 | 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] | 6965 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6966 | uint32_t i, orig_used, mod_idx; |
| 6967 | const struct lys_module *mod; |
| 6968 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6969 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6970 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6971 | return LY_SUCCESS; |
| 6972 | } |
| 6973 | |
| 6974 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6975 | 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] | 6976 | return LY_EVALID; |
| 6977 | } |
| 6978 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6979 | orig_used = set->used; |
| 6980 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6981 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6982 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6983 | continue; |
| 6984 | } |
| 6985 | |
| 6986 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6987 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6988 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6989 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6990 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6991 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6992 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6993 | /* traverse all top-level nodes in all the modules */ |
| 6994 | mod_idx = 0; |
| 6995 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6996 | /* module may not be implemented or not compiled yet */ |
| 6997 | if (!mod->compiled) { |
| 6998 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6999 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7000 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7001 | root = NULL; |
| 7002 | /* no getnext opts needed */ |
| 7003 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 7004 | 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] | 7005 | } |
| 7006 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7007 | |
| 7008 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 7009 | /* add all the descendants recursively */ |
| 7010 | 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] | 7011 | } |
| 7012 | } |
| 7013 | |
| 7014 | return LY_SUCCESS; |
| 7015 | } |
| 7016 | |
| 7017 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7018 | * @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] | 7019 | * Indirectly context position aware. |
| 7020 | * |
| 7021 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7022 | * @param[in] mod Matching metadata module, NULL for any. |
| 7023 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7024 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7025 | * @return LY_ERR |
| 7026 | */ |
| 7027 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7028 | 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] | 7029 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7030 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7031 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7032 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7033 | return LY_SUCCESS; |
| 7034 | } |
| 7035 | |
| 7036 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7037 | 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] | 7038 | return LY_EVALID; |
| 7039 | } |
| 7040 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7041 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7042 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7043 | |
| 7044 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 7045 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7046 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7047 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7048 | |
| 7049 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7050 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7051 | continue; |
| 7052 | } |
| 7053 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7054 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7055 | /* match */ |
| 7056 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7057 | set->val.meta[i].meta = sub; |
| 7058 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7059 | /* pos does not change */ |
| 7060 | replaced = 1; |
| 7061 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7062 | 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] | 7063 | } |
| 7064 | ++i; |
| 7065 | } |
| 7066 | } |
| 7067 | } |
| 7068 | |
| 7069 | if (!replaced) { |
| 7070 | /* no match */ |
| 7071 | set_remove_node(set, i); |
| 7072 | } |
| 7073 | } |
| 7074 | |
| 7075 | return LY_SUCCESS; |
| 7076 | } |
| 7077 | |
| 7078 | /** |
| 7079 | * @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] | 7080 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7081 | * |
| 7082 | * @param[in,out] set1 Set to use for the result. |
| 7083 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7084 | * @return LY_ERR |
| 7085 | */ |
| 7086 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7087 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7088 | { |
| 7089 | LY_ERR rc; |
| 7090 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7091 | 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] | 7092 | 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] | 7093 | return LY_EVALID; |
| 7094 | } |
| 7095 | |
| 7096 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7097 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7098 | return LY_SUCCESS; |
| 7099 | } |
| 7100 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7101 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 7102 | /* release hidden allocated data (lyxp_set.size) */ |
| 7103 | lyxp_set_free_content(set1); |
| 7104 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7105 | memcpy(set1, set2, sizeof *set1); |
| 7106 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7107 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7108 | return LY_SUCCESS; |
| 7109 | } |
| 7110 | |
| 7111 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7112 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7113 | |
| 7114 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7115 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7116 | LY_CHECK_RET(rc); |
| 7117 | |
| 7118 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7119 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7120 | |
| 7121 | return LY_SUCCESS; |
| 7122 | } |
| 7123 | |
| 7124 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7125 | * @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] | 7126 | * Context position aware. |
| 7127 | * |
| 7128 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7129 | * @param[in] mod Matching metadata module, NULL for any. |
| 7130 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7131 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7132 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7133 | */ |
| 7134 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7135 | 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] | 7136 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7137 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7138 | struct lyxp_set *set_all_desc = NULL; |
| 7139 | LY_ERR rc; |
| 7140 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7141 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7142 | return LY_SUCCESS; |
| 7143 | } |
| 7144 | |
| 7145 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7146 | 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] | 7147 | return LY_EVALID; |
| 7148 | } |
| 7149 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7150 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7151 | * but it likely won't be used much, so it's a waste of time */ |
| 7152 | /* copy the context */ |
| 7153 | set_all_desc = set_copy(set); |
| 7154 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7155 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7156 | if (rc != LY_SUCCESS) { |
| 7157 | lyxp_set_free(set_all_desc); |
| 7158 | return rc; |
| 7159 | } |
| 7160 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7161 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7162 | if (rc != LY_SUCCESS) { |
| 7163 | lyxp_set_free(set_all_desc); |
| 7164 | return rc; |
| 7165 | } |
| 7166 | lyxp_set_free(set_all_desc); |
| 7167 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7168 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7169 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7170 | |
| 7171 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7172 | * we have all attributes qualified in lyd tree */ |
| 7173 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7174 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7175 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7176 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7177 | continue; |
| 7178 | } |
| 7179 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7180 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7181 | /* match */ |
| 7182 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7183 | set->val.meta[i].meta = sub; |
| 7184 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7185 | /* pos does not change */ |
| 7186 | replaced = 1; |
| 7187 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7188 | 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] | 7189 | } |
| 7190 | ++i; |
| 7191 | } |
| 7192 | } |
| 7193 | } |
| 7194 | |
| 7195 | if (!replaced) { |
| 7196 | /* no match */ |
| 7197 | set_remove_node(set, i); |
| 7198 | } |
| 7199 | } |
| 7200 | |
| 7201 | return LY_SUCCESS; |
| 7202 | } |
| 7203 | |
| 7204 | /** |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7205 | * @brief Move context @p set1 single item to the result of a comparison. |
| 7206 | * |
| 7207 | * @param[in] set1 First set with the item to compare. |
| 7208 | * @param[in] idx1 Index of the item in @p set1. |
| 7209 | * @param[in] set2 Second set. |
| 7210 | * @param[in] op Comparison operator to process. |
| 7211 | * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`. |
| 7212 | * @param[out] result Result of the comparison. |
| 7213 | * @return LY_ERR value. |
| 7214 | */ |
| 7215 | static LY_ERR |
| 7216 | moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op, |
| 7217 | ly_bool switch_operands, ly_bool *result) |
| 7218 | { |
| 7219 | struct lyxp_set tmp1 = {0}; |
| 7220 | LY_ERR rc = LY_SUCCESS; |
| 7221 | |
| 7222 | assert(set1->type == LYXP_SET_NODE_SET); |
| 7223 | |
| 7224 | /* cast set1 */ |
| 7225 | switch (set2->type) { |
| 7226 | case LYXP_SET_NUMBER: |
| 7227 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); |
| 7228 | break; |
| 7229 | case LYXP_SET_BOOLEAN: |
| 7230 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); |
| 7231 | break; |
| 7232 | default: |
| 7233 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); |
| 7234 | break; |
| 7235 | } |
| 7236 | LY_CHECK_GOTO(rc, cleanup); |
| 7237 | |
| 7238 | /* canonize set2 */ |
| 7239 | LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup); |
| 7240 | |
| 7241 | /* compare recursively and store the result */ |
| 7242 | if (switch_operands) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7243 | LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7244 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7245 | LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7246 | } |
| 7247 | |
| 7248 | cleanup: |
| 7249 | lyxp_set_free_content(&tmp1); |
| 7250 | return rc; |
| 7251 | } |
| 7252 | |
| 7253 | /** |
| 7254 | * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7255 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7256 | * |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7257 | * @param[in] set1 Set acting as the first operand for @p op. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7258 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7259 | * @param[in] op Comparison operator to process. |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7260 | * @param[out] result Result of the comparison. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7261 | * @return LY_ERR |
| 7262 | */ |
| 7263 | static LY_ERR |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7264 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7265 | { |
| 7266 | /* |
| 7267 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7268 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7269 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7270 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7271 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7272 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7273 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7274 | * |
| 7275 | * '=' or '!=' |
| 7276 | * BOOLEAN + BOOLEAN |
| 7277 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7278 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7279 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7280 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7281 | * NUMBER + NUMBER |
| 7282 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7283 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7284 | * STRING + STRING |
| 7285 | * |
| 7286 | * '<=', '<', '>=', '>' |
| 7287 | * NUMBER + NUMBER |
| 7288 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7289 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7290 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7291 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7292 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7293 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7294 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7295 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7296 | */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7297 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7298 | LY_ERR rc; |
| 7299 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7300 | /* iterative evaluation with node-sets */ |
| 7301 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7302 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7303 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7304 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7305 | LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7306 | |
| 7307 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7308 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7309 | return LY_SUCCESS; |
| 7310 | } |
| 7311 | } |
| 7312 | } else { |
| 7313 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7314 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7315 | LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7316 | |
| 7317 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7318 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7319 | return LY_SUCCESS; |
| 7320 | } |
| 7321 | } |
| 7322 | } |
| 7323 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7324 | /* false for all the nodes */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7325 | *result = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7326 | return LY_SUCCESS; |
| 7327 | } |
| 7328 | |
| 7329 | /* first convert properly */ |
| 7330 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7331 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7332 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7333 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7334 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7335 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7336 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7337 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7338 | LY_CHECK_RET(rc); |
| 7339 | } /* else we have 2 strings */ |
| 7340 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7341 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7342 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7343 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7344 | LY_CHECK_RET(rc); |
| 7345 | } |
| 7346 | |
| 7347 | assert(set1->type == set2->type); |
| 7348 | |
| 7349 | /* compute result */ |
| 7350 | if (op[0] == '=') { |
| 7351 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7352 | *result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7353 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7354 | *result = (set1->val.num == set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7355 | } else { |
| 7356 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7357 | *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7358 | } |
| 7359 | } else if (op[0] == '!') { |
| 7360 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7361 | *result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7362 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7363 | *result = (set1->val.num != set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7364 | } else { |
| 7365 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7366 | *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7367 | } |
| 7368 | } else { |
| 7369 | assert(set1->type == LYXP_SET_NUMBER); |
| 7370 | if (op[0] == '<') { |
| 7371 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7372 | *result = (set1->val.num <= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7373 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7374 | *result = (set1->val.num < set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7375 | } |
| 7376 | } else { |
| 7377 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7378 | *result = (set1->val.num >= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7379 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7380 | *result = (set1->val.num > set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7381 | } |
| 7382 | } |
| 7383 | } |
| 7384 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7385 | return LY_SUCCESS; |
| 7386 | } |
| 7387 | |
| 7388 | /** |
| 7389 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7390 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7391 | * |
| 7392 | * @param[in,out] set1 Set to use for the result. |
| 7393 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7394 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7395 | * @return LY_ERR |
| 7396 | */ |
| 7397 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7398 | 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] | 7399 | { |
| 7400 | LY_ERR rc; |
| 7401 | |
| 7402 | /* unary '-' */ |
| 7403 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7404 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7405 | LY_CHECK_RET(rc); |
| 7406 | set1->val.num *= -1; |
| 7407 | lyxp_set_free(set2); |
| 7408 | return LY_SUCCESS; |
| 7409 | } |
| 7410 | |
| 7411 | assert(set1 && set2); |
| 7412 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7413 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7414 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7415 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7416 | LY_CHECK_RET(rc); |
| 7417 | |
| 7418 | switch (op[0]) { |
| 7419 | /* '+' */ |
| 7420 | case '+': |
| 7421 | set1->val.num += set2->val.num; |
| 7422 | break; |
| 7423 | |
| 7424 | /* '-' */ |
| 7425 | case '-': |
| 7426 | set1->val.num -= set2->val.num; |
| 7427 | break; |
| 7428 | |
| 7429 | /* '*' */ |
| 7430 | case '*': |
| 7431 | set1->val.num *= set2->val.num; |
| 7432 | break; |
| 7433 | |
| 7434 | /* 'div' */ |
| 7435 | case 'd': |
| 7436 | set1->val.num /= set2->val.num; |
| 7437 | break; |
| 7438 | |
| 7439 | /* 'mod' */ |
| 7440 | case 'm': |
| 7441 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7442 | break; |
| 7443 | |
| 7444 | default: |
| 7445 | LOGINT_RET(set1->ctx); |
| 7446 | } |
| 7447 | |
| 7448 | return LY_SUCCESS; |
| 7449 | } |
| 7450 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7451 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7452 | * @brief Evaluate Predicate. Logs directly on error. |
| 7453 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7454 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7455 | * |
| 7456 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7458 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7459 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7460 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7461 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7462 | */ |
| 7463 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7464 | eval_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options, enum lyxp_axis axis) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7465 | { |
| 7466 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7467 | uint32_t i, orig_exp, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7468 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7469 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7470 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | |
| 7472 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7473 | 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] | 7474 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7475 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7476 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7477 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7478 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7479 | 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] | 7480 | LY_CHECK_RET(rc); |
| 7481 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7482 | /* 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] | 7483 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7484 | |
| 7485 | /* empty set, nothing to evaluate */ |
| 7486 | if (!set->used) { |
| 7487 | goto only_parse; |
| 7488 | } |
| 7489 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7490 | /* decide forward or reverse axis */ |
| 7491 | switch (axis) { |
| 7492 | case LYXP_AXIS_ANCESTOR: |
| 7493 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7494 | case LYXP_AXIS_PRECEDING: |
| 7495 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7496 | reverse_axis = 1; |
| 7497 | break; |
| 7498 | case LYXP_AXIS_DESCENDANT: |
| 7499 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7500 | case LYXP_AXIS_FOLLOWING: |
| 7501 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7502 | case LYXP_AXIS_PARENT: |
| 7503 | case LYXP_AXIS_CHILD: |
| 7504 | case LYXP_AXIS_SELF: |
| 7505 | case LYXP_AXIS_ATTRIBUTE: |
| 7506 | reverse_axis = 0; |
| 7507 | break; |
| 7508 | } |
| 7509 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7510 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7511 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7512 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7513 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7514 | set_init(&set2, set); |
| 7515 | 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] | 7516 | |
| 7517 | /* remember the node context position for position() and context size for last() */ |
| 7518 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7519 | |
| 7520 | set2.ctx_pos = orig_pos; |
| 7521 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7522 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7523 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7524 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7525 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7526 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7527 | return rc; |
| 7528 | } |
| 7529 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7530 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7531 | if (set2.type == LYXP_SET_NUMBER) { |
| 7532 | if ((long long)set2.val.num == orig_pos) { |
| 7533 | set2.val.num = 1; |
| 7534 | } else { |
| 7535 | set2.val.num = 0; |
| 7536 | } |
| 7537 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7538 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7539 | |
| 7540 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7541 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7542 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7543 | } |
| 7544 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7545 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7546 | |
| 7547 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7548 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7549 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7550 | /* there is a currently-valid node */ |
| 7551 | break; |
| 7552 | } |
| 7553 | } |
| 7554 | /* empty set, nothing to evaluate */ |
| 7555 | if (i == set->used) { |
| 7556 | goto only_parse; |
| 7557 | } |
| 7558 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7559 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7560 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7561 | /* set special in_ctx to all the valid snodes */ |
| 7562 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7563 | |
| 7564 | /* use the valid snodes one-by-one */ |
| 7565 | for (i = 0; i < set->used; ++i) { |
| 7566 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7567 | continue; |
| 7568 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7569 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7570 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7571 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7572 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7573 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7574 | LY_CHECK_RET(rc); |
| 7575 | |
| 7576 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7577 | } |
| 7578 | |
| 7579 | /* restore the state as it was before the predicate */ |
| 7580 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7581 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7582 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7583 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7584 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7585 | } |
| 7586 | } |
| 7587 | |
| 7588 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7589 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7590 | set_fill_set(&set2, set); |
| 7591 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7592 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7593 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7594 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7595 | return rc; |
| 7596 | } |
| 7597 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7598 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7599 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7600 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7601 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7602 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7603 | } |
| 7604 | |
| 7605 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7606 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7607 | 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] | 7608 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7609 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7610 | |
| 7611 | return LY_SUCCESS; |
| 7612 | } |
| 7613 | |
| 7614 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7615 | * @brief Evaluate Literal. Logs directly on error. |
| 7616 | * |
| 7617 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7618 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7619 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7620 | */ |
| 7621 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7622 | eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7623 | { |
| 7624 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7625 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7626 | set_fill_string(set, "", 0); |
| 7627 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7628 | 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] | 7629 | } |
| 7630 | } |
| 7631 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7632 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7633 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7634 | } |
| 7635 | |
| 7636 | /** |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7637 | * @brief Check that a nametest in a predicate matches a key node. |
| 7638 | * |
| 7639 | * @param[in] nametest Nametest to check. |
| 7640 | * @param[in] len Length of @p nametest. |
| 7641 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7642 | * @param[in] set Context set. |
| 7643 | * @param[in] key Expected key node. |
| 7644 | * @return LY_SUCCESS on success, |
| 7645 | * @return LY_ENOT if a predicate could not be compiled. |
| 7646 | * @return LY_ERR on any error. |
| 7647 | */ |
| 7648 | static LY_ERR |
| 7649 | eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode, |
| 7650 | const struct lyxp_set *set, const struct lysc_node *key) |
| 7651 | { |
| 7652 | const struct lys_module *mod; |
| 7653 | |
| 7654 | /* prefix (module) */ |
| 7655 | LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod)); |
| 7656 | if (mod != key->module) { |
| 7657 | return LY_ENOT; |
| 7658 | } |
| 7659 | |
| 7660 | /* node name */ |
| 7661 | if (ly_strncmp(key->name, nametest, len)) { |
| 7662 | return LY_ENOT; |
| 7663 | } |
| 7664 | |
| 7665 | return LY_SUCCESS; |
| 7666 | } |
| 7667 | |
| 7668 | /** |
| 7669 | * @brief Append a simple predicate for the node. |
| 7670 | * |
| 7671 | * @param[in] exp Full parsed XPath expression. |
| 7672 | * @param[in] tok_idx Predicate start index in @p exp. |
| 7673 | * @param[in] end_tok_idx Predicate end index in @p exp. |
| 7674 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7675 | * @param[in] set Context set. |
| 7676 | * @param[in] pred_node Node with the value referenced in the predicate. |
| 7677 | * @param[in,out] pred Predicate to append to. |
| 7678 | * @param[in,out] pred_len Length of @p pred, is updated. |
| 7679 | * @return LY_SUCCESS on success, |
| 7680 | * @return LY_ENOT if a predicate could not be compiled. |
| 7681 | * @return LY_ERR on any error. |
| 7682 | */ |
| 7683 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7684 | eval_name_test_try_compile_predicate_append(const struct lyxp_expr *exp, uint32_t tok_idx, uint32_t end_tok_idx, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7685 | const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred, |
| 7686 | uint32_t *pred_len) |
| 7687 | { |
| 7688 | LY_ERR rc = LY_SUCCESS; |
| 7689 | uint32_t i; |
| 7690 | const struct lyd_node *siblings; |
| 7691 | struct lyd_node *ctx_node; |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7692 | const struct lysc_node *sparent, *cur_scnode; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7693 | struct lyxp_expr *val_exp = NULL; |
| 7694 | struct lyxp_set set2 = {0}; |
| 7695 | char quot; |
| 7696 | |
| 7697 | /* duplicate the value expression */ |
| 7698 | LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup); |
| 7699 | |
| 7700 | /* get its atoms */ |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7701 | cur_scnode = set->cur_node ? set->cur_node->schema : NULL; |
| 7702 | LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode, |
| 7703 | ctx_scnode, &set2, LYXP_SCNODE), cleanup); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7704 | |
| 7705 | /* check whether we can compile a single predicate (evaluation result value is always the same) */ |
| 7706 | for (i = 0; i < set2.used; ++i) { |
| 7707 | if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) { |
| 7708 | /* skip root and context node */ |
| 7709 | continue; |
| 7710 | } |
| 7711 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7712 | /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the |
| 7713 | * context node instance */ |
| 7714 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) { |
| 7715 | /* 1.1) context node child was accessed on the child axis, certain dependency */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7716 | rc = LY_ENOT; |
| 7717 | goto cleanup; |
| 7718 | } |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7719 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) { |
| 7720 | for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {} |
| 7721 | if (sparent) { |
| 7722 | /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */ |
| 7723 | rc = LY_ENOT; |
| 7724 | goto cleanup; |
| 7725 | } |
| 7726 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7727 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7728 | /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered, |
| 7729 | * but the current node can be safely ignored, it is always the same data instance */ |
| 7730 | if ((set2.val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (cur_scnode != set2.val.scnodes[i].scnode)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7731 | rc = LY_ENOT; |
| 7732 | goto cleanup; |
| 7733 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7734 | } |
| 7735 | |
| 7736 | /* get any data instance of the context node, we checked it makes no difference */ |
| 7737 | siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree; |
| 7738 | LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup); |
| 7739 | |
| 7740 | /* evaluate the value subexpression with the root context node */ |
| 7741 | lyxp_set_free_content(&set2); |
| 7742 | LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node, |
| 7743 | ctx_node, set->tree, NULL, &set2, 0), cleanup); |
| 7744 | |
| 7745 | /* cast it into a string */ |
| 7746 | LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup); |
| 7747 | |
| 7748 | /* append the JSON predicate */ |
| 7749 | *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3); |
| 7750 | LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7751 | quot = strchr(set2.val.str, '\'') ? '\"' : '\''; |
| 7752 | *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot); |
| 7753 | |
| 7754 | cleanup: |
| 7755 | lyxp_expr_free(set->ctx, val_exp); |
| 7756 | lyxp_set_free_content(&set2); |
| 7757 | return rc; |
| 7758 | } |
| 7759 | |
| 7760 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7761 | * @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] | 7762 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7763 | * @param[in] exp Full parsed XPath expression. |
| 7764 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7765 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7766 | * @param[in] set Context set. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7767 | * @param[out] predicates Parsed predicates. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7768 | * @return LY_SUCCESS on success, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7769 | * @return LY_ENOT if a predicate could not be compiled. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7770 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7771 | */ |
| 7772 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7773 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode, |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 7774 | const struct lyxp_set *set, struct ly_path_predicate **predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7775 | { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7776 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7777 | uint32_t e_idx, val_start_idx, pred_idx = 0, temp_lo = 0, pred_len = 0, nested_pred; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7778 | const struct lysc_node *key; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7779 | char *pred = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7780 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7781 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7782 | assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7783 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7784 | /* turn logging off */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7785 | ly_temp_log_options(&temp_lo); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7786 | |
| 7787 | if (ctx_scnode->nodetype == LYS_LIST) { |
| 7788 | /* check for predicates "[key1=...][key2=...]..." */ |
| 7789 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7790 | /* get key count */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7791 | if (ctx_scnode->flags & LYS_KEYLESS) { |
| 7792 | rc = LY_ENOT; |
| 7793 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7794 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7795 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7796 | /* learn where the predicates end */ |
| 7797 | e_idx = *tok_idx; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7798 | for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7799 | /* '[' */ |
| 7800 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7801 | rc = LY_ENOT; |
| 7802 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7803 | } |
| 7804 | ++e_idx; |
| 7805 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7806 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7807 | /* not a key */ |
| 7808 | rc = LY_ENOT; |
| 7809 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7810 | } |
| 7811 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7812 | /* check key */ |
| 7813 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx], |
| 7814 | exp->tok_len[e_idx], ctx_scnode, set, key), cleanup); |
| 7815 | |
| 7816 | ++e_idx; |
| 7817 | |
| 7818 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7819 | /* not '=' */ |
| 7820 | rc = LY_ENOT; |
| 7821 | goto cleanup; |
| 7822 | } |
| 7823 | ++e_idx; |
| 7824 | |
| 7825 | /* value start */ |
| 7826 | val_start_idx = e_idx; |
| 7827 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7828 | /* ']' */ |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7829 | nested_pred = 1; |
| 7830 | do { |
| 7831 | ++e_idx; |
| 7832 | |
| 7833 | if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7834 | /* higher priority than '=' */ |
| 7835 | rc = LY_ENOT; |
| 7836 | goto cleanup; |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7837 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7838 | /* nested predicate */ |
| 7839 | ++nested_pred; |
| 7840 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7841 | /* predicate end */ |
| 7842 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7843 | } |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7844 | } while (nested_pred); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7845 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7846 | /* try to evaluate the value */ |
| 7847 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, |
| 7848 | set, key, &pred, &pred_len), cleanup); |
| 7849 | |
| 7850 | ++e_idx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7851 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7852 | } else { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7853 | /* check for predicate "[.=...]" */ |
| 7854 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7855 | /* learn just where this single predicate ends */ |
| 7856 | e_idx = *tok_idx; |
| 7857 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7858 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7859 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7860 | rc = LY_ENOT; |
| 7861 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7862 | } |
| 7863 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7864 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7865 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7866 | /* not the node value */ |
| 7867 | rc = LY_ENOT; |
| 7868 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7869 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7870 | ++e_idx; |
| 7871 | |
| 7872 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7873 | /* not '=' */ |
| 7874 | rc = LY_ENOT; |
| 7875 | goto cleanup; |
| 7876 | } |
| 7877 | ++e_idx; |
| 7878 | |
| 7879 | /* value start */ |
| 7880 | val_start_idx = e_idx; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7881 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7882 | /* ']' */ |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7883 | nested_pred = 1; |
| 7884 | do { |
| 7885 | ++e_idx; |
| 7886 | |
| 7887 | if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) { |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7888 | /* higher priority than '=' */ |
| 7889 | rc = LY_ENOT; |
| 7890 | goto cleanup; |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7891 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7892 | /* nested predicate */ |
| 7893 | ++nested_pred; |
| 7894 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7895 | /* predicate end */ |
| 7896 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7897 | } |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7898 | } while (nested_pred); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7899 | |
| 7900 | /* try to evaluate the value */ |
| 7901 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set, |
| 7902 | ctx_scnode, &pred, &pred_len), cleanup); |
| 7903 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7904 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7905 | } |
| 7906 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7907 | /* parse the predicate(s) */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7908 | LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL, |
| 7909 | LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7910 | |
| 7911 | /* compile */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7912 | rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2, |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 7913 | &pred_idx, LY_VALUE_JSON, NULL, predicates); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7914 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7915 | |
| 7916 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7917 | *tok_idx = e_idx; |
| 7918 | |
| 7919 | cleanup: |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 7920 | ly_temp_log_options(NULL); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7921 | lyxp_expr_free(set->ctx, exp2); |
| 7922 | free(pred); |
| 7923 | return rc; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7924 | } |
| 7925 | |
| 7926 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7927 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7928 | * data node(s) could be found using a single hash-based search. |
| 7929 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7930 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7931 | * @param[in] node Next context node to check. |
| 7932 | * @param[in] name Expected node name. |
| 7933 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7934 | * @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] | 7935 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7936 | * @param[in] format Prefix format. |
| 7937 | * @param[in,out] found Previously found node, is updated. |
| 7938 | * @return LY_SUCCESS on success, |
| 7939 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7940 | */ |
| 7941 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7942 | eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7943 | uint32_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7944 | const struct lysc_node **found) |
| 7945 | { |
| 7946 | const struct lysc_node *scnode; |
| 7947 | const struct lys_module *mod; |
| 7948 | uint32_t idx = 0; |
| 7949 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7950 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7951 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7952 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7953 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7954 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7955 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7956 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7957 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7958 | if (!mod->implemented) { |
| 7959 | continue; |
| 7960 | } |
| 7961 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7962 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7963 | if (scnode) { |
| 7964 | /* we have found a match */ |
| 7965 | break; |
| 7966 | } |
| 7967 | } |
| 7968 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7969 | if (!scnode) { |
| 7970 | /* all modules searched */ |
| 7971 | idx = 0; |
| 7972 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7973 | } else { |
| 7974 | /* search in top-level */ |
| 7975 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7976 | } |
| 7977 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7978 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7979 | /* we must adjust the module to inherit the one from the context node */ |
| 7980 | moveto_mod = node->schema->module; |
| 7981 | } |
| 7982 | |
| 7983 | /* search in children, do not repeat the same search */ |
| 7984 | 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] | 7985 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7986 | |
| 7987 | /* additional context check */ |
| 7988 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7989 | scnode = NULL; |
| 7990 | } |
| 7991 | |
| 7992 | if (scnode) { |
| 7993 | if (*found) { |
| 7994 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7995 | * (more hash-based searches would be required, not supported) */ |
| 7996 | return LY_ENOT; |
| 7997 | } else { |
| 7998 | /* remember the found schema node and continue to make sure it can be used */ |
| 7999 | *found = scnode; |
| 8000 | } |
| 8001 | } |
| 8002 | |
| 8003 | if (idx) { |
| 8004 | /* continue searching all the following models */ |
| 8005 | goto continue_search; |
| 8006 | } |
| 8007 | |
| 8008 | return LY_SUCCESS; |
| 8009 | } |
| 8010 | |
| 8011 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8012 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 8013 | * |
| 8014 | * @param[in] set XPath set. |
| 8015 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 8016 | * @param[in] ncname XPath NCName being evaluated. |
| 8017 | * @param[in] ncname_len Length of @p ncname. |
| 8018 | * @param[in] expr Whole XPath expression. |
| 8019 | * @param[in] options XPath options. |
| 8020 | */ |
| 8021 | static void |
| 8022 | eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8023 | uint32_t ncname_len, const char *expr, uint32_t options) |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8024 | { |
| 8025 | const char *format; |
| 8026 | char *path = NULL, *ppath = NULL; |
| 8027 | |
| 8028 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 8029 | if (scparent) { |
| 8030 | /* generate path for the parent */ |
| 8031 | if (scparent->type == LYXP_NODE_ELEM) { |
| 8032 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 8033 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 8034 | ppath = strdup("<root>"); |
| 8035 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 8036 | ppath = strdup("<config-root>"); |
| 8037 | } |
| 8038 | } |
| 8039 | if (ppath) { |
| 8040 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 8041 | if (options & LYXP_SCNODE_ERROR) { |
| 8042 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 8043 | } else { |
| 8044 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 8045 | } |
| 8046 | } else { |
| 8047 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 8048 | if (options & LYXP_SCNODE_ERROR) { |
| 8049 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 8050 | } else { |
| 8051 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 8052 | } |
| 8053 | } |
| 8054 | free(path); |
| 8055 | free(ppath); |
| 8056 | } |
| 8057 | |
| 8058 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8059 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 8060 | * |
| 8061 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8062 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8063 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 8064 | * |
| 8065 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8066 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8067 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8068 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8069 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8070 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8071 | * @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] | 8072 | */ |
| 8073 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8074 | eval_name_test_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8075 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8076 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8077 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8078 | const char *ncname, *ncname_dict = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8079 | uint32_t i, ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8080 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8081 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8082 | struct ly_path_predicate *predicates = NULL; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8083 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8084 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8085 | 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] | 8086 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8087 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8088 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8089 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8090 | goto moveto; |
| 8091 | } |
| 8092 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8093 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 8094 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8095 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8096 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8097 | /* all nodes will match */ |
| 8098 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8099 | } |
| 8100 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8101 | /* parse (and skip) module name */ |
| 8102 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8103 | LY_CHECK_GOTO(rc, cleanup); |
| 8104 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8105 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8106 | /* all nodes from the module will match */ |
| 8107 | goto moveto; |
| 8108 | } |
| 8109 | |
| 8110 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 8111 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8112 | /* find the matching schema node in some parent in the context */ |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8113 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8114 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 8115 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8116 | /* check failed */ |
| 8117 | scnode = NULL; |
| 8118 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8119 | } |
| 8120 | } |
| 8121 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8122 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 8123 | /* try to create the predicates */ |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8124 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8125 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8126 | scnode = NULL; |
| 8127 | } |
| 8128 | } |
| 8129 | } |
| 8130 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8131 | if (!scnode) { |
| 8132 | /* 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] | 8133 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 8134 | 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] | 8135 | } |
| 8136 | |
| 8137 | moveto: |
| 8138 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8139 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8140 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8141 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8142 | } else { |
| 8143 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 8144 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8145 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8146 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8147 | } |
| 8148 | LY_CHECK_GOTO(rc, cleanup); |
| 8149 | } |
| 8150 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8151 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8152 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8153 | ly_bool found = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8154 | |
| 8155 | /* remember parent if there is only one, to print in the warning */ |
| 8156 | for (i = 0; i < set->used; ++i) { |
| 8157 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 8158 | if (!scparent) { |
| 8159 | /* remember the context node */ |
| 8160 | scparent = &set->val.scnodes[i]; |
| 8161 | } else { |
| 8162 | /* several context nodes, no reasonable error possible */ |
| 8163 | scparent = NULL; |
| 8164 | break; |
| 8165 | } |
| 8166 | } |
| 8167 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8168 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8169 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8170 | /* efficient evaluation that does not add all the descendants into the set */ |
| 8171 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8172 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8173 | if (all_desc) { |
| 8174 | /* "//" == "/descendant-or-self::node()/" */ |
| 8175 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8176 | LY_CHECK_GOTO(rc, cleanup); |
| 8177 | } |
| 8178 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8179 | } |
| 8180 | LY_CHECK_GOTO(rc, cleanup); |
| 8181 | |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8182 | i = set->used; |
| 8183 | do { |
| 8184 | --i; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8185 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8186 | found = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8187 | break; |
| 8188 | } |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8189 | } while (i); |
| 8190 | if (!found) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8191 | /* generate message */ |
| 8192 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 8193 | |
| 8194 | if (options & LYXP_SCNODE_ERROR) { |
| 8195 | /* error */ |
| 8196 | rc = LY_EVALID; |
| 8197 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8198 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8199 | |
| 8200 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 8201 | rc = LY_ENOT; |
| 8202 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8203 | } |
| 8204 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8205 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8206 | /* efficient evaluation */ |
| 8207 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 8208 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 8209 | /* we can find the child nodes using hashes */ |
| 8210 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8211 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8212 | if (all_desc) { |
| 8213 | /* "//" == "/descendant-or-self::node()/" */ |
| 8214 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8215 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8216 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8217 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8218 | } |
| 8219 | LY_CHECK_GOTO(rc, cleanup); |
| 8220 | } |
| 8221 | } |
| 8222 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8223 | if (scnode_skip_pred) { |
| 8224 | /* skip predicates */ |
| 8225 | options |= LYXP_SKIP_EXPR; |
| 8226 | } |
| 8227 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8228 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8229 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8230 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8231 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8232 | } |
| 8233 | |
| 8234 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8235 | if (scnode_skip_pred) { |
| 8236 | /* restore options */ |
| 8237 | options &= ~LYXP_SKIP_EXPR; |
| 8238 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8239 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8240 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8241 | ly_path_predicates_free(set->ctx, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 8242 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8243 | return rc; |
| 8244 | } |
| 8245 | |
| 8246 | /** |
| 8247 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 8248 | * |
| 8249 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8250 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8251 | * [8] NodeType ::= 'text' | 'node' |
| 8252 | * |
| 8253 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8254 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8255 | * @param[in] axis Axis to search on. |
| 8256 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8257 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8258 | * @param[in] options XPath options. |
| 8259 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8260 | */ |
| 8261 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8262 | eval_node_type_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8263 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8264 | { |
| 8265 | LY_ERR rc; |
| 8266 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8267 | (void)all_desc; |
| 8268 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8269 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8270 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8271 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 8272 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8273 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8274 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 8275 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8276 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8277 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8278 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8279 | 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] | 8280 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8281 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8282 | |
| 8283 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8284 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8285 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8286 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8287 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8288 | |
| 8289 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8290 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8291 | 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] | 8292 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8293 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8294 | |
| 8295 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8296 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8297 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8298 | LY_CHECK_RET(rc); |
| 8299 | } |
| 8300 | |
| 8301 | return LY_SUCCESS; |
| 8302 | } |
| 8303 | |
| 8304 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8305 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8306 | * |
| 8307 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8308 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8309 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8310 | * |
| 8311 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8312 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8313 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8314 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8315 | * @param[in] options XPath options. |
| 8316 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8317 | */ |
| 8318 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8319 | eval_relative_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8320 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8321 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8322 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8323 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8324 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8325 | |
| 8326 | goto step; |
| 8327 | do { |
| 8328 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8329 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8330 | all_desc = 0; |
| 8331 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8332 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8333 | all_desc = 1; |
| 8334 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8335 | 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] | 8336 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8337 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8338 | |
| 8339 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8340 | /* AxisSpecifier */ |
| 8341 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8342 | 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] | 8343 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8344 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8345 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8346 | ++(*tok_idx); |
| 8347 | |
| 8348 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8349 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8350 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8351 | ++(*tok_idx); |
| 8352 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8353 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8354 | |
| 8355 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8356 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8357 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8358 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8359 | /* default */ |
| 8360 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8361 | } |
| 8362 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8363 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8364 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8365 | case LYXP_TOKEN_DOT: |
| 8366 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8367 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8368 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8369 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8370 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8371 | rc = LY_EVALID; |
| 8372 | goto cleanup; |
| 8373 | } |
| 8374 | |
| 8375 | if (all_desc) { |
| 8376 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8377 | LY_CHECK_GOTO(rc, cleanup); |
| 8378 | } |
| 8379 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8380 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8381 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8382 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8383 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8384 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8385 | break; |
| 8386 | |
| 8387 | case LYXP_TOKEN_DDOT: |
| 8388 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8389 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8390 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8391 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8392 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8393 | rc = LY_EVALID; |
| 8394 | goto cleanup; |
| 8395 | } |
| 8396 | |
| 8397 | if (all_desc) { |
| 8398 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8399 | LY_CHECK_GOTO(rc, cleanup); |
| 8400 | } |
| 8401 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8402 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8403 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8404 | 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] | 8405 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8406 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8407 | break; |
| 8408 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8409 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8410 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8411 | 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] | 8412 | if (rc == LY_ENOT) { |
| 8413 | assert(options & LYXP_SCNODE_ALL); |
| 8414 | /* skip the rest of this path */ |
| 8415 | rc = LY_SUCCESS; |
| 8416 | scnode_skip_path = 1; |
| 8417 | options |= LYXP_SKIP_EXPR; |
| 8418 | } |
| 8419 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8420 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8421 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8422 | case LYXP_TOKEN_NODETYPE: |
| 8423 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8424 | 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] | 8425 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8426 | break; |
| 8427 | |
| 8428 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8429 | LOGINT(set->ctx); |
| 8430 | rc = LY_EINT; |
| 8431 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8432 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8433 | } 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] | 8434 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8435 | cleanup: |
| 8436 | if (scnode_skip_path) { |
| 8437 | options &= ~LYXP_SKIP_EXPR; |
| 8438 | } |
| 8439 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8440 | } |
| 8441 | |
| 8442 | /** |
| 8443 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8444 | * |
| 8445 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8446 | * |
| 8447 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8448 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8449 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8450 | * @param[in] options XPath options. |
| 8451 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8452 | */ |
| 8453 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8454 | eval_absolute_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8455 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8456 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8457 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8458 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8459 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8460 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8461 | } |
| 8462 | |
| 8463 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8464 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8465 | /* evaluate '/' - deferred */ |
| 8466 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8467 | 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] | 8468 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8469 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8470 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8471 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8472 | return LY_SUCCESS; |
| 8473 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8474 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8475 | case LYXP_TOKEN_DOT: |
| 8476 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8477 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8478 | case LYXP_TOKEN_AT: |
| 8479 | case LYXP_TOKEN_NAMETEST: |
| 8480 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8481 | 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] | 8482 | break; |
| 8483 | default: |
| 8484 | break; |
| 8485 | } |
| 8486 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8487 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8488 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8489 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8490 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8491 | 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] | 8492 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8493 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8494 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8495 | 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] | 8496 | } |
| 8497 | |
| 8498 | return LY_SUCCESS; |
| 8499 | } |
| 8500 | |
| 8501 | /** |
| 8502 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8503 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8504 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8505 | * |
| 8506 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8507 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8508 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8509 | * @param[in] options XPath options. |
| 8510 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8511 | */ |
| 8512 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8513 | eval_function_call(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8514 | { |
| 8515 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8516 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8517 | LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL; |
| 8518 | uint32_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8519 | struct lyxp_set **args = NULL, **args_aux; |
| 8520 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8521 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8522 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8523 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8524 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8525 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8526 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8527 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8528 | xpath_func = &xpath_sum; |
| 8529 | } |
| 8530 | break; |
| 8531 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8532 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8534 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8535 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8536 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8537 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8538 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8539 | xpath_func = &xpath_true; |
| 8540 | } |
| 8541 | break; |
| 8542 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8543 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8544 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8545 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8546 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8547 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8548 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8549 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8550 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8551 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8552 | xpath_func = &xpath_deref; |
| 8553 | } |
| 8554 | break; |
| 8555 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8556 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8557 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8558 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8559 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8560 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8561 | xpath_func = &xpath_string; |
| 8562 | } |
| 8563 | break; |
| 8564 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8565 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8566 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8567 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8568 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8569 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8570 | xpath_func = &xpath_current; |
| 8571 | } |
| 8572 | break; |
| 8573 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8574 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8575 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8576 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8577 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8578 | } 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] | 8579 | xpath_func = &xpath_re_match; |
| 8580 | } |
| 8581 | break; |
| 8582 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8583 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8584 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8585 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8586 | xpath_func = &xpath_translate; |
| 8587 | } |
| 8588 | break; |
| 8589 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8590 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8591 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8592 | } 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] | 8593 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8594 | } 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] | 8595 | xpath_func = &xpath_bit_is_set; |
| 8596 | } |
| 8597 | break; |
| 8598 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8599 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8600 | xpath_func = &xpath_starts_with; |
| 8601 | } |
| 8602 | break; |
| 8603 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8604 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8605 | xpath_func = &xpath_derived_from; |
| 8606 | } |
| 8607 | break; |
| 8608 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8609 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8610 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8611 | } 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] | 8612 | xpath_func = &xpath_string_length; |
| 8613 | } |
| 8614 | break; |
| 8615 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8616 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8617 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8618 | } 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] | 8619 | xpath_func = &xpath_substring_after; |
| 8620 | } |
| 8621 | break; |
| 8622 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8623 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8624 | xpath_func = &xpath_substring_before; |
| 8625 | } |
| 8626 | break; |
| 8627 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8628 | 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] | 8629 | xpath_func = &xpath_derived_from_or_self; |
| 8630 | } |
| 8631 | break; |
| 8632 | } |
| 8633 | |
| 8634 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8635 | 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] | 8636 | return LY_EVALID; |
| 8637 | } |
| 8638 | } |
| 8639 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8640 | 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] | 8641 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8642 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8643 | |
| 8644 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8645 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8646 | 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] | 8647 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8648 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8649 | |
| 8650 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8651 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8652 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8653 | args = malloc(sizeof *args); |
| 8654 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8655 | arg_count = 1; |
| 8656 | args[0] = set_copy(set); |
| 8657 | if (!args[0]) { |
| 8658 | rc = LY_EMEM; |
| 8659 | goto cleanup; |
| 8660 | } |
| 8661 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8662 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8663 | LY_CHECK_GOTO(rc, cleanup); |
| 8664 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8665 | 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] | 8666 | LY_CHECK_GOTO(rc, cleanup); |
| 8667 | } |
| 8668 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8669 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8670 | 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] | 8671 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8672 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8673 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8674 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8675 | ++arg_count; |
| 8676 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8677 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8678 | args = args_aux; |
| 8679 | args[arg_count - 1] = set_copy(set); |
| 8680 | if (!args[arg_count - 1]) { |
| 8681 | rc = LY_EMEM; |
| 8682 | goto cleanup; |
| 8683 | } |
| 8684 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8685 | 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] | 8686 | LY_CHECK_GOTO(rc, cleanup); |
| 8687 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8688 | 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] | 8689 | LY_CHECK_GOTO(rc, cleanup); |
| 8690 | } |
| 8691 | } |
| 8692 | |
| 8693 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8694 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8695 | 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] | 8696 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8697 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8698 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8699 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8700 | /* evaluate function */ |
| 8701 | rc = xpath_func(args, arg_count, set, options); |
| 8702 | |
| 8703 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8704 | /* merge all nodes from arg evaluations */ |
| 8705 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8706 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8707 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8708 | } |
| 8709 | } |
| 8710 | } else { |
| 8711 | rc = LY_SUCCESS; |
| 8712 | } |
| 8713 | |
| 8714 | cleanup: |
| 8715 | for (i = 0; i < arg_count; ++i) { |
| 8716 | lyxp_set_free(args[i]); |
| 8717 | } |
| 8718 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8719 | return rc; |
| 8720 | } |
| 8721 | |
| 8722 | /** |
| 8723 | * @brief Evaluate Number. Logs directly on error. |
| 8724 | * |
| 8725 | * @param[in] ctx Context for errors. |
| 8726 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8727 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8728 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8729 | * @return LY_ERR |
| 8730 | */ |
| 8731 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8732 | eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8733 | { |
| 8734 | long double num; |
| 8735 | char *endptr; |
| 8736 | |
| 8737 | if (set) { |
| 8738 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8739 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8740 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8741 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8742 | 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] | 8743 | 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] | 8744 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8745 | } 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] | 8746 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8747 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8748 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8749 | return LY_EVALID; |
| 8750 | } |
| 8751 | |
| 8752 | set_fill_number(set, num); |
| 8753 | } |
| 8754 | |
| 8755 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8756 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8757 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8758 | return LY_SUCCESS; |
| 8759 | } |
| 8760 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8761 | LY_ERR |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8762 | lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len, |
| 8763 | struct lyxp_var **var) |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8764 | { |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8765 | LY_ARRAY_COUNT_TYPE u; |
| 8766 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8767 | assert(name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8768 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8769 | if (!name_len) { |
| 8770 | name_len = strlen(name); |
| 8771 | } |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8772 | |
| 8773 | LY_ARRAY_FOR(vars, u) { |
| 8774 | if (!strncmp(vars[u].name, name, name_len)) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8775 | if (var) { |
| 8776 | *var = (struct lyxp_var *)&vars[u]; |
| 8777 | } |
| 8778 | return LY_SUCCESS; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8779 | } |
| 8780 | } |
| 8781 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8782 | if (ctx) { |
| 8783 | LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8784 | } |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8785 | return LY_ENOTFOUND; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8786 | } |
| 8787 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8788 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8789 | * @brief Evaluate VariableReference. |
| 8790 | * |
| 8791 | * @param[in] exp Parsed XPath expression. |
| 8792 | * @param[in] tok_idx Position in the expression @p exp. |
| 8793 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8794 | * @param[in,out] set Context and result set. |
| 8795 | * @param[in] options XPath options. |
| 8796 | * @return LY_ERR value. |
| 8797 | */ |
| 8798 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8799 | eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8800 | { |
| 8801 | LY_ERR ret; |
| 8802 | const char *name; |
| 8803 | struct lyxp_var *var; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8804 | struct lyxp_expr *tokens = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8805 | uint32_t token_index, name_len; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8806 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8807 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8808 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
Michal Vasko | e68b540 | 2022-07-29 14:58:15 +0200 | [diff] [blame] | 8809 | name_len = exp->tok_len[*tok_idx]; |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8810 | ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var); |
| 8811 | LY_CHECK_RET(ret); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8812 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8813 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8814 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8815 | LY_CHECK_GOTO(ret, cleanup); |
| 8816 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8817 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8818 | token_index = 0; |
| 8819 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8820 | LY_CHECK_GOTO(ret, cleanup); |
| 8821 | |
| 8822 | cleanup: |
| 8823 | lyxp_expr_free(set->ctx, tokens); |
| 8824 | |
| 8825 | return ret; |
| 8826 | } |
| 8827 | |
| 8828 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8829 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8830 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8831 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8832 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8833 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8834 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8835 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8836 | * |
| 8837 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8838 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8839 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8840 | * @param[in] options XPath options. |
| 8841 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8842 | */ |
| 8843 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8844 | eval_path_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8845 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8846 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8847 | LY_ERR rc; |
| 8848 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8849 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8850 | case LYXP_TOKEN_PAR1: |
| 8851 | /* '(' Expr ')' */ |
| 8852 | |
| 8853 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8854 | 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] | 8855 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8856 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8857 | |
| 8858 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8859 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8860 | LY_CHECK_RET(rc); |
| 8861 | |
| 8862 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8863 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8864 | 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] | 8865 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8866 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8867 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8868 | goto predicate; |
| 8869 | |
| 8870 | case LYXP_TOKEN_DOT: |
| 8871 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8872 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8873 | case LYXP_TOKEN_AT: |
| 8874 | case LYXP_TOKEN_NAMETEST: |
| 8875 | case LYXP_TOKEN_NODETYPE: |
| 8876 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8877 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8878 | LY_CHECK_RET(rc); |
| 8879 | break; |
| 8880 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8881 | case LYXP_TOKEN_VARREF: |
| 8882 | /* VariableReference */ |
| 8883 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8884 | LY_CHECK_RET(rc); |
| 8885 | ++(*tok_idx); |
| 8886 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8887 | goto predicate; |
| 8888 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8889 | case LYXP_TOKEN_FUNCNAME: |
| 8890 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8891 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8892 | LY_CHECK_RET(rc); |
| 8893 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8894 | goto predicate; |
| 8895 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8896 | case LYXP_TOKEN_OPER_PATH: |
| 8897 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8898 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8899 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8900 | LY_CHECK_RET(rc); |
| 8901 | break; |
| 8902 | |
| 8903 | case LYXP_TOKEN_LITERAL: |
| 8904 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8905 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8906 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8907 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8908 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8909 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8910 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8911 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8912 | } |
| 8913 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8914 | goto predicate; |
| 8915 | |
| 8916 | case LYXP_TOKEN_NUMBER: |
| 8917 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8918 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8919 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8920 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8921 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8922 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8923 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8924 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8925 | } |
| 8926 | LY_CHECK_RET(rc); |
| 8927 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8928 | goto predicate; |
| 8929 | |
| 8930 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8931 | 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] | 8932 | return LY_EVALID; |
| 8933 | } |
| 8934 | |
| 8935 | return LY_SUCCESS; |
| 8936 | |
| 8937 | predicate: |
| 8938 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8939 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8940 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8941 | LY_CHECK_RET(rc); |
| 8942 | } |
| 8943 | |
| 8944 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8945 | 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] | 8946 | |
| 8947 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8948 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8949 | all_desc = 0; |
| 8950 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8951 | all_desc = 1; |
| 8952 | } |
| 8953 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8954 | 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] | 8955 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8956 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8957 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8958 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8959 | LY_CHECK_RET(rc); |
| 8960 | } |
| 8961 | |
| 8962 | return LY_SUCCESS; |
| 8963 | } |
| 8964 | |
| 8965 | /** |
| 8966 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8967 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8968 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8969 | * |
| 8970 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8971 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8972 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8973 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8974 | * @param[in] options XPath options. |
| 8975 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8976 | */ |
| 8977 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8978 | eval_union_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8979 | { |
| 8980 | LY_ERR rc = LY_SUCCESS; |
| 8981 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8982 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8983 | |
| 8984 | assert(repeat); |
| 8985 | |
| 8986 | set_init(&orig_set, set); |
| 8987 | set_init(&set2, set); |
| 8988 | |
| 8989 | set_fill_set(&orig_set, set); |
| 8990 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8991 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8992 | LY_CHECK_GOTO(rc, cleanup); |
| 8993 | |
| 8994 | /* ('|' PathExpr)* */ |
| 8995 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8996 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8997 | 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] | 8998 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8999 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9000 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9001 | if (options & LYXP_SKIP_EXPR) { |
| 9002 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9003 | LY_CHECK_GOTO(rc, cleanup); |
| 9004 | continue; |
| 9005 | } |
| 9006 | |
| 9007 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9008 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9009 | LY_CHECK_GOTO(rc, cleanup); |
| 9010 | |
| 9011 | /* eval */ |
| 9012 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9013 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9014 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9015 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9016 | LY_CHECK_GOTO(rc, cleanup); |
| 9017 | } |
| 9018 | } |
| 9019 | |
| 9020 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9021 | lyxp_set_free_content(&orig_set); |
| 9022 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9023 | return rc; |
| 9024 | } |
| 9025 | |
| 9026 | /** |
| 9027 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 9028 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9029 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9030 | * |
| 9031 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9032 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9033 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9034 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9035 | * @param[in] options XPath options. |
| 9036 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9037 | */ |
| 9038 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9039 | eval_unary_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9040 | { |
| 9041 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9042 | uint32_t this_op, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9043 | |
| 9044 | assert(repeat); |
| 9045 | |
| 9046 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9047 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9048 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9049 | 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] | 9050 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9051 | 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] | 9052 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9053 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9054 | } |
| 9055 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9056 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9057 | LY_CHECK_RET(rc); |
| 9058 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9059 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9060 | if (options & LYXP_SCNODE_ALL) { |
| 9061 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 9062 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9063 | 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] | 9064 | LY_CHECK_RET(rc); |
| 9065 | } |
| 9066 | } |
| 9067 | |
| 9068 | return LY_SUCCESS; |
| 9069 | } |
| 9070 | |
| 9071 | /** |
| 9072 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 9073 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9074 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9075 | * | MultiplicativeExpr '*' UnaryExpr |
| 9076 | * | MultiplicativeExpr 'div' UnaryExpr |
| 9077 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 9078 | * |
| 9079 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9080 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9081 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9082 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9083 | * @param[in] options XPath options. |
| 9084 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9085 | */ |
| 9086 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9087 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9088 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9089 | { |
| 9090 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9091 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9092 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9093 | |
| 9094 | assert(repeat); |
| 9095 | |
| 9096 | set_init(&orig_set, set); |
| 9097 | set_init(&set2, set); |
| 9098 | |
| 9099 | set_fill_set(&orig_set, set); |
| 9100 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9101 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9102 | LY_CHECK_GOTO(rc, cleanup); |
| 9103 | |
| 9104 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 9105 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9106 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9107 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9108 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9109 | 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] | 9110 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9111 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9112 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9113 | if (options & LYXP_SKIP_EXPR) { |
| 9114 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9115 | LY_CHECK_GOTO(rc, cleanup); |
| 9116 | continue; |
| 9117 | } |
| 9118 | |
| 9119 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9120 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9121 | LY_CHECK_GOTO(rc, cleanup); |
| 9122 | |
| 9123 | /* eval */ |
| 9124 | if (options & LYXP_SCNODE_ALL) { |
| 9125 | 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] | 9126 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9127 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9128 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9129 | 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] | 9130 | LY_CHECK_GOTO(rc, cleanup); |
| 9131 | } |
| 9132 | } |
| 9133 | |
| 9134 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9135 | lyxp_set_free_content(&orig_set); |
| 9136 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9137 | return rc; |
| 9138 | } |
| 9139 | |
| 9140 | /** |
| 9141 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 9142 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9143 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9144 | * | AdditiveExpr '+' MultiplicativeExpr |
| 9145 | * | AdditiveExpr '-' MultiplicativeExpr |
| 9146 | * |
| 9147 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9148 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9149 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9150 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9151 | * @param[in] options XPath options. |
| 9152 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9153 | */ |
| 9154 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9155 | eval_additive_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9156 | { |
| 9157 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9158 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9159 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9160 | |
| 9161 | assert(repeat); |
| 9162 | |
| 9163 | set_init(&orig_set, set); |
| 9164 | set_init(&set2, set); |
| 9165 | |
| 9166 | set_fill_set(&orig_set, set); |
| 9167 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9168 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9169 | LY_CHECK_GOTO(rc, cleanup); |
| 9170 | |
| 9171 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 9172 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9173 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9174 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9175 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9176 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9177 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9178 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9179 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9180 | if (options & LYXP_SKIP_EXPR) { |
| 9181 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9182 | LY_CHECK_GOTO(rc, cleanup); |
| 9183 | continue; |
| 9184 | } |
| 9185 | |
| 9186 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9187 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9188 | LY_CHECK_GOTO(rc, cleanup); |
| 9189 | |
| 9190 | /* eval */ |
| 9191 | if (options & LYXP_SCNODE_ALL) { |
| 9192 | 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] | 9193 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9194 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9195 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9196 | 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] | 9197 | LY_CHECK_GOTO(rc, cleanup); |
| 9198 | } |
| 9199 | } |
| 9200 | |
| 9201 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9202 | lyxp_set_free_content(&orig_set); |
| 9203 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9204 | return rc; |
| 9205 | } |
| 9206 | |
| 9207 | /** |
| 9208 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 9209 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9210 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9211 | * | RelationalExpr '<' AdditiveExpr |
| 9212 | * | RelationalExpr '>' AdditiveExpr |
| 9213 | * | RelationalExpr '<=' AdditiveExpr |
| 9214 | * | RelationalExpr '>=' AdditiveExpr |
| 9215 | * |
| 9216 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9217 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9218 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9219 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9220 | * @param[in] options XPath options. |
| 9221 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9222 | */ |
| 9223 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9224 | eval_relational_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9225 | { |
| 9226 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9227 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9228 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9229 | |
| 9230 | assert(repeat); |
| 9231 | |
| 9232 | set_init(&orig_set, set); |
| 9233 | set_init(&set2, set); |
| 9234 | |
| 9235 | set_fill_set(&orig_set, set); |
| 9236 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9237 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9238 | LY_CHECK_GOTO(rc, cleanup); |
| 9239 | |
| 9240 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 9241 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9242 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9243 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9244 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9245 | 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] | 9246 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9247 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9248 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9249 | if (options & LYXP_SKIP_EXPR) { |
| 9250 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9251 | LY_CHECK_GOTO(rc, cleanup); |
| 9252 | continue; |
| 9253 | } |
| 9254 | |
| 9255 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9256 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9257 | LY_CHECK_GOTO(rc, cleanup); |
| 9258 | |
| 9259 | /* eval */ |
| 9260 | if (options & LYXP_SCNODE_ALL) { |
| 9261 | 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] | 9262 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9263 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9264 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9265 | ly_bool result; |
| 9266 | |
| 9267 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9268 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9269 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9270 | } |
| 9271 | } |
| 9272 | |
| 9273 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9274 | lyxp_set_free_content(&orig_set); |
| 9275 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9276 | return rc; |
| 9277 | } |
| 9278 | |
| 9279 | /** |
| 9280 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 9281 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9282 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9283 | * | EqualityExpr '!=' RelationalExpr |
| 9284 | * |
| 9285 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9286 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9287 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9288 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9289 | * @param[in] options XPath options. |
| 9290 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9291 | */ |
| 9292 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9293 | eval_equality_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9294 | { |
| 9295 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9296 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9297 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9298 | |
| 9299 | assert(repeat); |
| 9300 | |
| 9301 | set_init(&orig_set, set); |
| 9302 | set_init(&set2, set); |
| 9303 | |
| 9304 | set_fill_set(&orig_set, set); |
| 9305 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9306 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9307 | LY_CHECK_GOTO(rc, cleanup); |
| 9308 | |
| 9309 | /* ('=' / '!=' RelationalExpr)* */ |
| 9310 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9311 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9312 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9313 | 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] | 9314 | 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] | 9315 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9316 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9317 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9318 | if (options & LYXP_SKIP_EXPR) { |
| 9319 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9320 | LY_CHECK_GOTO(rc, cleanup); |
| 9321 | continue; |
| 9322 | } |
| 9323 | |
| 9324 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9325 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9326 | LY_CHECK_GOTO(rc, cleanup); |
| 9327 | |
| 9328 | /* eval */ |
| 9329 | if (options & LYXP_SCNODE_ALL) { |
| 9330 | 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] | 9331 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9332 | 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] | 9333 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9334 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9335 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9336 | ly_bool result; |
| 9337 | |
| 9338 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9339 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9340 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9341 | } |
| 9342 | } |
| 9343 | |
| 9344 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9345 | lyxp_set_free_content(&orig_set); |
| 9346 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9347 | return rc; |
| 9348 | } |
| 9349 | |
| 9350 | /** |
| 9351 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9352 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9353 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9354 | * |
| 9355 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9356 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9357 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9358 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9359 | * @param[in] options XPath options. |
| 9360 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9361 | */ |
| 9362 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9363 | eval_and_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9364 | { |
| 9365 | LY_ERR rc; |
| 9366 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9367 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9368 | |
| 9369 | assert(repeat); |
| 9370 | |
| 9371 | set_init(&orig_set, set); |
| 9372 | set_init(&set2, set); |
| 9373 | |
| 9374 | set_fill_set(&orig_set, set); |
| 9375 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9376 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9377 | LY_CHECK_GOTO(rc, cleanup); |
| 9378 | |
Michal Vasko | 3ff82dd | 2023-05-09 14:09:21 +0200 | [diff] [blame] | 9379 | if (!(options & LYXP_SKIP_EXPR)) { |
| 9380 | if (options & LYXP_SCNODE_ALL) { |
| 9381 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
| 9382 | } else { |
| 9383 | /* cast to boolean, we know that will be the final result */ |
| 9384 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
| 9385 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9386 | } |
| 9387 | |
| 9388 | /* ('and' EqualityExpr)* */ |
| 9389 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9390 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9391 | 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] | 9392 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9393 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9394 | |
| 9395 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9396 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9397 | 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] | 9398 | LY_CHECK_GOTO(rc, cleanup); |
| 9399 | continue; |
| 9400 | } |
| 9401 | |
| 9402 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9403 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9404 | LY_CHECK_GOTO(rc, cleanup); |
| 9405 | |
| 9406 | /* eval - just get boolean value actually */ |
| 9407 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9408 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9409 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9410 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9411 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9412 | set_fill_set(set, &set2); |
| 9413 | } |
| 9414 | } |
| 9415 | |
| 9416 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9417 | lyxp_set_free_content(&orig_set); |
| 9418 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9419 | return rc; |
| 9420 | } |
| 9421 | |
| 9422 | /** |
| 9423 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9424 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9425 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9426 | * |
| 9427 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9428 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9429 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9430 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9431 | * @param[in] options XPath options. |
| 9432 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9433 | */ |
| 9434 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9435 | eval_or_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9436 | { |
| 9437 | LY_ERR rc; |
| 9438 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9439 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9440 | |
| 9441 | assert(repeat); |
| 9442 | |
| 9443 | set_init(&orig_set, set); |
| 9444 | set_init(&set2, set); |
| 9445 | |
| 9446 | set_fill_set(&orig_set, set); |
| 9447 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9448 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9449 | LY_CHECK_GOTO(rc, cleanup); |
| 9450 | |
Michal Vasko | 3ff82dd | 2023-05-09 14:09:21 +0200 | [diff] [blame] | 9451 | if (!(options & LYXP_SKIP_EXPR)) { |
| 9452 | if (options & LYXP_SCNODE_ALL) { |
| 9453 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
| 9454 | } else { |
| 9455 | /* cast to boolean, we know that will be the final result */ |
| 9456 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
| 9457 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9458 | } |
| 9459 | |
| 9460 | /* ('or' AndExpr)* */ |
| 9461 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9462 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9463 | 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] | 9464 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9465 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9466 | |
| 9467 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9468 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9469 | 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] | 9470 | LY_CHECK_GOTO(rc, cleanup); |
| 9471 | continue; |
| 9472 | } |
| 9473 | |
| 9474 | set_fill_set(&set2, &orig_set); |
| 9475 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9476 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9477 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9478 | LY_CHECK_GOTO(rc, cleanup); |
| 9479 | |
| 9480 | /* eval - just get boolean value actually */ |
| 9481 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9482 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9483 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9484 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9485 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9486 | set_fill_set(set, &set2); |
| 9487 | } |
| 9488 | } |
| 9489 | |
| 9490 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9491 | lyxp_set_free_content(&orig_set); |
| 9492 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9493 | return rc; |
| 9494 | } |
| 9495 | |
| 9496 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9497 | * @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] | 9498 | * |
| 9499 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9500 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9501 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9502 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9503 | * @param[in] options XPath options. |
| 9504 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9505 | */ |
| 9506 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9507 | eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 9508 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9509 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9510 | uint32_t i, count; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9511 | enum lyxp_expr_type next_etype; |
| 9512 | LY_ERR rc; |
| 9513 | |
| 9514 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9515 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9516 | next_etype = LYXP_EXPR_NONE; |
| 9517 | } else { |
| 9518 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9519 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9520 | |
| 9521 | /* select one-priority lower because etype expression called us */ |
| 9522 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9523 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9524 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9525 | 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] | 9526 | } else { |
| 9527 | next_etype = LYXP_EXPR_NONE; |
| 9528 | } |
| 9529 | } |
| 9530 | |
| 9531 | /* decide what expression are we parsing based on the repeat */ |
| 9532 | switch (next_etype) { |
| 9533 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9534 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9535 | break; |
| 9536 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9537 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9538 | break; |
| 9539 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9540 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9541 | break; |
| 9542 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9543 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9544 | break; |
| 9545 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9546 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9547 | break; |
| 9548 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9549 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9550 | break; |
| 9551 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9552 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9553 | break; |
| 9554 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9555 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9556 | break; |
| 9557 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9558 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9559 | break; |
| 9560 | default: |
| 9561 | LOGINT_RET(set->ctx); |
| 9562 | } |
| 9563 | |
| 9564 | return rc; |
| 9565 | } |
| 9566 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9567 | /** |
| 9568 | * @brief Get root type. |
| 9569 | * |
| 9570 | * @param[in] ctx_node Context node. |
| 9571 | * @param[in] ctx_scnode Schema context node. |
| 9572 | * @param[in] options XPath options. |
| 9573 | * @return Root type. |
| 9574 | */ |
| 9575 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9576 | 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] | 9577 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9578 | const struct lysc_node *op; |
| 9579 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9580 | /* explicit */ |
| 9581 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9582 | return LYXP_NODE_ROOT; |
| 9583 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9584 | return LYXP_NODE_ROOT_CONFIG; |
| 9585 | } |
| 9586 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9587 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9588 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9589 | 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] | 9590 | |
| 9591 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9592 | /* general root that can access everything */ |
| 9593 | return LYXP_NODE_ROOT; |
| 9594 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9595 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9596 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9597 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9598 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9599 | } |
| 9600 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9601 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9602 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9603 | 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] | 9604 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9605 | if (op || !(options & LYXP_SCHEMA)) { |
| 9606 | /* general root that can access everything */ |
| 9607 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9608 | } 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] | 9609 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9610 | return LYXP_NODE_ROOT_CONFIG; |
| 9611 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9612 | return LYXP_NODE_ROOT; |
| 9613 | } |
| 9614 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9615 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9616 | lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9617 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node, |
| 9618 | const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9619 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9620 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9621 | LY_ERR rc; |
| 9622 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9623 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9624 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9625 | 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] | 9626 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9627 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9628 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9629 | if (tree) { |
| 9630 | /* adjust the pointer to be the first top-level sibling */ |
| 9631 | while (tree->parent) { |
| 9632 | tree = lyd_parent(tree); |
| 9633 | } |
| 9634 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9635 | |
Michal Vasko | 6f78a77 | 2022-11-10 08:13:52 +0100 | [diff] [blame] | 9636 | if (lysc_data_parent(tree->schema)) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9637 | /* unable to evaluate absolute paths */ |
| 9638 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9639 | LYD_NAME(tree)); |
| 9640 | return LY_EINVAL; |
| 9641 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9642 | } |
| 9643 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9644 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9645 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9646 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9647 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9648 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9649 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9650 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9651 | set->cur_node = cur_node; |
| 9652 | for (set->context_op = cur_node ? cur_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9653 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9654 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9655 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9656 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9657 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9658 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9659 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9660 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9661 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9662 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9663 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9664 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9665 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9666 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9667 | } |
| 9668 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9669 | if (set->cur_node) { |
| 9670 | LOG_LOCBACK(0, 1, 0, 0); |
| 9671 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9672 | return rc; |
| 9673 | } |
| 9674 | |
| 9675 | #if 0 |
| 9676 | |
| 9677 | /* full xml printing of set elements, not used currently */ |
| 9678 | |
| 9679 | void |
| 9680 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9681 | { |
| 9682 | uint32_t i; |
| 9683 | char *str_num; |
| 9684 | struct lyout out; |
| 9685 | |
| 9686 | memset(&out, 0, sizeof out); |
| 9687 | |
| 9688 | out.type = LYOUT_STREAM; |
| 9689 | out.method.f = f; |
| 9690 | |
| 9691 | switch (set->type) { |
| 9692 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9693 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9694 | break; |
| 9695 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9696 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9697 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9698 | break; |
| 9699 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9700 | ly_print_(&out, "String XPath set:\n"); |
| 9701 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9702 | break; |
| 9703 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9704 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9705 | |
| 9706 | if (isnan(set->value.num)) { |
| 9707 | str_num = strdup("NaN"); |
| 9708 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9709 | str_num = strdup("0"); |
| 9710 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9711 | str_num = strdup("Infinity"); |
| 9712 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9713 | str_num = strdup("-Infinity"); |
| 9714 | } else if ((long long)set->value.num == set->value.num) { |
| 9715 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9716 | str_num = NULL; |
| 9717 | } |
| 9718 | } else { |
| 9719 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9720 | str_num = NULL; |
| 9721 | } |
| 9722 | } |
| 9723 | if (!str_num) { |
| 9724 | LOGMEM; |
| 9725 | return; |
| 9726 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9727 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9728 | free(str_num); |
| 9729 | break; |
| 9730 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9731 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9732 | |
| 9733 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9734 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9735 | switch (set->node_type[i]) { |
| 9736 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9737 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9738 | break; |
| 9739 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9740 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9741 | break; |
| 9742 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9743 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9744 | break; |
| 9745 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9746 | 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] | 9747 | break; |
| 9748 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9749 | 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] | 9750 | break; |
| 9751 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9752 | 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] | 9753 | break; |
| 9754 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9755 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9756 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9757 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9758 | break; |
| 9759 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9760 | 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] | 9761 | break; |
| 9762 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9763 | 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] | 9764 | break; |
| 9765 | } |
| 9766 | } |
| 9767 | break; |
| 9768 | } |
| 9769 | } |
| 9770 | |
| 9771 | #endif |
| 9772 | |
| 9773 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9774 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9775 | { |
| 9776 | long double num; |
| 9777 | char *str; |
| 9778 | LY_ERR rc; |
| 9779 | |
| 9780 | if (!set || (set->type == target)) { |
| 9781 | return LY_SUCCESS; |
| 9782 | } |
| 9783 | |
| 9784 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9785 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9786 | |
| 9787 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9788 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9789 | return LY_EINVAL; |
| 9790 | } |
| 9791 | |
| 9792 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9793 | 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] | 9794 | switch (set->type) { |
| 9795 | case LYXP_SET_NUMBER: |
| 9796 | if (isnan(set->val.num)) { |
| 9797 | set->val.str = strdup("NaN"); |
| 9798 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9799 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9800 | set->val.str = strdup("0"); |
| 9801 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9802 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9803 | set->val.str = strdup("Infinity"); |
| 9804 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9805 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9806 | set->val.str = strdup("-Infinity"); |
| 9807 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9808 | } else if ((long long)set->val.num == set->val.num) { |
| 9809 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9810 | LOGMEM_RET(set->ctx); |
| 9811 | } |
| 9812 | set->val.str = str; |
| 9813 | } else { |
| 9814 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9815 | LOGMEM_RET(set->ctx); |
| 9816 | } |
| 9817 | set->val.str = str; |
| 9818 | } |
| 9819 | break; |
| 9820 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9821 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9822 | set->val.str = strdup("true"); |
| 9823 | } else { |
| 9824 | set->val.str = strdup("false"); |
| 9825 | } |
| 9826 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9827 | break; |
| 9828 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9829 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9830 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9831 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9832 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9833 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9834 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9835 | set->val.str = str; |
| 9836 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9837 | default: |
| 9838 | LOGINT_RET(set->ctx); |
| 9839 | } |
| 9840 | set->type = LYXP_SET_STRING; |
| 9841 | } |
| 9842 | |
| 9843 | /* to NUMBER */ |
| 9844 | if (target == LYXP_SET_NUMBER) { |
| 9845 | switch (set->type) { |
| 9846 | case LYXP_SET_STRING: |
| 9847 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9848 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9849 | set->val.num = num; |
| 9850 | break; |
| 9851 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9852 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9853 | set->val.num = 1; |
| 9854 | } else { |
| 9855 | set->val.num = 0; |
| 9856 | } |
| 9857 | break; |
| 9858 | default: |
| 9859 | LOGINT_RET(set->ctx); |
| 9860 | } |
| 9861 | set->type = LYXP_SET_NUMBER; |
| 9862 | } |
| 9863 | |
| 9864 | /* to BOOLEAN */ |
| 9865 | if (target == LYXP_SET_BOOLEAN) { |
| 9866 | switch (set->type) { |
| 9867 | case LYXP_SET_NUMBER: |
| 9868 | 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] | 9869 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9870 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9871 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9872 | } |
| 9873 | break; |
| 9874 | case LYXP_SET_STRING: |
| 9875 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9876 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9877 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9878 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9879 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9880 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9881 | } |
| 9882 | break; |
| 9883 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9884 | if (set->used) { |
| 9885 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9886 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9887 | } else { |
| 9888 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9889 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9890 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9891 | break; |
| 9892 | default: |
| 9893 | LOGINT_RET(set->ctx); |
| 9894 | } |
| 9895 | set->type = LYXP_SET_BOOLEAN; |
| 9896 | } |
| 9897 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9898 | return LY_SUCCESS; |
| 9899 | } |
| 9900 | |
| 9901 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9902 | lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9903 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode, |
| 9904 | const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9905 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9906 | LY_ERR ret; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9907 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9908 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9909 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9910 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9911 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9912 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9913 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9914 | |
| 9915 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9916 | memset(set, 0, sizeof *set); |
| 9917 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9918 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 9919 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, LYXP_AXIS_SELF, NULL)); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 9920 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9921 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9922 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9923 | set->cur_scnode = cur_scnode; |
| 9924 | for (set->context_op = cur_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9925 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9926 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9927 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9928 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9929 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9930 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9931 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9932 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9933 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9934 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9935 | |
Michal Vasko | f69e455 | 2022-12-02 10:27:42 +0100 | [diff] [blame] | 9936 | LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9937 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9938 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9939 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9940 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9941 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9942 | { |
| 9943 | if (!path) { |
| 9944 | return NULL; |
| 9945 | } |
| 9946 | |
| 9947 | return path->expr; |
| 9948 | } |